TimestampableTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace Gedmo\Timestampable;
  3. use Doctrine\Common\EventManager;
  4. use Tool\BaseTestCaseORM;
  5. use Doctrine\Common\Util\Debug,
  6. Timestampable\Fixture\Article,
  7. Timestampable\Fixture\Comment,
  8. Timestampable\Fixture\Type;
  9. /**
  10. * These are tests for Timestampable 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 TimestampableTest extends BaseTestCaseORM
  17. {
  18. const ARTICLE = "Timestampable\\Fixture\\Article";
  19. const COMMENT = "Timestampable\\Fixture\\Comment";
  20. const TYPE = "Timestampable\\Fixture\\Type";
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $evm = new EventManager;
  25. $evm->addEventSubscriber(new TimestampableListener);
  26. $this->getMockSqliteEntityManager($evm);
  27. }
  28. public function testTimestampable()
  29. {
  30. $sport = new Article();
  31. $sport->setTitle('Sport');
  32. $sport->setBody('Sport article body.');
  33. $this->assertTrue($sport instanceof Timestampable);
  34. $sportComment = new Comment();
  35. $sportComment->setMessage('hello');
  36. $sportComment->setArticle($sport);
  37. $sportComment->setStatus(0);
  38. $this->assertTrue($sportComment instanceof Timestampable);
  39. $dateCreated = new \DateTime('now');
  40. $this->em->persist($sport);
  41. $this->em->persist($sportComment);
  42. $this->em->flush();
  43. $sport = $this->em->getRepository(self::ARTICLE)->findOneByTitle('Sport');
  44. $this->assertEquals(
  45. $dateCreated->format('Y-m-d'),
  46. $sport->getCreated()->format('Y-m-d')
  47. );
  48. $this->assertEquals(
  49. $dateCreated->format('Y-m-d H:i'),
  50. $sport->getUpdated()->format('Y-m-d H:i')
  51. );
  52. $this->assertEquals(
  53. null,
  54. $sport->getContentChanged()
  55. );
  56. $this->assertNull($sport->getPublished());
  57. $sportComment = $this->em->getRepository(self::COMMENT)->findOneByMessage('hello');
  58. $this->assertEquals(
  59. $dateCreated->format('H:i'),
  60. $sportComment->getModified()->format('H:i')
  61. );
  62. $this->assertNull($sportComment->getClosed());
  63. $sportComment->setStatus(1);
  64. $published = new Type();
  65. $published->setTitle('Published');
  66. $sport->setType($published);
  67. $datePublished = new \DateTime('now');
  68. $this->em->persist($sport);
  69. $this->em->persist($published);
  70. $this->em->persist($sportComment);
  71. $this->em->flush();
  72. $sportComment = $this->em->getRepository(self::COMMENT)->findOneByMessage('hello');
  73. $this->assertEquals(
  74. $datePublished->format('Y-m-d H:i'),
  75. $sportComment->getClosed()->format('Y-m-d H:i')
  76. );
  77. $this->assertEquals(
  78. $datePublished->format('Y-m-d H:i'),
  79. $sport->getPublished()->format('Y-m-d H:i')
  80. );
  81. sleep(1);
  82. $dateUpdated1 = new \DateTime('now');
  83. $sport->setTitle('Updated');
  84. $this->em->persist($sport);
  85. $this->em->persist($published);
  86. $this->em->persist($sportComment);
  87. $this->em->flush();
  88. $this->assertEquals(
  89. $dateCreated->format('Y-m-d'),
  90. $sport->getCreated()->format('Y-m-d')
  91. );
  92. $this->assertEquals(
  93. $dateUpdated1->format('Y-m-d H:i:s'),
  94. $sport->getUpdated()->format('Y-m-d H:i:s')
  95. );
  96. $this->assertEquals(
  97. $datePublished->format('Y-m-d H:i:s'),
  98. $sport->getPublished()->format('Y-m-d H:i:s')
  99. );
  100. $this->assertEquals(
  101. $dateUpdated1->format('Y-m-d H:i:s'),
  102. $sport->getContentChanged()->format('Y-m-d H:i:s')
  103. );
  104. sleep(1);
  105. $dateUpdated2 = new \DateTime('now');
  106. $sport->setBody('Body updated');
  107. $this->em->persist($sport);
  108. $this->em->persist($published);
  109. $this->em->persist($sportComment);
  110. $this->em->flush();
  111. $this->assertEquals(
  112. $dateCreated->format('Y-m-d'),
  113. $sport->getCreated()->format('Y-m-d')
  114. );
  115. $this->assertEquals(
  116. $dateUpdated2->format('Y-m-d H:i:s'),
  117. $sport->getUpdated()->format('Y-m-d H:i:s')
  118. );
  119. $this->assertEquals(
  120. $datePublished->format('Y-m-d H:i:s'),
  121. $sport->getPublished()->format('Y-m-d H:i:s')
  122. );
  123. $this->assertEquals(
  124. $dateUpdated2->format('Y-m-d H:i:s'),
  125. $sport->getContentChanged()->format('Y-m-d H:i:s')
  126. );
  127. $this->em->clear();
  128. }
  129. public function testForcedValues()
  130. {
  131. $sport = new Article();
  132. $sport->setTitle('sport forced');
  133. $sport->setBody('Sport article body.');
  134. $sport->setCreated(new \DateTime('2000-01-01'));
  135. $sport->setUpdated(new \DateTime('2000-01-01 12:00:00'));
  136. $sport->setContentChanged(new \DateTime('2000-01-01 12:00:00'));
  137. $this->em->persist($sport);
  138. $this->em->flush();
  139. $repo = $this->em->getRepository(self::ARTICLE);
  140. $sport = $repo->findOneByTitle('sport forced');
  141. $this->assertEquals(
  142. '2000-01-01',
  143. $sport->getCreated()->format('Y-m-d')
  144. );
  145. $this->assertEquals(
  146. '2000-01-01 12:00:00',
  147. $sport->getUpdated()->format('Y-m-d H:i:s')
  148. );
  149. $this->assertEquals(
  150. '2000-01-01 12:00:00',
  151. $sport->getContentChanged()->format('Y-m-d H:i:s')
  152. );
  153. $published = new Type();
  154. $published->setTitle('Published');
  155. $sport->setType($published);
  156. $sport->setPublished(new \DateTime('2000-01-01 12:00:00'));
  157. $this->em->persist($sport);
  158. $this->em->persist($published);
  159. $this->em->flush();
  160. $sport = $repo->findOneByTitle('sport forced');
  161. $this->assertEquals(
  162. '2000-01-01 12:00:00',
  163. $sport->getPublished()->format('Y-m-d H:i:s')
  164. );
  165. $this->em->clear();
  166. }
  167. /**
  168. * @test
  169. */
  170. function shouldSolveIssue767()
  171. {
  172. $type = new Type;
  173. $type->setTitle('Published');
  174. $this->em->persist($type);
  175. $this->em->flush();
  176. $this->em->clear();
  177. $type = $this->em->getReference(self::TYPE, $type->getId());
  178. $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $type);
  179. $art = new Article;
  180. $art->setTitle('Art');
  181. $art->setBody('body');
  182. $this->em->persist($art);
  183. $this->em->flush();
  184. $art->setType($type);
  185. $this->em->flush(); // in v2.4.x will work on insert too
  186. $this->assertNotNull($art->getPublished());
  187. }
  188. protected function getUsedEntityFixtures()
  189. {
  190. return array(
  191. self::ARTICLE,
  192. self::COMMENT,
  193. self::TYPE
  194. );
  195. }
  196. }