Skip to content

Commit

Permalink
Add tests for extended class extraction and hydration.
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Foulds <[email protected]>
  • Loading branch information
ianef committed Oct 3, 2023
1 parent 8dbfc34 commit 2cf8252
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
50 changes: 46 additions & 4 deletions test/ReflectionHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace LaminasTest\Hydrator;

use Laminas\Hydrator\ReflectionHydrator;
use LaminasTest\Hydrator\TestAsset\ReflectionHydratorTestData;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
Expand Down Expand Up @@ -61,10 +62,7 @@ public function testHydrateRaisesExceptionForInvalidArgument(): void

public function testCanExtractFromAnonymousClass(): void
{
$instance = new class {
private string $foo = 'bar';
private string $bar = 'baz';
};
$instance = new ReflectionHydratorTestData();
$this->assertSame([
'foo' => 'bar',
'bar' => 'baz',
Expand All @@ -83,4 +81,48 @@ public function testCanHydrateAnonymousObject(): void
$r = new ReflectionProperty($hydrated, 'foo');
$this->assertSame('bar', $r->getValue($hydrated));
}

public function testCanExtractFromExtendedClass(): void
{
$instance = new class extends ReflectionHydratorTestData {
};
$this->assertSame([
'foo' => 'bar',
'bar' => 'baz',
], $this->hydrator->extract($instance, true));
}

public function testFailToExtractFromExtendedClass(): void
{
$instance = new class extends ReflectionHydratorTestData {
};
$this->assertNotSame([
'foo' => 'bar',
'bar' => 'baz',
], $this->hydrator->extract($instance, false));
}

public function testCanHydrateExtendedClass(): void
{
$instance = new class extends ReflectionHydratorTestData {
};

$hydrated = $this->hydrator->hydrate(['foo' => 'foo-foo'], $instance, true);

$this->assertSame($instance, $hydrated);
$r = new ReflectionProperty(get_parent_class($hydrated), 'foo');
$this->assertSame('foo-foo', $r->getValue($hydrated));
}

public function testFailToHydrateExtendedClass(): void
{
$instance = new class extends ReflectionHydratorTestData {
};

$hydrated = $this->hydrator->hydrate(['foo' => 'foo-foo'], $instance, false);

$this->assertSame($instance, $hydrated);
$r = new ReflectionProperty(get_parent_class($hydrated), 'foo');
$this->assertSame('bar', $r->getValue($hydrated));
}
}
11 changes: 11 additions & 0 deletions test/TestAsset/ReflectionHydratorTestData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Hydrator\TestAsset;

class ReflectionHydratorTestData
{
private string $foo = 'bar';
private string $bar = 'baz';
}

0 comments on commit 2cf8252

Please sign in to comment.