MultiInheritanceTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Gedmo\Tree;
  3. use Doctrine\Common\Util\Debug;
  4. /**
  5. * These are tests for Tree behavior
  6. *
  7. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  8. * @package Gedmo.Tree
  9. * @link http://www.gediminasm.org
  10. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  11. */
  12. class MultiInheritanceTest extends \PHPUnit_Framework_TestCase
  13. {
  14. const TEST_ENTITY_CLASS = "Tree\Fixture\Node";
  15. const TEST_BASE_NODE_CLASS = "Tree\Fixture\BaseNode";
  16. const TEST_TREE_NODE_CLASS = "Tree\Fixture\ANode";
  17. const TEST_ENTITY_TRANSLATION = "Gedmo\Translatable\Entity\Translation";
  18. private $em;
  19. public function setUp()
  20. {
  21. $classLoader = new \Doctrine\Common\ClassLoader('Tree\Fixture', __DIR__ . '/../');
  22. $classLoader->register();
  23. $config = new \Doctrine\ORM\Configuration();
  24. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  25. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  26. $config->setProxyDir(__DIR__ . '/Proxy');
  27. $config->setProxyNamespace('Gedmo\Tree\Proxies');
  28. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  29. $conn = array(
  30. 'driver' => 'pdo_sqlite',
  31. 'memory' => true,
  32. );
  33. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  34. $evm = new \Doctrine\Common\EventManager();
  35. $evm->addEventSubscriber(new \Gedmo\Timestampable\TimestampableListener());
  36. $evm->addEventSubscriber(new \Gedmo\Sluggable\SluggableListener());
  37. $evm->addEventSubscriber(new \Gedmo\Tree\TreeListener());
  38. $translationListener = new \Gedmo\Translatable\TranslationListener();
  39. $translationListener->setTranslatableLocale('en_us');
  40. $evm->addEventSubscriber($translationListener);
  41. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  42. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  43. $schemaTool->dropSchema(array());
  44. $schemaTool->createSchema(array(
  45. $this->em->getClassMetadata(self::TEST_TREE_NODE_CLASS),
  46. $this->em->getClassMetadata(self::TEST_BASE_NODE_CLASS),
  47. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS),
  48. $this->em->getClassMetadata(self::TEST_ENTITY_TRANSLATION)
  49. ));
  50. $this->_populate();
  51. }
  52. public function testInheritance()
  53. {
  54. $meta = $this->em->getClassMetadata(self::TEST_ENTITY_CLASS);
  55. $repo = $this->em->getRepository(self::TEST_ENTITY_CLASS);
  56. $food = $repo->findOneByIdentifier('food');
  57. $left = $meta->getReflectionProperty('lft')->getValue($food);
  58. $right = $meta->getReflectionProperty('rgt')->getValue($food);
  59. $this->assertEquals(1, $left);
  60. $this->assertTrue($food->getCreated() !== null);
  61. $this->assertTrue($food->getUpdated() !== null);
  62. $translationRepo = $this->em->getRepository(self::TEST_ENTITY_TRANSLATION);
  63. $translations = $translationRepo->findTranslations($food);
  64. $this->assertEquals(1, count($translations));
  65. $this->assertEquals('food', $food->getSlug());
  66. $this->assertEquals(2, count($translations['en_us']));
  67. }
  68. private function _populate()
  69. {
  70. $root = new \Tree\Fixture\Node();
  71. $root->setTitle("Food");
  72. $root->setIdentifier('food');
  73. $root2 = new \Tree\Fixture\Node();
  74. $root2->setTitle("Sports");
  75. $root2->setIdentifier('sport');
  76. $child = new \Tree\Fixture\Node();
  77. $child->setTitle("Fruits");
  78. $child->setParent($root);
  79. $child->setIdentifier('fruit');
  80. $child2 = new \Tree\Fixture\Node();
  81. $child2->setTitle("Vegitables");
  82. $child2->setParent($root);
  83. $child2->setIdentifier('vegie');
  84. $childsChild = new \Tree\Fixture\Node();
  85. $childsChild->setTitle("Carrots");
  86. $childsChild->setParent($child2);
  87. $childsChild->setIdentifier('carrot');
  88. $potatoes = new \Tree\Fixture\Node();
  89. $potatoes->setTitle("Potatoes");
  90. $potatoes->setParent($child2);
  91. $potatoes->setIdentifier('potatoe');
  92. $cabbages = new \Tree\Fixture\BaseNode();
  93. $cabbages->setIdentifier('cabbage');
  94. $cabbages->setParent($child2);
  95. $this->em->persist($root);
  96. $this->em->persist($root2);
  97. $this->em->persist($child);
  98. $this->em->persist($child2);
  99. $this->em->persist($childsChild);
  100. $this->em->persist($potatoes);
  101. $this->em->persist($cabbages);
  102. $this->em->flush();
  103. $this->em->clear();
  104. }
  105. }