Skip to content

Commit 830fee6

Browse files
committed
[PropertyAccess] WIP: Allow customizing which methods get called when accessing properties
1 parent 0f303e9 commit 830fee6

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

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

+54
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@
1111

1212
namespace Symfony\Component\PropertyAccess\Tests;
1313

14+
use Doctrine\Common\Annotations\AnnotationReader;
15+
use Doctrine\Common\Annotations\AnnotationRegistry;
16+
use Symfony\Component\PropertyAccess\PropertyAccessor;
17+
1418
class PropertyAccessorCollectionTest_Car
1519
{
1620
private $axes;
1721

22+
/**
23+
* @Symfony\Component\PropertyAccess\Annotation\PropertyAccessor(adder="addAxisTest", remover="removeAxisTest")
24+
*/
25+
private $customAxes;
26+
1827
public function __construct($axes = null)
1928
{
2029
$this->axes = $axes;
30+
$this->customAxes = $axes;
2131
}
2232

2333
// In the test, use a name that StringUtil can't uniquely singularify
@@ -26,6 +36,12 @@ public function addAxis($axis)
2636
$this->axes[] = $axis;
2737
}
2838

39+
// In the test, use a name that StringUtil can't uniquely singularify
40+
public function addAxisTest($axis)
41+
{
42+
$this->customAxes[] = $axis;
43+
}
44+
2945
public function removeAxis($axis)
3046
{
3147
foreach ($this->axes as $key => $value) {
@@ -37,10 +53,26 @@ public function removeAxis($axis)
3753
}
3854
}
3955

56+
public function removeAxisTest($axis)
57+
{
58+
foreach ($this->customAxes as $key => $value) {
59+
if ($value === $axis) {
60+
unset($this->customAxes[$key]);
61+
62+
return;
63+
}
64+
}
65+
}
66+
4067
public function getAxes()
4168
{
4269
return $this->axes;
4370
}
71+
72+
public function getCustomAxes()
73+
{
74+
return $this->customAxes;
75+
}
4476
}
4577

4678
class PropertyAccessorCollectionTest_CarOnlyAdder
@@ -146,6 +178,28 @@ public function testSetValueCallsAdderAndRemoverForNestedCollections()
146178
$this->propertyAccessor->setValue($car, 'structure.axes', $axesAfter);
147179
}
148180

181+
public function testSetValueCallsCustomAdderAndRemoverForCollections()
182+
{
183+
$axesBefore = $this->getContainer(array(1 => 'second', 3 => 'fourth', 4 => 'fifth'));
184+
$axesMerged = $this->getContainer(array(1 => 'first', 2 => 'second', 3 => 'third'));
185+
$axesAfter = $this->getContainer(array(1 => 'second', 5 => 'first', 6 => 'third'));
186+
$axesMergedCopy = is_object($axesMerged) ? clone $axesMerged : $axesMerged;
187+
188+
// Don't use a mock in order to test whether the collections are
189+
// modified while iterating them
190+
$car = new PropertyAccessorCollectionTest_Car($axesBefore);
191+
192+
AnnotationRegistry::registerAutoloadNamespace('Symfony\Component\PropertyAccess\Annotation', __DIR__.'/../../../..');
193+
$this->propertyAccessor = new PropertyAccessor(false, false, new AnnotationReader());
194+
195+
$this->propertyAccessor->setValue($car, 'customAxes', $axesMerged);
196+
197+
$this->assertEquals($axesAfter, $car->getCustomAxes());
198+
199+
// The passed collection was not modified
200+
$this->assertEquals($axesMergedCopy, $axesMerged);
201+
}
202+
149203
/**
150204
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
151205
* @expectedExceptionMessage Neither the property "axes" nor one of the methods "addAx()"/"removeAx()", "addAxe()"/"removeAxe()", "addAxis()"/"removeAxis()", "setAxes()", "axes()", "__set()" or "__call()" exist and have public access in class "Mock_PropertyAccessorCollectionTest_CarNoAdderAndRemover

0 commit comments

Comments
 (0)