|
14 | 14 | namespace ApiPlatform\State\Tests; |
15 | 15 |
|
16 | 16 | use ApiPlatform\Metadata\Get; |
| 17 | +use ApiPlatform\State\ApiResource\Error; |
17 | 18 | use ApiPlatform\State\ErrorProvider; |
| 19 | +use ApiPlatform\Validator\Exception\ValidationException; |
18 | 20 | use PHPUnit\Framework\TestCase; |
19 | 21 | use Symfony\Component\HttpFoundation\Request; |
| 22 | +use Symfony\Component\Validator\ConstraintViolation; |
| 23 | +use Symfony\Component\Validator\ConstraintViolationList; |
20 | 24 |
|
21 | 25 | class ErrorProviderTest extends TestCase |
22 | 26 | { |
| 27 | + public function testCreateFromExceptionWithValidationException(): void |
| 28 | + { |
| 29 | + $violation = new ConstraintViolation('This value is too long.', null, [], null, 'name', 'toolong'); |
| 30 | + $exception = new ValidationException(new ConstraintViolationList([$violation])); |
| 31 | + $error = Error::createFromException($exception, 422); |
| 32 | + |
| 33 | + $this->assertSame('An error occurred', $error->getTitle()); |
| 34 | + $this->assertSame($exception->getMessage(), $error->getDetail()); |
| 35 | + $this->assertSame(422, $error->getStatus()); |
| 36 | + } |
| 37 | + |
23 | 38 | public function testErrorProviderProduction(): void |
24 | 39 | { |
25 | 40 | $provider = new ErrorProvider(debug: false); |
26 | 41 | $request = Request::create('/'); |
27 | 42 | $request->attributes->set('exception', new \Exception()); |
28 | | - /** @var \ApiPlatform\State\ApiResource\Error */ |
| 43 | + /** @var Error */ |
29 | 44 | $error = $provider->provide(new Get(), [], ['request' => $request]); |
30 | 45 | $this->assertEquals('Internal Server Error', $error->getDetail()); |
31 | 46 | } |
|
0 commit comments