BlameableTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Gedmo\Blameable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug,
  6. Blameable\Fixture\Entity\Article,
  7. Blameable\Fixture\Entity\Comment,
  8. Blameable\Fixture\Entity\Type;
  9. /**
  10. * These are tests for Blameable 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 BlameableTest extends BaseTestCaseORM
  17. {
  18. const ARTICLE = "Blameable\\Fixture\\Entity\\Article";
  19. const COMMENT = "Blameable\\Fixture\\Entity\\Comment";
  20. const TYPE = "Blameable\\Fixture\\Entity\\Type";
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $listener = new BlameableListener;
  25. $listener->setUserValue('testuser');
  26. $evm = new EventManager;
  27. $evm->addEventSubscriber($listener);
  28. $this->getMockSqliteEntityManager($evm);
  29. }
  30. public function testBlameable()
  31. {
  32. $sport = new Article();
  33. $sport->setTitle('Sport');
  34. $this->assertTrue($sport instanceof Blameable);
  35. $sportComment = new Comment();
  36. $sportComment->setMessage('hello');
  37. $sportComment->setArticle($sport);
  38. $sportComment->setStatus(0);
  39. $this->assertTrue($sportComment instanceof Blameable);
  40. $this->em->persist($sport);
  41. $this->em->persist($sportComment);
  42. $this->em->flush();
  43. $this->em->clear();
  44. $sport = $this->em->getRepository(self::ARTICLE)->findOneByTitle('Sport');
  45. $this->assertEquals('testuser', $sport->getCreated());
  46. $this->assertEquals('testuser', $sport->getUpdated());
  47. $this->assertNull($sport->getPublished());
  48. $sportComment = $this->em->getRepository(self::COMMENT)->findOneByMessage('hello');
  49. $this->assertEquals('testuser', $sportComment->getModified());
  50. $this->assertNull($sportComment->getClosed());
  51. $sportComment->setStatus(1);
  52. $published = new Type();
  53. $published->setTitle('Published');
  54. $sport->setTitle('Updated');
  55. $sport->setType($published);
  56. $this->em->persist($sport);
  57. $this->em->persist($published);
  58. $this->em->persist($sportComment);
  59. $this->em->flush();
  60. $this->em->clear();
  61. $sportComment = $this->em->getRepository(self::COMMENT)->findOneByMessage('hello');
  62. $this->assertEquals('testuser', $sportComment->getClosed());
  63. $this->assertEquals('testuser', $sport->getPublished());
  64. }
  65. public function testForcedValues()
  66. {
  67. $sport = new Article();
  68. $sport->setTitle('sport forced');
  69. $sport->setCreated('myuser');
  70. $sport->setUpdated('myuser');
  71. $this->em->persist($sport);
  72. $this->em->flush();
  73. $this->em->clear();
  74. $repo = $this->em->getRepository(self::ARTICLE);
  75. $sport = $repo->findOneByTitle('sport forced');
  76. $this->assertEquals('myuser', $sport->getCreated());
  77. $this->assertEquals('myuser', $sport->getUpdated());
  78. $published = new Type();
  79. $published->setTitle('Published');
  80. $sport->setType($published);
  81. $sport->setPublished('myuser');
  82. $this->em->persist($sport);
  83. $this->em->persist($published);
  84. $this->em->flush();
  85. $this->em->clear();
  86. $sport = $repo->findOneByTitle('sport forced');
  87. $this->assertEquals('myuser', $sport->getPublished());
  88. }
  89. protected function getUsedEntityFixtures()
  90. {
  91. return array(
  92. self::ARTICLE,
  93. self::COMMENT,
  94. self::TYPE
  95. );
  96. }
  97. }