2
0

HydratorTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace Test\Michel\RequestKit;
  3. use DateTime;
  4. use Michel\RequestKit\Builder\SchemaObjectFactory;
  5. use Michel\RequestKit\Exceptions\InvalidDataException;
  6. use Michel\RequestKit\Schema\Schema;
  7. use Michel\RequestKit\Type;
  8. use Michel\UniTester\TestCase;
  9. use Test\Michel\RequestKit\Model\AddressTest;
  10. use Test\Michel\RequestKit\Model\UserModelTest;
  11. class HydratorTest extends TestCase
  12. {
  13. private ?Schema $schema = null;
  14. protected function setUp(): void
  15. {
  16. $user = new UserModelTest();
  17. $user->setName('John Doe 3');
  18. $user->setActive(false);
  19. $this->schema = (new SchemaObjectFactory(sys_get_temp_dir()))->createSchemaFromObject($user);
  20. $this->schema = $this->schema->extend([
  21. 'email' => Type::email()->lowercase(),
  22. ]);
  23. }
  24. protected function tearDown(): void
  25. {
  26. // TODO: Implement tearDown() method.
  27. }
  28. protected function execute(): void
  29. {
  30. $this->testHydratorSimple();
  31. $this->testAddingNewProperty();
  32. $this->testForceNullOnDefaultValue();
  33. $this->testMissingNestedProperty();
  34. $this->testEmptyArrayForAddresses();
  35. $this->testInvalidTypeForName();
  36. $this->testInvalidDate();
  37. }
  38. public function testHydratorSimple(): void
  39. {
  40. $data = [
  41. 'name' => 'John Doe 3',
  42. 'active' => true,
  43. // 'created_at' => null,
  44. 'address' => [
  45. 'street' => '10 rue de la paix',
  46. 'city' => 'Paris',
  47. ],
  48. ];
  49. $result = $this->schema->process($data);
  50. $result = $result->toObject();
  51. $this->assertInstanceOf(UserModelTest::class, $result);
  52. $this->assertStrictEquals('John Doe 3', $result->getName());
  53. $this->assertStrictEquals(null, $result->getAge());
  54. $this->assertStrictEquals(null, $result->getEmail());
  55. $this->assertInstanceOf(AddressTest::class, $result->getAddress());
  56. $this->assertStrictEquals('10 rue de la paix', $result->getAddress()->getStreet());
  57. $this->assertStrictEquals('Paris', $result->getAddress()->getCity());
  58. $this->assertInstanceOf(DateTime::class, $result->getCreatedAt());
  59. $this->assertStrictEquals(true, $result->isActive());
  60. $data = [
  61. 'name' => 'John Doe',
  62. 'age' => 25,
  63. 'email' => 'JOHN@EXAMPLE.COM',
  64. 'date_of_birth' => '1990-01-01',
  65. 'created_at' => '2023-01-01 12:00:00',
  66. 'active' => 'off',
  67. 'address' => null,
  68. 'addresses' => null,
  69. ];
  70. $result = $this->schema->process($data);
  71. $result = $result->toObject();
  72. $this->assertInstanceOf(UserModelTest::class, $result);
  73. $this->assertStrictEquals('John Doe', $result->getName());
  74. $this->assertStrictEquals(25, $result->getAge());
  75. $this->assertStrictEquals('john@example.com', $result->getEmail());
  76. $this->assertInstanceOf(DateTime::class, $result->getDateOfBirth());
  77. $this->assertInstanceOf(DateTime::class, $result->getCreatedAt());
  78. $this->assertStrictEquals(null, $result->getAddress());
  79. $this->assertStrictEquals(false, $result->isActive());
  80. $this->assertStrictEquals([], $result->getAddresses());
  81. $data = [
  82. 'name' => 'John Doe',
  83. 'age' => 25,
  84. 'email' => 'JOHN@EXAMPLE.COM',
  85. 'date_of_birth' => '1990-01-01',
  86. 'created_at' => '2023-01-01 12:00:00',
  87. 'active' => true,
  88. 'address' => null,
  89. 'addresses' => [
  90. [
  91. 'street' => '10 rue de la paix',
  92. 'city' => 'Paris',
  93. 'tags' => [
  94. 'tag1',
  95. 'tag2',
  96. 1
  97. ],
  98. ],
  99. [
  100. 'street' => 'Marsupilami',
  101. 'city' => 'Paris',
  102. ],
  103. ],
  104. ];
  105. $result = $this->schema->process($data);
  106. $result = $result->toObject();
  107. $this->assertInstanceOf(UserModelTest::class, $result);
  108. $this->assertStrictEquals('John Doe', $result->getName());
  109. $this->assertStrictEquals(25, $result->getAge());
  110. $this->assertStrictEquals('john@example.com', $result->getEmail());
  111. $this->assertInstanceOf(DateTime::class, $result->getDateOfBirth());
  112. $this->assertInstanceOf(DateTime::class, $result->getCreatedAt());
  113. $this->assertStrictEquals(null, $result->getAddress());
  114. $this->assertStrictEquals(2, count($result->getAddresses()));
  115. $this->assertStrictEquals('10 rue de la paix', $result->getAddresses()[0]->getStreet());
  116. $this->assertStrictEquals('Paris', $result->getAddresses()[0]->getCity());
  117. $this->assertStrictEquals(['tag1', 'tag2', '1'], $result->getAddresses()[0]->getTags());
  118. $this->assertStrictEquals('Marsupilami', $result->getAddresses()[1]->getStreet());
  119. $this->assertStrictEquals('Paris', $result->getAddresses()[1]->getCity());
  120. $this->assertStrictEquals([], $result->getAddresses()[1]->getTags());
  121. }
  122. public function testAddingNewProperty(): void
  123. {
  124. $data = [
  125. 'phone' => '0606060606',
  126. ];
  127. $result = $this->schema->process($data);
  128. $result = $result->toArray();
  129. $this->assertTrue(!isset($result['phone']));
  130. }
  131. public function testForceNullOnDefaultValue(): void
  132. {
  133. $data = [
  134. 'email' => null,
  135. ];
  136. $result = $this->schema->process($data);
  137. $result = $result->toObject();
  138. $this->assertNull($result->getEmail()); // Attendu : null ou "John Doe 3" si l'hydrateur ignore les nulls
  139. }
  140. public function testMissingNestedProperty(): void
  141. {
  142. $data = [
  143. 'address' => [
  144. 'street' => '10 rue de la paix',
  145. // 'city' est manquant
  146. ],
  147. ];
  148. $this->expectException(InvalidDataException::class, function () use ($data) {
  149. try {
  150. $this->schema->process($data);
  151. } catch (InvalidDataException $e) {
  152. $this->assertNotEmpty($e->getErrors());
  153. $this->assertEquals(1, count($e->getErrors()));
  154. $this->assertNotEmpty($e->getError('address.city'));
  155. throw $e;
  156. }
  157. });
  158. }
  159. public function testEmptyArrayForAddresses(): void
  160. {
  161. $data = [
  162. 'addresses' => [], // Devrait être une liste d'adresses, mais on met un tableau vide
  163. ];
  164. $result = $this->schema->process($data);
  165. $result = $result->toObject();
  166. $this->assertTrue(is_array($result->getAddresses()));
  167. $this->assertStrictEquals(0, count($result->getAddresses())); // Doit retourner un tableau vide
  168. }
  169. public function testInvalidTypeForName(): void
  170. {
  171. $data = [
  172. 'name' => ['John', 'Doe'], // Mauvais type (tableau au lieu de string)
  173. ];
  174. $this->expectException(InvalidDataException::class, function () use ($data) {
  175. try {
  176. $this->schema->process($data);
  177. } catch (InvalidDataException $e) {
  178. $this->assertNotEmpty($e->getErrors());
  179. $this->assertEquals(1, count($e->getErrors()));
  180. $this->assertNotEmpty($e->getError('name'));
  181. throw $e;
  182. }
  183. });
  184. }
  185. public function testInvalidDate(): void
  186. {
  187. $data = [
  188. 'date_of_birth' => '99-99-9999', // Date invalide
  189. ];
  190. $this->expectException(InvalidDataException::class, function () use ($data) {
  191. try {
  192. $this->schema->process($data);
  193. } catch (InvalidDataException $e) {
  194. $this->assertNotEmpty($e->getErrors());
  195. $this->assertEquals(1, count($e->getErrors()));
  196. $this->assertNotEmpty($e->getError('date_of_birth'));
  197. throw $e;
  198. }
  199. });
  200. }
  201. }