Skip to content
This repository
Browse code

[Validator] Improved test coverage of CollectionValidator and reduced…

… test code duplication
  • Loading branch information...
commit e6e3da50633ff6e0c5df5d12a3c1a8f7a21dfd44 1 parent 509c7bf
Bernhard Schussek authored January 16, 2012
20  tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayObjectTest.php
... ...
@@ -0,0 +1,20 @@
  1
+<?php
  2
+
  3
+/*
  4
+ * This file is part of the Symfony package.
  5
+ *
  6
+ * (c) Fabien Potencier <fabien@symfony.com>
  7
+ *
  8
+ * For the full copyright and license information, please view the LICENSE
  9
+ * file that was distributed with this source code.
  10
+ */
  11
+
  12
+namespace Symfony\Tests\Component\Validator\Constraints;
  13
+
  14
+class CollectionValidatorArrayObjectTest extends CollectionValidatorTest
  15
+{
  16
+    public function prepareTestData(array $contents)
  17
+    {
  18
+        return new \ArrayObject($contents);
  19
+    }
  20
+}
20  tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorArrayTest.php
... ...
@@ -0,0 +1,20 @@
  1
+<?php
  2
+
  3
+/*
  4
+ * This file is part of the Symfony package.
  5
+ *
  6
+ * (c) Fabien Potencier <fabien@symfony.com>
  7
+ *
  8
+ * For the full copyright and license information, please view the LICENSE
  9
+ * file that was distributed with this source code.
  10
+ */
  11
+
  12
+namespace Symfony\Tests\Component\Validator\Constraints;
  13
+
  14
+class CollectionValidatorArrayTest extends CollectionValidatorTest
  15
+{
  16
+    public function prepareTestData(array $contents)
  17
+    {
  18
+        return $contents;
  19
+    }
  20
+}
80  tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorCustomArrayObjectTest.php
... ...
@@ -0,0 +1,80 @@
  1
+<?php
  2
+
  3
+/*
  4
+ * This file is part of the Symfony package.
  5
+ *
  6
+ * (c) Fabien Potencier <fabien@symfony.com>
  7
+ *
  8
+ * For the full copyright and license information, please view the LICENSE
  9
+ * file that was distributed with this source code.
  10
+ */
  11
+
  12
+namespace Symfony\Tests\Component\Validator\Constraints;
  13
+
  14
+/**
  15
+* This class is a hand written simplified version of PHP native `ArrayObject`
  16
+* class, to show that it behaves different than PHP native implementation.
  17
+*/
  18
+class CustomArrayObject implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable
  19
+{
  20
+    private $array;
  21
+
  22
+    public function __construct(array $array = null)
  23
+    {
  24
+        $this->array = (array) ($array ?: array());
  25
+    }
  26
+
  27
+    public function offsetExists($offset)
  28
+    {
  29
+        return array_key_exists($offset, $this->array);
  30
+    }
  31
+
  32
+    public function offsetGet($offset)
  33
+    {
  34
+        return $this->array[$offset];
  35
+    }
  36
+
  37
+    public function offsetSet($offset, $value)
  38
+    {
  39
+        if (null === $offset) {
  40
+            $this->array[] = $value;
  41
+        } else {
  42
+            $this->array[$offset] = $value;
  43
+        }
  44
+    }
  45
+
  46
+    public function offsetUnset($offset)
  47
+    {
  48
+        if (array_key_exists($offset, $this->array)) {
  49
+            unset($this->array[$offset]);
  50
+        }
  51
+    }
  52
+
  53
+    public function getIterator()
  54
+    {
  55
+        return new \ArrayIterator($this->array);
  56
+    }
  57
+
  58
+    public function count()
  59
+    {
  60
+        return count($this->array);
  61
+    }
  62
+
  63
+    public function serialize()
  64
+    {
  65
+        return serialize($this->array);
  66
+    }
  67
+
  68
+    public function unserialize($serialized)
  69
+    {
  70
+        $this->array = (array) unserialize((string) $serialized);
  71
+    }
  72
+}
  73
+
  74
+class CollectionValidatorCustomArrayObjectTest extends CollectionValidatorTest
  75
+{
  76
+    public function prepareTestData(array $contents)
  77
+    {
  78
+        return new CustomArrayObject($contents);
  79
+    }
  80
+}
287  tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php
@@ -20,67 +20,7 @@
20 20
 use Symfony\Component\Validator\Constraints\Collection;
