Completed
Push — master ( 4859e1...c3d3e1 )
by
unknown
11:17
created

CategoriesValidatorTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 281
Duplicated Lines 5.69 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 7
dl 16
loc 281
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testValidatedBy() 0 4 1
B testValidate() 0 25 3
B validateDataProvider() 0 182 1
A getCategory() 0 12 1
A getCollection() 16 16 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
namespace OroCRM\Bundle\AnalyticsBundle\Tests\Unit\Validator;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
use Doctrine\ORM\PersistentCollection;
9
10
use Symfony\Component\Validator\ExecutionContextInterface;
11
12
use OroCRM\Bundle\AnalyticsBundle\Entity\RFMMetricCategory;
13
use OroCRM\Bundle\AnalyticsBundle\Validator\CategoriesConstraint;
14
use OroCRM\Bundle\AnalyticsBundle\Validator\CategoriesValidator;
15
16
class CategoriesValidatorTest extends \PHPUnit_Framework_TestCase
17
{
18
    /**
19
     * @var CategoriesValidator
20
     */
21
    protected $validator;
22
23
    protected function setUp()
24
    {
25
        $this->validator = new CategoriesValidator();
26
    }
27
28
    public function testValidatedBy()
29
    {
30
        $this->assertInternalType('string', $this->validator->validatedBy());
31
    }
32
33
    /**
34
     * @param PersistentCollection $collection
35
     * @param string $type
36
     * @param array $expectedViolationsMessages
37
     *
38
     * @dataProvider validateDataProvider
39
     */
40
    public function testValidate($collection, $type, $expectedViolationsMessages = [])
41
    {
42
        /** @var \PHPUnit_Framework_MockObject_MockObject|ExecutionContextInterface $context */
43
        $context = $this->getMockForAbstractClass('Symfony\Component\Validator\ExecutionContextInterface');
44
45
        if (!$expectedViolationsMessages) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $expectedViolationsMessages of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
46
            $context->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Valida...ecutionContextInterface.

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...
47
                ->method('addViolationAt');
48
        } else {
49
            foreach ($expectedViolationsMessages as $key => $expectedViolationsMessage) {
50
                $context->expects($this->at($key))
51
                    ->method('addViolationAt')
52
                    ->with($this->equalTo($type), $this->equalTo($expectedViolationsMessage), $this->isType('array'));
53
            }
54
        }
55
56
        /** @var \PHPUnit_Framework_MockObject_MockObject|CategoriesConstraint $constraint */
57
        $constraint = $this->getMock('OroCRM\Bundle\AnalyticsBundle\Validator\CategoriesConstraint');
58
        $constraint->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in OroCRM\Bundle\AnalyticsB...or\CategoriesConstraint.

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...
59
            ->method('getType')
60
            ->will($this->returnValue($type));
61
62
        $this->validator->initialize($context);
0 ignored issues
show
Bug introduced by
It seems like $context defined by $this->getMockForAbstrac...utionContextInterface') on line 43 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Symfony\Component\Valida...Validator::initialize() does only seem to accept object<Symfony\Component...cutionContextInterface>, 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...
63
        $this->validator->validate($collection, $constraint);
64
    }
65
66
    /**
67
     * @return array
68
     *
69
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
70
     */
71
    public function validateDataProvider()
72
    {
73
        $constraint = new CategoriesConstraint();
74
75
        return [
76
            'not collection' => [null, RFMMetricCategory::TYPE_FREQUENCY],
77
            'count violation' => [
78
                'collection' => $this->getCollection(
79
                    [
80
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 1, null, 100),
81
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 2, 100, null),
82
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 3, 1000, null),
83
                    ]
84
                ),
85
                'type' => RFMMetricCategory::TYPE_FREQUENCY,
86
                'expectedViolationsMessages' =>
87
                    [
88
                        $constraint->blankMessage,
89
                    ]
90
            ],
91
            'ordered' => [
92
                'collection' => $this->getCollection(
93
                    [
94
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 1, null, 20),
95
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 2, 20, 30),
96
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 3, 30, null),
97
                    ]
98
                ),
99
                'type' => RFMMetricCategory::TYPE_FREQUENCY,
100
            ],
101
            'asc order violation' => [
102
                'collection' => $this->getCollection(
103
                    [
104
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 1, null, 20),
105
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 2, 20, 30),
106
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 3, 10, 40),
107
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 4, 40, null),
108
                    ]
109
                ),
110
                'type' => RFMMetricCategory::TYPE_FREQUENCY,
111
                'expectedViolationsMessage' => [
112
                    $constraint->message
113
                ],
114
            ],
115
            'desc order' => [
116
                'collection' => $this->getCollection(
117
                    [
118
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 1, 30, null),
119
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 2, 20, 30),
120
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 3, null, 20),
121
                    ]
122
                ),
123
                'type' => RFMMetricCategory::TYPE_FREQUENCY,
124
            ],
125
            'desc order violation' => [
126
                'collection' => $this->getCollection(
127
                    [
128
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 1, 30, null),
129
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 2, 20, 30),
130
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 3, 25, 20),
131
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 4, null, 10),
132
                    ]
