LoggableEntityTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Gedmo\Loggable;
  3. use Doctrine\Common\Util\Debug,
  4. Loggable\Fixture\Entity\Article,
  5. Loggable\Fixture\Entity\Comment;
  6. /**
  7. * These are tests for loggable behavior
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @package Gedmo.Loggable
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class LoggableEntityTest extends \PHPUnit_Framework_TestCase
  15. {
  16. const TEST_ENTITY_CLASS_ARTICLE = 'Loggable\Fixture\Entity\Article';
  17. const TEST_ENTITY_CLASS_COMMENT = 'Loggable\Fixture\Entity\Comment';
  18. private $articleId;
  19. private $LoggableListener;
  20. private $em;
  21. public function setUp()
  22. {
  23. $config = new \Doctrine\ORM\Configuration();
  24. $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  25. $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
  26. $config->setProxyDir(__DIR__ . '/Proxy');
  27. $config->setProxyNamespace('Gedmo\Loggable\Proxies');
  28. $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver());
  29. $conn = array(
  30. 'driver' => 'pdo_sqlite',
  31. 'memory' => true,
  32. );
  33. //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
  34. $evm = new \Doctrine\Common\EventManager();
  35. $this->LoggableListener = new ORM\LoggableListener();
  36. Configuration::setUser('jules');
  37. $evm->addEventSubscriber($this->LoggableListener);
  38. $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm);
  39. $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
  40. $schemaTool->dropSchema(array());
  41. $schemaTool->createSchema(array(
  42. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS_ARTICLE),
  43. $this->em->getClassMetadata(self::TEST_ENTITY_CLASS_COMMENT),
  44. $this->em->getClassMetadata('Gedmo\Loggable\Entity\Log'),
  45. ));
  46. $this->clearLogs();
  47. }
  48. public function testLoggableAllActions()
  49. {
  50. $repo = $this->em->getRepository('Gedmo\Loggable\Entity\Log');
  51. $this->assertEquals(0, count($repo->findAll()));
  52. $art0 = new Article();
  53. $art0->setTitle('My Title');
  54. $this->em->persist($art0);
  55. $this->em->flush();
  56. $log = $repo->findOneBy(array());
  57. $this->assertNotEquals(null, $log);
  58. $this->assertEquals('create', $log->getAction());
  59. $this->assertEquals((string) $art0, $log->getObject());
  60. $this->assertEquals('jules', $log->getUser());
  61. $this->clearLogs();
  62. }
  63. public function testLoggableNotAllowedAction()
  64. {
  65. $repo = $this->em->getRepository('Gedmo\Loggable\Entity\Log');
  66. $comment = new Comment();
  67. $comment->setTitle('My Title');
  68. $this->em->persist($comment);
  69. $this->em->flush();
  70. $this->assertEquals(1, count($repo->findAll()));
  71. $this->clearLogs();
  72. $this->em->remove($comment);
  73. $this->em->flush();
  74. $this->assertEquals(0, count($repo->findAll()));
  75. }
  76. private function clearLogs()
  77. {
  78. $meta = $this->em->getClassMetadata('Gedmo\Loggable\Entity\Log');
  79. $this->em->getConnection()->delete($meta->getTableName(), array('object' => 'My Title'));
  80. }
  81. }