PersonalTranslationDocumentTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Gedmo\Translatable;
  3. use Tool\BaseTestCaseMongoODM;
  4. use Doctrine\Common\EventManager;
  5. use Translatable\Fixture\Document\Personal\Article;
  6. use Translatable\Fixture\Document\Personal\ArticleTranslation;
  7. /**
  8. * These are tests for translatable behavior
  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 PersonalTranslationDocumentTest extends BaseTestCaseMongoODM
  15. {
  16. const ARTICLE = 'Translatable\Fixture\Document\Personal\Article';
  17. const TRANSLATION = 'Translatable\Fixture\Document\Personal\ArticleTranslation';
  18. private $translatableListener;
  19. private $id;
  20. protected function setUp()
  21. {
  22. parent::setUp();
  23. $evm = new EventManager;
  24. $this->translatableListener = new TranslatableListener();
  25. $this->translatableListener->setDefaultLocale('en');
  26. $this->translatableListener->setTranslatableLocale('en');
  27. $evm->addEventSubscriber($this->translatableListener);
  28. $this->getMockDocumentManager($evm);
  29. }
  30. /**
  31. * @test
  32. */
  33. function shouldCreateTranslations()
  34. {
  35. $this->populate();
  36. $article = $this->dm->getRepository(self::ARTICLE)->find($this->id);
  37. $translations = $article->getTranslations();
  38. $this->assertCount(2, $translations);
  39. }
  40. /**
  41. * @test
  42. */
  43. function shouldTranslateTheRecord()
  44. {
  45. $this->populate();
  46. $this->translatableListener->setTranslatableLocale('lt');
  47. $article = $this->dm->getRepository(self::ARTICLE)->find($this->id);
  48. $this->assertEquals('lt', $article->getTitle());
  49. }
  50. private function populate()
  51. {
  52. $article = new Article;
  53. $article->setTitle('en');
  54. $this->dm->persist($article);
  55. $this->dm->flush();
  56. $this->id = $article->getId();
  57. $this->translatableListener->setTranslatableLocale('de');
  58. $article->setTitle('de');
  59. $ltTranslation = new ArticleTranslation;
  60. $ltTranslation
  61. ->setField('title')
  62. ->setContent('lt')
  63. ->setObject($article)
  64. ->setLocale('lt')
  65. ;
  66. $this->dm->persist($ltTranslation);
  67. $this->dm->persist($article);
  68. $this->dm->flush();
  69. $this->dm->clear();
  70. }
  71. }