TimestampableTest.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * @package Gedmo.Timestampable
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class TimestampableTest extends BaseTestCaseORM
  18. {
  19. const ARTICLE = "Timestampable\\Fixture\\Article";
  20. const COMMENT = "Timestampable\\Fixture\\Comment";
  21. const TYPE = "Timestampable\\Fixture\\Type";
  22. protected function setUp()
  23. {
  24. parent::setUp();
  25. $evm = new EventManager;
  26. $evm->addEventSubscriber(new TimestampableListener);
  27. $this->getMockSqliteEntityManager($evm);
  28. }
  29. public function testTimestampable()
  30. {
  31. $sport = new Article();
  32. $sport->setTitle('Sport');
  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. $date = new \DateTime('now');
  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(
  46. $date->format('Y-m-d 00:00:00'),
  47. $sport->getCreated()->format('Y-m-d H:i:s')
  48. );
  49. $this->assertEquals(
  50. $date->format('Y-m-d H:i'),
  51. $sport->getUpdated()->format('Y-m-d H:i')
  52. );
  53. $this->assertNull($sport->getPublished());
  54. $sportComment = $this->em->getRepository(self::COMMENT)->findOneByMessage('hello');
  55. $this->assertEquals(
  56. $date->format('H:i'),
  57. $sportComment->getModified()->format('H:i')
  58. );
  59. $this->assertNull($sportComment->getClosed());
  60. $sportComment->setStatus(1);
  61. $published = new Type();
  62. $published->setTitle('Published');
  63. $sport->setTitle('Updated');
  64. $sport->setType($published);
  65. $date = new \DateTime('now');
  66. $this->em->persist($sport);
  67. $this->em->persist($published);
  68. $this->em->persist($sportComment);
  69. $this->em->flush();
  70. $this->em->clear();
  71. $sportComment = $this->em->getRepository(self::COMMENT)->findOneByMessage('hello');
  72. $this->assertEquals(
  73. $date->format('Y-m-d H:i'),
  74. $sportComment->getClosed()->format('Y-m-d H:i')
  75. );
  76. $this->assertEquals(
  77. $date->format('Y-m-d H:i'),
  78. $sport->getPublished()->format('Y-m-d H:i')
  79. );
  80. }
  81. public function testForcedValues()
  82. {
  83. $sport = new Article();
  84. $sport->setTitle('sport forced');
  85. $sport->setCreated(new \DateTime('2000-01-01'));
  86. $sport->setUpdated(new \DateTime('2000-01-01 12:00:00'));
  87. $this->em->persist($sport);
  88. $this->em->flush();
  89. $this->em->clear();
  90. $repo = $this->em->getRepository(self::ARTICLE);
  91. $sport = $repo->findOneByTitle('sport forced');
  92. $this->assertEquals(
  93. '2000-01-01',
  94. $sport->getCreated()->format('Y-m-d')
  95. );
  96. $this->assertEquals(
  97. '2000-01-01 12:00:00',
  98. $sport->getUpdated()->format('Y-m-d H:i:s')
  99. );
  100. $published = new Type();
  101. $published->setTitle('Published');
  102. $sport->setType($published);
  103. $sport->setPublished(new \DateTime('2000-01-01 12:00:00'));
  104. $this->em->persist($sport);
  105. $this->em->persist($published);
  106. $this->em->flush();
  107. $this->em->clear();
  108. $sport = $repo->findOneByTitle('sport forced');
  109. $this->assertEquals(
  110. '2000-01-01 12:00:00',
  111. $sport->getPublished()->format('Y-m-d H:i:s')
  112. );
  113. }
  114. protected function getUsedEntityFixtures()
  115. {
  116. return array(
  117. self::ARTICLE,
  118. self::COMMENT,
  119. self::TYPE
  120. );
  121. }
  122. }