|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the contentful/contentful-core package. |
| 5 | + * |
| 6 | + * @copyright 2015-2018 Contentful GmbH |
| 7 | + * @license MIT |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Contentful\Tests\Core\Unit\ResourceBuilder; |
| 11 | + |
| 12 | +use Contentful\Core\ResourceBuilder\ObjectHydrator; |
| 13 | +use Contentful\Tests\Core\TestCase; |
| 14 | + |
| 15 | +class ObjectHydratorTest extends TestCase |
| 16 | +{ |
| 17 | + public function testHydration() |
| 18 | + { |
| 19 | + $hydrator = new ObjectHydrator(); |
| 20 | + |
| 21 | + /** @var TestPerson $person */ |
| 22 | + $person = $hydrator->hydrate(TestPerson::class, [ |
| 23 | + 'name' => 'Kanji Tatsumi', |
| 24 | + 'age' => 15, |
| 25 | + ]); |
| 26 | + |
| 27 | + $this->assertSame('Kanji Tatsumi', $person->getName()); |
| 28 | + $this->assertSame(15, $person->getAge()); |
| 29 | + |
| 30 | + /** @var TestPerson $person */ |
| 31 | + $person = $hydrator->hydrate(TestPerson::class, [ |
| 32 | + 'name' => 'Makoto Niijima', |
| 33 | + 'age' => 17, |
| 34 | + ]); |
| 35 | + |
| 36 | + $this->assertSame('Makoto Niijima', $person->getName()); |
| 37 | + $this->assertSame(17, $person->getAge()); |
| 38 | + |
| 39 | + $videogame = new TestVideogame(); |
| 40 | + $this->assertNull($videogame->getTitle()); |
| 41 | + $this->assertNull($videogame->getConsole()); |
| 42 | + |
| 43 | + $hydrator->hydrate($videogame, [ |
| 44 | + 'title' => 'Persona 5', |
| 45 | + 'console' => 'PS4', |
| 46 | + ]); |
| 47 | + |
| 48 | + $this->assertSame('Persona 5', $videogame->getTitle()); |
| 49 | + $this->assertSame('PS4', $videogame->getConsole()); |
| 50 | + |
| 51 | + $reflectionObject = new \ReflectionObject($hydrator); |
| 52 | + $property = $reflectionObject->getProperty('hydrators'); |
| 53 | + $property->setAccessible(\true); |
| 54 | + $hydrators = $property->getValue($hydrator); |
| 55 | + |
| 56 | + $this->assertCount(2, $hydrators); |
| 57 | + $this->assertArrayHasKey(TestPerson::class, $hydrators); |
| 58 | + $this->assertArrayHasKey(TestVideogame::class, $hydrators); |
| 59 | + |
| 60 | + $this->assertInstanceOf(\Closure::class, $hydrators[TestPerson::class]); |
| 61 | + $this->assertInstanceOf(\Closure::class, $hydrators[TestVideogame::class]); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class TestPerson |
| 66 | +{ |
| 67 | + private $name; |
| 68 | + |
| 69 | + private $age; |
| 70 | + |
| 71 | + private function __construct() |
| 72 | + { |
| 73 | + } |
| 74 | + |
| 75 | + public function getName() |
| 76 | + { |
| 77 | + return $this->name; |
| 78 | + } |
| 79 | + |
| 80 | + public function getAge() |
| 81 | + { |
| 82 | + return $this->age; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +class TestVideogame |
| 87 | +{ |
| 88 | + private $title; |
| 89 | + |
| 90 | + private $console; |
| 91 | + |
| 92 | + public function getTitle() |
| 93 | + { |
| 94 | + return $this->title; |
| 95 | + } |
| 96 | + |
| 97 | + public function getConsole() |
| 98 | + { |
| 99 | + return $this->console; |
| 100 | + } |
| 101 | +} |
0 commit comments