1111
1212namespace Symfony \Component \Validator \Tests \Mapping \Factory ;
1313
14+ use Symfony \Component \Validator \Constraints \Callback ;
1415use Symfony \Component \Validator \Mapping \ClassMetadata ;
1516use Symfony \Component \Validator \Mapping \Factory \LazyLoadingMetadataFactory ;
1617use Symfony \Component \Validator \Mapping \Loader \LoaderInterface ;
@@ -30,8 +31,8 @@ public function testLoadClassMetadataWithInterface()
3031 $ metadata = $ factory ->getMetadataFor (self ::PARENT_CLASS );
3132
3233 $ constraints = array (
33- new ConstraintA (array ('groups ' => array ('Default ' , 'EntityInterfaceA ' , 'EntityParent ' ))),
3434 new ConstraintA (array ('groups ' => array ('Default ' , 'EntityParent ' ))),
35+ new ConstraintA (array ('groups ' => array ('Default ' , 'EntityInterfaceA ' , 'EntityParent ' ))),
3536 );
3637
3738 $ this ->assertEquals ($ constraints , $ metadata ->getConstraints ());
@@ -45,8 +46,6 @@ public function testMergeParentConstraints()
4546 $ constraints = array (
4647 new ConstraintA (array ('groups ' => array (
4748 'Default ' ,
48- 'EntityInterfaceA ' ,
49- 'EntityParent ' ,
5049 'Entity ' ,
5150 ))),
5251
8000
new ConstraintA (array ('groups ' => array (
@@ -56,8 +55,8 @@ public function testMergeParentConstraints()
5655 ))),
5756 new ConstraintA (array ('groups ' => array (
5857 'Default ' ,
59- 'EntityParentInterface ' ,
60- 'EntityInterfaceB ' ,
58+ 'EntityInterfaceA ' ,
59+ 'EntityParent ' ,
6160 'Entity ' ,
6261 ))),
6362 new ConstraintA (array ('groups ' => array (
@@ -67,6 +66,8 @@ public function testMergeParentConstraints()
6766 ))),
6867 new ConstraintA (array ('groups ' => array (
6968 'Default ' ,
69+ 'EntityParentInterface ' ,
70+ 'EntityInterfaceB ' ,
7071 'Entity ' ,
7172 ))),
7273 );
@@ -80,8 +81,8 @@ public function testWriteMetadataToCache()
8081 $ factory = new LazyLoadingMetadataFactory (new TestLoader (), $ cache );
8182
8283 $ parentClassConstraints = array (
83- new ConstraintA (array ('groups ' => array ('Default ' , 'EntityInterfaceA ' , 'EntityParent ' ))),
8484 new ConstraintA (array ('groups ' => array ('Default ' , 'EntityParent ' ))),
85+ new ConstraintA (array ('groups ' => array ('Default ' , 'EntityInterfaceA ' , 'EntityParent ' ))),
8586 );
8687 $ interfaceAConstraints = array (
8788 new ConstraintA (array ('groups ' => array ('Default ' , 'EntityInterfaceA ' ))),
@@ -122,17 +123,51 @@ public function testReadMetadataFromCache()
122123 $ metadata = new ClassMetadata (self ::PARENT_CLASS );
123124 $ metadata ->addConstraint (new ConstraintA ());
124125
126+ $ parentClass = self ::PARENT_CLASS ;
127+ $ interfaceClass = self ::INTERFACE_A_CLASS ;
128+
125129 $ loader ->expects ($ this ->never ())
126130 ->method ('loadClassMetadata ' );
127131
128132 $ cache ->expects ($ this ->never ())
129133 ->method ('has ' );
130- $ cache ->expects ($ this ->once ( ))
134+ $ cache ->expects ($ this ->exactly ( 2 ))
131135 ->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+ });
133147
134148 $ this ->assertEquals ($ metadata , $ factory ->getMetadataFor (self ::PARENT_CLASS ));
135149 }
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+ }
136171}
137172
138173class TestLoader implements LoaderInterface
0 commit comments