TimestampableDocumentTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Gedmo\Timestampable;
  3. use Tool\BaseTestCaseMongoODM;
  4. use Doctrine\Common\EventManager;
  5. use Timestampable\Fixture\Document\Article,
  6. Timestampable\Fixture\Document\Type;
  7. /**
  8. * These are tests for Timestampable behavior ODM implementation
  9. *
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class TimestampableDocumentTest extends BaseTestCaseMongoODM
  15. {
  16. const ARTICLE = 'Timestampable\Fixture\Document\Article';
  17. const TYPE = 'Timestampable\Fixture\Document\Type';
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. $evm = new EventManager();
  22. $evm->addEventSubscriber(new TimestampableListener);
  23. $this->getMockDocumentManager($evm);
  24. $this->populate();
  25. }
  26. public function testTimestampable()
  27. {
  28. $repo = $this->dm->getRepository(self::ARTICLE);
  29. $article = $repo->findOneByTitle('Timestampable Article');
  30. $date = new \DateTime();
  31. $now = time();
  32. $created = intval((string)$article->getCreated());
  33. $this->assertTrue($created > $now - 5 && $created < $now + 5); // 5 seconds interval if lag
  34. $this->assertEquals(
  35. $date->format('Y-m-d H:i'),
  36. $article->getUpdated()->format('Y-m-d H:i')
  37. );
  38. $published = new Type;
  39. $published->setIdentifier('published');
  40. $published->setTitle('Published');
  41. $article->setType($published);
  42. $this->dm->persist($article);
  43. $this->dm->persist($published);
  44. $this->dm->flush();
  45. $this->dm->clear();
  46. $article = $repo->findOneByTitle('Timestampable Article');
  47. $date = new \DateTime();
  48. $this->assertEquals(
  49. $date->format('Y-m-d H:i'),
  50. $article->getPublished()->format('Y-m-d H:i')
  51. );
  52. }
  53. public function testForcedValues()
  54. {
  55. $sport = new Article();
  56. $sport->setTitle('sport forced');
  57. $created = strtotime('2000-01-01 12:00:00');
  58. $sport->setCreated($created);
  59. $sport->setUpdated(new \DateTime('2000-01-01 12:00:00'));
  60. $this->dm->persist($sport);
  61. $this->dm->flush();
  62. $this->dm->clear();
  63. $repo = $this->dm->getRepository(self::ARTICLE);
  64. $sport = $repo->findOneByTitle('sport forced');
  65. $this->assertEquals(
  66. $created,
  67. (string)$sport->getCreated()
  68. );
  69. $this->assertEquals(
  70. '2000-01-01 12:00:00',
  71. $sport->getUpdated()->format('Y-m-d H:i:s')
  72. );
  73. $published = new Type;
  74. $published->setIdentifier('published');
  75. $published->setTitle('Published');
  76. $sport->setType($published);
  77. $sport->setPublished(new \DateTime('2000-01-01 12:00:00'));
  78. $this->dm->persist($sport);
  79. $this->dm->persist($published);
  80. $this->dm->flush();
  81. $this->dm->clear();
  82. $sport = $repo->findOneByTitle('sport forced');
  83. $this->assertEquals(
  84. '2000-01-01 12:00:00',
  85. $sport->getPublished()->format('Y-m-d H:i:s')
  86. );
  87. }
  88. /**
  89. * @test
  90. */
  91. function shouldHandleOnChangeWithBooleanValue()
  92. {
  93. $repo = $this->dm->getRepository(self::ARTICLE);
  94. $article = $repo->findOneByTitle('Timestampable Article');
  95. $this->assertNull($article->getReady());
  96. $article->setIsReady(true);
  97. $this->dm->persist($article);
  98. $this->dm->flush();
  99. $this->assertNotNull($article->getReady());
  100. }
  101. private function populate()
  102. {
  103. $art0 = new Article();
  104. $art0->setTitle('Timestampable Article');
  105. $this->dm->persist($art0);
  106. $this->dm->flush();
  107. $this->dm->clear();
  108. }
  109. }