Test Failed
Pull Request — master (#3)
by
unknown
03:31
created

FacadeDocBodyResponseBodyDenormalizerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Impl\Serializer;
4
5
use Lamoda\IsmpClient\Impl\Serializer\FacadeDocBodyResponseBodyDenormalizer;
6
use Lamoda\IsmpClient\V3\Dto\FacadeDocBodyResponse\Body;
7
use PHPUnit\Framework\MockObject\MockObject;
8
use PHPUnit\Framework\TestCase;
9
use Symfony\Component\Serializer\Exception\BadMethodCallException;
10
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
11
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
12
13
final class FacadeDocBodyResponseBodyDenormalizerTest extends TestCase
14
{
15
    /**
16
     * @var FacadeDocBodyResponseBodyDenormalizer
17
     */
18
    private $denormalizer;
19
    /**
20
     * @var MockObject|DenormalizerInterface
21
     */
22
    private $inner;
23
24
    protected function setUp(): void
25
    {
26
        $this->inner = $this->getMockBuilder(DenormalizerInterface::class)
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/pull/3687

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
27
            ->setMethods(['serialize', 'denormalize', 'supportsDenormalization'])
28
            ->getMock();
29
        $this->denormalizer = new FacadeDocBodyResponseBodyDenormalizer();
30
    }
31
32
    /**
33
     * @dataProvider dataSupportsDenormalization
34
     */
35
    public function testSupportsDenormalization(string $type, array $data, bool $expected): void
36
    {
37
        $result = $this->denormalizer->supportsDenormalization($data, $type);
38
39
        $this->assertEquals($expected, $result);
40
    }
41
42
    public function dataSupportsDenormalization(): array
43
    {
44
        return [
45
            [
46
                Body::class,
47
                [
48
                    'original_string' => 'foobar',
49
                ],
50
                false,
51
            ],
52
            [
53
                Body::class,
54
                [],
55
                true,
56
            ],
57
            [
58
                \stdClass::class,
59
                [],
60
                false,
61
            ],
62
        ];
63
    }
64
65
    public function testDenormalizeWithDenormalizerIsNotSet(): void
66
    {
67
        $this->expectException(BadMethodCallException::class);
68
        $this->denormalizer->denormalize([], Body::class);
69
    }
70
71 View Code Duplication
    public function testDenormalizeWithWrongData(): void
0 ignored issues
show
Duplication introduced by
This method 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...
72
    {
73
        $this->denormalizer->setDenormalizer($this->inner);
1 ignored issue
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...
74
75
        $this->expectException(InvalidArgumentException::class);
76
        $this->denormalizer->denormalize('wrong', Body::class);
77
    }
78
79 View Code Duplication
    public function testDenormalizeWithWrongType(): void
0 ignored issues
show
Duplication introduced by
This method 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...
80
    {
81
        $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...
82
83
        $this->expectException(InvalidArgumentException::class);
84
        $this->denormalizer->denormalize([], \stdClass::class);
85
    }
86
87
    public function testDenormalize(): void
88
    {
89
        $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...
90
91
        $originalBodyData = [
92
            'some_new_crpt_field' => true,
93
            'document_description' => 'description',
94
            'products' => [
95
                [
96
                    'uit_code' => '010464005741102021PMRanDom32406404',
97
                    'product_cost' => 42,
98
                    'product_tax' => 1.1,
99
                    'product_description' => 'product description 2'
100
                ],
101
                [
102
                    'uit_code' => '010RanDom463007357256021c140640422',
103
                    'product_cost' => 42,
104
                    'product_tax' => 1.1,
105
                    'product_description' => 'product description 2'
106
                ],
107
            ]
108
        ];
109
        $expectedResult = new Body();
110
        $property = new \ReflectionProperty(Body::class, 'originalString');
111
        $property->setAccessible(true);
112
        $property->setValue($expectedResult, json_encode($originalBodyData));
113
        $property = new \ReflectionProperty(Body::class, 'documentDescription');
114
        $property->setAccessible(true);
115
        $property->setValue($expectedResult, 'description');
116
117
        $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...
118
            ->method('serialize')
119
            ->with($originalBodyData, null, [])
120
            ->willReturn($originalBodyData);
121
        $enrichedOriginalBodyData = array_merge($originalBodyData, ['original_string' => $originalBodyData]);
122
        $this->inner->expects($this->once())
123
            ->method('denormalize')
124
            ->with($enrichedOriginalBodyData, Body::class, null, [])
125
            ->willReturn($expectedResult);
126
127
        $result = $this->denormalizer->denormalize(
128
            $originalBodyData,
129
            Body::class
130
        );
131
132
        $this->assertEquals($expectedResult, $result);
133
    }
134
135
    public function testHasCacheableSupportsMethod(): void
136
    {
137
        $this->denormalizer->setDenormalizer($this->inner);
1 ignored issue
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...
138
139
        $result = $this->denormalizer->hasCacheableSupportsMethod();
140
        $this->assertFalse($result);
141
    }
142
}
143