Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7f7c2a7

Browse files
committed
Add prof-of-concept test, this test will fail without changes in previous commit
1 parent 253eeba commit 7f7c2a7

File tree

1 file changed

+179
-24
lines changed

1 file changed

+179
-24
lines changed

tests/Symfony/Tests/Component/Validator/Constraints/CollectionValidatorTest.php

Lines changed: 179 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,71 @@
1313

1414
use Symfony\Component\Validator\ExecutionContext;
1515
use Symfony\Component\Validator\Constraints\Min;
16+
use Symfony\Component\Validator\Constraints\NotBlank;
17+
use Symfony\Component\Validator\Constraints\NotNull;
1618
use Symfony\Component\Validator\Constraints\Collection;
1719
use Symfony\Component\Validator\Constraints\CollectionValidator;
1820

21+
/**
22+
* This class is a hand written simplified version of PHP native `ArrayObject`
23+
* class, to show that it behaves different than PHP native implementation.
24+
*/
25+
class TestArrayObject implements \ArrayAccess, \IteratorAggregate, \Countable, \Serializable
26+
{
27+
private $array;
28+
29+
public function __construct(array $array = null)
30+
{
31+
$this->array = (array) ($array ?: array());
32+
}
33+
34+
public function offsetExists($offset)
35+
{
36+
return array_key_exists($offset, $this->array);
37+
}
38+
39+
public function offsetGet($offset)
40+
{
41+
return $this->array[$offset];
42+
}
43+
44+
public function offsetSet($offset, $value)
45+
{
46+
if (null === $offset) {
47+
$this->array[] = $value;
48+
} else {
49+
$this->array[$offset] = $value;
50+
}
51+
}
52+
53+
public function offsetUnset($offset)
54+
{
55+
if (array_key_exists($offset, $this->array)) {
56+
unset($this->array[$offset]);
57+
}
58+
}
59+
60+
public function getIterator()
61+
{
62+
return new \ArrayIterator($this->array);
63+
}
64+
65+
public function count()
66+
{
67+
return count($this->array);
68+
}
69+
70+
public function serialize()
71+
{
72+
return serialize($this->array);
73+
}
74+
75+
public function unserialize($serialized)
76+
{
77+
$this->array = (array) unserialize((string) $serialized);
78+
}
79+
}
80+
1981
class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
2082
{
2183
protected $validator;
@@ -49,15 +111,22 @@ public function testNullIsValid()
49111

50112
public function testFieldsAsDefaultOption()
51113
{
52-
$this->validator->isValid(array('foo' => 'foobar'), new Collection(array(
114+
$this->assertTrue($this->validator->isValid(array('foo' => 'foobar'), new Collection(array(
53115
'foo' => new Min(4),
54-
)));
116+
))));
117+
$this->assertTrue($this->validator->isValid(new \ArrayObject(array('foo' => 'foobar')), new Collection(array(
118+
'foo' => new Min(4),
119+
))));
120+
$this->assertTrue($this->validator->isValid(new TestArrayObject(array('foo' => 'foobar')), new Collection(array(
121+
'foo' => new Min(4),
122+
))));
55123
}
56124

125+
/**
126+
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
127+
*/
57128
public function testThrowsExceptionIfNotTraversable()
58129
{
59-
$this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
60-
61130
$this->validator->isValid('foobar', new Collection(array('fields' => array(
62131
'foo' => new Min(4),
63132
))));
@@ -112,13 +181,11 @@ public function testWalkMultipleConstraints($array)
112181
))));
113182
}
114183

