TimestampableDocumentTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. $this->assertEquals(time(), (string)$article->getCreated());
  33. $this->assertEquals(
  34. $date->format('Y-m-d H:i:s'),
  35. $article->getUpdated()->format('Y-m-d H:i:s')
  36. );
  37. $published = new Type;
  38. $published->setIdentifier('published');
  39. $published->setTitle('Published');
  40. $article->setType($published);
  41. $this->dm->persist($article);
  42. $this->dm->persist($published);
  43. $this->dm->flush();
  44. $this->dm->clear();
  45. $article = $repo->findOneByTitle('Timestampable Article');
  46. $date = new \DateTime();
  47. $this->assertEquals(
  48. $date->format('Y-m-d H:i:s'),
  49. $article->getPublished()->format('Y-m-d H:i:s')
  50. );
  51. }
  52. public function testForcedValues()
  53. {
  54. $sport = new Article();
  55. $sport->setTitle('sport forced');
  56. $created = strtotime('2000-01-01 12:00:00');
  57. $sport->setCreated($created);
  58. $sport->setUpdated(new \DateTime('2000-01-01 12:00:00'));
  59. $this->dm->persist($sport);
  60. $this->dm->flush();
  61. $this->dm->clear();
  62. $repo = $this->dm->getRepository(self::ARTICLE);
  63. $sport = $repo->findOneByTitle('sport forced');
  64. $this->assertEquals(
  65. $created,
  66. (string)$sport->getCreated()
  67. );
  68. $this->assertEquals(
  69. '2000-01-01 12:00:00',
  70. $sport->getUpdated()->format('Y-m-d H:i:s')
  71. );
  72. $published = new Type;
  73. $published->setIdentifier('published');
  74. $published->setTitle('Published');
  75. $sport->setType($published);
  76. $sport->setPublished(new \DateTime('2000-01-01 12:00:00'));
  77. $this->dm->persist($sport);
  78. $this->dm->persist($published);
  79. $this->dm->flush();
  80. $this->dm->clear();
  81. $sport = $repo->findOneByTitle('sport forced');
  82. $this->assertEquals(
  83. '2000-01-01 12:00:00',
  84. $sport->getPublished()->format('Y-m-d H:i:s')
  85. );
  86. }
  87. private function populate()
  88. {
  89. $art0 = new Article();
  90. $art0->setTitle('Timestampable Article');
  91. $this->dm->persist($art0);
  92. $this->dm->flush();
  93. $this->dm->clear();
  94. }
  95. }