8000 [PropertyAccess] Refactored PropertyAccessorCollectionTest · symfony/symfony@20e6bf8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 20e6bf8

Browse files
committed
[PropertyAccess] Refactored PropertyAccessorCollectionTest
1 parent 0488389 commit 20e6bf8

File tree

4 files changed

+46
-134
lines changed

4 files changed

+46
-134
lines changed

src/Symfony/Component/PropertyAccess/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ CHANGELOG
55
------
66

77
* allowed non alpha numeric characters in second level and deeper object properties names
8+
* [BC BREAK] when accessing an index on an object that does not implement
9+
ArrayAccess, a NoSuchIndexException is now thrown instead of the
10+
semantically wrong NoSuchPropertyException
811

912
2.3.0
1013
------

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr
160160
*
161161
* @return mixed The value of the key
162162
*
163-
* @throws NoSuchPropertyException If the array does not implement \ArrayAccess or it is not an array
163+
* @throws NoSuchIndexException If the array does not implement \ArrayAccess or it is not an array
164164
*/
165165
private function &readIndex(&$array, $index)
166166
{
167167
if (!$array instanceof \ArrayAccess && !is_array($array)) {
168-
throw new NoSuchPropertyException(sprintf('Index "%s" cannot be read from object of type "%s" because it doesn\'t implement \ArrayAccess', $index, get_class($array)));
168+
throw new NoSuchIndexException(sprintf('Index "%s" cannot be read from object of type "%s" because it doesn\'t implement \ArrayAccess', $index, get_class($array)));
169169
}
170170

171171
// Use an array instead of an object since performance is very crucial here
@@ -271,12 +271,12 @@ private function &readProperty(&$object, $property)
271271
* @param string|integer $index The index to write at
272272
* @param mixed $value The value to write
273273
*
274-
* @throws NoSuchPropertyException If the array does not implement \ArrayAccess or it is not an array
274+
* @throws NoSuchIndexException If the array does not implement \ArrayAccess or it is not an array
275275
*/
276276
private function writeIndex(&$array, $index, $value)
277277
{
278278
if (!$array instanceof \ArrayAccess && !is_array($array)) {
279-
throw new NoSuchPropertyException(sprintf('Index "%s" cannot be modified in object of type "%s" because it doesn\'t implement \ArrayAccess', $index, get_class($array)));
279+
throw new NoSuchIndexException(sprintf('Index "%s" cannot be modified in object of type "%s" because it doesn\'t implement \ArrayAccess', $index, get_class($array)));
280280
}
281281

282282
$array[$index] = $value;

src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorCollectionTest.php

Lines changed: 23 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,6 @@ public function getAxes()
4747
}
4848
}
4949

50-
class PropertyAccessorCollectionTest_CarCustomSingular
51-
{
52-
public function addFoo($axis) {}
53-
54-
public function removeFoo($axis) {}
55-
56-
public function getAxes() {}
57-
}
58-
59-
class PropertyAccessorCollectionTest_Engine
60-
{
61-
}
62-
6350
class PropertyAccessorCollectionTest_CarOnlyAdder
6451
{
6552
public function addAxis($axis) {}
@@ -79,13 +66,6 @@ class PropertyAccessorCollectionTest_CarNoAdderAndRemover
7966
public function getAxes() {}
8067
}
8168

