|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PHPModelGenerator\Tests\Issues\Issue; |
| 6 | + |
| 7 | +use PHPModelGenerator\Exception\Dependency\InvalidSchemaDependencyException; |
| 8 | +use PHPModelGenerator\Exception\Generic\InvalidTypeException; |
| 9 | +use PHPModelGenerator\Exception\Object\RequiredValueException; |
| 10 | +use PHPModelGenerator\Tests\Issues\AbstractIssueTestCase; |
| 11 | + |
| 12 | +class Issue86Test extends AbstractIssueTestCase |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @dataProvider validRefDataProvider |
| 16 | + */ |
| 17 | + public function testDifferentRequiredConfigurationForReferencedObject(array $input, bool $implicitNull): void |
| 18 | + { |
| 19 | + $className = $this->generateClassFromFile('ref.json', implicitNull: $implicitNull); |
| 20 | + |
| 21 | + $object = new $className($input); |
| 22 | + $this->assertSame($input['required'], $object->getRequired()); |
| 23 | + $this->assertSame($input['optional'] ?? null, $object->getOptional()); |
| 24 | + } |
| 25 | + |
| 26 | + public function validRefDataProvider(): array |
| 27 | + { |
| 28 | + return [ |
| 29 | + 'both properties provided' => [['required' => 'Hello', 'optional' => 'World'], true], |
| 30 | + 'optional property not provided' => [['required' => 'Hello'], false], |
| 31 | + 'optional property null' => [['required' => 'Hello', 'optional' => null], true], |
| 32 | + ]; |
| 33 | + } |
| 34 | + /** |
| 35 | + * @dataProvider invalidRefDataProvider |
| 36 | + */ |
| 37 | + public function testDifferentRequiredConfigurationForReferencedObjectWithInvalidInput( |
| 38 | + array $input, |
| 39 | + bool $implicitNull, |
| 40 | + string $expectedException, |
| 41 | + ): void { |
| 42 | + $className = $this->generateClassFromFile('ref.json', implicitNull: $implicitNull); |
| 43 | + |
| 44 | + $this->expectException($expectedException); |
| 45 | + |
| 46 | + new $className($input); |
| 47 | + } |
| 48 | + |
| 49 | + public function invalidRefDataProvider(): array |
| 50 | + { |
| 51 | + return [ |
| 52 | + 'required property not provided' => [['optional' => 'World'], true, RequiredValueException::class], |
| 53 | + 'optional property null - implicit null disabled' => [['required' => 'Hello', 'optional' => null], false, InvalidTypeException::class], |
| 54 | + 'required property wrong type' => [['required' => 1, 'optional' => 'World'], true, InvalidTypeException::class], |
| 55 | + 'optional property wrong type' => [['required' => 'Hello', 'optional' => 1], true, InvalidTypeException::class], |
| 56 | + ]; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @dataProvider validSchemaDependencyDataProvider |
| 61 | + */ |
| 62 | + public function testDifferentSchemaDependenciesForReferencedObject(array $input): void |
| 63 | + { |
| 64 | + $className = $this->generateClassFromFile('schemaDependency.json'); |
| 65 | + |
| 66 | + $object = new $className($input); |
| 67 | + $this->assertSame($input['property1'] ?? null, $object->getProperty1()); |
| 68 | + $this->assertSame($input['property2'] ?? null, $object->getProperty2()); |
| 69 | + $this->assertSame($input['property3'] ?? null, $object->getProperty3()); |
| 70 | + } |
| 71 | + |
| 72 | + public function validSchemaDependencyDataProvider(): array |
| 73 | + { |
| 74 | + return [ |
| 75 | + 'No properties provided' => [[]], |
| 76 | + 'Property 3 without dependency' => [['property3' => true]], |
| 77 | + 'Dependency Property 1' => [['property1' => 'Hello', 'property3' => 'World']], |
| 78 | + 'Dependency Property 2' => [['property2' => 'Hello', 'property3' => 1]], |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @dataProvider invalidSchemaDependencyDataProvider |
| 84 | + */ |
| 85 | + public function testDifferentSchemaDependenciesForReferencedObjectWithInvalidInput(array $input): void |
| 86 | + { |
| 87 | + $className = $this->generateClassFromFile('schemaDependency.json'); |
| 88 | + |
| 89 | + $this->expectException(InvalidSchemaDependencyException::class); |
| 90 | + |
| 91 | + new $className($input); |
| 92 | + } |
| 93 | + |
| 94 | + public function invalidSchemaDependencyDataProvider(): array |
| 95 | + { |
| 96 | + return [ |
| 97 | + 'Dependency missing property 1' => [['property1' => 'Hello']], |
| 98 | + 'Dependency missing property 2' => [['property2' => 'Hello']], |
| 99 | + 'Dependency Property 1 invalid type' => [['property1' => 'Hello', 'property3' => 1]], |
| 100 | + 'Dependency Property 2 invalid type' => [['property2' => 'Hello', 'property3' => 'World']], |
| 101 | + 'Both dependencies required 1' => [['property1' => 'Hello', 'property2' => 'Hello', 'property3' => 'World']], |
| 102 | + 'Both dependencies required 2' => [['property1' => 'Hello', 'property2' => 'Hello', 'property3' => 1]], |
| 103 | + ]; |
| 104 | + } |
| 105 | +} |
0 commit comments