|
| 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 | +} |
0 commit comments