TimestampableTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Gedmo\Timestampable;
  3. use Doctrine\Common\Util\Debug,
  4. Timestampable\Fixture\Article,
  5. Timestampable\Fixture\Comment,
  6. Timestampable\Fixture\Type;
  7. /**
  8. * These are tests for Timestampable behavior
  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 TimestampableTest extends \PHPUnit_Framework_TestCase
  16. {
  17. const TEST_ENTITY_ARTICLE = "Timestampable\Fixture\Article";
  18. const TEST_ENTITY_COMMENT = "Timestampable\Fixture\Comment";
  19. const TEST_ENTITY_TYPE = "Timestampable\Fixture\Type";
  20. private $em;
  21. public function setUp()
  22. {
  23. $classLoader = new \Doctrine\Common\ClassLoader('Timestampable\Fixture', __DIR__ . '/../');
  24. $classLoader->register();
  25. $config = new \Doctrine\ORM\Configuration();
  26. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  27. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  28. $config->setProxyDir(__DIR__ . '/Proxy');
  29. $config->setProxyNamespace('Gedmo\Timestampable\Proxies');
  30. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  31. $conn = array(
  32. 'driver' => 'pdo_sqlite',
  33. 'memory' => true,
  34. );
  35. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  36. $evm = new \Doctrine\Common\EventManager();
  37. $timestampableListener = new TimestampableListener();
  38. $evm->addEventSubscriber($timestampableListener);
  39. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  40. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  41. $schemaTool->dropSchema(array());
  42. $schemaTool->createSchema(array(
  43. $this->em->getClassMetadata(self::TEST_ENTITY_ARTICLE),
  44. $this->em->getClassMetadata(self::TEST_ENTITY_COMMENT),
  45. $this->em->getClassMetadata(self::TEST_ENTITY_TYPE)
  46. ));
  47. }
  48. public function testTimestampable()
  49. {
  50. $sport = new Article();
  51. $sport->setTitle('Sport');
  52. $this->assertTrue($sport instanceof Timestampable);
  53. $sportComment = new Comment();
  54. $sportComment->setMessage('hello');
  55. $sportComment->setArticle($sport);
  56. $sportComment->setStatus(0);
  57. $this->assertTrue($sportComment instanceof Timestampable);
  58. $date = new \DateTime('now');
  59. $this->em->persist($sport);
  60. $this->em->persist($sportComment);
  61. $this->em->flush();
  62. $this->em->clear();
  63. $sport = $this->em->getRepository(self::TEST_ENTITY_ARTICLE)->findOneByTitle('Sport');
  64. $this->assertEquals(
  65. $date->format('Y-m-d 00:00:00'),
  66. $sport->getCreated()->format('Y-m-d H:i:s')
  67. );
  68. $this->assertEquals(
  69. $date->format('Y-m-d H:i:s'),
  70. $sport->getUpdated()->format('Y-m-d H:i:s')
  71. );
  72. $this->assertEquals(null, $sport->getPublished());
  73. $sportComment = $this->em->getRepository(self::TEST_ENTITY_COMMENT)->findOneByMessage('hello');
  74. $this->assertEquals(
  75. $date->format('H:i:s'),
  76. $sportComment->getModified()->format('H:i:s')
  77. );
  78. $this->assertEquals(null, $sportComment->getClosed());
  79. sleep(1);
  80. $sportComment->setStatus(1);
  81. $published = new Type();
  82. $published->setTitle('Published');
  83. $sport->setTitle('Updated');
  84. $sport->setType($published);
  85. $date = new \DateTime('now');
  86. $this->em->persist($sport);
  87. $this->em->persist($published);
  88. $this->em->persist($sportComment);
  89. $this->em->flush();
  90. $this->em->clear();
  91. $sportComment = $this->em->getRepository(self::TEST_ENTITY_COMMENT)->findOneByMessage('hello');
  92. $this->assertEquals(
  93. $date->format('Y-m-d H:i:s'),
  94. $sportComment->getClosed()->format('Y-m-d H:i:s')
  95. );
  96. $sport = $this->em->getRepository(self::TEST_ENTITY_ARTICLE)->findOneByTitle('Updated');
  97. $this->assertEquals(
  98. $date->format('Y-m-d H:i:s'),
  99. $sport->getUpdated()->format('Y-m-d H:i:s')
  100. );
  101. $this->assertEquals(
  102. $date->format('Y-m-d H:i:s'),
  103. $sport->getPublished()->format('Y-m-d H:i:s')
  104. );
  105. }
  106. }