TreeMappingTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Gedmo\Tree;
  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. Gedmo\Tree\TreeListener;
  9. /**
  10. * These are mapping tests for tree extension
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @link http://www.gediminasm.org
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class TreeMappingTest extends \PHPUnit_Framework_TestCase
  17. {
  18. const TEST_YAML_ENTITY_CLASS = 'Mapping\Fixture\Yaml\Category';
  19. const YAML_CLOSURE_CATEGORY = 'Mapping\Fixture\Yaml\ClosureCategory';
  20. const YAML_MATERIALIZED_PATH_CATEGORY = 'Mapping\Fixture\Yaml\MaterializedPathCategory';
  21. private $em;
  22. private $listener;
  23. public function setUp()
  24. {
  25. $config = new \Doctrine\ORM\Configuration();
  26. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ApcCache);
  27. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  28. $config->setProxyDir(TESTS_TEMP_DIR);
  29. $config->setProxyNamespace('Gedmo\Mapping\Proxy');
  30. $chainDriverImpl = new DriverChain;
  31. $chainDriverImpl->addDriver(
  32. new YamlDriver(array(__DIR__ . '/Driver/Yaml')),
  33. 'Mapping\Fixture\Yaml'
  34. );
  35. $chainDriverImpl->addDriver(
  36. $config->newDefaultAnnotationDriver(),
  37. 'Tree\Fixture'
  38. );
  39. $chainDriverImpl->addDriver(
  40. $config->newDefaultAnnotationDriver(),
  41. 'Gedmo\Tree'
  42. );
  43. $config->setMetadataDriverImpl($chainDriverImpl);
  44. $conn = array(
  45. 'driver' => 'pdo_sqlite',
  46. 'memory' => true,
  47. );
  48. $this->listener = new TreeListener;
  49. $evm = new \Doctrine\Common\EventManager();
  50. $evm->addEventSubscriber(new TreeListener());
  51. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  52. }
  53. public function testApcCached()
  54. {
  55. if (!extension_loaded('apc') || !ini_get('apc.enable_cli')) {
  56. $this->markTestSkipped('APC extension is not loaded.');
  57. }
  58. $meta = $this->em->getClassMetadata(self::YAML_CLOSURE_CATEGORY);
  59. $this->em->getClassMetadata('Tree\Fixture\Closure\CategoryClosure');
  60. $meta = $this->em->getMetadataFactory()->getCacheDriver()->fetch(
  61. "Tree\\Fixture\\Closure\\CategoryClosure\$CLASSMETADATA"
  62. );
  63. $this->assertTrue($meta->hasAssociation('ancestor'));
  64. $this->assertTrue($meta->hasAssociation('descendant'));
  65. }
  66. public function testYamlNestedMapping()
  67. {
  68. if (!extension_loaded('apc') || !ini_get('apc.enable_cli')) {
  69. $this->markTestSkipped('APC extension is not loaded.');
  70. }
  71. $meta = $this->em->getClassMetadata(self::TEST_YAML_ENTITY_CLASS);
  72. $cacheId = ExtensionMetadataFactory::getCacheId(
  73. self::TEST_YAML_ENTITY_CLASS,
  74. 'Gedmo\Tree'
  75. );
  76. $config = $this->em->getMetadataFactory()->getCacheDriver()->fetch($cacheId);
  77. $this->assertArrayHasKey('left', $config);
  78. $this->assertEquals('lft', $config['left']);
  79. $this->assertArrayHasKey('right', $config);
  80. $this->assertEquals('rgt', $config['right']);
  81. $this->assertArrayHasKey('parent', $config);
  82. $this->assertEquals('parent', $config['parent']);
  83. $this->assertArrayHasKey('level', $config);
  84. $this->assertEquals('lvl', $config['level']);
  85. $this->assertArrayHasKey('root', $config);
  86. $this->assertEquals('root', $config['root']);
  87. $this->assertArrayHasKey('strategy', $config);
  88. $this->assertEquals('nested', $config['strategy']);
  89. }
  90. public function testYamlClosureMapping()
  91. {
  92. if (!extension_loaded('apc') || !ini_get('apc.enable_cli')) {
  93. $this->markTestSkipped('APC extension is not loaded.');
  94. }
  95. $meta = $this->em->getClassMetadata(self::YAML_CLOSURE_CATEGORY);
  96. $cacheId = ExtensionMetadataFactory::getCacheId(self::YAML_CLOSURE_CATEGORY, 'Gedmo\Tree');
  97. $config = $this->em->getMetadataFactory()->getCacheDriver()->fetch($cacheId);
  98. $this->assertArrayHasKey('parent', $config);
  99. $this->assertEquals('parent', $config['parent']);
  100. $this->assertArrayHasKey('strategy', $config);
  101. $this->assertEquals('closure', $config['strategy']);
  102. $this->assertArrayHasKey('closure', $config);
  103. $this->assertEquals('Tree\\Fixture\\Closure\\CategoryClosure', $config['closure']);
  104. }
  105. public function testYamlMaterializedPathMapping()
  106. {
  107. if (!extension_loaded('apc') || !ini_get('apc.enable_cli')) {
  108. $this->markTestSkipped('APC extension is not loaded.');
  109. }
  110. $meta = $this->em->getClassMetadata(self::YAML_MATERIALIZED_PATH_CATEGORY);
  111. $config = $this->listener->getConfiguration($this->em, $meta->name);
  112. $this->assertArrayHasKey('strategy', $config);
  113. $this->assertEquals('materializedPath', $config['strategy']);
  114. $this->assertArrayHasKey('parent', $config);
  115. $this->assertEquals('parent', $config['parent']);
  116. $this->assertArrayHasKey('activate_locking', $config);
  117. $this->assertTrue($config['activate_locking']);
  118. $this->assertArrayHasKey('locking_timeout', $config);
  119. $this->assertEquals(3, $config['locking_timeout']);
  120. $this->assertArrayHasKey('level', $config);
  121. $this->assertEquals('level', $config['level']);
  122. $this->assertArrayHasKey('path', $config);
  123. $this->assertEquals('path', $config['path']);
  124. $this->assertArrayHasKey('path_separator', $config);
  125. $this->assertEquals(',', $config['path_separator']);
  126. }
  127. }