LoggableEntityTest.php 4.9 KB

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