11
11
12
12
namespace Symfony \Component \PropertyAccess \Tests ;
13
13
14
+ use Doctrine \Common \Annotations \AnnotationReader ;
15
+ use Doctrine \Common \Annotations \AnnotationRegistry ;
16
+ use Symfony \Component \PropertyAccess \PropertyAccessor ;
17
+
14
18
class PropertyAccessorCollectionTest_Car
15
19
{
16
20
private $ axes ;
17
21
22
+ /**
23
+ * @Symfony\Component\PropertyAccess\Annotation\PropertyAccessor(adder="addAxisTest", remover="removeAxisTest")
24
+ */
25
+ private $ customAxes ;
26
+
18
27
public function __construct ($ axes = null )
19
28
{
20
29
$ this ->axes = $ axes ;
30
+ $ this ->customAxes = $ axes ;
21
31
}
22
32
23
33
// In the test, use a name that StringUtil can't uniquely singularify
@@ -26,6 +36,12 @@ public function addAxis($axis)
26
36
$ this ->axes [] = $ axis ;
27
37
}
28
38
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
+
29
45
public function removeAxis ($ axis )
30
46
{
31
47
foreach ($ this ->axes as $ key => $ value ) {
@@ -37,10 +53,26 @@ public function removeAxis($axis)
37
53
}
38
54
}
39
55
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
+
40
67
public function getAxes ()
41
68
{
42
69
return $ this ->axes ;
43
70
}
71
+
72
+ public function getCustomAxes ()
73
+ {
74
+ return $ this ->customAxes ;
75
+ }
44
76
}
45
77
46
78
class PropertyAccessorCollectionTest_CarOnlyAdder
@@ -146,6 +178,28 @@ public function testSetValueCallsAdderAndRemoverForNestedCollections()
146
178
$ this ->propertyAccessor ->setValue ($ car , 'structure.axes ' , $ axesAfter );
147
179
}
148
180
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
+
149
203
/**
150
204
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
151
205
* @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