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

FacadeCisListResponseDenormalizerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 99
loc 99
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 5 5 1
A testSupportsDenormalization() 6 6 1
A dataSupportsDenormalization() 13 13 1
A testDenormalizationWhenDenormalizerIsNotSet() 5 5 1
A testDenormalizationWhenWrongData() 7 7 1
A testDenormalizationWhenWrongType() 7 7 1
A testDenormalizationWorks() 27 27 1
A testHasCacheableSupportsMethod() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\IsmpClient\Tests\Impl\Serializer\V4;
6
7
use Lamoda\IsmpClient\Impl\Serializer\V4\FacadeCisListResponseDenormalizer;
8
use Lamoda\IsmpClient\V4\Dto\FacadeCisItemResponse;
9
use Lamoda\IsmpClient\V4\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 View Code Duplication
final class FacadeCisListResponseDenormalizerTest 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...
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
    public function testDenormalizationWhenWrongData(): void
64
    {
65
        $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...
66
67
        $this->expectException(InvalidArgumentException::class);
68
        $this->denormalizer->denormalize('wrong', FacadeCisListResponse::class);
69
    }
70
71
    public function testDenormalizationWhenWrongType(): void
72
    {
73
        $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...
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);
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
        $item1 = new FacadeCisItemResponse('', '', '', '', '', 0);
84
        $item2 = new FacadeCisItemResponse('', '', '', '', '', 0);
85
86
        $expectedResult = new FacadeCisListResponse($item1, $item2);
87
88
        $this->inner->expects($this->exactly(2))
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...
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
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...
110
111
        $result = $this->denormalizer->hasCacheableSupportsMethod();
112
        $this->assertFalse($result);
113
    }
114
}
115