ChangeTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Gedmo\Timestampable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug,
  6. Timestampable\Fixture\TitledArticle,
  7. Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM,
  8. Gedmo\Timestampable\Mapping\Event\TimestampableAdapter,
  9. Doctrine\Common\EventArgs;
  10. /**
  11. * These are tests for Timestampable behavior
  12. *
  13. * @author Ivan Borzenkov <ivan.borzenkov@gmail.com>
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class ChangeTest extends BaseTestCaseORM
  18. {
  19. const FIXTURE = "Timestampable\\Fixture\\TitledArticle";
  20. protected $listener;
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $this->listener = new TimestampableListenerStub();
  25. $this->listener->eventAdapter = new EventAdapterORMStub();
  26. $evm = new EventManager;
  27. $evm->addEventSubscriber($this->listener);
  28. $this->getMockSqliteEntityManager($evm);
  29. }
  30. public function testChange()
  31. {
  32. $test = new TitledArticle();
  33. $test->setTitle('Test');
  34. $test->setText('Test');
  35. $test->setState('Open');
  36. $currentDate = new \DateTime('now');
  37. $this->listener->eventAdapter->setDateValue($currentDate);
  38. $this->em->persist($test);
  39. $this->em->flush();
  40. $this->em->clear();
  41. $test = $this->em->getRepository(self::FIXTURE)->findOneByTitle('Test');
  42. $test->setTitle('New Title');
  43. $test->setState('Closed');
  44. $this->em->persist($test);
  45. $this->em->flush();
  46. $this->em->clear();
  47. //Changed
  48. $this->assertEquals(
  49. $currentDate->format('Y-m-d H:i:s'),
  50. $test->getChtitle()->format('Y-m-d H:i:s')
  51. );
  52. $this->assertEquals(
  53. $currentDate->format('Y-m-d H:i:s'),
  54. $test->getClosed()->format('Y-m-d H:i:s')
  55. );
  56. $anotherDate = \DateTime::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00');
  57. $this->listener->eventAdapter->setDateValue($anotherDate);
  58. $test = $this->em->getRepository(self::FIXTURE)->findOneByTitle('New Title');
  59. $test->setText('New Text');
  60. $test->setState('Open');
  61. $this->em->persist($test);
  62. $this->em->flush();
  63. $this->em->clear();
  64. //Not Changed
  65. $this->assertEquals(
  66. $currentDate->format('Y-m-d H:i:s'),
  67. $test->getChtitle()->format('Y-m-d H:i:s')
  68. );
  69. $this->assertEquals(
  70. $currentDate->format('Y-m-d H:i:s'),
  71. $test->getClosed()->format('Y-m-d H:i:s')
  72. );
  73. $test = $this->em->getRepository(self::FIXTURE)->findOneByTitle('New Title');
  74. $test->setState('Published');
  75. $this->em->persist($test);
  76. $this->em->flush();
  77. $this->em->clear();
  78. //Changed
  79. $this->assertEquals(
  80. $anotherDate->format('Y-m-d H:i:s'),
  81. $test->getClosed()->format('Y-m-d H:i:s')
  82. );
  83. }
  84. protected function getUsedEntityFixtures()
  85. {
  86. return array(
  87. self::FIXTURE,
  88. );
  89. }
  90. }
  91. class EventAdapterORMStub extends BaseAdapterORM implements TimestampableAdapter
  92. {
  93. protected $dateTime;
  94. public function setDateValue(\DateTime $dateTime)
  95. {
  96. $this->dateTime = $dateTime;
  97. }
  98. public function getDateValue($meta, $field)
  99. {
  100. return $this->dateTime;
  101. }
  102. }
  103. class TimestampableListenerStub extends TimestampableListener
  104. {
  105. public $eventAdapter;
  106. protected function getEventAdapter(EventArgs $args)
  107. {
  108. $this->eventAdapter->setEventArgs($args);
  109. return $this->eventAdapter;
  110. }
  111. }