|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPModelGenerator\Tests\PostProcessor; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use PHPModelGenerator\Exception\ErrorRegistryException; |
| 7 | +use PHPModelGenerator\Exception\Object\InvalidAdditionalPropertiesException; |
| 8 | +use PHPModelGenerator\Exception\Object\InvalidPropertyNamesException; |
| 9 | +use PHPModelGenerator\Exception\Object\MaxPropertiesException; |
| 10 | +use PHPModelGenerator\Exception\String\PatternException; |
| 11 | +use PHPModelGenerator\Model\GeneratorConfiguration; |
| 12 | +use PHPModelGenerator\SchemaProcessor\PostProcessor\PopulatePostProcessor; |
| 13 | +use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest; |
| 14 | + |
| 15 | +class PopulatePostProcessorTest extends AbstractPHPModelGeneratorTest |
| 16 | +{ |
| 17 | + protected const POST_PROCESSORS = [PopulatePostProcessor::class]; |
| 18 | + |
| 19 | + public function testPopulateMethod(): void |
| 20 | + { |
| 21 | + $className = $this->generateClassFromFile( |
| 22 | + 'BasicSchema.json', |
| 23 | + (new GeneratorConfiguration())->setSerialization(true) |
| 24 | + ); |
| 25 | + $object = new $className(['name' => 'Albert']); |
| 26 | + |
| 27 | + $this->assertTrue(is_callable([$object, 'populate'])); |
| 28 | + |
| 29 | + // test an empty populate call doesn't change the internal behaviour |
| 30 | + $object->populate([]); |
| 31 | + $this->assertSame(['name' => 'Albert'], $object->getRawModelDataInput()); |
| 32 | + $this->assertSame(['name' => 'Albert', 'age' => null], $object->toArray()); |
| 33 | + |
| 34 | + // test adding an additional property to the model |
| 35 | + $object->populate(['birthdate' => '10.10.1990']); |
| 36 | + $this->assertSame(['name' => 'Albert', 'birthdate' => '10.10.1990'], $object->getRawModelDataInput()); |
| 37 | + $this->assertSame(['name' => 'Albert', 'age' => null], $object->toArray()); |
| 38 | + |
| 39 | + // test overwriting a single property |
| 40 | + $object->populate(['age' => 30]); |
| 41 | + $this->assertSame( |
| 42 | + ['name' => 'Albert', 'birthdate' => '10.10.1990', 'age' => 30], |
| 43 | + $object->getRawModelDataInput() |
| 44 | + ); |
| 45 | + $this->assertSame(['name' => 'Albert', 'age' => 30], $object->toArray()); |
| 46 | + |
| 47 | + // test overwriting multiple properties |
| 48 | + $object->populate(['age' => 26, 'name' => 'Harry']); |
| 49 | + $this->assertSame( |
| 50 | + ['name' => 'Harry', 'birthdate' => '10.10.1990', 'age' => 26], |
| 51 | + $object->getRawModelDataInput() |
| 52 | + ); |
| 53 | + $this->assertSame(['name' => 'Harry', 'age' => 26], $object->toArray()); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @dataProvider invalidPopulateDataProvider |
| 58 | + */ |
| 59 | + public function testInvalidPopulateThrowsAnException( |
| 60 | + array $data, |
| 61 | + bool $collectErrors, |
| 62 | + string $expectedException, |
| 63 | + string $expectedMessage |
| 64 | + ): void { |
| 65 | + $this->expectException($expectedException); |
| 66 | + $this->expectExceptionMessage($expectedMessage); |
| 67 | + |
| 68 | + $className = $this->generateClassFromFile( |
| 69 | + 'BasicSchema.json', |
| 70 | + (new GeneratorConfiguration())->setCollectErrors($collectErrors) |
| 71 | + ); |
| 72 | + $object = new $className(['name' => 'Albert', 'age' => 30]); |
| 73 | + |
| 74 | + try { |
| 75 | + $object->populate($data); |
| 76 | + $this->fail('No exception thrown'); |
| 77 | + } catch (Exception $exception) { |
| 78 | + // test if the internal state hasn't been changed |
| 79 | + $this->assertSame(['name' => 'Albert', 'age' => 30], $object->getRawModelDataInput()); |
| 80 | + |
| 81 | + throw $exception; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public function invalidPopulateDataProvider(): array |
| 86 | + { |
| 87 | + return [ |
| 88 | + 'No error collection - multiple violations' => [ |
| 89 | + ['name' => 'Anne-Marie', 'age' => false], |
| 90 | + false, |
| 91 | + PatternException::class, |
| 92 | + "Value for name doesn't match pattern ^[a-zA-Z]*$" |
| 93 | + ], |
| 94 | + 'Error collection - multiple violations' => [ |
| 95 | + ['name' => 'Anne-Marie', 'age' => false], |
| 96 | + true, |
| 97 | + ErrorRegistryException::class, |
| 98 | + "Value for name doesn't match pattern ^[a-zA-Z]*$ |
| 99 | +Value for name must not be longer than 8 |
| 100 | +Invalid type for age. Requires int, got boolean" |
| 101 | + ], |
| 102 | + 'Invalid additional property' => [ |
| 103 | + ['numeric' => 9], |
| 104 | + false, |
| 105 | + InvalidAdditionalPropertiesException::class, |
| 106 | + "contains invalid additional properties. |
| 107 | + - invalid additional property 'numeric' |
| 108 | + * Invalid type for additional property. Requires string, got integer" |
| 109 | + ], |
| 110 | + 'Invalid additional property name' => [ |
| 111 | + ['invalid name' => 'Hi'], |
| 112 | + false, |
| 113 | + InvalidPropertyNamesException::class, |
| 114 | + "contains properties with invalid names. |
| 115 | + - invalid property 'invalid name' |
| 116 | + * Value for property name doesn't match pattern ^[a-zA-Z]*$" |
| 117 | + ], |
| 118 | + 'Too many properties' => [ |
| 119 | + ['additional' => 'Hi', 'another' => 'Ho'], |
| 120 | + false, |
| 121 | + MaxPropertiesException::class, |
| 122 | + "must not contain more than 3 properties" |
| 123 | + ], |
| 124 | + ]; |
| 125 | + } |
| 126 | +} |
0 commit comments