TranslatableDocumentTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Tool\BaseTestCaseMongoODM;
  4. use Gedmo\Sluggable\SluggableListener;
  5. use Doctrine\Common\EventManager;
  6. use Translatable\Fixture\Document\Article;
  7. /**
  8. * These are tests for Translatable behavior ODM implementation
  9. *
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class TranslatableDocumentTest extends BaseTestCaseMongoODM
  15. {
  16. const ARTICLE = 'Translatable\\Fixture\\Document\\Article';
  17. const TRANSLATION = 'Gedmo\\Translatable\\Document\\Translation';
  18. private $translatableListener;
  19. private $articleId;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $evm = new EventManager();
  24. $this->translatableListener = new TranslatableListener;
  25. $this->translatableListener->setDefaultLocale('en_us');
  26. $this->translatableListener->setTranslatableLocale('en_us');
  27. $evm->addEventSubscriber(new SluggableListener);
  28. $evm->addEventSubscriber($this->translatableListener);
  29. $this->getMockDocumentManager($evm);
  30. $this->populate();
  31. }
  32. public function testTranslation()
  33. {
  34. // test inserted translations
  35. $repo = $this->dm->getRepository(self::ARTICLE);
  36. /*$article = $repo->findOneByTitle('Title EN');
  37. $transRepo = $this->dm->getRepository(self::TRANSLATION);
  38. $this->assertTrue($transRepo instanceof Document\Repository\TranslationRepository);
  39. $translations = $transRepo->findTranslations($article);
  40. $this->assertCount(0, $translations);
  41. // test second translations
  42. $this->translatableListener->setTranslatableLocale('de_de');
  43. $article->setTitle('Title DE');
  44. $article->setCode('Code DE');
  45. $this->dm->persist($article);
  46. $this->dm->flush();
  47. $this->dm->clear();
  48. $article = $repo->find($this->articleId);
  49. $translations = $transRepo->findTranslations($article);
  50. $this->assertCount(1, $translations);
  51. $this->assertArrayHasKey('de_de', $translations);
  52. $this->assertArrayHasKey('title', $translations['de_de']);
  53. $this->assertEquals('Title DE', $translations['de_de']['title']);
  54. $this->assertArrayHasKey('code', $translations['de_de']);
  55. $this->assertEquals('Code DE', $translations['de_de']['code']);
  56. $this->assertArrayHasKey('slug', $translations['de_de']);
  57. $this->assertEquals('title-de-code-de', $translations['de_de']['slug']);
  58. // test value update
  59. $this->dm->clear();*/
  60. $this->translatableListener->setTranslatableLocale('en_us');
  61. $article = $repo->find($this->articleId);
  62. $this->assertEquals('Title EN', $article->getTitle());
  63. $this->assertEquals('Code EN', $article->getCode());
  64. $this->assertEquals('title-en-code-en', $article->getSlug());
  65. // test translation update
  66. /*$article->setTitle('Title EN Updated');
  67. $article->setCode('Code EN Updated');
  68. $this->dm->persist($article);
  69. $this->dm->flush();
  70. $this->dm->clear();
  71. $article = $repo->find($this->articleId);
  72. $this->assertEquals('Title EN Updated', $article->getTitle());
  73. $this->assertEquals('Code EN Updated', $article->getCode());
  74. // test removal of translations
  75. $this->dm->remove($article);
  76. $this->dm->flush();
  77. $this->dm->clear();
  78. $article = $repo->find($this->articleId);
  79. $this->assertNull($article);
  80. $translations = $transRepo->findTranslationsByObjectId($this->articleId);
  81. $this->assertCount(0, $translations);*/
  82. }
  83. private function populate()
  84. {
  85. $art0 = new Article();
  86. $art0->setTitle('Title EN');
  87. $art0->setCode('Code EN');
  88. $this->dm->persist($art0);
  89. $this->dm->flush();
  90. $this->articleId = $art0->getId();
  91. $this->dm->clear();
  92. }
  93. }