MetadataFactoryTest.php 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Metadata\Tests;
  3. use Metadata\ClassMetadata;
  4. use Metadata\MetadataFactory;
  5. class MetadataFactoryTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testGetMetadataForClass()
  8. {
  9. $driver = $this->getMock('Metadata\Driver\DriverInterface');
  10. $driver
  11. ->expects($this->at(0))
  12. ->method('loadMetadataForClass')
  13. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\TestObject')))
  14. ->will($this->returnCallback(function($class) {
  15. return new ClassMetadata($class->getName());
  16. }))
  17. ;
  18. $driver
  19. ->expects($this->at(1))
  20. ->method('loadMetadataForClass')
  21. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\TestParent')))
  22. ->will($this->returnCallback(function($class) {
  23. return new ClassMetadata($class->getName());
  24. }))
  25. ;
  26. $factory = new MetadataFactory($driver);
  27. $metadata = $factory->getMetadataForClass('Metadata\Tests\Fixtures\TestParent');
  28. $this->assertInstanceOf('Metadata\ClassHierarchyMetadata', $metadata);
  29. $this->assertEquals(2, count($metadata->classMetadata));
  30. }
  31. public function testGetMetadataWithCache()
  32. {
  33. $driver = $this->getMock('Metadata\Driver\DriverInterface');
  34. $driver
  35. ->expects($this->once())
  36. ->method('loadMetadataForClass')
  37. ->will($this->returnValue($metadata = new ClassMetadata('Metadata\Tests\Fixtures\TestObject')))
  38. ;
  39. $factory = new MetadataFactory($driver);
  40. $cache = $this->getMock('Metadata\Cache\CacheInterface');
  41. $cache
  42. ->expects($this->once())
  43. ->method('loadClassMetadataFromCache')
  44. ->with($this->equalTo(new \ReflectionClass('Metadata\Tests\Fixtures\TestObject')))
  45. ->will($this->returnValue(null))
  46. ;
  47. $cache
  48. ->expects($this->once())
  49. ->method('putClassMetadataInCache')
  50. ->with($this->equalTo($metadata))
  51. ;
  52. $factory->setCache($cache);
  53. $this->assertSame($metadata, reset($factory->getMetadataForClass('Metadata\Tests\Fixtures\TestObject')->classMetadata));
  54. }
  55. public function testGetMetadataReturnsNullIfNoMetadataIsFound()
  56. {
  57. $driver = $this->getMock('Metadata\Driver\DriverInterface');
  58. $driver
  59. ->expects($this->once())
  60. ->method('loadMetadataForClass')
  61. ->will($this->returnValue(null))
  62. ;
  63. $factory = new MetadataFactory($driver);
  64. $this->assertNull($factory->getMetadataForClass('stdClass'));
  65. }
  66. }