TimestampableDocumentTest.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * @package Gedmo.Timestampable
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class TimestampableDocumentTest extends BaseTestCaseMongoODM
  16. {
  17. const ARTICLE = 'Timestampable\Fixture\Document\Article';
  18. const TYPE = 'Timestampable\Fixture\Document\Type';
  19. protected function setUp()
  20. {
  21. parent::setUp();
  22. $evm = new EventManager();
  23. $evm->addEventSubscriber(new TimestampableListener);
  24. $this->getMockDocumentManager($evm);
  25. $this->populate();
  26. }
  27. public function testTimestampable()
  28. {
  29. $repo = $this->dm->getRepository(self::ARTICLE);
  30. $article = $repo->findOneByTitle('Timestampable Article');
  31. $date = new \DateTime();
  32. $now = time();
  33. $created = intval((string)$article->getCreated());
  34. $this->assertTrue($created > $now - 5 && $created < $now + 5); // 5 seconds interval if lag
  35. $this->assertEquals(
  36. $date->format('Y-m-d H:i'),
  37. $article->getUpdated()->format('Y-m-d H:i')
  38. );
  39. $published = new Type;
  40. $published->setIdentifier('published');
  41. $published->setTitle('Published');
  42. $article->setType($published);
  43. $this->dm->persist($article);
  44. $this->dm->persist($published);
  45. $this->dm->flush();
  46. $this->dm->clear();
  47. $article = $repo->findOneByTitle('Timestampable Article');
  48. $date = new \DateTime();
  49. $this->assertEquals(
  50. $date->format('Y-m-d H:i'),
  51. $article->getPublished()->format('Y-m-d H:i')
  52. );
  53. }
  54. public function testForcedValues()
  55. {
  56. $sport = new Article();
  57. $sport->setTitle('sport forced');
  58. $created = strtotime('2000-01-01 12:00:00');
  59. $sport->setCreated($created);
  60. $sport->setUpdated(new \DateTime('2000-01-01 12:00:00'));
  61. $this->dm->persist($sport);
  62. $this->dm->flush();
  63. $this->dm->clear();
  64. $repo = $this->dm->getRepository(self::ARTICLE);
  65. $sport = $repo->findOneByTitle('sport forced');
  66. $this->assertEquals(
  67. $created,
  68. (string)$sport->getCreated()
  69. );
  70. $this->assertEquals(
  71. '2000-01-01 12:00:00',
  72. $sport->getUpdated()->format('Y-m-d H:i:s')
  73. );
  74. $published = new Type;
  75. $published->setIdentifier('published');
  76. $published->setTitle('Published');
  77. $sport->setType($published);
  78. $sport->setPublished(new \DateTime('2000-01-01 12:00:00'));
  79. $this->dm->persist($sport);
  80. $this->dm->persist($published);
  81. $this->dm->flush();
  82. $this->dm->clear();
  83. $sport = $repo->findOneByTitle('sport forced');
  84. $this->assertEquals(
  85. '2000-01-01 12:00:00',
  86. $sport->getPublished()->format('Y-m-d H:i:s')
  87. );
  88. }
  89. private function populate()
  90. {
  91. $art0 = new Article();
  92. $art0->setTitle('Timestampable Article');
  93. $this->dm->persist($art0);
  94. $this->dm->flush();
  95. $this->dm->clear();
  96. }
  97. }