|
2 | 2 |
|
3 | 3 | namespace PHPModelGenerator\Tests\Basic;
|
4 | 4 |
|
| 5 | +use DateTime; |
| 6 | +use PHPModelGenerator\Exception\ErrorRegistryException; |
5 | 7 | use PHPModelGenerator\Exception\InvalidFilterException;
|
6 | 8 | use PHPModelGenerator\Exception\SchemaException;
|
7 | 9 | use PHPModelGenerator\Exception\ValidationException;
|
8 | 10 | use PHPModelGenerator\Filter\Trim;
|
9 | 11 | use PHPModelGenerator\Model\GeneratorConfiguration;
|
| 12 | +use PHPModelGenerator\PropertyProcessor\Filter\DateTimeFilter; |
10 | 13 | use PHPModelGenerator\PropertyProcessor\Filter\FilterInterface;
|
11 | 14 | use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
|
12 | 15 |
|
@@ -99,18 +102,18 @@ public function getFilter(): array
|
99 | 102 | * @dataProvider validBuiltInFilterDataProvider
|
100 | 103 | *
|
101 | 104 | * @param string $template
|
102 |
| - * @param string|null $input |
| 105 | + * @param array $input |
103 | 106 | * @param string|null $expected
|
104 | 107 | */
|
105 |
| - public function testValidUsageOfBuiltInFilter(string $template, ?string $input, ?string $expected): void |
| 108 | + public function testValidUsageOfBuiltInFilter(string $template, array $input, ?string $expected): void |
106 | 109 | {
|
107 | 110 | $className = $this->generateClassFromFileTemplate($template, ['"string"'], null, false);
|
108 | 111 |
|
109 |
| - $object = new $className(['property' => $input]); |
| 112 | + $object = new $className($input); |
110 | 113 |
|
111 | 114 | $this->assertSame($object->getProperty(), $expected);
|
112 | 115 | // make sure the raw inout isn't affected by the filter
|
113 |
| - $this->assertSame($input, $object->getRawModelDataInput()['property']); |
| 116 | + $this->assertSame($input, $object->getRawModelDataInput()); |
114 | 117 | }
|
115 | 118 |
|
116 | 119 | /**
|
@@ -140,11 +143,12 @@ public function validBuiltInFilterDataProvider(): array
|
140 | 143 | return $this->combineDataProvider(
|
141 | 144 | $this->validTrimDataFormatProvider(),
|
142 | 145 | [
|
143 |
| - 'Null' => [null, null], |
144 |
| - 'Empty string' => ['', ''], |
145 |
| - 'String containing only whitespaces' => [" \t \n \r ", ''], |
146 |
| - 'Numeric string' => [' 12 ', '12'], |
147 |
| - 'Text' => [' Hello World! ', 'Hello World!'], |
| 146 | + 'Optional Value not provided' => [[], null], |
| 147 | + 'Null' => [['property' => null], null], |
| 148 | + 'Empty string' => [['property' => ''], ''], |
| 149 | + 'String containing only whitespaces' => [['property' => " \t \n \r "], ''], |
| 150 | + 'Numeric string' => [['property' => ' 12 '], '12'], |
| 151 | + 'Text' => [['property' => ' Hello World! '], 'Hello World!'], |
148 | 152 | ]
|
149 | 153 | );
|
150 | 154 | }
|
@@ -301,4 +305,119 @@ public function multipleFilterDataProvider(): array
|
301 | 305 | 'mixed string' => [" \t Hello World! ", 'HELLO WORLD!'],
|
302 | 306 | ];
|
303 | 307 | }
|
| 308 | + |
| 309 | + /** |
| 310 | + * @dataProvider validDateTimeFilterDataProvider |
| 311 | + */ |
| 312 | + public function testTransformingFilter(array $input, ?string $expected): void |
| 313 | + { |
| 314 | + $className = $this->generateClassFromFile( |
| 315 | + 'TransformingFilter.json', |
| 316 | + (new GeneratorConfiguration())->setImmutable(false)->setSerialization(true) |
| 317 | + ); |
| 318 | + |
| 319 | + $object = new $className($input); |
| 320 | + |
| 321 | + if ($expected === null) { |
| 322 | + $this->assertNull($object->getCreated()); |
| 323 | + } else { |
| 324 | + $expectedDateTime = new DateTime($expected); |
| 325 | + |
| 326 | + $this->assertInstanceOf(DateTime::class, $object->getCreated()); |
| 327 | + $this->assertSame($expectedDateTime->format(DATE_ATOM), $object->getCreated()->format(DATE_ATOM)); |
| 328 | + } |
| 329 | + |
| 330 | + // test if the setter accepts the raw model data |
| 331 | + if (isset($input['created'])) { |
| 332 | + $object->setCreated($input['created']); |
| 333 | + |
| 334 | + if ($expected === null) { |
| 335 | + $this->assertNull($object->getCreated()); |
| 336 | + } else { |
| 337 | + $expectedDateTime = new DateTime($expected); |
| 338 | + |
| 339 | + $this->assertInstanceOf(DateTime::class, $object->getCreated()); |
| 340 | + $this->assertSame($expectedDateTime->format(DATE_ATOM), $object->getCreated()->format(DATE_ATOM)); |
| 341 | + |
| 342 | + // test if the setter accepts a DateTime object |
| 343 | + $object->setCreated($expectedDateTime); |
| 344 | + |
| 345 | + $this->assertInstanceOf(DateTime::class, $object->getCreated()); |
| 346 | + $this->assertSame($expectedDateTime->format(DATE_ATOM), $object->getCreated()->format(DATE_ATOM)); |
| 347 | + } |
| 348 | + } |
| 349 | + |
| 350 | + // test if the model can be serialized |
| 351 | + $expectedSerialization = [ |
| 352 | + 'created' => $expected !== null ? (new DateTime($expected))->format(DATE_ISO8601) : null, |
| 353 | + 'name' => null, |
| 354 | + ]; |
| 355 | + |
| 356 | + $this->assertSame($expectedSerialization, $object->toArray()); |
| 357 | + $this->assertSame(json_encode($expectedSerialization), $object->toJSON()); |
| 358 | + } |
| 359 | + |
| 360 | + public function validDateTimeFilterDataProvider(): array |
| 361 | + { |
| 362 | + return [ |
| 363 | + 'Optional Value not provided' => [[], null], |
| 364 | + 'Null' => [['created' => null], null], |
| 365 | + 'Empty string' => [['created' => ''], 'now'], |
| 366 | + 'valid date' => [['created' => "12.12.2020 12:00"], '12.12.2020 12:00'], |
| 367 | + 'valid DateTime constructor string' => [['created' => '+1 day'], '+1 day'], |
| 368 | + ]; |
| 369 | + } |
| 370 | + |
| 371 | + public function testFilterExceptionsAreCaught(): void |
| 372 | + { |
| 373 | + $this->expectException(ErrorRegistryException::class); |
| 374 | + $this->expectExceptionMessage(<<<ERROR |
| 375 | +Invalid value for property created denied by filter dateTime: Invalid Date Time value "Hello" |
| 376 | +Invalid type for name. Requires string, got integer |
| 377 | +ERROR |
| 378 | + ); |
| 379 | + |
| 380 | + $className = $this->generateClassFromFile( |
| 381 | + 'TransformingFilter.json', |
| 382 | + (new GeneratorConfiguration())->setCollectErrors(true) |
| 383 | + ); |
| 384 | + |
| 385 | + new $className(['created' => 'Hello', 'name' => 12]); |
| 386 | + } |
| 387 | + |
| 388 | + public function testAdditionalFilterOptions(): void |
| 389 | + { |
| 390 | + $className = $this->generateClassFromFile( |
| 391 | + 'FilterOptions.json', |
| 392 | + (new GeneratorConfiguration())->setSerialization(true) |
| 393 | + ); |
| 394 | + |
| 395 | + $object = new $className(['created' => '10122020']); |
| 396 | + |
| 397 | + $this->assertSame((new DateTime('2020-12-10'))->format(DATE_ATOM), $object->getCreated()->format(DATE_ATOM)); |
| 398 | + |
| 399 | + $expectedSerialization = ['created' => '20201210']; |
| 400 | + $this->assertSame($expectedSerialization, $object->toArray()); |
| 401 | + $this->assertSame(json_encode($expectedSerialization), $object->toJSON()); |
| 402 | + } |
| 403 | + |
| 404 | + public function testMultipleTransformingFiltersAppliedToOnePropertyThrowsAnException(): void |
| 405 | + { |
| 406 | + $this->expectException(SchemaException::class); |
| 407 | + $this->expectExceptionMessage( |
| 408 | + 'Applying multiple transforming filters for property filteredProperty is not supported' |
| 409 | + ); |
| 410 | + |
| 411 | + $this->generateClassFromFile( |
| 412 | + 'MultipleTransformingFilters.json', |
| 413 | + (new GeneratorConfiguration())->addFilter( |
| 414 | + new class () extends DateTimeFilter { |
| 415 | + public function getToken(): string |
| 416 | + { |
| 417 | + return 'customTransformer'; |
| 418 | + } |
| 419 | + } |
| 420 | + ) |
| 421 | + ); |
| 422 | + } |
304 | 423 | }
|
0 commit comments