82-
class PropertyAccessorCollectionTest_CarNoAdderAndRemoverWithProperty
83-
{
84-
protected $axes = array();
85-
86-
public function getAxes() {}
87-
}
88-
8969
class PropertyAccessorCollectionTest_CompositeCar
9070
{
9171
public function getStructure() {}
@@ -116,52 +96,34 @@ protected function setUp()
11696

11797
abstract protected function getCollection(array $array);
11898

119-
public function testGetValueReadsArrayAccess()
99+
public function getValidPropertyPaths()
120100
{
121-
$object = $this->getCollection(array('firstName' => 'Bernhard'));
122-
123-
$this->assertEquals('Bernhard', $this->propertyAccessor->getValue($object, '[firstName]'));
124-
}
125-
126-
public function testGetValueReadsNestedArrayAccess()
127-
{
128-
$object = $this->getCollection(array('person' => array('firstName' => 'Bernhard')));
129-
130-
$this->assertEquals('Bernhard', $this->propertyAccessor->getValue($object, '[person][firstName]'));
101+
return array(
102+
array(array('firstName' => 'Bernhard'), '[firstName]', 'Bernhard'),
103+
array(array('person' => array('firstName' => 'Bernhard')), '[person][firstName]', 'Bernhard'),
104+
);
131105
}
132106

133107
/**
134-
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
108+
* @dataProvider getValidPropertyPaths
135109
*/
136-
public function testGetValueThrowsExceptionIfArrayAccessExpected()
137-
{
138-
$this->propertyAccessor->getValue(new \stdClass(), '[firstName]');
139-
}
140-
141-
public function testSetValueUpdatesArrayAccess()
142-
{
143-
$object = $this->getCollection(array());
144-
145-
$this->propertyAccessor->setValue($object, '[firstName]', 'Bernhard');
146-
147-
$this->assertEquals('Bernhard', $object['firstName']);
148-
}
149-
150-
public function testSetValueUpdatesNestedArrayAccess()
110+
public function testGetValue(array $array, $path, $value)
151111
{
152-
$object = $this->getCollection(array());
112+
$collection = $this->getCollection($array);
153113

154-
$this->propertyAccessor->setValue($object, '[person][firstName]', 'Bernhard');
155-
156-
$this->assertEquals('Bernhard', $object['person']['firstName']);
114+
$this->assertSame($value, $this->propertyAccessor->getValue($collection, $path));
157115
}
158116

159117
/**
160-
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
118+
* @dataProvider getValidPropertyPaths
161119
*/
162-
public function testSetValueThrowsExceptionIfArrayAccessExpected()
120+
public function testSetValue(array $array, $path)
163121
{
164-
$this->propertyAccessor->setValue(new \stdClass(), '[firstName]', 'Bernhard');
122+
$collection = $this->getCollection($array);
123+
124+
$this->propertyAccessor->setValue($collection, $path, 'Updated');
125+
126+
$this->assertSame('Updated', $this->propertyAccessor->getValue($collection, $path));
165127
}
166128

167129
public function testSetValueCallsAdderAndRemoverForCollections()
@@ -210,32 +172,9 @@ public function testSetValueCallsAdderAndRemoverForNestedCollections()
210172
$this->propertyAccessor->setValue($car, 'structure.axes', $axesAfter);
211173
}
212174

213-
public function testSetValueCallsCustomAdderAndRemover()
214-
{
215-
$this->markTestSkipped('This feature is temporarily disabled as of 2.1');
216-
217-
$car = $this->getMock(__CLASS__.'_CarCustomSingular');
218-
$axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth'));
219-
$axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
220-
221-
$car->expects($this->at(0))
222-
->method('getAxes')
223-
->will($this->returnValue($axesBefore));
224-
$car->expects($this->at(1))
225-
->method('removeFoo')
226-
->with('fourth');
227-
$car->expects($this->at(2))
228-
->method('addFoo')
229-
->with('first');
230-
$car->expects($this->at(3))
231-
->method('addFoo')
232-
->with('third');
233-
234-
$this->propertyAccessor->setValue($car, 'axes|foo', $axesAfter);
235-
}
236-
237175
/**
238176
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
177+
* @expectedExceptionMessage Found the public method "addAxis()", but did not find a public "removeAxis()" on class Mock_PropertyAccessorCollectionTest_CarOnlyAdder
239178
*/
240179
public function testSetValueFailsIfOnlyAdderFound()
241180
{
@@ -252,6 +191,7 @@ public function testSetValueFailsIfOnlyAdderFound()
252191

253192
/**
254193
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
194+
* @expectedExceptionMessage Found the public method "removeAxis()", but did not find a public "addAxis()" on class Mock_PropertyAccessorCollectionTest_CarOnlyRemover
255195
*/
256196
public function testSetValueFailsIfOnlyRemoverFound()
257197
{
@@ -267,58 +207,14 @@ public function testSetValueFailsIfOnlyRemoverFound()
267207
}
268208

269209
/**
270-
* @dataProvider noAdderRemoverData
210+
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
211+
* @expectedExceptionMessage Neither the property "axes" nor one of the methods "addAx()", "addAxe()", "addAxis()", "setAxes()", "__set()" or "__call()" exist and have public access in class "Mock_PropertyAccessorCollectionTest_CarNoAdderAndRemover
271212
*/
272-
public function testNoAdderAndRemoverThrowsSensibleError($car, $path, $message)
273-
{
274-
$axes = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
275-
276-
try {
277-
$this->propertyAccessor->setValue($car, $path, $axes);
278-
$this->fail('An expected exception was not thrown!');
279-
} catch (ExceptionInterface $e) {
280-
$this->assertEquals($message, $e->getMessage());
281-
}
282-
}
283-
284-
public function noAdderRemoverData()
213+
public function testSetValueFailsIfNoAdderAndNoRemoverFound()
285214
{
286-
$data = array();
287-
288215
$car = $this->getMock(__CLASS__.'_CarNoAdderAndRemover');
289-
$propertyPath = 'axes';
290-
$expectedMessage = sprintf(
291-
'Neither the property "axes" nor one of the methods "addAx()", '.
292-
'"addAxe()", "addAxis()", "setAxes()", "__set()" or "__call()" exist and have '.
293-
'public access in class "%s".',
294-
get_class($car)
295-
);
296-
$data[] = array($car, $propertyPath, $expectedMessage);
297-
298-
/*
299-
Temporarily disabled in 2.1
300-
301-
$propertyPath = new PropertyPath('axes|boo');
302-
$expectedMessage = sprintf(
303-
'Neither element "axes" nor method "setAxes()" exists in class '
304-
.'"%s", nor could adders and removers be found based on the '
305-
.'passed singular: %s',
306-
get_class($car),
307-
'boo'
308-
);
309-
$data[] = array($car, $propertyPath, $expectedMessage);
310-
*/
311-
312-
$car = $this->getMock(__CLASS__.'_CarNoAdderAndRemoverWithProperty');
313-
$propertyPath = 'axes';
314-
$expectedMessage = sprintf(
315-
'Neither the property "axes" nor one of the methods "addAx()", '.
316-
'"addAxe()", "addAxis()", "setAxes()", "__set()" or "__call()" exist and have '.
317-
'public access in class "%s".',
318-
get_class($car)
319-
);
320-
$data[] = array($car, $propertyPath, $expectedMessage);
216+
$axes = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
321217

322-
return $data;
218+
$this->propertyAccessor->setValue($car, 'axes', $axes);
323219
}
324220
}

