Issue173Test.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Gedmo\Translatable\Query\TreeWalker\TranslationWalker;
  6. use Translatable\Fixture\Issue173\Article;
  7. use Translatable\Fixture\Issue173\Category;
  8. use Translatable\Fixture\Issue173\Product;
  9. /**
  10. * These are tests for translatable behavior
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @contributor Oscar Balladares liebegrube@gmail.com https://github.com/oscarballadares
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class Issue173Test extends BaseTestCaseORM
  18. {
  19. const CATEGORY = 'Translatable\\Fixture\\Issue173\\Category';
  20. const ARTICLE = 'Translatable\\Fixture\\Issue173\\Article';
  21. const PRODUCT = 'Translatable\\Fixture\\Issue173\\Product';
  22. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  23. private $translatableListener;
  24. protected function setUp()
  25. {
  26. parent::setUp();
  27. $evm = new EventManager;
  28. $this->translatableListener = new TranslatableListener();
  29. $this->translatableListener->setTranslatableLocale('en');
  30. $this->translatableListener->setDefaultLocale('en');
  31. $evm->addEventSubscriber($this->translatableListener);
  32. $this->getMockSqliteEntityManager($evm);
  33. $this->populate();
  34. }
  35. public function testIssue173()
  36. {
  37. $this->em
  38. ->getConfiguration()
  39. ->expects($this->any())
  40. ->method('getCustomHydrationMode')
  41. ->with(TranslationWalker::HYDRATE_OBJECT_TRANSLATION)
  42. ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator'))
  43. ;
  44. $categories = $this->getCategoriesThatHasNoAssociations();
  45. $this->assertEquals(count($categories), 1, '$categoriy3 has no associations');
  46. }
  47. public function getCategoriesThatHasNoAssociations()
  48. {
  49. $query = $this->em->createQueryBuilder();
  50. $query2 = $this->em->createQueryBuilder();
  51. $query3 = $this->em->createQueryBuilder();
  52. $dql1 = $query2
  53. ->select('c1')
  54. ->from(self::CATEGORY, 'c1')
  55. ->join('c1.products', 'p')
  56. ->getDql()
  57. ;
  58. $dql2 = $query3
  59. ->select('c2')
  60. ->from(self::CATEGORY, 'c2')
  61. ->join('c2.articles', 'a')
  62. ->getDql()
  63. ;
  64. $query
  65. ->select('c')
  66. ->from(self::CATEGORY, 'c')
  67. ->where($query->expr()->notIn('c.id', $dql1))
  68. ->andWhere($query->expr()->notIn('c.id', $dql2))
  69. ;
  70. return $query->getQuery()->setHint(
  71. \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
  72. 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
  73. )->getResult();
  74. }
  75. private function populate()
  76. {
  77. //Categories
  78. $category1 = new Category;
  79. $category1->setTitle('en category1');
  80. $category2 = new Category;
  81. $category2->setTitle('en category2');
  82. $category3 = new Category;
  83. $category3->setTitle('en category3');
  84. $this->em->persist($category1);
  85. $this->em->persist($category2);
  86. $this->em->persist($category3);
  87. $this->em->flush();
  88. //Articles
  89. $article1 = new Article;
  90. $article1->setTitle('en article1');
  91. $article1->setCategory($category1);
  92. //Products
  93. $product1 = new Product;
  94. $product1->setTitle('en product1');
  95. $product1->setCategory($category2);
  96. $this->em->persist($article1);
  97. $this->em->persist($product1);
  98. $this->em->flush();
  99. $this->translatableListener->setTranslatableLocale('es');
  100. // Categories
  101. $category1->setTitle('es title');
  102. $category2->setTitle('es title');
  103. $category3->setTitle('es title');
  104. $article1->setTitle('es title');
  105. $product1->setTitle('es name');
  106. $this->em->persist($category1);
  107. $this->em->persist($category2);
  108. $this->em->persist($category3);
  109. $this->em->persist($article1);
  110. $this->em->persist($product1);
  111. $this->em->flush();
  112. }
  113. protected function getUsedEntityFixtures()
  114. {
  115. return array(
  116. self::CATEGORY,
  117. self::ARTICLE,
  118. self::PRODUCT,
  119. self::TRANSLATION,
  120. );
  121. }
  122. }