MetadataFactoryTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace Metadata\Tests;
  3. use Metadata\PropertyMetadata;
  4. use Metadata\MergeableClassMetadata;
  5. use Metadata\ClassMetadata;
  6. use Metadata\MetadataFactory;
  7. class MetadataFactoryTest extends \PHPUnit_Framework_TestCase
  8. {
  9. public function testGetMetadataForClass()
  10. {
  11. $driver = $this->getMock('Metadata\Driver\DriverInterface');
  12. $driver
  13. ->expects($this->at(0))
  14. ->method('loadMetadataForClass')
  15. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\TestObject')))
  16. ->will($this->returnCallback(function($class) {
  17. return new ClassMetadata($class->getName());
  18. }))
  19. ;
  20. $driver
  21. ->expects($this->at(1))
  22. ->method('loadMetadataForClass')
  23. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\TestParent')))
  24. ->will($this->returnCallback(function($class) {
  25. return new ClassMetadata($class->getName());
  26. }))
  27. ;
  28. $factory = new MetadataFactory($driver);
  29. $metadata = $factory->getMetadataForClass('Metadata\Tests\Fixtures\TestParent');
  30. $this->assertInstanceOf('Metadata\ClassHierarchyMetadata', $metadata);
  31. $this->assertEquals(2, count($metadata->classMetadata));
  32. }
  33. public function testGetMetadataForClassWhenMergeable()
  34. {
  35. $driver = $this->getMock('Metadata\Driver\DriverInterface');
  36. $driver
  37. ->expects($this->at(0))
  38. ->method('loadMetadataForClass')
  39. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\TestObject')))
  40. ->will($this->returnCallback(function($class) {
  41. return new MergeableClassMetadata($class->getName());
  42. }))
  43. ;
  44. $driver
  45. ->expects($this->at(1))
  46. ->method('loadMetadataForClass')
  47. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\TestParent')))
  48. ->will($this->returnCallback(function($class) {
  49. return new MergeableClassMetadata($class->getName());
  50. }))
  51. ;
  52. $factory = new MetadataFactory($driver);
  53. $metadata = $factory->getMetadataForClass('Metadata\Tests\Fixtures\TestParent');
  54. $this->assertInstanceOf('Metadata\MergeableClassMetadata', $metadata);
  55. $this->assertEquals('Metadata\Tests\Fixtures\TestParent', $metadata->name);
  56. }
  57. public function testGetMetadataWithComplexHierarchy()
  58. {
  59. $driver = $this->getMock('Metadata\Driver\DriverInterface');
  60. $driver
  61. ->expects($this->any())
  62. ->method('loadMetadataForClass')
  63. ->will($this->returnCallback(function($class) {
  64. $metadata = new MergeableClassMetadata($class->name);
  65. switch ($class->name) {
  66. case 'Metadata\Tests\Fixtures\ComplexHierarchy\BaseClass':
  67. $metadata->propertyMetadata['foo'] = new PropertyMetadata($class->name, 'foo');
  68. break;
  69. case 'Metadata\Tests\Fixtures\ComplexHierarchy\SubClassA':
  70. $metadata->propertyMetadata['bar'] = new PropertyMetadata($class->name, 'bar');
  71. break;
  72. case 'Metadata\Tests\Fixtures\ComplexHierarchy\SubClassB':
  73. $metadata->propertyMetadata['baz'] = new PropertyMetadata($class->name, 'baz');
  74. break;
  75. default:
  76. throw new \RuntimeException(sprintf('Unsupported class "%s".', $class->name));
  77. }
  78. return $metadata;
  79. }))
  80. ;
  81. $factory = new MetadataFactory($driver);
  82. $subClassA = $factory->getMetadataForClass('Metadata\Tests\Fixtures\ComplexHierarchy\SubClassA');
  83. $this->assertInstanceOf('Metadata\MergeableClassMetadata', $subClassA);
  84. $this->assertEquals(array('foo', 'bar'), array_keys($subClassA->propertyMetadata));
  85. $subClassB = $factory->getMetadataForClass('Metadata\Tests\Fixtures\ComplexHierarchy\SubClassB');
  86. $this->assertInstanceOf('Metadata\MergeableClassMetadata', $subClassB);
  87. $this->assertEquals(array('foo', 'baz'), array_keys($subClassB->propertyMetadata));
  88. }
  89. public function testGetMetadataWithCache()
  90. {
  91. $driver = $this->getMock('Metadata\Driver\DriverInterface');
  92. $driver
  93. ->expects($this->once())
  94. ->method('loadMetadataForClass')
  95. ->will($this->returnValue($metadata = new ClassMetadata('Metadata\Tests\Fixtures\TestObject')))
  96. ;
  97. $factory = new MetadataFactory($driver);
  98. $cache = $this->getMock('Metadata\Cache\CacheInterface');
  99. $cache
  100. ->expects($this->once())
  101. ->method('loadClassMetadataFromCache')
  102. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\TestObject')))
  103. ->will($this->returnValue(null))
  104. ;
  105. $cache
  106. ->expects($this->once())
  107. ->method('putClassMetadataInCache')
  108. ->with($this->equalTo($metadata))
  109. ;
  110. $factory->setCache($cache);
  111. $factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject');
  112. $factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject');
  113. $this->assertSame($metadata, reset($factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject')->classMetadata));
  114. }
  115. public function testGetMetadataReturnsNullIfNoMetadataIsFound()
  116. {
  117. $driver = $this->getMock('Metadata\Driver\DriverInterface');
  118. $driver
  119. ->expects($this->once())
  120. ->method('loadMetadataForClass')
  121. ->will($this->returnValue(null))
  122. ;
  123. $factory = new MetadataFactory($driver);
  124. $this->assertNull($factory->getMetadataForClass('stdClass'));
  125. }
  126. public function testGetMetadataWithInterfaces()
  127. {
  128. $driver = $this->getMock('Metadata\Driver\DriverInterface');
  129. $driver
  130. ->expects($this->at(3))
  131. ->method('loadMetadataForClass')
  132. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\ComplexHierarchy\SubClassA')))
  133. ;
  134. $driver
  135. ->expects($this->at(2))
  136. ->method('loadMetadataForClass')
  137. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\ComplexHierarchy\InterfaceB')))
  138. ;
  139. $driver
  140. ->expects($this->at(1))
  141. ->method('loadMetadataForClass')
  142. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\ComplexHierarchy\BaseClass')))
  143. ;
  144. $driver
  145. ->expects($this->at(0))
  146. ->method('loadMetadataForClass')
  147. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\ComplexHierarchy\InterfaceA')))
  148. ;
  149. $factory = new MetadataFactory($driver);
  150. $factory->setIncludeInterfaces(true);
  151. $factory->getMetadataForClass('Metadata\Tests\Fixtures\ComplexHierarchy\SubClassA');
  152. }
  153. public function testGetAllClassNames()
  154. {
  155. $driver = $this->getMock('Metadata\Driver\AdvancedDriverInterface');
  156. $driver
  157. ->expects($this->once())
  158. ->method('getAllClassNames')
  159. ->will($this->returnValue(array()));
  160. $factory = new MetadataFactory($driver);
  161. $this->assertSame(array(), $factory->getAllClassNames());
  162. }
  163. public function testGetAllClassNamesThrowsException()
  164. {
  165. $this->setExpectedException('RuntimeException');
  166. $factory = new MetadataFactory($this->getMock('Metadata\Driver\DriverInterface'));
  167. $factory->getAllClassNames();
  168. }
  169. public function testNotFoundMetadataIsCached()
  170. {
  171. $driver = $this->getMock('Metadata\Driver\DriverInterface');
  172. $driver
  173. ->expects($this->once()) // This is the important part of this test
  174. ->method('loadMetadataForClass')
  175. ->will($this->returnValue(null))
  176. ;
  177. $cachedMetadata = null;
  178. $cache = $this->getMock('Metadata\Cache\CacheInterface');
  179. $cache
  180. ->expects($this->any())
  181. ->method('loadClassMetadataFromCache')
  182. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\TestObject')))
  183. ->will($this->returnCallback(function () use (&$cachedMetadata) {
  184. return $cachedMetadata;
  185. }))
  186. ;
  187. $cache
  188. ->expects($this->once())
  189. ->method('putClassMetadataInCache')
  190. ->will($this->returnCallback(function ($metadata) use (&$cachedMetadata) {
  191. $cachedMetadata = $metadata;
  192. }))
  193. ;
  194. $factory = new MetadataFactory($driver);
  195. $factory->setCache($cache);
  196. $factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject');
  197. $factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject');
  198. $this->assertNull($factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject'));
  199. // We use another factory with the same cache, to simulate another request and skip the in memory
  200. $factory = new MetadataFactory($driver);
  201. $factory->setCache($cache);
  202. $factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject');
  203. $factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject');
  204. $this->assertNull($factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject'));
  205. }
  206. public function testNotFoundMetadataIsNotCachedInDebug()
  207. {
  208. $driver = $this->getMock('Metadata\Driver\DriverInterface');
  209. $driver
  210. ->expects($this->exactly(2))
  211. ->method('loadMetadataForClass')
  212. ->will($this->returnValue(null))
  213. ;
  214. $cachedMetadata = null;
  215. $cache = $this->getMock('Metadata\Cache\CacheInterface');
  216. $cache
  217. ->expects($this->any())
  218. ->method('loadClassMetadataFromCache')
  219. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\TestObject')))
  220. ->will($this->returnValue(null))
  221. ;
  222. $cache
  223. ->expects($this->never())
  224. ->method('putClassMetadataInCache')
  225. ;
  226. $factory = new MetadataFactory($driver, 'Metadata\ClassHierarchyMetadata', true);
  227. $factory->setCache($cache);
  228. $factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject');
  229. $this->assertNull($factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject'));
  230. // We use another factory with the same cache, to simulate another request and skip the in memory
  231. $factory = new MetadataFactory($driver, 'Metadata\ClassHierarchyMetadata', true);
  232. $factory->setCache($cache);
  233. $factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject');
  234. $this->assertNull($factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject'));
  235. }
  236. }