Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef0b8b3

Browse files
committed
Added FlattenExceptionNormalizer
1 parent 9d882e8 commit ef0b8b3

File tree

4 files changed

+266
-0
lines changed

4 files changed

+266
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
<service id="Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface" alias="serializer.mapping.class_discriminator_resolver" />
3232

3333
<!-- Normalizer -->
34+
<service id="serializer.normalizer.flatten_exception" class="Symfony\Component\Serializer\Normalizer\FlattenExceptionNormalizer">
35+
<!-- Run before serializer.normalizer.object -->
36+
<tag name="serializer.normalizer" priority="-915" />
37+
</service>
38+
3439
<service id="serializer.normalizer.constraint_violation_list" class="Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer">
3540
<argument type="collection" />
3641
<argument type="service" id="serializer.name_converter.metadata_aware" />
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Normalizer;
13+
14+
use Symfony\Component\Debug\Exception\FlattenException;
15+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
17+
18+
/**
19+
* Normalizes FlattenException instances.
20+
*
21+
* @author Pascal Luna <[email protected]>
22+
*/
23+
class FlattenExceptionNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*
28+
* @throws InvalidArgumentException
29+
*/
30+
public function normalize($object, $format = null, array $context = [])
31+
{
32+
if (!$object instanceof FlattenException) {
33+
throw new InvalidArgumentException(sprintf('The object must be an instance of %s.', FlattenException::class));
34+
}
35+
/* @var FlattenException $object */
36+
37+
return [
38+
'message' => $object->getMessage(),
39+
'code' => $object->getCode(),
40+
'status_code' => $object->getStatusCode(),
41+
'headers' => $object->getHeaders(),
42+
'class' => $object->getClass(),
43+
'file' => $object->getFile(),
44+
'line' => $object->getLine(),
45+
'previous' => null === $object->getPrevious() ? null : $this->normalize($object->getPrevious(), $format, $context),
46+
'trace' => $object->getTrace(),
47+
];
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function supportsNormalization($data, $format = null)
54+
{
55+
return $data instanceof FlattenException;
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function denormalize($data, $type, $format = null, array $context = [])
62+
{
63+
if (!\is_array($data)) {
64+
throw new NotNormalizableValueException(sprintf(
65+
'The normalized data must be an array, got %s.',
66+
\is_object($data) ? \get_class($data) : \gettype($data)
67+
));
68+
}
69+
70+
$object = new FlattenException();
71+
72+
$object->setMessage($data['message'] ?? null);
73+
$object->setCode($data['code'] ?? null);
74+
$object->setStatusCode($data['status_code'] ?? null);
75+
$object->setClass($data['class'] ?? null);
76+
$object->setFile($data['file'] ?? null);
77+
$object->setLine($data['line'] ?? null);
78+
79+
if (isset($data['headers'])) {
80+
$object->setHeaders((array) $data['headers']);
81+
}
82+
if (isset($data['previous'])) {
83+
$object->setPrevious($this->denormalize($data['previous'], $type, $format, $context));
84+
}
85+
if (isset($data['trace'])) {
86+
$property = new \ReflectionProperty(FlattenException::class, 'trace');
87+
$property->setAccessible(true);
88+
$property->setValue($object, (array) $data['trace']);
89+
}
90+
91+
return $object;
92+
}
93+
94+
/**
95+
* {@inheritdoc}
96+
*/
97+
public function supportsDenormalization($data, $type, $format = null, array $context = [])
98+
{
99+
return FlattenException::class === $type;
100+
}
101+
102+
/**
103+
* {@inheritdoc}
104+
*/
105+
public function hasCacheableSupportsMethod(): bool
106+
{
107+
return __CLASS__ === \get_class($this);
108+
}
109+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Normalizer;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Debug\Exception\FlattenException;
16+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
17+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
18+
use Symfony\Component\Serializer\Normalizer\FlattenExceptionNormalizer;
19+
20+
/**
21+
* @author Pascal Luna <[email protected]>
22+
*/
23+
class FlattenExceptionNormalizerTest extends TestCase
24+
{
25+
/**
26+
* @var FlattenExceptionNormalizer
27+
*/
28+
private $normalizer;
29+
30+
protected function setUp(): void
31+
{
32+
$this->normalizer = new FlattenExceptionNormalizer();
33+
}
34+
35+
public function testSupportsNormalization(): void
36+
{
37+
$this->assertTrue($this->normalizer->supportsNormalization(new FlattenException()));
38+
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
39+
}
40+
41+
/**
42+
* @dataProvider provideFlattenException
43+
*/
44+
public function testNormalize(FlattenException $exception): void
45+
{
46+
$normalized = $this->normalizer->normalize($exception);
47+
$previous = null === $exception->getPrevious() ? null : $this->normalizer->normalize($exception->getPrevious());
48+
49+
$this->assertSame($exception->getMessage(), $normalized['message']);
50+
$this->assertSame($exception->getCode(), $normalized['code']);
51+
$this->assertSame($exception->getStatusCode(), $normalized['status_code']);
52+
$this->assertSame($exception->getHeaders(), $normalized['headers']);
53+
$this->assertSame($exception->getClass(), $normalized['class']);
54+
$this->assertSame($exception->getFile(), $normalized['file']);
55+
$this->assertSame($exception->getLine(), $normalized['line']);
56+
$this->assertSame($previous, $normalized['previous']);
57+
$this->assertSame($exception->getTrace(), $normalized['trace']);
58+
}
59+
60+
public function provideFlattenException(): array
61+
{
62+
return [
63+
'instance from constructor' => [new FlattenException()],
64+
'instance from exception' => [FlattenException::createFromThrowable(new \RuntimeException('foo', 42))],
65+
'instance with previous exception' => [FlattenException::createFromThrowable(new \RuntimeException('foo', 42, new \Exception()))],
66+
'instance with headers' => [FlattenException::createFromThrowable(new \RuntimeException('foo', 42), 404, ['Foo' => 'Bar'])],
67+
];
68+
}
69+
70+
public function testNormalizeBadObjectTypeThrowsException(): void
71+
{
72+
$this->expectException(InvalidArgumentException::class);
73+
$this->normalizer->normalize(new \stdClass());
74+
}
75+
76+
public function testSupportsDenormalization(): void
77+
{
78+
$this->assertTrue($this->normalizer->supportsDenormalization(null, FlattenException::class));
79+
$this->assertFalse($this->normalizer->supportsDenormalization(null, \stdClass::class));
80+
}
81+
82+
public function testDenormalizeValidData(): void
83+
{
84+
$normalized = [];
85+
$exception = $this->normalizer->denormalize($normalized, FlattenException::class);
86+
87+
$this->assertInstanceOf(FlattenException::class, $exception);
88+
$this->assertNull($exception->getMessage());
89+
$this->assertNull($exception->getCode());
90+
$this->assertNull($exception->getStatusCode());
91+
$this->assertNull($exception->getHeaders());
92+
$this->assertNull($exception->getClass());
93+
$this->assertNull($exception->getFile());
94+
$this->assertNull($exception->getLine());
95+
$this->assertNull($exception->getPrevious());
96+
$this->assertNull($exception->getTrace());
97+
98+
$normalized = [
99+
'message' => 'Something went foobar.',
100+
'code' => 42,
101+
'status_code' => 404,
102+
'headers' => ['Content-Type' => 'application/json'],
103+
'class' => \get_class($this),
104+
'file' => 'foo.php',
105+
'line' => 123,
106+
'previous' => [
107+
'message' => 'Previous exception',
108+
'code' => 0,
109+
],
110+
'trace' => [
111+
[
112+
'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => '', 'file' => 'foo.php', 'line' => 123, 'args' => [],
113+
],
114+
],
115+
];
116+
$exception = $this->normalizer->denormalize($normalized, FlattenException::class);
117+
118+
$this->assertInstanceOf(FlattenException::class, $exception);
119+
$this->assertSame($normalized['message'], $exception->getMessage());
120+
$this->assertSame($normalized['code'], $exception->getCode());
121+
$this->assertSame($normalized['status_code'], $exception->getStatusCode());
122+
$this->assertSame($normalized['headers'], $exception->getHeaders());
123+
$this->assertSame($normalized['class'], $exception->getClass());
124+
$this->assertSame($normalized['file'], $exception->getFile());
125+
$this->assertSame($normalized['line'], $exception->getLine());
126+
$this->assertSame($normalized['trace'], $exception->getTrace());
127+
128+
$this->assertInstanceOf(FlattenException::class, $previous = $exception->getPrevious());
129+
$this->assertSame($normalized['previous']['message'], $previous->getMessage());
130+
$this->assertSame($normalized['previous']['code'], $previous->getCode());
131+
}
132+
133+
/**
134+
* @dataProvider provideInvalidNormalizedData
135+
*/
136+
public function testDenormalizeInvalidDataThrowsException($normalized): void
137+
{
138+
$this->expectException(NotNormalizableValueException::class);
139+
$this->normalizer->denormalize($normalized, FlattenException::class);
140+
}
141+
142+
public function provideInvalidNormalizedData(): array
143+
{
144+
return [
145+
'null' => [null],
146+
'string' => ['foo'],
147+
'integer' => [42],
148+
'object' => [new \stdClass()],
149+
];
150+
}
151+
}

src/Symfony/Component/Serializer/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"symfony/property-access": "~3.4|~4.0",
2626
"symfony/http-foundation": "~3.4|~4.0",
2727
"symfony/cache": "~3.4|~4.0",
28+
"symfony/debug": "~3.4|~4.0",
2829
"symfony/property-info": "^3.4.13|~4.0",
2930
"symfony/validator": "~3.4|~4.0",
3031
"doctrine/annotations": "~1.0",

0 commit comments

Comments
 (0)