/
PropertyPathCollectionTest.php
286 lines (225 loc) · 8.36 KB
/
PropertyPathCollectionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\Util;
use Symfony\Component\Form\Util\PropertyPath;
use Symfony\Component\Form\Util\FormUtil;
class PropertyPathCollectionTest_Car
{
private $axes;
public function __construct($axes = null)
{
$this->axes = $axes;
}
// In the test, use a name that FormUtil can't uniquely singularify
public function addAxis($axis)
{
$this->axes[] = $axis;
}
public function removeAxis($axis)
{
foreach ($this->axes as $key => $value) {
if ($value === $axis) {
unset($this->axes[$key]);
return;
}
}
}
public function getAxes()
{
return $this->axes;
}
}
class PropertyPathCollectionTest_CarCustomSingular
{
public function addFoo($axis) {}
public function removeFoo($axis) {}
public function getAxes() {}
}
class PropertyPathCollectionTest_Engine
{
}
class PropertyPathCollectionTest_CarOnlyAdder
{
public function addAxis($axis) {}
public function getAxes() {}
}
class PropertyPathCollectionTest_CarOnlyRemover
{
public function removeAxis($axis) {}
public function getAxes() {}
}
class PropertyPathCollectionTest_CarNoAdderAndRemover
{
public function getAxes() {}
}
class PropertyPathCollectionTest_CompositeCar
{
public function getStructure() {}
}
class PropertyPathCollectionTest_CarStructure
{
public function addAxis($axis) {}
public function removeAxis($axis) {}
public function getAxes() {}
}
abstract class PropertyPathCollectionTest extends \PHPUnit_Framework_TestCase
{
abstract protected function getCollection(array $array);
public function testGetValueReadsArrayAccess()
{
$object = $this->getCollection(array('firstName' => 'Bernhard'));
$path = new PropertyPath('[firstName]');
$this->assertEquals('Bernhard', $path->getValue($object));
}
/**
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
*/
public function testGetValueThrowsExceptionIfArrayAccessExpected()
{
$path = new PropertyPath('[firstName]');
$path->getValue(new \stdClass());
}
public function testSetValueUpdatesArrayAccess()
{
$object = $this->getCollection(array());
$path = new PropertyPath('[firstName]');
$path->setValue($object, 'Bernhard');
$this->assertEquals('Bernhard', $object['firstName']);
}
/**
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
*/
public function testSetValueThrowsExceptionIfArrayAccessExpected()
{
$path = new PropertyPath('[firstName]');
$path->setValue(new \stdClass(), 'Bernhard');
}
public function testSetValueCallsAdderAndRemoverForCollections()
{
$axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth', 4 => 'fifth'));
$axesMerged = $this->getCollection(array(1 => 'first', 2 => 'second', 3 => 'third'));
$axesAfter = $this->getCollection(array(1 => 'second', 5 => 'first', 6 => 'third'));
// Don't use a mock in order to test whether the collections are
// modified while iterating them
$car = new PropertyPathCollectionTest_Car($axesBefore);
$path = new PropertyPath('axes');
$path->setValue($car, $axesMerged);
$this->assertEquals($axesAfter, $car->getAxes());
}
public function testSetValueCallsAdderAndRemoverForNestedCollections()
{
$car = $this->getMock(__CLASS__ . '_CompositeCar');
$structure = $this->getMock(__CLASS__ . '_CarStructure');
$axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth'));
$axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
$path = new PropertyPath('structure.axes');
$car->expects($this->any())
->method('getStructure')
->will($this->returnValue($structure));
$structure->expects($this->at(0))
->method('getAxes')
->will($this->returnValue($axesBefore));
$structure->expects($this->at(1))
->method('removeAxis')
->with('fourth');
$structure->expects($this->at(2))
->method('addAxis')
->with('first');
$structure->expects($this->at(3))
->method('addAxis')
->with('third');
$path->setValue($car, $axesAfter);
}
public function testSetValueCallsCustomAdderAndRemover()
{
$car = $this->getMock(__CLASS__ . '_CarCustomSingular');
$axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth'));
$axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
$path = new PropertyPath('axes|foo');
$car->expects($this->at(0))
->method('getAxes')
->will($this->returnValue($axesBefore));
$car->expects($this->at(1))
->method('removeFoo')
->with('fourth');
$car->expects($this->at(2))
->method('addFoo')
->with('first');
$car->expects($this->at(3))
->method('addFoo')
->with('third');
$path->setValue($car, $axesAfter);
}
/**
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
*/
public function testMapFormToDataFailsIfOnlyAdderFound()
{
$car = $this->getMock(__CLASS__ . '_CarOnlyAdder');
$axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth'));
$axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
$path = new PropertyPath('axes');
$car->expects($this->any())
->method('getAxes')
->will($this->returnValue($axesBefore));
$path->setValue($car, $axesAfter);
}
/**
* @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
*/
public function testMapFormToDataFailsIfOnlyRemoverFound()
{
$car = $this->getMock(__CLASS__ . '_CarOnlyRemover');
$axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth'));
$axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
$path = new PropertyPath('axes');
$car->expects($this->any())
->method('getAxes')
->will($this->returnValue($axesBefore));
$path->setValue($car, $axesAfter);
}
/**
* @dataProvider noAdderRemoverData
*/
public function testNoAdderAndRemoverThrowsSensibleError($path, $message)
{
$car = $this->getMock(__CLASS__ . '_CarNoAdderAndRemover');
$axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth'));
$axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
try {
$path->setValue($car, $axesAfter);
$this->fail('An expected exception was not thrown!');
} catch (\Symfony\Component\Form\Exception\InvalidPropertyException $e) {
$this->assertEquals(str_replace("{class}", get_class($car), $message), $e->getMessage());
}
}
public function noAdderRemoverData()
{
$data = array();
$propertyPath = new PropertyPath('axes');
$expectedMessage = sprintf(
'Neither element "axes" nor method "setAxes()" exists in class '
.'"{class}", nor could adders and removers be found based on the '
.'guessed singulars: %s (provide a singular by suffixing the '
.'property path with "|{singular}" to override the guesser)',
implode(', ', (array) $singulars = FormUtil::singularify('Axes'))
);
$data[] = array($propertyPath, $expectedMessage);
$propertyPath = new PropertyPath('axes|boo');
$expectedMessage = sprintf(
'Neither element "axes" nor method "setAxes()" exists in class '
.'"{class}", nor could adders and removers be found based on the '
.'passed singular: %s',
'boo'
);
$data[] = array($propertyPath, $expectedMessage);
return $data;
}
}