|
16 | 16 | use PHPModelGenerator\ModelGenerator;
|
17 | 17 | use PHPModelGenerator\SchemaProcessor\Hook\SetterBeforeValidationHookInterface;
|
18 | 18 | use PHPModelGenerator\SchemaProcessor\PostProcessor\AdditionalPropertiesAccessorPostProcessor;
|
| 19 | +use PHPModelGenerator\SchemaProcessor\PostProcessor\PopulatePostProcessor; |
19 | 20 | use PHPModelGenerator\SchemaProcessor\PostProcessor\PostProcessor;
|
20 | 21 | use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest;
|
21 | 22 |
|
@@ -405,6 +406,66 @@ public function testAdditionalPropertiesAreSerialized(bool $implicitNull): void
|
405 | 406 | $this->assertNull($this->getParameterType($object, 'setAdditionalProperty', 1));
|
406 | 407 | }
|
407 | 408 |
|
| 409 | + |
| 410 | + public function testAdditionalPropertiesAreSerializedWithoutAdditionalPropertiesAccessorPostProcessor(): void |
| 411 | + { |
| 412 | + $this->modifyModelGenerator = function (ModelGenerator $generator): void { |
| 413 | + $generator->addPostProcessor(new PopulatePostProcessor()); |
| 414 | + }; |
| 415 | + |
| 416 | + $className = $this->generateClassFromFile( |
| 417 | + 'AdditionalPropertiesTransformingFilter.json', |
| 418 | + (new GeneratorConfiguration())->setSerialization(true) |
| 419 | + ); |
| 420 | + |
| 421 | + $object = new $className(['name' => 'Late autumn', 'start' => '2020-10-10']); |
| 422 | + $this->assertEqualsCanonicalizing(['name' => 'Late autumn', 'start' => '20201010'], $object->toArray()); |
| 423 | + |
| 424 | + $object->populate(['end' => '20201212']); |
| 425 | + $this->assertEqualsCanonicalizing( |
| 426 | + ['name' => 'Late autumn', 'start' => '20201010', 'end' => '20201212'], |
| 427 | + $object->toArray() |
| 428 | + ); |
| 429 | + } |
| 430 | + |
| 431 | + public function testAdditionalPropertiesAreNotSerializedWhenNotDefinedWithoutExplicitAccessorMethods(): void |
| 432 | + { |
| 433 | + $this->modifyModelGenerator = function (ModelGenerator $generator): void { |
| 434 | + $generator->addPostProcessor(new PopulatePostProcessor()); |
| 435 | + }; |
| 436 | + |
| 437 | + $className = $this->generateClassFromFile( |
| 438 | + 'AdditionalPropertiesNotDefined.json', |
| 439 | + (new GeneratorConfiguration())->setSerialization(true) |
| 440 | + ); |
| 441 | + |
| 442 | + $object = new $className(['a' => 1, 'b' => 2]); |
| 443 | + $this->assertSame([], $object->toArray()); |
| 444 | + |
| 445 | + $object->populate(['a' => 3, 'c' => 4]); |
| 446 | + $this->assertSame([], $object->toArray()); |
| 447 | + } |
| 448 | + |
| 449 | + public function testAdditionalPropertiesAreSerializedWhenNotDefinedWithExplicitAccessorMethods(): void |
| 450 | + { |
| 451 | + $this->modifyModelGenerator = function (ModelGenerator $generator): void { |
| 452 | + $generator |
| 453 | + ->addPostProcessor(new PopulatePostProcessor()) |
| 454 | + ->addPostProcessor(new AdditionalPropertiesAccessorPostProcessor(true)); |
| 455 | + }; |
| 456 | + |
| 457 | + $className = $this->generateClassFromFile( |
| 458 | + 'AdditionalPropertiesNotDefined.json', |
| 459 | + (new GeneratorConfiguration())->setSerialization(true) |
| 460 | + ); |
| 461 | + |
| 462 | + $object = new $className(['a' => 1, 'b' => 2]); |
| 463 | + $this->assertEqualsCanonicalizing(['a' => 1, 'b' => 2], $object->toArray()); |
| 464 | + |
| 465 | + $object->populate(['a' => 3, 'c' => 4]); |
| 466 | + $this->assertEqualsCanonicalizing(['a' => 3, 'b' => 2, 'c' => 4], $object->toArray()); |
| 467 | + } |
| 468 | + |
408 | 469 | public function testMultiTypeAdditionalProperties(): void
|
409 | 470 | {
|
410 | 471 | $this->addPostProcessor(true);
|
|
0 commit comments