Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all traces of reflection in ObjectSnapshot #34

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/Snapshot/ObjectSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,14 @@ public function __construct($object)

$this->raw = $object;
$this->oid = spl_object_hash($object);
$refl = new ReflectionObject($object);

if (method_exists('\\ReflectionObject', 'isCloneable') && $refl->isCloneable()) {
$this->raw = clone $object;
}
$export = (array) $object;
$class = get_class($object);

/** @var \ReflectionProperty $reflProperty */
foreach ($refl->getProperties() as $reflProperty) {
$reflProperty->setAccessible(true);
$value = $reflProperty->getValue($object);
foreach ($export as $property => $value) {
$property = str_replace(["\x00*\x00", "\x00{$class}\x00"], '', $property); // not accessible properties

$this->data[$reflProperty->getName()] = $value;
$this->data[$property] = $value;
}

parent::normalize();
Expand Down
51 changes: 42 additions & 9 deletions test/Snapshot/ObjectSnapshotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,52 @@ public function testConstructWithoutObject()
new ObjectSnapshot([]);
}

/**
* @dataProvider deepProvider
*/
public function testDeepConstructor($value)
public function testExportAllProperties()
{
new ObjectSnapshot((object) ['foo' => $value]);
$snapshot = new ObjectSnapshot(new Foo('foo', 'bar', 'baz'));
$data = $snapshot->getComparableData();

$this->assertCount(3, $data);

$this->assertArrayHasKey('foo', $data);
$this->assertArrayHasKey('bar', $data);
$this->assertArrayHasKey('baz', $data);

$this->assertSame('foo', $data['foo']);
$this->assertSame('bar', $data['bar']);
$this->assertSame('baz', $data['baz']);
}

public function deepProvider()
public function testDeepConstructor()
{
return [[(object) ['bar' => 'baz']],
[['bar' => 'baz']],
['fubar']];
$object = new Foo(
(object) ['foo' => 'bar'], // object
['baz' => 'fubar'], // array
'fubaz' // scalar
);

$snapshot = new ObjectSnapshot($object);
$data = $snapshot->getComparableData();

$this->assertInstanceOf('Totem\\Snapshot\\ObjectSnapshot', $data['foo']);
$this->assertInstanceOf('Totem\\Snapshot\\ArraySnapshot', $data['bar']);
$this->assertNotInstanceOf('Totem\\AbstractSnapshot', $data['baz']);
}
}

// todo in php7 : use anon class !
class Foo
{
public $foo;
protected $bar;
private $baz;

public function __construct($foo, $bar, $baz)
{
$this->foo = $foo;
$this->bar = $bar;
$this->baz = $baz;
}
}