浏览代码

[Loggable] Modified Log properties (we now use objectClass and foreignKey like in Translatable), remove __toString() requirement, improve listeners and tests

Boussekeyt Jules 14 年之前
父节点
当前提交
354f90a2ea

+ 19 - 19
lib/Gedmo/Loggable/HistoryLog.php

@@ -12,35 +12,23 @@ namespace Gedmo\Loggable;
  * @link http://www.gediminasm.org
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
-abstract class HistoryLog
+abstract class AbstractHistoryLog
 {
     public function __construct()
     {
         $this->actualizeDate();
     }
 
-    public function __toString()
+    public function getId()
     {
-        return sprintf('%s %s %s %s',
-            $this->user,
-            $this->action,
-            $this->object,
-            $this->date->format('Y-m-d H:i:s')
-        );
+        return $this->id;
     }
 
-    abstract function actualizeDate();
-
     public function getDate()
     {
         return $this->date;
     }
 
-    public function setDate(\DateTime $date)
-    {
-        $this->date = $date;
-    }
-
     public function getUser()
     {
         return $this->user;
@@ -61,13 +49,25 @@ abstract class HistoryLog
         $this->action = $action;
     }
 
-    public function getObject()
+    public function getObjectClass()
     {
-        return $this->object;
+        return $this->objectClass;
     }
 
-    public function setObject($object)
+    public function setObjectClass($objectClass)
     {
-        $this->object = (string) $object;
+        $this->objectClass = $objectClass;
     }
+
+    public function setForeignKey($foreignKey)
+    {
+        $this->foreignKey = $foreignKey;
+    }
+
+    public function getForeignKey()
+    {
+        return $this->foreignKey;
+    }
+
+    abstract protected function actualizeDate();
 }

+ 115 - 25
lib/Gedmo/Loggable/AbstractLoggableListener.php

