|
3 | 3 | namespace PHPModelGenerator\Tests; |
4 | 4 |
|
5 | 5 | use FilesystemIterator; |
| 6 | +use PHPModelGenerator\Exception\ErrorRegistryException; |
6 | 7 | use PHPModelGenerator\Exception\FileSystemException; |
7 | 8 | use PHPModelGenerator\Exception\RenderException; |
8 | 9 | use PHPModelGenerator\Exception\SchemaException; |
| 10 | +use PHPModelGenerator\Exception\ValidationException; |
9 | 11 | use PHPModelGenerator\ModelGenerator; |
10 | 12 | use PHPModelGenerator\Model\GeneratorConfiguration; |
11 | 13 | use PHPUnit\Framework\TestCase; |
@@ -230,6 +232,61 @@ protected function combineDataProvider(array $dataProvider1, array $dataProvider |
230 | 232 | return $result; |
231 | 233 | } |
232 | 234 |
|
| 235 | + protected function expectValidationError(GeneratorConfiguration $configuration, $messages): void |
| 236 | + { |
| 237 | + if (!is_array($messages)) { |
| 238 | + $messages = [$messages]; |
| 239 | + } |
| 240 | + |
| 241 | + if ($configuration->collectErrors()) { |
| 242 | + $this->expectExceptionObject($this->getErrorRegistryException($messages)); |
| 243 | + } else { |
| 244 | + $this->expectException(ValidationException::class); |
| 245 | + $this->expectExceptionMessage($messages[0]); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + protected function expectValidationErrorRegExp(GeneratorConfiguration $configuration, $messages) |
| 250 | + { |
| 251 | + if (!is_array($messages)) { |
| 252 | + $messages = [$messages]; |
| 253 | + } |
| 254 | + |
| 255 | + if ($configuration->collectErrors()) { |
| 256 | + $exception = $this->getErrorRegistryException($messages); |
| 257 | + $this->expectException(get_class($exception)); |
| 258 | + $this->expectExceptionMessageRegExp($exception->getMessage()); |
| 259 | + } else { |
| 260 | + $this->expectException(ValidationException::class); |
| 261 | + $this->expectExceptionMessageRegExp($messages[0]); |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + /** |
| 266 | + * Set up an ErrorRegistryException containing the given messages |
| 267 | + * |
| 268 | + * @param array $messages |
| 269 | + * |
| 270 | + * @return ErrorRegistryException |
| 271 | + */ |
| 272 | + protected function getErrorRegistryException(array $messages): ErrorRegistryException |
| 273 | + { |
| 274 | + $errorRegistry = new ErrorRegistryException(); |
| 275 | + |
| 276 | + foreach ($messages as $message) { |
| 277 | + $errorRegistry->addError($message); |
| 278 | + } |
| 279 | + |
| 280 | + return $errorRegistry; |
| 281 | + } |
| 282 | + |
| 283 | + public function validationMethodDataProvider(): array { |
| 284 | + return [ |
| 285 | + 'Error Collection' => [new GeneratorConfiguration()], |
| 286 | + 'Direct Exception' => [(new GeneratorConfiguration())->setCollectErrors(false)], |
| 287 | + ]; |
| 288 | + } |
| 289 | + |
233 | 290 | /** |
234 | 291 | * Get the annotated type for an object property |
235 | 292 | * |
|
0 commit comments