|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace PHPModelGenerator\Tests\Basic; |
| 6 | + |
| 7 | +use PHPModelGenerator\Exception\ErrorRegistryException; |
| 8 | +use PHPModelGenerator\Model\GeneratorConfiguration; |
| 9 | +use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTest; |
| 10 | +use stdClass; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class ErrorCollectionTest |
| 14 | + * |
| 15 | + * @package PHPModelGenerator\Tests\Basic |
| 16 | + */ |
| 17 | +class ErrorCollectionTest extends AbstractPHPModelGeneratorTest |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @dataProvider validValuesForSinglePropertyDataProvider |
| 21 | + * |
| 22 | + * @param string $value |
| 23 | + */ |
| 24 | + public function testValidValuesForMultipleChecksForSingleProperty(string $value): void |
| 25 | + { |
| 26 | + $className = $this->generateClassFromFile( |
| 27 | + 'MultipleChecksForSingleProperty.json', |
| 28 | + (new GeneratorConfiguration())->setCollectErrors(true) |
| 29 | + ); |
| 30 | + |
| 31 | + $object = new $className(['property' => $value]); |
| 32 | + $this->assertSame($value, $object->getProperty()); |
| 33 | + } |
| 34 | + |
| 35 | + public function validValuesForSinglePropertyDataProvider(): array |
| 36 | + { |
| 37 | + return [ |
| 38 | + 'numeric string' => ['10'], |
| 39 | + 'letter string' => ['Ab'], |
| 40 | + 'special chars' => ['+.'], |
| 41 | + ]; |
| 42 | + } |
| 43 | + /** |
| 44 | + * @dataProvider invalidValuesForSinglePropertyDataProvider |
| 45 | + * |
| 46 | + * @param string $value |
| 47 | + */ |
| 48 | + public function testInvalidValuesForMultipleChecksForSinglePropertyThrowsAnException( |
| 49 | + $value, |
| 50 | + array $messages |
| 51 | + ): void { |
| 52 | + $this->expectExceptionObject($this->getErrorRegistryException($messages)); |
| 53 | + |
| 54 | + $className = $this->generateClassFromFile( |
| 55 | + 'MultipleChecksForSingleProperty.json', |
| 56 | + (new GeneratorConfiguration())->setCollectErrors(true) |
| 57 | + ); |
| 58 | + |
| 59 | + new $className(['property' => $value]); |
| 60 | + } |
| 61 | + |
| 62 | + public function invalidValuesForSinglePropertyDataProvider(): array |
| 63 | + { |
| 64 | + return [ |
| 65 | + 'pattern invalid' => [ |
| 66 | + ' ', |
| 67 | + ['property doesn\'t match pattern ^[^\s]+$'] |
| 68 | + ], |
| 69 | + 'length invalid' => [ |
| 70 | + 'a', |
| 71 | + ['property must not be shorter than 2'] |
| 72 | + ], |
| 73 | + 'pattern and length invalid' => [ |
| 74 | + ' ', |
| 75 | + [ |
| 76 | + 'property doesn\'t match pattern ^[^\s]+$', |
| 77 | + 'property must not be shorter than 2' |
| 78 | + ] |
| 79 | + ], |
| 80 | + 'null' => [null, ['invalid type for property']], |
| 81 | + 'int' => [1, ['invalid type for property']], |
| 82 | + 'float' => [0.92, ['invalid type for property']], |
| 83 | + 'bool' => [true, ['invalid type for property']], |
| 84 | + 'array' => [[], ['invalid type for property']], |
| 85 | + 'object' => [new stdClass(), ['invalid type for property']], |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Set up an ErrorRegistryException containing the given messages |
| 91 | + * |
| 92 | + * @param array $messages |
| 93 | + * |
| 94 | + * @return ErrorRegistryException |
| 95 | + */ |
| 96 | + protected function getErrorRegistryException(array $messages): ErrorRegistryException |
| 97 | + { |
| 98 | + $errorRegistry = new ErrorRegistryException(); |
| 99 | + |
| 100 | + foreach ($messages as $message) { |
| 101 | + $errorRegistry->addError($message); |
| 102 | + } |
| 103 | + |
| 104 | + return $errorRegistry; |
| 105 | + } |
| 106 | +} |
0 commit comments