Completed
Push — master ( 9c45fe...2e51e7 )
by
unknown
29:13 queued 13:18
created

GenericTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 163
Duplicated Lines 15.95 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 26
loc 163
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 6

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testValidateValid() 0 9 1
A testValidateInvalid() 0 13 1
A provideFieldTypeIdentifier() 0 4 1
A createFieldTypeUnderTest() 0 4 1
A getValidatorConfigurationSchemaExpectation() 0 4 1
A getSettingsSchemaExpectation() 0 4 1
A getEmptyValueExpectation() 0 4 1
A provideInvalidInputForAcceptValue() 0 9 1
A provideValidInputForAcceptValue() 0 17 1
A provideInputForToHash() 13 13 1
A provideInputForFromHash() 13 13 1
A provideDataForGetName() 0 6 1
A createSerializerMock() 0 28 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
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\SPI\FieldType\Generic\Tests;
10
11
use eZ\Publish\SPI\FieldType\Tests\FieldTypeTest;
12
use eZ\Publish\SPI\FieldType\ValueSerializerInterface;
13
use eZ\Publish\SPI\FieldType\Generic\Tests\Stubs\Type as GenericFieldTypeStub;
14
use eZ\Publish\SPI\FieldType\Generic\Tests\Stubs\Value as GenericFieldValueStub;
15
use eZ\Publish\SPI\FieldType\ValidationError;
16
use eZ\Publish\SPI\Exception\InvalidArgumentException;
17
use Symfony\Component\Validator\ConstraintViolation;
18
use Symfony\Component\Validator\ConstraintViolationList;
19
use Symfony\Component\Validator\ConstraintViolationListInterface;
20
use Symfony\Component\Validator\Validator\ValidatorInterface;
21
22
class GenericTest extends FieldTypeTest
23
{
24
    /** @var \eZ\Publish\SPI\FieldType\ValueSerializerInterface|\PHPUnit\Framework\MockObject\MockObject */
25
    private $serializer;
26
27
    /** @var \Symfony\Component\Validator\Validator\ValidatorInterface|\PHPUnit\Framework\MockObject\MockObject */
28
    private $validator;
29
30
    protected function setUp(): void
31
    {
32
        parent::setUp();
33
34
        $this->serializer = $this->createSerializerMock();
35
        $this->validator = $this->createMock(ValidatorInterface::class);
36
    }
37
38
    /**
39
     * @dataProvider provideValidDataForValidate
40
     */
41
    public function testValidateValid($fieldDefinitionData, $value): void
42
    {
43
        $this->validator
44
            ->method('validate')
45
            ->with($value, null)
46
            ->willReturn($this->createMock(ConstraintViolationListInterface::class));
47
48
        parent::testValidateValid($fieldDefinitionData, $value);
49
    }
50
51
    /**
52
     * @dataProvider provideInvalidDataForValidate
53
     */
54
    public function testValidateInvalid($fieldDefinitionData, $value, $errors): void
55
    {
56
        $constraintViolationList = new ConstraintViolationList(array_map(function (ValidationError $error) {
57
            return new ConstraintViolation((string) $error->getTranslatableMessage());
0 ignored issues
show
Bug introduced by
The call to ConstraintViolation::__construct() misses some required arguments starting with $messageTemplate.
Loading history...
58
        }, $errors));
59
60
        $this->validator
61
            ->method('validate')
62
            ->with($value, null)
63
            ->willReturn($constraintViolationList);
64
65
        parent::testValidateInvalid($fieldDefinitionData, $value, $errors);
66
    }
67
68
    protected function provideFieldTypeIdentifier(): string
69
    {
70
        return 'generic';
71
    }
72
73
    protected function createFieldTypeUnderTest()
74
    {
75
        return new GenericFieldTypeStub($this->serializer, $this->validator);
0 ignored issues
show
Bug introduced by
It seems like $this->serializer can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\SPI\FieldType...ric\Type::__construct() does only seem to accept object<eZ\Publish\SPI\Fi...lueSerializerInterface>, 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...
Bug introduced by
It seems like $this->validator can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\SPI\FieldType...ric\Type::__construct() does only seem to accept object<Symfony\Component...tor\ValidatorInterface>, 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...
76
    }
77
78
    protected function getValidatorConfigurationSchemaExpectation(): array
79
    {
80
        return [];
81
    }
82
83
    protected function getSettingsSchemaExpectation(): array
84
    {
85
        return [];
86
    }
87
88
    protected function getEmptyValueExpectation()
89
    {
90
        return new GenericFieldValueStub();
91
    }
92
93
    public function provideInvalidInputForAcceptValue(): array
94
    {
95
        return [
96
            [
97
                23,
98
                InvalidArgumentException::class,
99
            ],
100
        ];
101
    }
102
103
    public function provideValidInputForAcceptValue(): array
104
    {
105
        return [
106
            [
107
                null,
108
                new GenericFieldValueStub(),
109
            ],
110
            [
111
                '{"value": "foo"}',
112
                new GenericFieldValueStub('foo'),
113
            ],
114
            [
115
                new GenericFieldValueStub('foo'),
116
                new GenericFieldValueStub('foo'),
117
            ],
118
        ];
119
    }
120
121 View Code Duplication
    public function provideInputForToHash(): array
122
    {
123
        return [
124
            [
125
                new GenericFieldValueStub(),
126
                null,
127
            ],
128
            [
129
                new GenericFieldValueStub('foo'),
130
                ['value' => 'foo'],
131
            ],
132
        ];
133
    }
134
135 View Code Duplication
    public function provideInputForFromHash(): array
136
    {
137
        return [
138
            [
139
                null,
140
                new GenericFieldValueStub(),
141
            ],
142
            [
143
                ['value' => 'foo'],
144
                new GenericFieldValueStub('foo'),
145
            ],
146
        ];
147
    }
148
149
    public function provideDataForGetName(): array
150
    {
151
        return [
152
            [new GenericFieldValueStub('This is a generic value.'), [], 'en_GB', 'This is a generic value.'],
153
        ];
154
    }
155
156
    private function createSerializerMock(): ValueSerializerInterface
157
    {
158
        $serializer = $this->createMock(ValueSerializerInterface::class);
159
160
        $serializer
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
161
            ->method('decode')
162
            ->willReturnCallback(function (string $json) {
163
                return json_decode($json, true);
164
            });
165
166
        $serializer
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
167
            ->method('normalize')
168
            ->willReturnCallback(function (GenericFieldValueStub $value) {
169
                return [
170
                    'value' => $value->getValue(),
171
                ];
172
            });
173
174
        $serializer
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
175
            ->method('denormalize')
176
            ->willReturnCallback(function (array $data, string $valueClass) {
177
                $this->assertEquals($valueClass, GenericFieldValueStub::class);
178
179
                return new GenericFieldValueStub($data['value']);
180
            });
181
182
        return $serializer;
183
    }
184
}
185