These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Lamoda\IsmpClient\Tests\Impl\Serializer; |
||
| 6 | |||
| 7 | use Lamoda\IsmpClient\Impl\Serializer\FacadeCisListResponseDenormalizer; |
||
| 8 | use Lamoda\IsmpClient\V3\Dto\FacadeCisItemResponse; |
||
| 9 | use Lamoda\IsmpClient\V3\Dto\FacadeCisListResponse; |
||
| 10 | use PHPUnit\Framework\MockObject\MockObject; |
||
| 11 | use PHPUnit\Framework\TestCase; |
||
| 12 | use Symfony\Component\Serializer\Exception\BadMethodCallException; |
||
| 13 | use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
||
| 14 | use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
||
| 15 | |||
| 16 | final class FacadeCisListResponseDenormalizerTest extends TestCase |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var FacadeCisListResponseDenormalizer |
||
| 20 | */ |
||
| 21 | private $denormalizer; |
||
| 22 | /** |
||
| 23 | * @var MockObject|DenormalizerInterface |
||
| 24 | */ |
||
| 25 | private $inner; |
||
| 26 | |||
| 27 | protected function setUp(): void |
||
| 28 | { |
||
| 29 | $this->inner = $this->createMock(DenormalizerInterface::class); |
||
| 30 | $this->denormalizer = new FacadeCisListResponseDenormalizer(); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @dataProvider dataSupportsDenormalization |
||
| 35 | */ |
||
| 36 | public function testSupportsDenormalization(string $type, bool $expected): void |
||
| 37 | { |
||
| 38 | $result = $this->denormalizer->supportsDenormalization([], $type); |
||
| 39 | |||
| 40 | $this->assertEquals($expected, $result); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function dataSupportsDenormalization(): array |
||
| 44 | { |
||
| 45 | return [ |
||
| 46 | [ |
||
| 47 | FacadeCisListResponse::class, |
||
| 48 | true, |
||
| 49 | ], |
||
| 50 | [ |
||
| 51 | \stdClass::class, |
||
| 52 | false |
||
| 53 | ] |
||
| 54 | ]; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function testDenormalizationWhenDenormalizerIsNotSet(): void |
||
| 58 | { |
||
| 59 | $this->expectException(BadMethodCallException::class); |
||
| 60 | $this->denormalizer->denormalize([], FacadeCisListResponse::class); |
||
| 61 | } |
||
| 62 | |||
| 63 | View Code Duplication | public function testDenormalizationWhenWrongData(): void |
|
| 64 | { |
||
| 65 | $this->denormalizer->setDenormalizer($this->inner); |
||
| 66 | |||
| 67 | $this->expectException(InvalidArgumentException::class); |
||
| 68 | $this->denormalizer->denormalize('wrong', FacadeCisListResponse::class); |
||
| 69 | } |
||
| 70 | |||
| 71 | View Code Duplication | public function testDenormalizationWhenWrongType(): void |
|
| 72 | { |
||
| 73 | $this->denormalizer->setDenormalizer($this->inner); |
||
| 74 | |||
| 75 | $this->expectException(InvalidArgumentException::class); |
||
| 76 | $this->denormalizer->denormalize([], \stdClass::class); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function testDenormalizationWorks(): void |
||
| 80 | { |
||
| 81 | $this->denormalizer->setDenormalizer($this->inner); |
||
| 82 | |||
| 83 | $item1 = new FacadeCisItemResponse('', '', '', '', 0, '', '', '', '', '', 0); |
||
| 84 | $item2 = new FacadeCisItemResponse('', '', '', '', 0, '', '', '', '', '', 0); |
||
| 85 | |||
| 86 | $expectedResult = new FacadeCisListResponse($item1, $item2); |
||
| 87 | |||
| 88 | $this->inner->expects($this->exactly(2)) |
||
| 89 | ->method('denormalize') |
||
| 90 | ->withConsecutive( |
||
| 91 | ['first', FacadeCisItemResponse::class, null, []], |
||
| 92 | ['second', FacadeCisItemResponse::class, null, []] |
||
| 93 | ) |
||
| 94 | ->willReturnOnConsecutiveCalls( |
||
| 95 | $item1, |
||
| 96 | $item2 |
||
| 97 | ); |
||
| 98 | |||
| 99 | $result = $this->denormalizer->denormalize([ |
||
| 100 | 'first', |
||
| 101 | 'second' |
||
| 102 | ], FacadeCisListResponse::class); |
||
| 103 | |||
| 104 | $this->assertEquals($expectedResult, $result); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function testHasCacheableSupportsMethod(): void |
||
| 108 | { |
||
| 109 | $this->denormalizer->setDenormalizer($this->inner); |
||
|
0 ignored issues
–
show
|
|||
| 110 | |||
| 111 | $result = $this->denormalizer->hasCacheableSupportsMethod(); |
||
| 112 | $this->assertFalse($result); |
||
| 113 | } |
||
| 114 | } |
||
| 115 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.