|
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) { |
|
|
|
|
|
|
46
|
|
|
$context->expects($this->never()) |
|
|
|
|
|
|
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()) |
|
|
|
|
|
|
59
|
|
|
->method('getType') |
|
60
|
|
|
->will($this->returnValue($type)); |
|
61
|
|
|
|
|
62
|
|
|
$this->validator->initialize($context); |
|
|
|
|
|
|
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 = []) |
|
|
|
|
|
|
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
|
|
|
|
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.