BlameableDocumentTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Gedmo\Blameable;
  3. use Tool\BaseTestCaseMongoODM;
  4. use Doctrine\Common\EventManager;
  5. use Blameable\Fixture\Document\Article,
  6. Blameable\Fixture\Document\Type,
  7. Blameable\Fixture\Document\User;
  8. /**
  9. * These are tests for Blameable behavior ODM implementation
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class BlameableDocumentTest extends BaseTestCaseMongoODM
  16. {
  17. const TEST_USERNAME = 'testuser';
  18. const TYPE = 'Blameable\Fixture\Document\Type';
  19. const USER = 'Blameable\Fixture\Document\User';
  20. const ARTICLE = 'Blameable\Fixture\Document\Article';
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $user = new User();
  25. $user->setUsername(self::TEST_USERNAME);
  26. $listener = new BlameableListener();
  27. $listener->setUserValue($user);
  28. $evm = new EventManager();
  29. $evm->addEventSubscriber($listener);
  30. $manager = $this->getMockDocumentManager($evm);
  31. $manager->persist($user);
  32. $manager->flush();
  33. $this->populate();
  34. }
  35. public function testBlameable()
  36. {
  37. $repo = $this->dm->getRepository(self::ARTICLE);
  38. $article = $repo->findOneByTitle('Blameable Article');
  39. $this->assertEquals(self::TEST_USERNAME, $article->getCreated());
  40. $this->assertEquals(self::TEST_USERNAME, $article->getUpdated());
  41. $published = new Type;
  42. $published->setIdentifier('published');
  43. $published->setTitle('Published');
  44. $article->setType($published);
  45. $this->dm->persist($article);
  46. $this->dm->persist($published);
  47. $this->dm->flush();
  48. $this->dm->clear();
  49. $article = $repo->findOneByTitle('Blameable Article');
  50. $this->assertEquals(self::TEST_USERNAME, $article->getPublished());
  51. $this->assertEquals(self::TEST_USERNAME, $article->getCreator()->getUsername());
  52. }
  53. public function testForcedValues()
  54. {
  55. $sport = new Article();
  56. $sport->setTitle('sport forced');
  57. $sport->setCreated(self::TEST_USERNAME);
  58. $sport->setUpdated(self::TEST_USERNAME);
  59. $this->dm->persist($sport);
  60. $this->dm->flush();
  61. $this->dm->clear();
  62. $repo = $this->dm->getRepository(self::ARTICLE);
  63. $sport = $repo->findOneByTitle('sport forced');
  64. $this->assertEquals(self::TEST_USERNAME, $sport->getCreated());
  65. $this->assertEquals(self::TEST_USERNAME, $sport->getUpdated());
  66. $published = new Type;
  67. $published->setIdentifier('published');
  68. $published->setTitle('Published');
  69. $sport->setType($published);
  70. $sport->setPublished(self::TEST_USERNAME);
  71. $this->dm->persist($sport);
  72. $this->dm->persist($published);
  73. $this->dm->flush();
  74. $this->dm->clear();
  75. $sport = $repo->findOneByTitle('sport forced');
  76. $this->assertEquals(self::TEST_USERNAME, $sport->getPublished());
  77. }
  78. private function populate()
  79. {
  80. $art0 = new Article();
  81. $art0->setTitle('Blameable Article');
  82. $this->dm->persist($art0);
  83. $this->dm->flush();
  84. $this->dm->clear();
  85. }
  86. }