|
11 | 11 | use PHPModelGenerator\Filter\FilterInterface;
|
12 | 12 | use PHPModelGenerator\Filter\TransformingFilterInterface;
|
13 | 13 | use PHPModelGenerator\Filter\Trim;
|
| 14 | +use PHPModelGenerator\Filter\ValidateOptionsInterface; |
14 | 15 | use PHPModelGenerator\Model\GeneratorConfiguration;
|
15 | 16 | use PHPModelGenerator\PropertyProcessor\Filter\DateTimeFilter;
|
16 | 17 | use PHPModelGenerator\PropertyProcessor\Filter\TrimFilter;
|
@@ -290,6 +291,97 @@ public function customFilterDataProvider(): array
|
290 | 291 | ];
|
291 | 292 | }
|
292 | 293 |
|
| 294 | + /** |
| 295 | + * @dataProvider invalidEncodingFilterConfigurationsDataProvider |
| 296 | + */ |
| 297 | + public function testInvalidCustomFilterOptionValidation(string $configuration, string $expectedErrorMessage): void |
| 298 | + { |
| 299 | + $this->expectException(SchemaException::class); |
| 300 | + $this->expectExceptionMessageMatches( |
| 301 | + "/Invalid filter options on filter encode on property .*\: $expectedErrorMessage/" |
| 302 | + ); |
| 303 | + |
| 304 | + $this->generateClassFromFileTemplate( |
| 305 | + 'Encode.json', |
| 306 | + [$configuration], |
| 307 | + (new GeneratorConfiguration())->setImmutable(false)->addFilter($this->getEncodeFilter()), |
| 308 | + false |
| 309 | + ); |
| 310 | + } |
| 311 | + |
| 312 | + public function invalidEncodingFilterConfigurationsDataProvider(): array |
| 313 | + { |
| 314 | + return [ |
| 315 | + 'simple notation without options' => ['"encode"', 'Missing charset configuration'], |
| 316 | + 'object notation without charset configuration' => ['{"filter": "encode"}', 'Missing charset configuration'], |
| 317 | + 'Invalid charset configuration' => ['{"filter": "encode", "charset": 1}', 'Unsupported charset'], |
| 318 | + 'Invalid charset configuration 2' => ['{"filter": "encode", "charset": "UTF-16"}', 'Unsupported charset'], |
| 319 | + ]; |
| 320 | + } |
| 321 | + |
| 322 | + /** |
| 323 | + * @dataProvider validEncodingsDataProvider |
| 324 | + */ |
| 325 | + public function testValidCustomFilterOptionValidation(string $encoding, string $input, string $output): void |
| 326 | + { |
| 327 | + $classname = $this->generateClassFromFileTemplate( |
| 328 | + 'Encode.json', |
| 329 | + [sprintf('{"filter": "encode", "charset": "%s"}', $encoding)], |
| 330 | + (new GeneratorConfiguration())->setImmutable(false)->addFilter($this->getEncodeFilter()), |
| 331 | + false |
| 332 | + ); |
| 333 | + |
| 334 | + $object = new $classname(['property' => $input]); |
| 335 | + |
| 336 | + $this->assertSame($encoding, mb_detect_encoding($object->getProperty())); |
| 337 | + $this->assertSame($output, $object->getProperty()); |
| 338 | + } |
| 339 | + |
| 340 | + public function validEncodingsDataProvider(): array |
| 341 | + { |
| 342 | + return [ |
| 343 | + 'ASCII to ASCII' => ['ASCII', 'Hello World', 'Hello World'], |
| 344 | + 'UTF-8 to ASCII' => ['ASCII', 'áéó', '???'], |
| 345 | + 'UTF-8 to UTF-8' => ['UTF-8', 'áéó', 'áéó'], |
| 346 | + ]; |
| 347 | + } |
| 348 | + |
| 349 | + private function getEncodeFilter() |
| 350 | + { |
| 351 | + return new class () implements FilterInterface, ValidateOptionsInterface { |
| 352 | + public function getAcceptedTypes(): array |
| 353 | + { |
| 354 | + return ['string']; |
| 355 | + } |
| 356 | + |
| 357 | + public function getToken(): string |
| 358 | + { |
| 359 | + return 'encode'; |
| 360 | + } |
| 361 | + |
| 362 | + public function getFilter(): array |
| 363 | + { |
| 364 | + return [FilterTest::class, 'encode']; |
| 365 | + } |
| 366 | + |
| 367 | + public function validateOptions(array $options): void |
| 368 | + { |
| 369 | + if (!isset($options['charset'])) { |
| 370 | + throw new Exception('Missing charset configuration'); |
| 371 | + } |
| 372 | + |
| 373 | + if (!in_array($options['charset'], ['UTF-8', 'ASCII'])) { |
| 374 | + throw new Exception('Unsupported charset'); |
| 375 | + } |
| 376 | + } |
| 377 | + }; |
| 378 | + } |
| 379 | + |
| 380 | + public static function encode(string $value, array $options): string |
| 381 | + { |
| 382 | + return mb_convert_encoding($value, $options['charset'], 'auto'); |
| 383 | + } |
| 384 | + |
293 | 385 | /**
|
294 | 386 | * @dataProvider multipleFilterDataProvider
|
295 | 387 | *
|
|
0 commit comments