21 21
 use Symfony\Component\Validator\Constraints\CollectionValidator;
22 22
 
23  
-/**
24  
- * This class is a hand written simplified version of PHP native `ArrayObject`
25  
- * class, to show that it behaves different than PHP native implementation.
26  
- */
27  
-class TestArrayObject implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable
28  
-{
29  
-    private $array;
30  
-
31  
-    public function __construct(array $array = null)
32  
-    {
33  
-        $this->array = (array) ($array ?: array());
34  
-    }
35  
-
36  
-    public function offsetExists($offset)
37  
-    {
38  
-        return array_key_exists($offset, $this->array);
39  
-    }
40  
-
41  
-    public function offsetGet($offset)
42  
-    {
43  
-        return $this->array[$offset];
44  
-    }
45  
-
46  
-    public function offsetSet($offset, $value)
47  
-    {
48  
-        if (null === $offset) {
49  
-            $this->array[] = $value;
50  
-        } else {
51  
-            $this->array[$offset] = $value;
52  
-        }
53  
-    }
54  
-
55  
-    public function offsetUnset($offset)
56  
-    {
57  
-        if (array_key_exists($offset, $this->array)) {
58  
-            unset($this->array[$offset]);
59  
-        }
60  
-    }
61  
-
62  
-    public function getIterator()
63  
-    {
64  
-        return new \ArrayIterator($this->array);
65  
-    }
66  
-
67  
-    public function count()
68  
-    {
69  
-        return count($this->array);
70  
-    }
71  
-
72  
-    public function serialize()
73  
-    {
74  
-        return serialize($this->array);
75  
-    }
76  
-
77  
-    public function unserialize($serialized)
78  
-    {
79  
-        $this->array = (array) unserialize((string) $serialized);
80  
-    }
81  
-}
82  
-
83  
-class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
  23
+abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
84 24
 {
85 25
     protected $validator;
86 26
     protected $walker;
@@ -104,6 +44,8 @@ protected function tearDown()
104 44
         $this->context = null;
105 45
     }
106 46
 
  47
+    abstract protected function prepareTestData(array $contents);
  48
+
107 49
     public function testNullIsValid()
