BlameableTest.php 3.6KB

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