Passed
Push — master ( 69b252...c02560 )
by
unknown
56s queued 11s
created

testDenormalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 47
Ratio 100 %

Importance

Changes 0
Metric Value
dl 47
loc 47
rs 9.1563
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Impl\Serializer\V3;
4
5
use Lamoda\IsmpClient\Impl\Serializer\V3\FacadeDocBodyResponseBodyDenormalizer;
6
use Lamoda\IsmpClient\Tests\Stub\SerializerDenormalizer;
7
use Lamoda\IsmpClient\V3\Dto\FacadeDocBodyResponse\Body;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
use Symfony\Component\Serializer\Exception\BadMethodCallException;
11
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
12
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
13
14 View Code Duplication
final class FacadeDocBodyResponseBodyDenormalizerTest extends TestCase
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    /**
17
     * @var FacadeDocBodyResponseBodyDenormalizer
18
     */
19
    private $denormalizer;
20
    /**
21
     * @var MockObject|DenormalizerInterface
22
     */
23
    private $inner;
24
25
    protected function setUp(): void
26
    {
27
        $this->inner = $this->createMock(SerializerDenormalizer::class);
28
        $this->denormalizer = new FacadeDocBodyResponseBodyDenormalizer();
29
    }
30
31
    /**
32
     * @dataProvider dataSupportsDenormalization
33
     */
34
    public function testSupportsDenormalization(string $type, array $data, bool $expected): void
35
    {
36
        $result = $this->denormalizer->supportsDenormalization($data, $type);
37
38
        $this->assertEquals($expected, $result);
39
    }
40
41
    public function dataSupportsDenormalization(): array
42
    {
43
        return [
44
            [
45
                Body::class,
46
                [
47
                    'original_string' => 'foobar',
48
                ],
49
                false,
50
            ],
51
            [
52
                Body::class,
53
                [],
54
                true,
55
            ],
56
            [
57
                \stdClass::class,
58
                [],
59
                false,
60
            ],
61
        ];
62
    }
63
64
    public function testDenormalizeWithDenormalizerIsNotSet(): void
65
    {
66
        $this->expectException(BadMethodCallException::class);
67
        $this->denormalizer->denormalize([], Body::class);
68
    }
69
70
    public function testDenormalizeWithWrongData(): void
71
    {
72
        $this->denormalizer->setDenormalizer($this->inner);
0 ignored issues
show
Bug introduced by
It seems like $this->inner can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Lamoda\IsmpClient\Impl\S...izer::setDenormalizer() does only seem to accept object<Symfony\Component...\DenormalizerInterface>, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
73
74
        $this->expectException(InvalidArgumentException::class);
75
        $this->denormalizer->denormalize('wrong', Body::class);
76
    }
77
78
    public function testDenormalizeWithWrongType(): void
79
    {
80
        $this->denormalizer->setDenormalizer($this->inner);
0 ignored issues
show
Bug introduced by
It seems like $this->inner can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Lamoda\IsmpClient\Impl\S...izer::setDenormalizer() does only seem to accept object<Symfony\Component...\DenormalizerInterface>, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
81
82
        $this->expectException(InvalidArgumentException::class);
83
        $this->denormalizer->denormalize([], \stdClass::class);
84
    }
85
86
    public function testSetDenormalizerWithNoSerializer(): void
87
    {
88
        $this->expectException(InvalidArgumentException::class);
89
        $this->denormalizer->setDenormalizer($this->createMock(DenormalizerInterface::class));
0 ignored issues
show
Documentation introduced by
$this->createMock(\Symfo...alizerInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...\DenormalizerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
    }
91
92
    public function testDenormalize(): void
93
    {
94
        $this->denormalizer->setDenormalizer($this->inner);
0 ignored issues
show
Bug introduced by
It seems like $this->inner can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Lamoda\IsmpClient\Impl\S...izer::setDenormalizer() does only seem to accept object<Symfony\Component...\DenormalizerInterface>, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
95
96
        $originalBodyData = [
97
            'some_new_crpt_field' => true,
98
            'document_description' => 'description',
99
            'products' => [
100
                [
101
                    'uit_code' => '010464005741102021PMRanDom32406404',
102
                    'product_cost' => 42,
103
                    'product_tax' => 1.1,
104
                    'product_description' => 'product description 2'
105
                ],
106
                [
107
                    'uit_code' => '010RanDom463007357256021c140640422',
108
                    'product_cost' => 42,
109
                    'product_tax' => 1.1,
110
                    'product_description' => 'product description 2'
111
                ],
112
            ]
113
        ];
114
        $expectedResult = new Body();
115
        $property = new \ReflectionProperty(Body::class, 'originalString');
116
        $property->setAccessible(true);
117
        $property->setValue($expectedResult, json_encode($originalBodyData));
118
        $property = new \ReflectionProperty(Body::class, 'documentDescription');
119
        $property->setAccessible(true);
120
        $property->setValue($expectedResult, 'description');
121
122
        $this->inner->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Symfony\Component\Serial...r\DenormalizerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
123
            ->method('serialize')
124
            ->with($originalBodyData, null, [])
125
            ->willReturn($originalBodyData);
126
        $enrichedOriginalBodyData = array_merge($originalBodyData, ['original_string' => $originalBodyData]);
127
        $this->inner->expects($this->once())
128
            ->method('denormalize')
129
            ->with($enrichedOriginalBodyData, Body::class, null, [])
130
            ->willReturn($expectedResult);
131
132
        $result = $this->denormalizer->denormalize(
133
            $originalBodyData,
134
            Body::class
135
        );
136
137
        $this->assertEquals($expectedResult, $result);
138
    }
139
140
    public function testHasCacheableSupportsMethod(): void
141
    {
142
        $this->denormalizer->setDenormalizer($this->inner);
0 ignored issues
show
Bug introduced by
It seems like $this->inner can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Lamoda\IsmpClient\Impl\S...izer::setDenormalizer() does only seem to accept object<Symfony\Component...\DenormalizerInterface>, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
143
144
        $result = $this->denormalizer->hasCacheableSupportsMethod();
145
        $this->assertFalse($result);
146
    }
147
}
148