@@ -20,16 +20,16 @@ use Doctrine\Common\EventArgs,
  */
 abstract class AbstractLoggableListener extends MappedEventSubscriber
 {
-    protected static $user;
-
     /**
-     * Set the current user
-     * @param $user string
+     * List of logs which do not have the foreign
+     * key generated yet - MySQL case. These logs
+     * will be updated with new keys on postPersist event
+     *
+     * @var array
      */
-    public static function setUser($user)
-    {
-        self::$user = $user; 
-    }
+    protected $pendingLogInserts = array();
+
+    protected static $user;
 
     /**
      * Mapps additional metadata
@@ -42,6 +42,22 @@ abstract class AbstractLoggableListener extends MappedEventSubscriber
         $this->loadMetadataForObjectClass($this->getObjectManager($eventArgs), $eventArgs->getClassMetadata());
     }
 
+    /**
+     * @param $user string
+     */
+    public static function setUser($user)
+    {
+        self::$user = $user;
+    }
+
+    /**
+     * @return string
+     */
+    protected function getUser()
+    {
+        return self::$user;
+    }
+
     /**
      * Looks for loggable objects being inserted or updated
      * for further processing
@@ -55,18 +71,51 @@ abstract class AbstractLoggableListener extends MappedEventSubscriber
         $uow = $om->getUnitOfWork();
 
         foreach ($this->getScheduledObjectInsertions($uow) as $object) {
-            if ($this->isAllowed('create', $object, $om)) {
-                $this->createLog('create', $object, $om);
+            if ($this->isObjectLoggableForAction($om, $object, 'create')) {
+                $this->handleObjectLogging($om, $object, 'create', true);
             }
         }
+
         foreach ($this->getScheduledObjectUpdates($uow) as $object) {
-            if ($this->isAllowed('update', $object, $om)) {
-                $this->createLog('update', $object, $om);
+            if ($this->isObjectLoggableForAction($om, $object, 'update')) {
+                $this->handleObjectLogging($om, $object, 'update', false);
             }
         }
+
         foreach ($this->getScheduledObjectDeletions($uow) as $object) {
-            if ($this->isAllowed('delete', $object, $om)) {
-                $this->createLog('delete', $object, $om);
+            if ($this->isObjectLoggableForAction($om, $object, 'delete')) {
+                $this->handleObjectLogging($om, $object, 'delete', false);
+            }
+        }
+    }
+
+    /**
+     * Checks for inserted object to update their log
+     * foreign keys
+     *
+     * @param EventArgs $args
+     * @return void
+     */
+    public function postPersist(EventArgs $args)
+    {
+        $om = $this->getObjectManager($args);
+        $object = $this->getObject($args);
+        $meta = $om->getClassMetadata(get_class($object));
+        // check if entity is tracked by loggable and without foreign key
+        if (array_key_exists($meta->name, $this->configurations) && count($this->pendingLogInserts)) {
+            $oid = spl_object_hash($object);
+
+            // there should be single identifier
+            $identifierField = $this->getSingleIdentifierFieldName($meta);
+            $logMeta = $om->getClassMetadata($this->getLogClass($meta->name));
+            if (array_key_exists($oid, $this->pendingLogInserts)) {
+                // load the pending logs without key
+                $log = $this->pendingLogInserts[$oid];
+                $logMeta->getReflectionProperty('foreignKey')->setValue(
+                    $log,
+                    $meta->getReflectionProperty($identifierField)->getValue($object)
+                );
+                $this->insertLogRecord($om, $log);
             }
         }
     }
@@ -79,14 +128,16 @@ abstract class AbstractLoggableListener extends MappedEventSubscriber
      * @param  $object
      * @return bool
      */
-    private function isAllowed($action, $object, $om)
+    protected function isObjectLoggableForAction($om, $object, $action)
     {
         $config = $this->getConfiguration($om, get_class($object));
 
+        // if object hasn't mapping informations
         if (!isset($config['actions'])) {
             return false;
         }
 
+        // if object action isn't allowed
         if (!in_array($action, $config['actions'])) {
             return false;
         }
@@ -102,18 +153,39 @@ abstract class AbstractLoggableListener extends MappedEventSubscriber
      * @param  $object
      * @return Log
      */
-    private function createLog($action, $object, $om)
+    protected function handleObjectLogging($om, $object, $action, $isInsert)
     {
-        $class = $this->getObjectClass();
-        $log = new $class();
-        
-        $log->setAction($action);
-        $log->setUser(self::$user);
-        $log->setObject($object);
+        $meta = $om->getClassMetadata(get_class($object));
+        // no need cache, metadata is loaded only once in MetadataFactoryClass
+        $logClass = $this->getLogClass($meta->name);
+        $logMetadata = $om->getClassMetadata($logClass);
+
+        // check for the availability of the primary key
+        $identifierField = $this->getSingleIdentifierFieldName($meta);
+        $objectId = $meta->getReflectionProperty($identifierField)->getValue($object);
+        if (!$object && $isInsert) {
+            $objectId = null;
+        }
+
+        $user = $this->getUser();
+
+        // create new log
+        $log = new $logClass();
+        $logMetadata->getReflectionProperty('action')->setValue($log, $action);
+        $logMetadata->getReflectionProperty('user')->setValue($log, $user);
+        $logMetadata->getReflectionProperty('objectClass')->setValue($log, $meta->name);
+        $logMetadata->getReflectionProperty('foreignKey')->setValue($log, $objectId);
 
-        $this->insertLogRecord($om, $log);
+        // set the logged field, take value using reflection
+        //$logMetadata->getReflectionProperty('content')->setValue($log, $meta->getReflectionProperty($field)->getValue($object));
 
-        return $log;
+        if ($isInsert && null === $objectId) {
+            // if we do not have the primary key yet available
+            // keep this log in memory to insert it later with foreign key
+            $this->pendingLogInserts[spl_object_hash($object)] = $log;
+        } else {
+            $this->insertLogRecord($om, $log);
+        }
     }
 
     /**
@@ -140,8 +212,16 @@ abstract class AbstractLoggableListener extends MappedEventSubscriber
      * @param EventArgs $args
      * @return object
      */
-    abstract protected function getObjectClass();
+    abstract protected function getLogClass();
 
+    /**
+     * Get the Object from EventArgs
+     *
+     * @param EventArgs $args
+     * @return object
+     */
+    abstract protected function getObject(EventArgs $args);
+        
     /**
      * Get the ObjectManager from EventArgs
      *
@@ -173,4 +253,14 @@ abstract class AbstractLoggableListener extends MappedEventSubscriber
      * @return array
      */
     abstract protected function getScheduledObjectDeletions($uow);
+
+    /**
+     * Get the single identifier field name
+     *
+     * @param ClassMetadata $meta
+     * @throws MappingException - if identifier is composite
+     * @return string
+     */
+    abstract protected function getSingleIdentifierFieldName($meta);
+
 }

+ 21 - 6
lib/Gedmo/Loggable/Document/HistoryLog.php

@@ -2,41 +2,56 @@
 
 namespace Gedmo\Loggable\Document;
 
-use Gedmo\Loggable\HistoryLog as BaseHistoryLog;
+use Gedmo\Loggable\AbstractHistoryLog;
 
 /**
  * @Document
  */
-class HistoryLog extends BaseHistoryLog
+class HistoryLog extends AbstractHistoryLog
 {
     /**
+     * @var string $id
+     *
      * @Id
      */
     protected $id;
 
     /**
+     * @var string $user
+     *
      * @String
      */
     protected $user;
 
     /**
+     * @var string $action
+     *
      * @String
      */
     protected $action;
 
     /**
-     * The return value of __toString
+     * @var string $objectClass
      *
-     * @String
+     * @String(name="object_class")
      */
-    protected $object;
+    protected $objectClass;
 
     /**
+     * @var string $foreignKey
+     *
+     * @String(name="foreign_key")
+     */
+    protected $foreignKey;
+
+    /**
+     * @var MongoData $date 
+     *
      * @Date
      */
     protected $date;
 
-    public function actualizeDate()
+    protected function actualizeDate()
     {
         $this->date = new \MongoDate();
     }

+ 15 - 8
lib/Gedmo/Loggable/Entity/HistoryLog.php

@@ -2,13 +2,13 @@
 
 namespace Gedmo\Loggable\Entity;
 
-use Gedmo\Loggable\HistoryLog as BaseHistoryLog;
+use Gedmo\Loggable\AbstractHistoryLog;
 
 /**
  * @Entity
  * @gedmo:Loggable
  */
-class HistoryLog extends BaseHistoryLog
+class HistoryLog extends AbstractHistoryLog
 {
     /**
      * @var integer $id
@@ -22,23 +22,30 @@ class HistoryLog extends BaseHistoryLog
     /**
      * @var string $user
      *
-     * @Column(name="user", type="string", length=8)
+     * @Column(name="user", type="string", length=128)
      */
     protected $user;
 
     /**
      * @var string $action
      *
-     * @Column(name="action", type="string", length=8)
+     * @Column(type="string", length=64)
      */
     protected $action;
 
     /**
-     * @var string $object
+     * @var string $objectClass
      *
-     * @Column(name="object", type="string", length=8)
+     * @Column(name="object_class", type="string", length=255)
      */
-    protected $object;
+    protected $objectClass;
+
+    /**
+     * @var string $foreignKey
+     *
+     * @Column(name="foreign_key", type="string", length=64)
+     */
+    protected $foreignKey;
 
     /**
      * @var string $date
@@ -47,7 +54,7 @@ class HistoryLog extends BaseHistoryLog
      */
     protected $date;
 
-    public function actualizeDate()
+    protected function actualizeDate()
     {
         $this->date = new \DateTime();
     }

+ 0 - 4
lib/Gedmo/Loggable/Mapping/Driver/Annotation.php

@@ -51,10 +51,6 @@ class Annotation implements Driver
         if (isset($config['actions']) && !is_array($config['actions'])) {
             throw new InvalidMappingException("Actions for class: {$meta->name} should be an array");
         }
-
-        if (!method_exists($meta->getReflectionClass(), '__toString')) {
-            throw new InvalidMappingException("class: {$meta->name} should implement __toString() method");
-        }
     }
 
     /**

+ 22 - 7
lib/Gedmo/Loggable/ODM/MongoDB/LoggableListener.php

@@ -2,10 +2,9 @@
 
 namespace Gedmo\Loggable\ODM\MongoDB;
 
-use Gedmo\Loggable\AbstractLoggableListener;
-
-use Doctrine\ODM\MongoDB\Events;
-use Doctrine\Common\EventArgs;
+use Gedmo\Loggable\AbstractLoggableListener,
+    Doctrine\Common\EventArgs,
+    Doctrine\ODM\MongoDB\Events;
 
 /**
  * @author Boussekeyt Jules <jules.boussekeyt@gmail.com>
@@ -28,7 +27,7 @@ class LoggableListener extends AbstractLoggableListener
 
     public function getSubscribedEvents()
     {
-        return array(Events::onFlush, Events::loadClassMetadata);
+        return array(Events::onFlush, Events::postPersist, Events::loadClassMetadata);
     }
 
     /**
@@ -54,11 +53,19 @@ class LoggableListener extends AbstractLoggableListener
     /**
      * {@inheritdoc}
      */
-    protected function getObjectClass()
+    protected function getLogClass()
     {
         return $this->defaultLoggableDocument;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    protected function getObject(EventArgs $args)
+    {
+        return $args->getDocument();
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -89,5 +96,13 @@ class LoggableListener extends AbstractLoggableListener
     protected function getScheduledObjectDeletions($uow)
     {
         return $uow->getScheduledDocumentDeletions();
-    } 
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getSingleIdentifierFieldName($meta)
+    {
+        return $meta->identifier;
+    }
 }

+ 18 - 2
lib/Gedmo/Loggable/ORM/LoggableListener.php

@@ -28,7 +28,7 @@ class LoggableListener extends AbstractLoggableListener
 
     public function getSubscribedEvents()
     {
-        return array(Events::onFlush, Events::loadClassMetadata);
+        return array(Events::onFlush, Events::postPersist, Events::loadClassMetadata);
     }
 
     /**
@@ -59,11 +59,19 @@ class LoggableListener extends AbstractLoggableListener
     /**
      * {@inheritdoc}
      */
-    protected function getObjectClass()
+    protected function getLogClass()
     {
         return $this->defaultLoggableEntity;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    protected function getObject(EventArgs $args)
+    {
+        return $args->getEntity();
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -95,4 +103,12 @@ class LoggableListener extends AbstractLoggableListener
     {
         return $uow->getScheduledEntityDeletions();
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function getSingleIdentifierFieldName($meta)
+    {
+        return $meta->getSingleIdentifierFieldName();
+    }
 }

+ 0 - 5
tests/Gedmo/Loggable/Fixture/Document/Article.php

@@ -17,11 +17,6 @@ class Article
      */
     private $title;
 
-    public function __toString()
-    {
-        return $this->title;
-    }
-
     public function getId()
     {
         return $this->id;

+ 35 - 0
tests/Gedmo/Loggable/Fixture/Document/Comment.php

@@ -0,0 +1,35 @@
+<?php
+
+namespace Loggable\Fixture\Document;
+
+/**
+ * @Document
+ * @gedmo:Loggable(actions={"create", "delete"})
+ */
+class Comment
+{
+    /**
+     * @Id
+     */
+    private $id;
+
+    /**
+     * @String
+     */
+    private $title;
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+}

+ 0 - 6
tests/Gedmo/Loggable/Fixture/Entity/Article.php

@@ -20,12 +20,6 @@ class Article
      */
     private $title;
 
-
-    public function __toString()
-    {
-        return $this->title;
-    }
-
     public function getId()
     {
         return $this->id;

+ 0 - 6
tests/Gedmo/Loggable/Fixture/Entity/Comment.php

@@ -20,12 +20,6 @@ class Comment
      */
     private $title;
 
-
-    public function __toString()
-    {
-        return $this->title;
-    }
-
     public function getId()
     {
         return $this->id;

+ 83 - 13
tests/Gedmo/Loggable/LoggableDocumentTest.php

@@ -2,7 +2,8 @@
 
 namespace Gedmo\Loggable;
 
-use Loggable\Fixture\Document\Article;
+use Loggable\Fixture\Document\Article,
+    Loggable\Fixture\Document\Comment;
 
 /**
  * These are tests for loggable behavior
@@ -17,6 +18,7 @@ use Loggable\Fixture\Document\Article;
 class LoggableDocumentTest extends \PHPUnit_Framework_TestCase
 {
     const TEST_CLASS_ARTICLE = 'Loggable\Fixture\Document\Article';
+    const TEST_CLASS_COMMENT = 'Loggable\Fixture\Document\Comment';
 
     /**
      * @var DocumentManager
@@ -32,12 +34,10 @@ class LoggableDocumentTest extends \PHPUnit_Framework_TestCase
         $config->setHydratorNamespace('Hydrator');
         $config->setDefaultDB('gedmo_loggable_tests');
 
-
         $config->setLoggerCallable(function(array $log) {
             print_r($log);
         });
 
-
         $reader = new \Doctrine\Common\Annotations\AnnotationReader();
         $reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\\');
         $config->setMetadataDriverImpl(
@@ -62,35 +62,105 @@ class LoggableDocumentTest extends \PHPUnit_Framework_TestCase
         } catch (\MongoException $e) {
             $this->markTestSkipped('Doctrine MongoDB ODM connection problem.');
         }
+    }
 
-        // if previous test failed
+    protected function tearDown()
+    {
         $this->clearLogs();
     }
 
-    public function testLogGeneration()
+    public function testLoggableAllActions()
     {
-        $collection = $this->dm->getDocumentCollection('Gedmo\Loggable\Document\HistoryLog');
+        $art0 = new Article();
 
-        $this->assertEquals(0, $collection->count());
+        // action create
 
-        $art0 = new Article();
         $art0->setTitle('My Title');
-
         $this->dm->persist($art0);
         $this->dm->flush();
 
-        $log = $this->dm->getRepository('Gedmo\Loggable\Document\HistoryLog')->findOneBy(array());
+        $logs = $this->getLogs();
+        $this->assertEquals(1, count($logs), 'a log is created');
+        $log = $logs->getSingleResult();
+
         $this->assertNotEquals(null, $log);
-        
+        $this->assertTrue($log instanceof Document\HistoryLog, 'a log instance of Document\HistoryLog');
+
         $this->assertEquals('create', $log->getAction());
-        $this->assertEquals((string) $art0, $log->getObject());
+        $this->assertEquals(get_class($art0), $log->getObjectClass());
+        $this->assertEquals($art0->getId(), $log->getForeignKey());
+        $this->assertEquals('jules', $log->getUser());
+
+        $this->clearLogs();
+
+        // action update
+
+        $art0->setTitle('Another Title');
+        $this->dm->persist($art0);
+        $this->dm->flush();
+
+        $logs = $this->getLogs();
+        $this->assertEquals(1, count($logs), 'a log is created');
+        $log = $logs->getSingleResult();
+        $this->assertNotEquals(null, $log);
+        $this->assertTrue($log instanceof Document\HistoryLog, 'a log instance of Document\HistoryLog');
+
+        $this->assertEquals('update', $log->getAction());
+        $this->assertEquals(get_class($art0), $log->getObjectClass());
+        $this->assertEquals($art0->getId(), $log->getForeignKey());
         $this->assertEquals('jules', $log->getUser());
 
         $this->clearLogs();
+
+        // action delete
+
+        $articleId = $art0->getId();
+        $this->dm->remove($art0);
+        $this->dm->flush();
+
+        $logs = $this->getLogs();
+        $this->assertEquals(1, count($logs), 'a log is created');
+        $log = $logs->getSingleResult();
+        $this->assertNotEquals(null, $log);
+        $this->assertTrue($log instanceof Document\HistoryLog, 'a log instance of Document\HistoryLog');
+
+        $this->assertEquals('delete', $log->getAction());
+        $this->assertEquals(get_class($art0), $log->getObjectClass());
+        $this->assertEquals($articleId, $log->getForeignKey());
+        $this->assertEquals('jules', $log->getUser());
+    }
+
+    public function testLoggableNotAllowedAction()
+    {
+        $comment = new Comment();
+        $comment->setTitle('My Title');
+
+        $this->dm->persist($comment);
+        $this->dm->flush();
+        $this->assertEquals(1, $this->getLogs()->count());
+        $this->clearLogs();
+
+        $comment->setTitle('Another Title');
+        $this->dm->persist($comment);
+        $this->dm->flush();
+        $this->assertEquals(0, $this->getLogs()->count());
+    }
+
+    private function getLogs()
+    {
+        return $this->dm->createQueryBuilder('Gedmo\Loggable\Document\HistoryLog')
+            ->select()
+            ->getQuery()
+            ->execute()
+        ;
     }
 
     private function clearLogs()
     {
-        $this->dm->getDocumentCollection('Gedmo\Loggable\Document\HistoryLog')->drop();
+        $this->dm->createQueryBuilder('Gedmo\Loggable\Document\HistoryLog')
+            ->remove()
+            ->getQuery()
+            ->execute()
+        ;
     }
 }

+ 65 - 15
tests/Gedmo/Loggable/LoggableEntityTest.php

@@ -52,54 +52,104 @@ class LoggableEntityTest extends \PHPUnit_Framework_TestCase
             $this->em->getClassMetadata(self::TEST_ENTITY_CLASS_COMMENT),
             $this->em->getClassMetadata('Gedmo\Loggable\Entity\HistoryLog'),
         ));
+    }
 
+    protected function tearDown()
+    {
         $this->clearLogs();
     }
 
     public function testLoggableAllActions()
     {
-        $repo = $this->em->getRepository('Gedmo\Loggable\Entity\HistoryLog');
+        $art0 = new Article();
 
-        $this->assertEquals(0, count($repo->findAll()));
+        // action create
 
-        $art0 = new Article();
         $art0->setTitle('My Title');
-        
         $this->em->persist($art0);
         $this->em->flush();
 
-        $log = $repo->findOneBy(array());
-        
+        $logs = $this->getLogs();
+        $this->assertEquals(1, count($logs), 'a log is created');
+        $log = $logs[0];
         $this->assertNotEquals(null, $log);
+        $this->assertTrue($log instanceof Entity\HistoryLog, 'a log instance of Entity\HistoryLog');
+
         $this->assertEquals('create', $log->getAction());
-        $this->assertEquals((string) $art0, $log->getObject());
+        $this->assertEquals(get_class($art0), $log->getObjectClass());
+        $this->assertEquals($art0->getId(), $log->getForeignKey());
+        $this->assertEquals('jules', $log->getUser());
+
+        $this->clearLogs();
+
+        // action update
+
+        $art0->setTitle('Another Title');
+        $this->em->persist($art0);
+        $this->em->flush();
+
+        $logs = $this->getLogs();
+        $this->assertEquals(1, count($logs), 'a log is created');
+        $log = $logs[0];
+        $this->assertNotEquals(null, $log);
+        $this->assertTrue($log instanceof Entity\HistoryLog, 'a log instance of Entity\HistoryLog');
+
+        $this->assertEquals('update', $log->getAction());
+        $this->assertEquals(get_class($art0), $log->getObjectClass());
+        $this->assertEquals($art0->getId(), $log->getForeignKey());
         $this->assertEquals('jules', $log->getUser());
 
         $this->clearLogs();
+
+        // action delete
+
+        $articleId = $art0->getId();
+        $this->em->remove($art0);
+        $this->em->flush();
+
+        $logs = $this->getLogs();
+        $this->assertEquals(1, count($logs), 'a log is created');
+        $log = $logs[0];
+        $this->assertNotEquals(null, $log);
+        $this->assertTrue($log instanceof Entity\HistoryLog, 'a log instance of Entity\HistoryLog');
+
+        $this->assertEquals('delete', $log->getAction());
+        $this->assertEquals(get_class($art0), $log->getObjectClass());
+        $this->assertEquals($articleId, $log->getForeignKey());
+        $this->assertEquals('jules', $log->getUser());
     }
 
     public function testLoggableNotAllowedAction()
     {
-        $repo = $this->em->getRepository('Gedmo\Loggable\Entity\HistoryLog');
-
         $comment = new Comment();
         $comment->setTitle('My Title');
 
         $this->em->persist($comment);
         $this->em->flush();
-
-        $this->assertEquals(1, count($repo->findAll()));
+        $this->assertEquals(1, count($this->getLogs()));
         $this->clearLogs();
-
+        
         $this->em->remove($comment);
         $this->em->flush();
+        $this->assertEquals(0, count($this->getLogs()));
+    }
 
-        $this->assertEquals(0, count($repo->findAll()));
+    private function getLogs()
+    {
+        return $this->em->createQueryBuilder()
+            ->select('log')
+            ->from('Gedmo\Loggable\Entity\HistoryLog', 'log')
+            ->getQuery()
+            ->execute(array())
+        ;
     }
 
     private function clearLogs()
     {
-        $meta = $this->em->getClassMetadata('Gedmo\Loggable\Entity\HistoryLog');
-        $this->em->getConnection()->delete($meta->getTableName(), array('object' => 'My Title'));
+        $this->em->createQueryBuilder()
+            ->delete('Gedmo\Loggable\Entity\HistoryLog', 'log')
+            ->getQuery()
+            ->execute()
+        ;
     }
 }