Skip to content

Commit

Permalink
Merge pull request #8 from MarkRedeman/fix-hydration-for-objects-with…
Browse files Browse the repository at this point in the history
…out-properties

Fix hydration of classes without properties
  • Loading branch information
matthiasnoback authored Jul 22, 2016
2 parents 85a5583 + 235c028 commit de52b13
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Hydration/HydrateUsingReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private function propertiesOf($object)
$className = get_class($object);

if (!isset($this->properties[$className])) {
$this->properties[$className] = [];
foreach ((new \ReflectionObject($object))->getProperties() as $property) {
/** @var \ReflectionProperty $property */
$property->setAccessible(true);
Expand Down
7 changes: 7 additions & 0 deletions test/Hydrate/Fixtures/ClassWithoutProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace BroadwaySerialization\Test\Hydrate\Fixtures;

class ClassWithoutProperties
{
}
14 changes: 14 additions & 0 deletions test/Hydrate/HydrateUsingReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BroadwaySerialization\Test\Hydrate;

use BroadwaySerialization\Hydration\HydrateUsingReflection;
use BroadwaySerialization\Test\Hydrate\Fixtures\ClassWithoutProperties;
use BroadwaySerialization\Test\Hydrate\Fixtures\ClassWithPrivateProperties;

class HydrateUsingReflectionTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -49,4 +50,17 @@ public function it_ignores_extra_keys_in_the_data_array()

$this->assertSame('bar', $object->foo());
}

/**
* @test
*/
public function it_works_for_objects_without_properties()
{
$object = new ClassWithoutProperties();

$hydrate = new HydrateUsingReflection();

// This class doesn't have any property which should be no problem
$hydrate->hydrate([], $object);
}
}

0 comments on commit de52b13

Please sign in to comment.