-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathProblemNormalizerTest.php
130 lines (113 loc) · 5.27 KB
/
ProblemNormalizerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Tests\Normalizer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Messenger\Exception\ValidationFailedException as MessageValidationFailedException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Exception\PartialDenormalizationException;
use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer;
use Symfony\Component\Serializer\Normalizer\ProblemNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Exception\ValidationFailedException;
class ProblemNormalizerTest extends TestCase
{
private ProblemNormalizer $normalizer;
protected function setUp(): void
{
$this->normalizer = new ProblemNormalizer(false);
}
public function testSupportNormalization()
{
$this->assertTrue($this->normalizer->supportsNormalization(FlattenException::createFromThrowable(new \Exception())));
$this->assertFalse($this->normalizer->supportsNormalization(new \Exception()));
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
}
public function testNormalize()
{
$expected = [
'type' => 'https://tools.ietf.org/html/rfc2616#section-10',
'title' => 'An error occurred',
'status' => 500,
'detail' => 'Internal Server Error',
];
$this->assertSame($expected, $this->normalizer->normalize(FlattenException::createFromThrowable(new \RuntimeException('Error'))));
}
public function testNormalizePartialDenormalizationException()
{
$this->normalizer->setSerializer(new Serializer());
$expected = [
'type' => 'https://symfony.com/errors/validation',
'title' => 'Validation Failed',
'status' => 422,
'detail' => 'foo: This value should be of type int.',
'violations' => [
[
'propertyPath' => 'foo',
'title' => 'This value should be of type int.',
'template' => 'This value should be of type {{ type }}.',
'parameters' => [
'{{ type }}' => 'int',
],
'hint' => 'Invalid value',
],
],
];
$exception = NotNormalizableValueException::createForUnexpectedDataType('Invalid value', null, ['int'], 'foo', true);
$exception = new PartialDenormalizationException('Validation Failed', [$exception]);
$exception = new HttpException(422, 'Validation Failed', $exception);
$this->assertSame($expected, $this->normalizer->normalize(FlattenException::createFromThrowable($exception), null, ['exception' => $exception]));
}
public function testNormalizeValidationFailedException()
{
$this->normalizer->setSerializer(new Serializer([new ConstraintViolationListNormalizer()]));
$expected = [
'type' => 'https://symfony.com/errors/validation',
'title' => 'Validation Failed',
'status' => 422,
'detail' => 'Invalid value',
'violations' => [
[
'propertyPath' => '',
'title' => 'Invalid value',
'template' => '',
'parameters' => [],
],
],
];
$exception = new ValidationFailedException('Validation Failed', new ConstraintViolationList([new ConstraintViolation('Invalid value', '', [], '', null, null)]));
$exception = new HttpException(422, 'Validation Failed', $exception);
$this->assertSame($expected, $this->normalizer->normalize(FlattenException::createFromThrowable($exception), null, ['exception' => $exception]));
}
public function testNormalizeMessageValidationFailedException()
{
$this->normalizer->setSerializer(new Serializer([new ConstraintViolationListNormalizer()]));
$expected = [
'type' => 'https://symfony.com/errors/validation',
'title' => 'Validation Failed',
'status' => 422,
'detail' => 'Invalid value',
'violations' => [
[
'propertyPath' => '',
'title' => 'Invalid value',
'template' => '',
'parameters' => [],
],
],
];
$exception = new MessageValidationFailedException(new \stdClass(), new ConstraintViolationList([new ConstraintViolation('Invalid value', '', [], '', null, null)]));
$exception = new HttpException(422, 'Validation Failed', $exception);
$this->assertSame($expected, $this->normalizer->normalize(FlattenException::createFromThrowable($exception), null, ['exception' => $exception]));
}
}