TranslatableDocumentTest.php 4.0KB

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