11
11
12
12
namespace Symfony \Component \Validator \Tests \Mapping \Factory ;
13
13
14
+ use Symfony \Component \Validator \Constraints \Callback ;
14
15
use Symfony \Component \Validator \Mapping \ClassMetadata ;
15
16
use Symfony \Component \Validator \Mapping \Factory \LazyLoadingMetadataFactory ;
16
17
use Symfony \Component \Validator \Mapping \Loader \LoaderInterface ;
@@ -30,8 +31,8 @@ public function testLoadClassMetadataWithInterface()
30
31
$ metadata = $ factory ->getMetadataFor (self ::PARENT_CLASS );
31
32
32
33
$ constraints = array (
33
- new ConstraintA (array ('groups ' => array ('Default ' , 'EntityInterfaceA ' , 'EntityParent ' ))),
34
34
new ConstraintA (array ('groups ' => array ('Default ' , 'EntityParent ' ))),
35
+ new ConstraintA (array ('groups ' => array ('Default ' , 'EntityInterfaceA ' , 'EntityParent ' ))),
35
36
);
36
37
37
38
$ this ->assertEquals ($ constraints , $ metadata ->getConstraints ());
@@ -45,8 +46,6 @@ public function testMergeParentConstraints()
45
46
$ constraints = array (
46
47
new ConstraintA (array ('groups ' => array (
47
48
'Default ' ,
48
- 'EntityInterfaceA ' ,
49
- 'EntityParent ' ,
50
49
'Entity ' ,
51
50
))),
52
51
new ConstraintA (array ('groups ' => array (
@@ -56,8 +55,8 @@ public function testMergeParentConstraints()
56
55
))),
57
56
new ConstraintA (array ('groups ' => array (
58
57
'Default ' ,
59
- 'EntityParentInterface ' ,
60
- 'EntityInterfaceB ' ,
58
+ 'EntityInterfaceA ' ,
59
+ 'EntityParent ' ,
61
60
'Entity ' ,
62
61
))),
63
62
new ConstraintA (array ('groups ' => array (
@@ -67,6 +66,8 @@ public function testMergeParentConstraints()
67
66
))),
68
67
new ConstraintA (array ('groups ' => array (
69
68
'Default ' ,
69
+ 'EntityParentInterface ' ,
70
+ 'EntityInterfaceB ' ,
70
71
'Entity ' ,
71
72
))),
72
73
);
@@ -80,8 +81,8 @@ public function testWriteMetadataToCache()
80
81
$ factory = new LazyLoadingMetadataFactory (new TestLoader (), $ cache );
81
82
82
83
$ parentClassConstraints = array (
83
- new ConstraintA (array ('groups ' => array ('Default ' , 'EntityInterfaceA ' , 'EntityParent ' ))),
84
84
new ConstraintA (array ('groups ' => array ('Default ' , 'EntityParent ' ))),
85
+ new ConstraintA (array ('groups ' => array ('Default ' , 'EntityInterfaceA ' , 'EntityParent ' ))),
85
86
);
86
87
$ interfaceAConstraints = array (
87
88
new ConstraintA (array ('groups ' => array ('Default ' , 'EntityInterfaceA ' ))),
@@ -122,17 +123,51 @@ public function testReadMetadataFromCache()
122
123
$ metadata = new ClassMetadata (self ::PARENT_CLASS );
123
124
$ metadata ->addConstraint (new ConstraintA ());
124
125
126
+ $ parentClass = self ::PARENT_CLASS ;
127
+ $ interfaceClass = self ::INTERFACE_A_CLASS ;
128
+
125
129
$ loader ->expects ($ this ->never ())
126
130
->method ('loadClassMetadata ' );
127
131
128
132
$ cache ->expects ($ this ->never ())
129
133
->method ('has ' );
130
- $ cache ->expects ($ this ->once ( ))
134
+ $ cache ->expects ($ this ->exactly ( 2 ))
131
135
->method ('read ' )
132
- ->will ($ this ->returnValue ($ metadata ));
136
+ ->withConsecutive (
137
+ array (self ::PARENT_CLASS ),
138
+ array (self ::INTERFACE_A_CLASS )
139
+ )
140
+ ->willReturnCallback (function ($ name ) use ($ metadata , $ parentClass , $ interfaceClass ) {
141
+ if ($ parentClass == $ name ) {
142
+ return $ metadata ;
143
+ }
144
+
145
+ return new ClassMetadata ($ interfaceClass );
146
+ });
133
147
134
148
$ this ->assertEquals ($ metadata , $ factory ->getMetadataFor (self ::PARENT_CLASS ));
135
149
}
150
+
151
+ public function testMetadataCacheWithRuntimeConstraint ()
152
+ {
153
+ $ cache = $ this ->getMock ('Symfony\Component\Validator\Mapping\Cache\CacheInterface ' );
154
+ $ factory = new LazyLoadingMetadataFactory (new TestLoader (), $ cache );
155
+
156
+ $ cache
157
+ ->expects ($ this ->any ())
158
+ ->method ('write ' )
159
+ ->will ($ this ->returnCallback (function ($ metadata ) { serialize ($ metadata );}))
160
+ ;
161
+
162
+ $ cache ->expects ($ this ->any ())
163
+ ->method ('read ' )
164
+ ->will ($ this ->returnValue (false ));
165
+
166
+ $ metadata = $ factory ->getMetadataFor (self ::PARENT_CLASS );
167
+ $ metadata ->addConstraint (new Callback (function () {}));
168
+
169
+ $ metadata = $ factory ->getMetadataFor (self ::CLASS_NAME );
170
+ }
136
171
}
137
172
138
173
class TestLoader implements LoaderInterface
0 commit comments