LoggableDocumentTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Gedmo\Loggable;
  3. use Tool\BaseTestCaseMongoODM;
  4. use Doctrine\Common\EventManager;
  5. use Loggable\Fixture\Document\Article,
  6. Loggable\Fixture\Document\RelatedArticle,
  7. Loggable\Fixture\Document\Comment;
  8. /**
  9. * These are tests for loggable behavior
  10. *
  11. * @author Boussekeyt Jules <jules.boussekeyt@gmail.com>
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. *
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class LoggableDocumentTest extends BaseTestCaseMongoODM
  18. {
  19. const ARTICLE = 'Loggable\\Fixture\\Document\\Article';
  20. const COMMENT = 'Loggable\\Fixture\\Document\\Comment';
  21. const RELATED_ARTICLE = 'Loggable\\Fixture\\Document\\RelatedArticle';
  22. const COMMENT_LOG = 'Loggable\\Fixture\\Document\\Log\\Comment';
  23. protected function setUp()
  24. {
  25. parent::setUp();
  26. $evm = new EventManager();
  27. $loggableListener = new LoggableListener();
  28. $loggableListener->setUsername('jules');
  29. $evm->addEventSubscriber($loggableListener);
  30. $this->getMockDocumentManager($evm);
  31. }
  32. public function testLogGeneration()
  33. {
  34. $logRepo = $this->dm->getRepository('Gedmo\\Loggable\\Document\\LogEntry');
  35. $articleRepo = $this->dm->getRepository(self::ARTICLE);
  36. $this->assertCount(0, $logRepo->findAll());
  37. $art0 = new Article();
  38. $art0->setTitle('Title');
  39. $this->dm->persist($art0);
  40. $this->dm->flush();
  41. $log = $logRepo->findOneByObjectId($art0->getId());
  42. $this->assertNotNull($log);
  43. $this->assertEquals('create', $log->getAction());
  44. $this->assertEquals(get_class($art0), $log->getObjectClass());
  45. $this->assertEquals('jules', $log->getUsername());
  46. $this->assertEquals(1, $log->getVersion());
  47. $data = $log->getData();
  48. $this->assertCount(1, $data);
  49. $this->assertArrayHasKey('title', $data);
  50. $this->assertEquals($data['title'], 'Title');
  51. // test update
  52. $article = $articleRepo->findOneByTitle('Title');
  53. $article->setTitle('New');
  54. $this->dm->persist($article);
  55. $this->dm->flush();
  56. $this->dm->clear();
  57. $log = $logRepo->findOneBy(array('version' => 2, 'objectId' => $article->getId()));
  58. $this->assertEquals('update', $log->getAction());
  59. // test delete
  60. $article = $articleRepo->findOneByTitle('New');
  61. $this->dm->remove($article);
  62. $this->dm->flush();
  63. $this->dm->clear();
  64. $log = $logRepo->findOneBy(array('version' => 3, 'objectId' => $article->getId()));
  65. $this->assertEquals('remove', $log->getAction());
  66. $this->assertNull($log->getData());
  67. }
  68. public function testVersionControl()
  69. {
  70. $this->populate();
  71. $commentLogRepo = $this->dm->getRepository(self::COMMENT_LOG);
  72. $commentRepo = $this->dm->getRepository(self::COMMENT);
  73. $comment = $commentRepo->findOneByMessage('m-v5');
  74. $commentId = $comment->getId();
  75. $this->assertEquals('m-v5', $comment->getMessage());
  76. $this->assertEquals('s-v3', $comment->getSubject());
  77. $this->assertEquals('a2-t-v1', $comment->getArticle()->getTitle());
  78. // test revert
  79. $commentLogRepo->revert($comment, 3);
  80. $this->assertEquals('s-v3', $comment->getSubject());
  81. $this->assertEquals('m-v2', $comment->getMessage());
  82. $this->assertEquals('a1-t-v1', $comment->getArticle()->getTitle());
  83. $this->dm->persist($comment);
  84. $this->dm->flush();
  85. // test get log entries
  86. $logEntries = $commentLogRepo->getLogEntries($comment);
  87. $this->assertCount(6, $logEntries);
  88. $latest = array_shift($logEntries);
  89. $this->assertEquals('update', $latest->getAction());
  90. }
  91. private function populate()
  92. {
  93. $article = new RelatedArticle;
  94. $article->setTitle('a1-t-v1');
  95. $article->setContent('a1-c-v1');
  96. $comment = new Comment;
  97. $comment->setArticle($article);
  98. $comment->setMessage('m-v1');
  99. $comment->setSubject('s-v1');
  100. $this->dm->persist($article);
  101. $this->dm->persist($comment);
  102. $this->dm->flush();
  103. $comment->setMessage('m-v2');
  104. $this->dm->persist($comment);
  105. $this->dm->flush();
  106. $comment->setSubject('s-v3');
  107. $this->dm->persist($comment);
  108. $this->dm->flush();
  109. $article2 = new RelatedArticle;
  110. $article2->setTitle('a2-t-v1');
  111. $article2->setContent('a2-c-v1');
  112. $comment->setArticle($article2);
  113. $this->dm->persist($article2);
  114. $this->dm->persist($comment);
  115. $this->dm->flush();
  116. $comment->setMessage('m-v5');
  117. $this->dm->persist($comment);
  118. $this->dm->flush();
  119. $this->dm->clear();
  120. }
  121. }