133
                ),
134
                'type' => RFMMetricCategory::TYPE_FREQUENCY,
135
                'expectedViolationsMessage' => [
136
                    $constraint->message
137
                ],
138
            ],
139
            'desc order same value violation' => [
140
                'collection' => $this->getCollection(
141
                    [
142
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 1, 30, null),
143
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 2, 20, 30),
144
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 3, 20, 20),
145
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 4, null, 10),
146
                    ]
147
                ),
148
                'type' => RFMMetricCategory::TYPE_FREQUENCY,
149
                'expectedViolationsMessage' => [
150
                    $constraint->message
151
                ],
152
            ],
153
            'asc order same value violation' => [
154
                'collection' => $this->getCollection(
155
                    [
156
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 1, null, 20),
157
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 2, 20, 30),
158
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 3, 30, 30),
159
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 4, 40, null),
160
                    ]
161
                ),
162
                'type' => RFMMetricCategory::TYPE_FREQUENCY,
163
                'expectedViolationsMessage' => [
164
                    $constraint->message
165
                ],
166
            ],
167
            'blank value violation asc mid' => [
168
                'collection' => $this->getCollection(
169
                    [
170
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 1, null, 100),
171
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 2, 100, null),
172
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 3, 1000, null),
173
                    ]
174
                ),
175
                'type' => RFMMetricCategory::TYPE_FREQUENCY,
176
                'expectedViolationsMessages' =>
177
                    [
178
                        $constraint->blankMessage,
179
                    ]
180
            ],
181
            'blank value violation desc mid' => [
182
                'collection' => $this->getCollection(
183
                    [
184
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 1, 1000, null),
185
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 2, null, 1000),
186
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 3, null, 100),
187
                    ]
188
                ),
189
                'type' => RFMMetricCategory::TYPE_FREQUENCY,
190
                'expectedViolationsMessages' =>
191
                    [
192
                        $constraint->blankMessage,
193
                    ]
194
            ],
195
            'blank value violation asc first empty' => [
196
                'collection' => $this->getCollection(
197
                    [
198
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 1, null, null),
199
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 2, 100, 1000),
200
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 3, 1000, null),
201
                    ]
202
                ),
203
                'type' => RFMMetricCategory::TYPE_FREQUENCY,
204
                'expectedViolationsMessages' =>
205
                    [
206
                        $constraint->blankMessage,
207
                    ]
208
            ],
209
            'blank value violation asc last empty' => [
210
                'collection' => $this->getCollection(
211
                    [
212
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 1, null, 100),
213
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 2, 100, 1000),
214
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 3, null, null),
215
                    ]
216
                ),
217
                'type' => RFMMetricCategory::TYPE_FREQUENCY,
218
                'expectedViolationsMessages' =>
219
                    [
220
                        $constraint->blankMessage,
221
                    ]
222
            ],
223
            'blank value violation desc first empty' => [
224
                'collection' => $this->getCollection(
225
                    [
226
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 1, null, null),
227
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 2, 1000, 1000),
228
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 3, null, 100),
229
                    ]
230
                ),
231
                'type' => RFMMetricCategory::TYPE_FREQUENCY,
232
                'expectedViolationsMessages' =>
233
                    [
234
                        $constraint->blankMessage,
235
                    ]
236
            ],
237
            'blank value violation desc last empty' => [
238
                'collection' => $this->getCollection(
239
                    [
240
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 1, 1000, null),
241
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 2, 1000, 1000),
242
                        $this->getCategory(RFMMetricCategory::TYPE_FREQUENCY, 3, null, null),
243
                    ]
244
                ),
245
                'type' => RFMMetricCategory::TYPE_FREQUENCY,
246
                'expectedViolationsMessages' =>
247
                    [
248
                        $constraint->blankMessage,
249
                    ]
250
            ]
251
        ];
252
    }
253
254
    /**
255
     * @param string $type
256
     * @param int $index
257
     * @param int $minValue
258
     * @param int $maxValue
259
     *
260
     * @return RFMMetricCategory
261
     */
262
    protected function getCategory($type, $index, $minValue, $maxValue)
263
    {
264
        $category = new RFMMetricCategory();
265
266
        $category
267
            ->setCategoryType($type)
268
            ->setCategoryIndex($index)
269
            ->setMinValue($minValue)
270
            ->setMaxValue($maxValue);
271
272
        return $category;
273
    }
274
275
    /**
276
     * @param array $items
277
     *
278
     * @return PersistentCollection
279
     */
280 View Code Duplication
    protected function getCollection(array $items = [])
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...
281
    {
282
        /** @var \PHPUnit_Framework_MockObject_MockObject|EntityManager $em */
283
        $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')->disableOriginalConstructor()->getMock();
284
285
        /** @var \PHPUnit_Framework_MockObject_MockObject|ClassMetadata $metadata */
286
        $metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')
287
            ->disableOriginalConstructor()
288
            ->getMock();
289
290
        $collection = new PersistentCollection($em, $metadata, new ArrayCollection($items));
291
292
        $collection->takeSnapshot();
293
294
        return $collection;
295
    }
296
}
297