BlameableDocumentTest.php 3.2KB

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