LoggableDocumentTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Gedmo\Loggable;
  3. use Loggable\Fixture\Document\Article;
  4. /**
  5. * These are tests for loggable behavior
  6. *
  7. * @author Boussekeyt Jules <jules.boussekeyt@gmail.com>
  8. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  9. *
  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 LoggableDocumentTest extends \PHPUnit_Framework_TestCase
  15. {
  16. const TEST_CLASS_ARTICLE = 'Loggable\Fixture\Document\Article';
  17. /**
  18. * @var DocumentManager
  19. */
  20. private $dm;
  21. public function setUp()
  22. {
  23. $config = new \Doctrine\ODM\MongoDB\Configuration();
  24. $config->setProxyDir(__DIR__ . '/Proxy');
  25. $config->setProxyNamespace('Gedmo\Loggable\Proxies');
  26. $config->setHydratorDir(__DIR__ . '/Hydrator');
  27. $config->setHydratorNamespace('Hydrator');
  28. $config->setDefaultDB('gedmo_loggable_tests');
  29. $config->setLoggerCallable(function(array $log) {
  30. print_r($log);
  31. });
  32. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  33. $reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\');
  34. $config->setMetadataDriverImpl(
  35. new \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver($reader, __DIR__ . '/Document')
  36. );
  37. $evm = new \Doctrine\Common\EventManager();
  38. $loggableListener = new ODM\MongoDB\LoggableListener();
  39. Configuration::setUser('jules');
  40. $evm->addEventSubscriber($loggableListener);
  41. if (!class_exists('Mongo')) {
  42. $this->markTestSkipped('Missing Mongo extension.');
  43. }
  44. try {
  45. $this->dm = \Doctrine\ODM\MongoDB\DocumentManager::create(
  46. new \Doctrine\MongoDB\Connection(),
  47. $config,
  48. $evm
  49. );
  50. } catch (\MongoException $e) {
  51. $this->markTestSkipped('Doctrine MongoDB ODM connection problem.');
  52. }
  53. // if previous test failed
  54. $this->clearLogs();
  55. }
  56. public function testLogGeneration()
  57. {
  58. $collection = $this->dm->getDocumentCollection('Gedmo\Loggable\Document\Log');
  59. $this->assertEquals(0, $collection->count());
  60. $art0 = new Article();
  61. $art0->setTitle('My Title');
  62. $this->dm->persist($art0);
  63. $this->dm->flush();
  64. $log = $this->dm->getRepository('Gedmo\Loggable\Document\Log')->findOneBy(array());
  65. $this->assertNotEquals(null, $log);
  66. $this->assertEquals('create', $log->getAction());
  67. $this->assertEquals((string) $art0, $log->getObject());
  68. $this->assertEquals('jules', $log->getUser());
  69. $this->clearLogs();
  70. }
  71. private function clearLogs()
  72. {
  73. $this->dm->getDocumentCollection('Gedmo\Loggable\Document\Log')->drop();
  74. }
  75. }