115-
public function testExtraFieldsDisallowed()
184+
/**
185+
* @dataProvider getArgumentsWithExtraFields
186+
*/
187+
public function testExtraFieldsDisallowed($array)
116188
{
117-
$array = array(
118-
'foo' => 5,
119-
'bar' => 6,
120-
);
121-
122189
$this->assertFalse($this->validator->isValid($array, new Collection(array(
123190
'fields' => array(
124191
'foo' => new Min(4),
@@ -132,12 +199,15 @@ public function testNullNotConsideredExtraField()
132199
$array = array(
133200
'foo' => null,
134201
);
135-
136-
$this->assertTrue($this->validator->isValid($array, new Collection(array(
202+
$collection = new Collection(array(
137203
'fields' => array(
138204
'foo' => new Min(4),
139205
),
140-
))));
206+
));
207+
208+
$this->assertTrue($this->validator->isValid($array, $collection));
209+
$this->assertTrue($this->validator->isValid(new \ArrayObject($array), $collection));
210+
$this->assertTrue($this->validator->isValid(new TestArrayObject($array), $collection));
141211
}
142212

143213
public function testExtraFieldsAllowed()
@@ -146,13 +216,16 @@ public function testExtraFieldsAllowed()
146216
'foo' => 5,
147217
'bar' => 6,
148218
);
149-
150-
$this->assertTrue($this->validator->isValid($array, new Collection(array(
219+
$collection = new Collection(array(
151220
'fields' => array(
152221
'foo' => new Min(4),
153222
),
154223
'allowExtraFields' => true,
155-
))));
224+
));
225+
226+
$this->assertTrue($this->validator->isValid($array, $collection));
227+
$this->assertTrue($this->validator->isValid(new \ArrayObject($array), $collection));
228+
$this->assertTrue($this->validator->isValid(new TestArrayObject($array), $collection));
156229
}
157230

158231
public function testMissingFieldsDisallowed()
@@ -162,6 +235,16 @@ public function testMissingFieldsDisallowed()
162235
'foo' => new Min(4),
163236
),
164237
))));
238+
$this->assertFalse($this->validator->isValid(new \ArrayObject(array()), new Collection(array(
239+
'fields' => array(
240+
'foo' => new Min(4),
241+
),
242+
))));
243+
$this->assertFalse($this->validator->isValid(new TestArrayObject(array()), new Collection(array(
244+
'fields' => array(
245+
'foo' => new Min(4),
246+
),
247+
))));
165248
}
166249

167250
public function testMissingFieldsAllowed()
@@ -172,16 +255,58 @@ public function testMissingFieldsAllowed()
172255
),
173256
'allowMissingFields' => true,
174257
))));
258+
$this->assertTrue($this->validator->isValid(new \ArrayObject(array()), new Collection(array(
259+
'fields' => array(
260+
'foo' => new Min(4),
261+
),
262+
'allowMissingFields' => true,
263+
))));
264+
$this->assertTrue($this->validator->isValid(new TestArrayObject(array()), new Collection(array(
265+
'fields' => array(
266+
'foo' => new Min(4),
267+
),
268+
'allowMissingFields' => true,
269+
))));
175270
}
176271

177-
public function getValidArguments()
178-
{
179-
return array(
180-
// can only test for one entry, because PHPUnits mocking does not allow
181-
// to expect multiple method calls with different arguments
182-
array(array('foo' => 3)),
183-
array(new \ArrayObject(array('foo' => 3))),
184-
);
272+
public function testArrayAccessObject() {
273+
$value = new TestArrayObject();
274+
$value['foo'] = 12;
275+
$value['asdf'] = 'asdfaf';
276+
277+
$this->assertTrue(isset($value['asdf']));
278+
$this->assertTrue(isset($value['foo']));
279+
$this->assertFalse(empty($value['asdf']));
280+
$this->assertFalse(empty($value['foo']));
281+
282+
$result = $this->validator->isValid($value, new Collection(array(
283+
'fields' => array(
284+
'foo' => new NotBlank(),
285+
'asdf' => new NotBlank()
286+
)
287+
)));
288+
289+
$this->assertTrue($result);
290+
}
291+
292+
public function testArrayObject() {
293+
$value = new \ArrayObject(array());
294+
$value['foo'] = 12;
295+
$value['asdf'] = 'asdfaf';
296+
297+
$this->assertTrue(isset($value['asdf']));
298+
$this->assertTrue(isset($value['foo']));
299+
$this->assertFalse(empty($value['asdf']));
300+
$this->assertFalse(empty($value['foo']));
301+
302+
$result = $this->validator->isValid($value, new Collection(array(
303+
'fields' => array(
304+
'foo' => new NotBlank(),
305+
'asdf' => new NotBlank()
306+
)
307+
)));
308+
309+
$this->assertTrue($result);
185310
}
186311

187312
public function testObjectShouldBeLeftUnchanged()
@@ -199,4 +324,34 @@ public function testObjectShouldBeLeftUnchanged()
199324
'foo' => 3
200325
), (array) $value);
201326
}
327+
328+
public function getValidArguments()
329+
{
330+
return array(
331+
// can only test for one entry, because PHPUnits mocking does not allow
332+
// to expect multiple method calls with different arguments
333+
array(array('foo' => 3)),
334+
array(new \ArrayObject(array('foo' => 3))),
335+
array(new TestArrayObject(array('foo' => 3))),
336+
);
337+
}
338+
339+
public function getArgumentsWithExtraFields()
340+
{
341+
return array(
342+
array(array(
343+
'foo' => 5,
344+
'bar' => 6,
345+
)),
346+
array(new \ArrayObject(array(
347+
'foo' => 5,
348+
'bar' => 6,
349+
))),
350+
array(new TestArrayObject(array(
351+
'foo' => 5,
352+
'bar' => 6,
353+
)))
354+
);
355+
}
202356
}
357+

0 commit comments

Comments
 (0)