108 50
     {
109 51
         $this->assertTrue($this->validator->isValid(null, new Collection(array('fields' => array(
@@ -113,13 +55,9 @@ public function testNullIsValid()
113 55
 
114 56
     public function testFieldsAsDefaultOption()
115 57
     {
116  
-        $this->assertTrue($this->validator->isValid(array('foo' => 'foobar'), new Collection(array(
117  
-            'foo' => new Min(4),
118  
-        ))));
119  
-        $this->assertTrue($this->validator->isValid(new \ArrayObject(array('foo' => 'foobar')), new Collection(array(
120  
-            'foo' => new Min(4),
121  
-        ))));
122  
-        $this->assertTrue($this->validator->isValid(new TestArrayObject(array('foo' => 'foobar')), new Collection(array(
  58
+        $data = $this->prepareTestData(array('foo' => 'foobar'));
  59
+
  60
+        $this->assertTrue($this->validator->isValid($data, new Collection(array(
123 61
             'foo' => new Min(4),
124 62
         ))));
125 63
     }
@@ -134,61 +72,66 @@ public function testThrowsExceptionIfNotTraversable()
134 72
         ))));
135 73
     }
136 74
 
137  
-    /**
138  
-     * @dataProvider getValidArguments
139  
-     */
140  
-    public function testWalkSingleConstraint($array)
  75
+    public function testWalkSingleConstraint()
141 76
     {
142 77
         $this->context->setGroup('MyGroup');
143 78
         $this->context->setPropertyPath('foo');
144 79
 
145 80
         $constraint = new Min(4);
146 81
 
  82
+        $array = array('foo' => 3);
  83
+
147 84
         foreach ($array as $key => $value) {
148 85
             $this->walker->expects($this->once())
149  
-                                     ->method('walkConstraint')
150  
-                                     ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
  86
+                ->method('walkConstraint')
  87
+                ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
151 88
         }
152 89
 
153  
-        $this->assertTrue($this->validator->isValid($array, new Collection(array(
  90
+        $data = $this->prepareTestData($array);
  91
+
  92
+        $this->assertTrue($this->validator->isValid($data, new Collection(array(
154 93
             'fields' => array(
155 94
                 'foo' => $constraint,
156 95
             ),
157 96
         ))));
158 97
     }
159 98
 
160  
-    /**
161  
-     * @dataProvider getValidArguments
162  
-     */
163  
-    public function testWalkMultipleConstraints($array)
  99
+    public function testWalkMultipleConstraints()
164 100
     {
165 101
         $this->context->setGroup('MyGroup');
166 102
         $this->context->setPropertyPath('foo');
167 103
 
168  
-        $constraint = new Min(4);
169  
-        // can only test for twice the same constraint because PHPUnits mocking
170  
-        // can't test method calls with different arguments
171  
-        $constraints = array($constraint, $constraint);
  104
+        $constraints = array(
  105
+            new Min(4),
  106
+            new NotNull(),
  107
+        );
  108
+        $array = array('foo' => 3);
172 109
 
173 110
         foreach ($array as $key => $value) {
174  
-            $this->walker->expects($this->exactly(2))
175  
-                                     ->method('walkConstraint')
176  
-                                     ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
  111
+            foreach ($constraints as $i => $constraint) {
  112
+                $this->walker->expects($this->at($i))
  113
+                    ->method('walkConstraint')
  114
+                    ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
  115
+            }
177 116
         }
178 117
 
179  
-        $this->assertTrue($this->validator->isValid($array, new Collection(array(
  118
+        $data = $this->prepareTestData($array);
  119
+
  120
+        $this->assertTrue($this->validator->isValid($data, new Collection(array(
180 121
             'fields' => array(
181 122
                 'foo' => $constraints,
182 123
             )
183 124
         ))));
184 125
     }
185 126
 
186  
-    /**
187  
-     * @dataProvider getArgumentsWithExtraFields
188  
-     */
189  
-    public function testExtraFieldsDisallowed($array)
  127
+    public function testExtraFieldsDisallowed()
190 128
     {
191  
-        $this->assertFalse($this->validator->isValid($array, new Collection(array(
  129
+        $data = $this->prepareTestData(array(
  130
+            'foo' => 5,
  131
+            'bar' => 6,
  132
+        ));
  133
+
  134
+        $this->assertFalse($this->validator->isValid($data, new Collection(array(
192 135
             'fields' => array(
193 136
                 'foo' => new Min(4),
194 137
             ),
@@ -198,26 +141,24 @@ public function testExtraFieldsDisallowed($array)
198 141
     // bug fix
199 142
     public function testNullNotConsideredExtraField()
200 143
     {
201  
-        $array = array(
  144
+        $data = $this->prepareTestData(array(
202 145
             'foo' => null,
203  
-        );
  146
+        ));
204 147
         $collection = new Collection(array(
205 148
             'fields' => array(
206 149
                 'foo' => new Min(4),
207 150
             ),
208 151
         ));
209 152
 
210  
-        $this->assertTrue($this->validator->isValid($array, $collection));
211  
-        $this->assertTrue($this->validator->isValid(new \ArrayObject($array), $collection));
212  
-        $this->assertTrue($this->validator->isValid(new TestArrayObject($array), $collection));
  153
+        $this->assertTrue($this->validator->isValid($data, $collection));
213 154
     }
214 155
 
215 156
     public function testExtraFieldsAllowed()
216 157
     {
217  
-        $array = array(
  158
+        $data = $this->prepareTestData(array(
218 159
             'foo' => 5,
219 160
             'bar' => 6,
220  
-        );
  161
+        ));
221 162
         $collection = new Collection(array(
222 163
             'fields' => array(
223 164
                 'foo' => new Min(4),
@@ -225,24 +166,14 @@ public function testExtraFieldsAllowed()
225 166
             'allowExtraFields' => true,
226 167
         ));
227 168
 
228  
-        $this->assertTrue($this->validator->isValid($array, $collection));
229  
-        $this->assertTrue($this->validator->isValid(new \ArrayObject($array), $collection));
230  
-        $this->assertTrue($this->validator->isValid(new TestArrayObject($array), $collection));
  169
+        $this->assertTrue($this->validator->isValid($data, $collection));
231 170
     }
232 171
 
233 172
     public function testMissingFieldsDisallowed()
234 173
     {
235  
-        $this->assertFalse($this->validator->isValid(array(), new Collection(array(
236  
-            'fields' => array(
237  
-                'foo' => new Min(4),
238  
-            ),
239  
-        ))));
240  
-        $this->assertFalse($this->validator->isValid(new \ArrayObject(array()), new Collection(array(
241  
-            'fields' => array(
242  
-                'foo' => new Min(4),
243  
-            ),
244  
-        ))));
245  
-        $this->assertFalse($this->validator->isValid(new TestArrayObject(array()), new Collection(array(
  174
+        $data = $this->prepareTestData(array());
  175
+
  176
+        $this->assertFalse($this->validator->isValid($data, new Collection(array(
246 177
             'fields' => array(
247 178
                 'foo' => new Min(4),
248 179
             ),
@@ -251,19 +182,9 @@ public function testMissingFieldsDisallowed()
251 182
 
252 183
     public function testMissingFieldsAllowed()
253 184
     {
254  
-        $this->assertTrue($this->validator->isValid(array(), new Collection(array(
255  
-            'fields' => array(
256  
-                'foo' => new Min(4),
257  
-            ),
258  
-            'allowMissingFields' => true,
259  
-        ))));
260  
-        $this->assertTrue($this->validator->isValid(new \ArrayObject(array()), new Collection(array(
261  
-            'fields' => array(
262  
-                'foo' => new Min(4),
263  
-            ),
264  
-            'allowMissingFields' => true,
265  
-        ))));
266  
-        $this->assertTrue($this->validator->isValid(new TestArrayObject(array()), new Collection(array(
  185
+        $data = $this->prepareTestData(array());
  186
+
  187
+        $this->assertTrue($this->validator->isValid($data, new Collection(array(
267 188
             'fields' => array(
268 189
                 'foo' => new Min(4),
269 190
             ),
@@ -271,63 +192,22 @@ public function testMissingFieldsAllowed()
271 192
         ))));
272 193
     }
273 194
 
274  
-    public function testArrayAccessObject() {
275  
-        $value = new TestArrayObject();
276  
-        $value['foo'] = 12;
277  
-        $value['asdf'] = 'asdfaf';
278  
-
279  
-        $this->assertTrue(isset($value['asdf']));
280  
-        $this->assertTrue(isset($value['foo']));
281  
-        $this->assertFalse(empty($value['asdf']));
282  
-        $this->assertFalse(empty($value['foo']));
283  
-
284  
-        $result = $this->validator->isValid($value, new Collection(array(
285  
-            'fields' => array(
286  
-                'foo' => new NotBlank(),
287  
-                'asdf' => new NotBlank()
288  
-            )
289  
-        )));
290  
-
291  
-        $this->assertTrue($result);
292  
-    }
293  
-
294  
-    public function testArrayObject() {
295  
-        $value = new \ArrayObject(array());
296  
-        $value['foo'] = 12;
297  
-        $value['asdf'] = 'asdfaf';
298  
-
299  
-        $this->assertTrue(isset($value['asdf']));
300  
-        $this->assertTrue(isset($value['foo']));
301  
-        $this->assertFalse(empty($value['asdf']));
302  
-        $this->assertFalse(empty($value['foo']));
303  
-
304  
-        $result = $this->validator->isValid($value, new Collection(array(
305  
-            'fields' => array(
306  
-                'foo' => new NotBlank(),
307  
-                'asdf' => new NotBlank()
308  
-            )
309  
-        )));
310  
-
311  
-        $this->assertTrue($result);
312  
-    }
313  
-
314 195
     public function testOptionalFieldPresent()
315 196
     {
316  
-        $array = array(
  197
+        $data = $this->prepareTestData(array(
317 198
             'foo' => null,
318  
-        );
  199
+        ));
319 200
 
320  
-        $this->assertTrue($this->validator->isValid($array, new Collection(array(
  201
+        $this->assertTrue($this->validator->isValid($data, new Collection(array(
321 202
             'foo' => new Optional(),
322 203
         ))));
323 204
     }
324 205
 
325 206
     public function testOptionalFieldNotPresent()
326 207
     {
327  
-        $array = array(
328  
-        );
  208
+        $data = $this->prepareTestData(array());
329 209
 
330  
-        $this->assertTrue($this->validator->isValid($array, new Collection(array(
  210
+        $this->assertTrue($this->validator->isValid($data, new Collection(array(
331 211
             'foo' => new Optional(),
332 212
         ))));
333 213
     }
@@ -347,7 +227,9 @@ public function testOptionalFieldSingleConstraint()
347 227
             ->method('walkConstraint')
348 228
             ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
349 229
 
350  
-        $this->assertTrue($this->validator->isValid($array, new Collection(array(
  230
+        $data = $this->prepareTestData($array);
  231
+
  232
+        $this->assertTrue($this->validator->isValid($data, new Collection(array(
351 233
             'foo' => new Optional($constraint),
352 234
         ))));
353 235
     }
@@ -372,28 +254,29 @@ public function testOptionalFieldMultipleConstraints()
372 254
                 ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
373 255
         }
374 256
 
375  
-        $this->assertTrue($this->validator->isValid($array, new Collection(array(
  257
+        $data = $this->prepareTestData($array);
  258
+
  259
+        $this->assertTrue($this->validator->isValid($data, new Collection(array(
376 260
             'foo' => new Optional($constraints),
377 261
         ))));
378 262
     }
379 263
 
380 264
     public function testRequiredFieldPresent()
381 265
     {
382  
-        $array = array(
  266
+        $data = $this->prepareTestData(array(
383 267
             'foo' => null,
384  
-        );
  268
+        ));
385 269
 
386  
-        $this->assertTrue($this->validator->isValid($array, new Collection(array(
  270
+        $this->assertTrue($this->validator->isValid($data, new Collection(array(
387 271
             'foo' => new Required(),
388 272
         ))));
389 273
     }
390 274
 
391 275
     public function testRequiredFieldNotPresent()
392 276
     {
393  
-        $array = array(
394  
-        );
  277
+        $data = $this->prepareTestData(array());
395 278
 
396  
-        $this->assertFalse($this->validator->isValid($array, new Collection(array(
  279
+        $this->assertFalse($this->validator->isValid($data, new Collection(array(
397 280
             'foo' => new Required(),
398 281
         ))));
399 282
     }
@@ -410,10 +293,12 @@ public function testRequiredFieldSingleConstraint()
410 293
         $constraint = new Min(4);
411 294
 
412 295
         $this->walker->expects($this->once())
413  
-                                 ->method('walkConstraint')
414  
-                                 ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
  296
+            ->method('walkConstraint')
  297
+            ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
415 298
 
416  
-        $this->assertTrue($this->validator->isValid($array, new Collection(array(
  299
+        $data = $this->prepareTestData($array);
  300
+
  301
+        $this->assertTrue($this->validator->isValid($data, new Collection(array(
417 302
             'foo' => new Required($constraint),
418 303
         ))));
419 304
     }
@@ -434,10 +319,12 @@ public function testRequiredFieldMultipleConstraints()
434 319
 
435 320
         foreach ($constraints as $i => $constraint) {
436 321
             $this->walker->expects($this->at($i))
437  
-                                     ->method('walkConstraint')
438  
-                                     ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
  322
+                ->method('walkConstraint')
  323
+                ->with($this->equalTo($constraint), $this->equalTo($array['foo']), $this->equalTo('MyGroup'), $this->equalTo('bar[foo]'));
439 324
         }
440 325
 
  326
+        $data = $this->prepareTestData($array);
  327
+
441 328
         $this->assertTrue($this->validator->isValid($array, new Collection(array(
442 329
             'foo' => new Required($constraints),
443 330
         ))));
@@ -458,34 +345,4 @@ public function testObjectShouldBeLeftUnchanged()
458 345
             'foo' => 3
459 346
         ), (array) $value);
460 347
     }
461  
-
462  
-    public function getValidArguments()
463  
-    {
464  
-        return array(
465  
-            // can only test for one entry, because PHPUnits mocking does not allow
466  
-            // to expect multiple method calls with different arguments
467  
-            array(array('foo' => 3)),
468  
-            array(new \ArrayObject(array('foo' => 3))),
469  
-            array(new TestArrayObject(array('foo' => 3))),
470  
-        );
471  
-    }
472  
-
473  
-    public function getArgumentsWithExtraFields()
474  
-    {
475  
-        return array(
476  
-            array(array(
477  
-                'foo' => 5,
478  
-                'bar' => 6,
479  
-            )),
480  
-            array(new \ArrayObject(array(
481  
-                'foo' => 5,
482  
-                'bar' => 6,
483  
-            ))),
484  
-            array(new TestArrayObject(array(
485  
-                'foo' => 5,
486  
-                'bar' => 6,
487  
-            )))
488  
-        );
489  
-    }
490 348
 }
491  
-

0 notes on commit e6e3da5

Please sign in to comment.
Something went wrong with that request. Please try again.