LoggableEntityTest.php 5.0KB

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