SluggableMappingTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Doctrine\Common\Util\Debug,
  4. Doctrine\ORM\Mapping\Driver\YamlDriver,
  5. Doctrine\ORM\Mapping\Driver\DriverChain,
  6. Mapping\Fixture\Yaml\Category,
  7. Gedmo\Mapping\ExtensionMetadataFactory;
  8. /**
  9. * These are mapping tests for sluggable extension
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class SluggableMappingTest extends \PHPUnit_Framework_TestCase
  16. {
  17. const TEST_YAML_ENTITY_CLASS = 'Mapping\Fixture\Yaml\Category';
  18. const SLUGGABLE = 'Mapping\Fixture\Sluggable';
  19. private $em;
  20. public function setUp()
  21. {
  22. $config = new \Doctrine\ORM\Configuration();
  23. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  24. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  25. $config->setProxyDir(TESTS_TEMP_DIR);
  26. $config->setProxyNamespace('Gedmo\Mapping\Proxy');
  27. $chainDriverImpl = new DriverChain;
  28. $chainDriverImpl->addDriver(
  29. new YamlDriver(array(__DIR__ . '/Driver/Yaml')),
  30. 'Mapping\Fixture\Yaml'
  31. );
  32. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  33. \Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace(
  34. 'Gedmo\\Mapping\\Annotation',
  35. VENDOR_PATH . '/../lib'
  36. );
  37. $chainDriverImpl->addDriver(
  38. new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader),
  39. 'Mapping\Fixture'
  40. );
  41. $config->setMetadataDriverImpl($chainDriverImpl);
  42. $conn = array(
  43. 'driver' => 'pdo_sqlite',
  44. 'memory' => true,
  45. );
  46. $evm = new \Doctrine\Common\EventManager();
  47. $evm->addEventSubscriber(new SluggableListener());
  48. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  49. }
  50. /**
  51. * @test
  52. */
  53. function shouldBeAbleToMapSluggableUsingYamlDriver()
  54. {
  55. $meta = $this->em->getClassMetadata(self::TEST_YAML_ENTITY_CLASS);
  56. $cacheId = ExtensionMetadataFactory::getCacheId(
  57. self::TEST_YAML_ENTITY_CLASS,
  58. 'Gedmo\Sluggable'
  59. );
  60. $config = $this->em->getMetadataFactory()->getCacheDriver()->fetch($cacheId);
  61. $this->assertArrayHasKey('slugs', $config);
  62. $this->assertArrayHasKey('slug', $config['slugs']);
  63. $this->assertEquals('slug', $config['slugs']['slug']['slug']);
  64. $this->assertArrayHasKey('fields', $config['slugs']['slug']);
  65. $this->assertCount(1, $config['slugs']['slug']['fields']);
  66. $this->assertEquals('title', $config['slugs']['slug']['fields'][0]);
  67. $this->assertArrayHasKey('style', $config['slugs']['slug']);
  68. $this->assertEquals('camel', $config['slugs']['slug']['style']);
  69. $this->assertArrayHasKey('separator', $config['slugs']['slug']);
  70. $this->assertEquals('_', $config['slugs']['slug']['separator']);
  71. $this->assertArrayHasKey('unique', $config['slugs']['slug']);
  72. $this->assertTrue($config['slugs']['slug']['unique']);
  73. $this->assertArrayHasKey('updatable', $config['slugs']['slug']);
  74. $this->assertTrue($config['slugs']['slug']['updatable']);
  75. $this->assertArrayHasKey('handlers', $config['slugs']['slug']);
  76. $handlers = $config['slugs']['slug']['handlers'];
  77. $this->assertEquals(2, count($handlers));
  78. $this->assertArrayHasKey('Gedmo\Sluggable\Handler\TreeSlugHandler', $handlers);
  79. $this->assertArrayHasKey('Gedmo\Sluggable\Handler\RelativeSlugHandler', $handlers);
  80. $first = $handlers['Gedmo\Sluggable\Handler\TreeSlugHandler'];
  81. $this->assertEquals(2, count($first));
  82. $this->assertArrayHasKey('parentRelationField', $first);
  83. $this->assertArrayHasKey('separator', $first);
  84. $this->assertEquals('parent', $first['parentRelationField']);
  85. $this->assertEquals('/', $first['separator']);
  86. $second = $handlers['Gedmo\Sluggable\Handler\RelativeSlugHandler'];
  87. $this->assertEquals(3, count($second));
  88. $this->assertArrayHasKey('relationField', $second);
  89. $this->assertArrayHasKey('relationSlugField', $second);
  90. $this->assertArrayHasKey('separator', $second);
  91. $this->assertEquals('parent', $second['relationField']);
  92. $this->assertEquals('slug', $second['relationSlugField']);
  93. $this->assertEquals('/', $second['separator']);
  94. }
  95. /**
  96. * @test
  97. */
  98. function shouldBeAbleToMapSluggableUsingAnnotationDriver()
  99. {
  100. $meta = $this->em->getClassMetadata(self::SLUGGABLE);
  101. $cacheId = ExtensionMetadataFactory::getCacheId(
  102. self::SLUGGABLE,
  103. 'Gedmo\Sluggable'
  104. );
  105. $config = $this->em->getMetadataFactory()->getCacheDriver()->fetch($cacheId);
  106. $this->assertArrayHasKey('handlers', $config['slugs']['slug']);
  107. $handlers = $config['slugs']['slug']['handlers'];
  108. $this->assertEquals(2, count($handlers));
  109. $this->assertArrayHasKey('Gedmo\Sluggable\Handler\TreeSlugHandler', $handlers);
  110. $this->assertArrayHasKey('Gedmo\Sluggable\Handler\RelativeSlugHandler', $handlers);
  111. $first = $handlers['Gedmo\Sluggable\Handler\TreeSlugHandler'];
  112. $this->assertEquals(2, count($first));
  113. $this->assertArrayHasKey('parentRelationField', $first);
  114. $this->assertArrayHasKey('separator', $first);
  115. $this->assertEquals('parent', $first['parentRelationField']);
  116. $this->assertEquals('/', $first['separator']);
  117. $second = $handlers['Gedmo\Sluggable\Handler\RelativeSlugHandler'];
  118. $this->assertEquals(3, count($second));
  119. $this->assertArrayHasKey('relationField', $second);
  120. $this->assertArrayHasKey('relationSlugField', $second);
  121. $this->assertArrayHasKey('separator', $second);
  122. $this->assertEquals('user', $second['relationField']);
  123. $this->assertEquals('slug', $second['relationSlugField']);
  124. $this->assertEquals('/', $second['separator']);
  125. }
  126. }