CacheMetadataFactoryTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 Symfony\Component\Cache\Adapter\ArrayAdapter;
  12. use Symfony\Component\Serializer\Mapping\ClassMetadata;
  13. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
  14. use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
  15. use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
  16. /**
  17. * @author Kévin Dunglas <dunglas@gmail.com>
  18. */
  19. class CacheMetadataFactoryTest extends \PHPUnit_Framework_TestCase
  20. {
  21. public function testGetMetadataFor()
  22. {
  23. $metadata = new ClassMetadata(Dummy::class);
  24. $decorated = $this->getMockBuilder(ClassMetadataFactoryInterface::class)->getMock();
  25. $decorated
  26. ->expects($this->once())
  27. ->method('getMetadataFor')
  28. ->will($this->returnValue($metadata))
  29. ;
  30. $factory = new CacheClassMetadataFactory($decorated, new ArrayAdapter());
  31. $this->assertEquals($metadata, $factory->getMetadataFor(Dummy::class));
  32. // The second call should retrieve the value from the cache
  33. $this->assertEquals($metadata, $factory->getMetadataFor(Dummy::class));
  34. }
  35. public function testHasMetadataFor()
  36. {
  37. $decorated = $this->getMockBuilder(ClassMetadataFactoryInterface::class)->getMock();
  38. $decorated
  39. ->expects($this->once())
  40. ->method('hasMetadataFor')
  41. ->will($this->returnValue(true))
  42. ;
  43. $factory = new CacheClassMetadataFactory($decorated, new ArrayAdapter());
  44. $this->assertTrue($factory->hasMetadataFor(Dummy::class));
  45. }
  46. /**
  47. * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
  48. */
  49. public function testInvalidClassThrowsException()
  50. {
  51. $decorated = $this->getMockBuilder(ClassMetadataFactoryInterface::class)->getMock();
  52. $factory = new CacheClassMetadataFactory($decorated, new ArrayAdapter());
  53. $factory->getMetadataFor('Not\Exist');
  54. }
  55. }