|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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 | +namespaceSymfony\Component\Messenger\Tests\Transport\Serialization\Normalizer; |
| 13 | + |
| 14 | +usePHPUnit\Framework\TestCase; |
| 15 | +useSymfony\Component\ErrorHandler\Exception\FlattenException; |
| 16 | +useSymfony\Component\Messenger\Transport\Serialization\Normalizer\FlattenExceptionNormalizer; |
| 17 | +useSymfony\Component\Messenger\Transport\Serialization\Serializer; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Pascal Luna <skalpa@zetareticuli.org> |
| 21 | + */ |
| 22 | +class FlattenExceptionNormalizerTestextends TestCase |
| 23 | +{ |
| 24 | +/** |
| 25 | + * @var FlattenExceptionNormalizer |
| 26 | + */ |
| 27 | +private$normalizer; |
| 28 | + |
| 29 | +protectedfunctionsetUp():void |
| 30 | + { |
| 31 | +$this->normalizer =newFlattenExceptionNormalizer(); |
| 32 | + } |
| 33 | + |
| 34 | +publicfunctiontestSupportsNormalization() |
| 35 | + { |
| 36 | +$this->assertTrue($this->normalizer->supportsNormalization(newFlattenException(),null,$this->getMessengerContext())); |
| 37 | +$this->assertFalse($this->normalizer->supportsNormalization(newFlattenException())); |
| 38 | +$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); |
| 39 | + } |
| 40 | + |
| 41 | +/** |
| 42 | + * @dataProvider provideFlattenException |
| 43 | + */ |
| 44 | +publicfunctiontestNormalize(FlattenException$exception) |
| 45 | + { |
| 46 | +$normalized =$this->normalizer->normalize($exception,null,$this->getMessengerContext()); |
| 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 | +if (null !==$exception->getStatusCode()) { |
| 52 | +$this->assertSame($exception->getStatusCode(),$normalized['status']); |
| 53 | + }else { |
| 54 | +$this->assertArrayNotHasKey('status',$normalized); |
| 55 | + } |
| 56 | +$this->assertSame($exception->getHeaders(),$normalized['headers']); |
| 57 | +$this->assertSame($exception->getClass(),$normalized['class']); |
| 58 | +$this->assertSame($exception->getFile(),$normalized['file']); |
| 59 | +$this->assertSame($exception->getLine(),$normalized['line']); |
| 60 | +$this->assertSame($previous,$normalized['previous']); |
| 61 | +$this->assertSame($exception->getTrace(),$normalized['trace']); |
| 62 | +$this->assertSame($exception->getTraceAsString(),$normalized['trace_as_string']); |
| 63 | + } |
| 64 | + |
| 65 | +publicfunctionprovideFlattenException():array |
| 66 | + { |
| 67 | +return [ |
| 68 | +'instance from exception' => [FlattenException::createFromThrowable(new \RuntimeException('foo',42))], |
| 69 | +'instance with previous exception' => [FlattenException::createFromThrowable(new \RuntimeException('foo',42,new \Exception()))], |
| 70 | +'instance with headers' => [FlattenException::createFromThrowable(new \RuntimeException('foo',42),404, ['Foo' =>'Bar'])], |
| 71 | + ]; |
| 72 | + } |
| 73 | + |
| 74 | +publicfunctiontestSupportsDenormalization() |
| 75 | + { |
| 76 | +$this->assertFalse($this->normalizer->supportsDenormalization(null, FlattenException::class)); |
| 77 | +$this->assertTrue($this->normalizer->supportsDenormalization(null, FlattenException::class,null,$this->getMessengerContext())); |
| 78 | +$this->assertFalse($this->normalizer->supportsDenormalization(null, \stdClass::class)); |
| 79 | + } |
| 80 | + |
| 81 | +publicfunctiontestDenormalizeValidData() |
| 82 | + { |
| 83 | +$normalized = [ |
| 84 | +'message' =>'Something went foobar.', |
| 85 | +'code' =>42, |
| 86 | +'status' =>404, |
| 87 | +'headers' => ['Content-Type' =>'application/json'], |
| 88 | +'class' =>static::class, |
| 89 | +'file' =>'foo.php', |
| 90 | +'line' =>123, |
| 91 | +'previous' => [ |
| 92 | +'message' =>'Previous exception', |
| 93 | +'code' =>0, |
| 94 | +'class' => FlattenException::class, |
| 95 | +'file' =>'foo.php', |
| 96 | +'line' =>123, |
| 97 | +'headers' => ['Content-Type' =>'application/json'], |
| 98 | +'trace' => [ |
| 99 | + [ |
| 100 | +'namespace' =>'','short_class' =>'','class' =>'','type' =>'','function' =>'','file' =>'foo.php','line' =>123,'args' => [], |
| 101 | + ], |
| 102 | + ], |
| 103 | +'trace_as_string' =>'#0 foo.php(123): foo()'.PHP_EOL.'#1 bar.php(456): bar()', |
| 104 | + ], |
| 105 | +'trace' => [ |
| 106 | + [ |
| 107 | +'namespace' =>'','short_class' =>'','class' =>'','type' =>'','function' =>'','file' =>'foo.php','line' =>123,'args' => [], |
| 108 | + ], |
| 109 | + ], |
| 110 | +'trace_as_string' =>'#0 foo.php(123): foo()'.PHP_EOL.'#1 bar.php(456): bar()', |
| 111 | + ]; |
| 112 | +$exception =$this->normalizer->denormalize($normalized, FlattenException::class); |
| 113 | + |
| 114 | +$this->assertInstanceOf(FlattenException::class,$exception); |
| 115 | +$this->assertSame($normalized['message'],$exception->getMessage()); |
| 116 | +$this->assertSame($normalized['code'],$exception->getCode()); |
| 117 | +$this->assertSame($normalized['status'],$exception->getStatusCode()); |
| 118 | +$this->assertSame($normalized['headers'],$exception->getHeaders()); |
| 119 | +$this->assertSame($normalized['class'],$exception->getClass()); |
| 120 | +$this->assertSame($normalized['file'],$exception->getFile()); |
| 121 | +$this->assertSame($normalized['line'],$exception->getLine()); |
| 122 | +$this->assertSame($normalized['trace'],$exception->getTrace()); |
| 123 | +$this->assertSame($normalized['trace_as_string'],$exception->getTraceAsString()); |
| 124 | + |
| 125 | +$this->assertInstanceOf(FlattenException::class,$previous =$exception->getPrevious()); |
| 126 | +$this->assertSame($normalized['previous']['message'],$previous->getMessage()); |
| 127 | +$this->assertSame($normalized['previous']['code'],$previous->getCode()); |
| 128 | + } |
| 129 | + |
| 130 | +privatefunctiongetMessengerContext():array |
| 131 | + { |
| 132 | +return [ |
| 133 | + Serializer::MESSENGER_SERIALIZATION_CONTEXT =>true, |
| 134 | + ]; |
| 135 | + } |
| 136 | +} |