TranslatableManySlugTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Gedmo\Sluggable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug,
  6. Gedmo\Translatable\Translatable,
  7. Gedmo\Translatable\Entity\Translation,
  8. Gedmo\Translatable\TranslationListener,
  9. Sluggable\Fixture\TransArticleManySlug;
  10. /**
  11. * These are tests for Sluggable behavior
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Sluggable
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class TranslatableManySlugTest extends BaseTestCaseORM
  19. {
  20. private $articleId;
  21. private $translationListener;
  22. const ARTICLE = 'Sluggable\\Fixture\\TransArticleManySlug';
  23. const TRANSLATION = 'Gedmo\\Translatable\\Entity\\Translation';
  24. protected function setUp()
  25. {
  26. parent::setUp();
  27. $evm = new EventManager;
  28. $this->translationListener = new TranslationListener();
  29. $this->translationListener->setTranslatableLocale('en_us');
  30. $evm->addEventSubscriber(new SluggableListener);
  31. $evm->addEventSubscriber($this->translationListener);
  32. $this->getMockSqliteEntityManager($evm);
  33. $this->populate();
  34. }
  35. public function testSlugAndTranslation()
  36. {
  37. $article = $this->em->find(self::ARTICLE, $this->articleId);
  38. $this->assertTrue($article instanceof Translatable && $article instanceof Sluggable);
  39. $this->assertEquals($article->getSlug(), 'the-title-my-code');
  40. $this->assertEquals($article->getUniqueSlug(), 'the-unique-title');
  41. $repo = $this->em->getRepository(self::TRANSLATION);
  42. $translations = $repo->findTranslations($article);
  43. $this->assertEquals(count($translations), 1);
  44. $this->assertArrayHasKey('en_us', $translations);
  45. $this->assertEquals(3, count($translations['en_us']));
  46. $this->assertArrayHasKey('slug', $translations['en_us']);
  47. $this->assertEquals('the-title-my-code', $translations['en_us']['slug']);
  48. $article = $this->em->find(self::ARTICLE, $this->articleId);
  49. $article->setTranslatableLocale('de_de');
  50. $article->setCode('code in de');
  51. $article->setTitle('title in de');
  52. $this->em->persist($article);
  53. $this->em->flush();
  54. $this->em->clear();
  55. $repo = $this->em->getRepository(self::TRANSLATION);
  56. $translations = $repo->findTranslations($article);
  57. $this->assertEquals(count($translations), 2);
  58. $this->assertArrayHasKey('de_de', $translations);
  59. $this->assertEquals(3, count($translations['de_de']));
  60. $this->assertEquals('title in de', $translations['de_de']['title']);
  61. $this->assertArrayHasKey('slug', $translations['de_de']);
  62. $this->assertEquals('title-in-de-code-in-de', $translations['de_de']['slug']);
  63. }
  64. protected function getUsedEntityFixtures()
  65. {
  66. return array(
  67. self::ARTICLE,
  68. self::TRANSLATION
  69. );
  70. }
  71. private function populate()
  72. {
  73. $article = new TransArticleManySlug();
  74. $article->setTitle('the title');
  75. $article->setCode('my code');
  76. $article->setUniqueTitle('the unique title');
  77. $this->em->persist($article);
  78. $this->em->flush();
  79. $this->em->clear();
  80. $this->articleId = $article->getId();
  81. }
  82. }