ClassMetadataFactoryTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Tests\Mapping\Factory;
  11. use Doctrine\Common\Annotations\AnnotationReader;
  12. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
  13. use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
  14. use Symfony\Component\Serializer\Mapping\Loader\LoaderChain;
  15. use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory;
  16. /**
  17. * @author Kévin Dunglas <dunglas@gmail.com>
  18. */
  19. class ClassMetadataFactoryTest extends \PHPUnit_Framework_TestCase
  20. {
  21. public function testInterface()
  22. {
  23. $classMetadata = new ClassMetadataFactory(new LoaderChain(array()));
  24. $this->assertInstanceOf('Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface', $classMetadata);
  25. }
  26. public function testGetMetadataFor()
  27. {
  28. $factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
  29. $classMetadata = $factory->getMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
  30. $this->assertEquals(TestClassMetadataFactory::createClassMetadata(true, true), $classMetadata);
  31. }
  32. public function testHasMetadataFor()
  33. {
  34. $factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
  35. $this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'));
  36. $this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummyParent'));
  37. $this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummyInterface'));
  38. $this->assertFalse($factory->hasMetadataFor('Dunglas\Entity'));
  39. }
  40. /**
  41. * @group legacy
  42. */
  43. public function testCacheExists()
  44. {
  45. $cache = $this->getMockBuilder('Doctrine\Common\Cache\Cache')->getMock();
  46. $cache
  47. ->expects($this->once())
  48. ->method('fetch')
  49. ->will($this->returnValue('foo'))
  50. ;
  51. $factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()), $cache);
  52. $this->assertEquals('foo', $factory->getMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'));
  53. }
  54. /**
  55. * @group legacy
  56. */
  57. public function testCacheNotExists()
  58. {
  59. $cache = $this->getMockBuilder('Doctrine\Common\Cache\Cache')->getMock();
  60. $cache->method('fetch')->will($this->returnValue(false));
  61. $cache->method('save');
  62. $factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()), $cache);
  63. $metadata = $factory->getMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
  64. $this->assertEquals(TestClassMetadataFactory::createClassMetadata(true, true), $metadata);
  65. }
  66. }