src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ protected function setUp()
3131

3232
public function getValidPropertyPaths()
3333
{
34-
3534
return array(
3635
array(array('Bernhard', 'Schussek'), '[0]', 'Bernhard'),
3736
array(array('Bernhard', 'Schussek'), '[1]', 'Schussek'),
@@ -65,7 +64,6 @@ public function getValidPropertyPaths()
6564

6665
public function getPathsWithMissingProperty()
6766
{
68-
6967
return array(
7068
array((object) array('firstName' => 'Bernhard'), 'lastName'),
7169
array((object) array('property' => (object) array('firstName' => 'Bernhard')), 'property.lastName'),
@@ -86,7 +84,6 @@ public function getPathsWithMissingProperty()
8684

8785
public function getPathsWithMissingIndex()
8886
{
89-
9087
return array(
9188
array(array('firstName' => 'Bernhard'), '[lastName]'),
9289
array(array(), '[index][lastName]'),
@@ -131,6 +128,14 @@ public function testGetValueThrowsExceptionIfIndexNotFoundAndIndexExceptionsEnab
131128
$this->propertyAccessor->getValue($objectOrArray, $path);
132129
}
133130

131+
/**
132+
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchIndexException
133+
*/
134+
public function testGetValueThrowsExceptionIfNotArrayAccess()
135+
{
136+
$this->propertyAccessor->getValue(new \stdClass(), '[index]');
137+
}
138+
134139
public function testGetValueReadsMagicGet()
135140
{
136141
$this->assertSame('Bernhard', $this->propertyAccessor->getValue(new TestClassMagicGet('Bernhard'), 'magicProperty'));
@@ -229,6 +234,14 @@ public function testSetValueThrowsNoExceptionIfIndexNotFoundAndIndexExceptionsEn
229234
$this->assertSame('Updated', $this->propertyAccessor->getValue($objectOrArray, $path));
230235
}
231236

237+
/**
238+
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchIndexException
239+
*/
240+
public function testSetValueThrowsExceptionIfNotArrayAccess()
241+
{
242+
$this->propertyAccessor->setValue(new \stdClass(), '[index]', 'Updated');
243+
}
244+
232245
public function testSetValueUpdatesMagicSet()
233246
{
234247
$author = new TestClassMagicGet('Bernhard');

0 commit comments

Comments
 (0)
0