4

I created an AuditLoggerBundle* which has a service that uses Doctrine Events (prePersist, preUpdate and preRemove) in order to create a new entry in an audit_log table (AuditLog Entity).

The bundle works fine with my other bundles but I would like to unit test it and functional test it.

The problem is that, in order to make functional tests on the AuditLoggerListener functions, I need to have at least two "fake" entities that I can persist, update etc.

In this bundle I don't know how to do this because I just have an AuditLog entity and I need to use two over entities (that will be only used in tests).

  1. The first entity will be "auditable" (I must have a new entry in audit_log if I do a persist, update or remove on this entity).
  2. The second one will be "non-auditable" (I must not have a new entry in audit_log table when I perform a persist, update or remove on this entity).*
  3. The two entities can be related to a unique EntityClass but must not be an instance of AuditLog

This is how I'm seeing the persist functional test:

<?php
$animal = new Animal(); //this is a fake Auditable entity
$animal->setName('toto');
$em = new EntityManager(); //actually I will use the container to get this manager
$em->persist($animal);
$em->flush();

//Here we test that I have a new line in audit_log table with the right informations

So my problem is that I don't have any Animal entity in my bundle and I only need this one to test the bundle, so it must be created only in the test database and not in the production environment (when I do an app/console doctrine:schema:update --force

EDIT_1: After reading your answers, Unit Tests on AuditLoggerListener functions are going to be performed but I still want to make functional tests

*yes I know there are plenty of them, but they don't feet with what I am looking for.

Thank you for your answers and I hope it will help some people !

EDIT_2: here is the code Service:

services:
    #add a prefix to the auditLogger table
    kali_audit_logger.doctrine.table.prefix:
        class: Kali\AuditLoggerBundle\EventListener\TablePrefixListener
        arguments: [%application.db.table.prefix%]
        tags:
            - { name: doctrine.event_listener, event: loadClassMetadata }

    #audit all doctrine actions made by a user
    kali_audit_logger.doctrine.event.logger:
        class: Kali\AuditLoggerBundle\EventListener\AuditLoggerListener
        arguments: [@kali_audit_log, @jms_serializer.serializer, @security.token_storage, %application.auditable.entities%, %application.non.auditable.entities%]
        tags:
            - { name: doctrine.event_listener, event: prePersist }
            - { name: doctrine.event_listener, event: preUpdate }
            - { name: doctrine.event_listener, event: preRemove }

    # new AuditLog
    kali_audit_log:
        class: Kali\AuditLoggerBundle\Entity\AuditLog

Listener:

namespace Kali\AuditLoggerBundle\EventListener;

use DateTime;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use JMS\Serializer\SerializerInterface;
use Kali\AuditLoggerBundle\Entity\AuditLog;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Serializer\Encoder\JsonEncoder;

/**
 * Class AuditLoggerListener
 * insert a new entry in audit_log table for every doctrine event
 *
 * @package Kali\AuditLoggerBundle\EventListener
 */
class AuditLoggerListener
{
    /**
     * @var TokenStorage
     */
    protected $securityToken;

    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * @var array
     */
    protected $auditableEntities;

    /**
     * @var array
     */
    protected $nonAuditableEntities  = ['Kali\AuditLoggerBundle\Entity\AuditLog'];

    /**
     * @var AuditLog
     */
    protected $auditLogger;

    /**
     * @var SerializerInterface
     */
    protected $serializer;

    /**
     * @param AuditLog $auditLogger
     * @param SerializerInterface $serializer
     * @param TokenStorage $securityToken
     * @param array $auditableEntities
     * @param array $nonAuditableEntities
     */
    public function __construct(
        AuditLog $auditLogger,
        SerializerInterface $serializer,
        TokenStorage $securityToken,
        $auditableEntities = [],
        $nonAuditableEntities = []
    ) {
        $this->auditLogger          =   $auditLogger;
        $this->serializer           =   $serializer;
        $this->securityToken        =   $securityToken;
        $this->auditableEntities    =   $auditableEntities;
        //add all non auditable entities to the current array of non auditable entities
        array_merge($this->nonAuditableEntities, $nonAuditableEntities);
    }

    /**
     *
     * @param LifecycleEventArgs $args
     *
     * @return boolean
     */
    public function prePersist(LifecycleEventArgs $args)
    {
        $this->em   =   $args->getEntityManager();
        $entity     =   $args->getEntity();

        $this->em
            ->getEventManager()
            ->removeEventListener('prePersist', $this);

        if ($this->isAuditableEntity($entity)) {
            $this->addAudit(
                $this->securityToken->getToken()->getUsername(),
                "INSERT",
                get_class($entity),
                $this->serializer->serialize($entity, JsonEncoder::FORMAT)
            );
        }

        return true;
    }

    /**
     *
     * @param PreUpdateEventArgs $args
     *
     * @return boolean
     */
    public function preUpdate(PreUpdateEventArgs $args)
    {
        $this->em   =   $args->getEntityManager();
        $entity     =   $args->getEntity();

        $this->em
            ->getEventManager()
            ->removeEventListener('preUpdate', $this);

        if ($this->isAuditableEntity($entity)) {
            $this->addAudit(
                $this->securityToken->getToken()->getUsername(),
                "UPDATE",
                get_class($entity),
                $this->serializer->serialize($entity, JsonEncoder::FORMAT),
                $this->serializer->serialize($args->getEntityChangeSet(), JsonEncoder::FORMAT)
            );
        }

        return true;
    }

    /**
     *
     * @param LifecycleEventArgs $args
     *
     * @return boolean
     */
    public function preRemove(LifecycleEventArgs $args)
    {
        $this->em   =   $args->getEntityManager();
        $entity     =   $args->getEntity();

        $this->em
            ->getEventManager()
            ->removeEventListener('preRemove', $this);

        if ($this->isAuditableEntity($entity)) {
            $this->addAudit(
                $this->securityToken->getToken()->getUsername(),
                "REMOVE",
                get_class($entity),
                $this->serializer->serialize($entity, JsonEncoder::FORMAT)
            );
        }

        return true;
    }

    /**
     * Insert a new line in audit_log table
     *
     * @param string      $user
     * @param string      $action
     * @param string      $entityClass
     * @param null|string $entityValue
     * @param null|string $entityChange
     *
     * @return void
     */
    private function addAudit($user, $action, $entityClass, $entityValue = null, $entityChange = null)
    {
        if ($this->auditLogger) {
            $this->auditLogger
                ->setUser($user)
                ->setAction($action)
                ->setEntityClass($entityClass)
                ->setEntityValue($entityValue)
                ->setEntityChange($entityChange)
                ->setDate(new DateTime());
        }

        if ($this->em) {
            $this->em->persist($this->auditLogger);
            $this->em->flush();
        }
    }

    /**
     * check if an entity is auditable
     *
     * @param $entity
     *
     * @return bool
     */
    private function isAuditableEntity($entity)
    {
        $auditable = false;

        //the entity must not be in the non auditable entity array
        if (!in_array(get_class($entity), $this->nonAuditableEntities)
            && (empty($this->auditableEntities) || (!empty($this->auditableEntities) && in_array(get_class($entity), $this->auditableEntities)))
        ) {
            $auditable = true;
        }

        return $auditable;
    }
}

I want to test the preXXXX functions of this listener ... So, for example, I need to test if when I do a persist on a fake entity (which I don't really know how to mock), there is a new entry in my audit_log table ...

Romain Masclef
  • 121
  • 1
  • 8
  • If you want to see a new entry in your database, this is not a Unit Test, it would be a functionnal test for which you can use frameworks like SF2 WebTestCase http://symfony.com/doc/current/book/testing.html#functional-tests or behat http://docs.behat.org/en/v2.5/. A Unit test will just check the IO of your class, there would be no database interaction, you would juste check that the EntityManager method is called and it should be enought as Doctrine EntityManager is also unit tested an trustable. – olaurendeau Sep 17 '15 at 07:29
  • OK I understand the point, I can make some unit tests on my function to make sure that some functions are actually called (such as persist, flush, getEntity, getEntityManager etc ...). So now i want to make a functional test for this listener. I have done plenty of functional tests on other projects but they are all using HMI or real entities that I can use (no mock). – Romain Masclef Sep 17 '15 at 07:47
  • In this bundle I don't know how to do this because I just have an AuditLog entity and I need to use two over entities (that will be only used in tests). The first entity will be "auditable" (I must have a new entry in audit_log if I do a persist, update or remove on this entity). The second on will be "non-auditable" (I must not have a new entry in audit_log table when I perform a persist, update or remove on this entity). – Romain Masclef Sep 17 '15 at 07:47
  • For a functionnal test you still can use mocks, but in your case I assume the easiest way should be to create a Dummy Entity that you could play with in your tests. – olaurendeau Sep 17 '15 at 08:05
  • What do you mean by "Dummy Entity" ? – Romain Masclef Sep 17 '15 at 08:07
  • I just manage to understand something, did you plan to share this bundle between various application ? And do you want to test it by itself as a library ? Something like that : https://github.com/schmittjoh/JMSSerializerBundle ? – olaurendeau Sep 17 '15 at 08:24
  • Yep, that is exactly what I am doing, the AuditLoggerBundle is used in two over projects. Is there a difference when testing as a library ? When I look at other bundles tests ... and don't understand how they manage to test their whole bundle. (As you can see in the JMSSerializer sources ...). – Romain Masclef Sep 17 '15 at 08:57
  • Yes that make a difference, it's almost not possible to do functional tests on a shared bundle, because you can't rely on the Symfony2 distribution. I think in this case the best thing to do is properly Unit Test your bundle. – olaurendeau Sep 17 '15 at 09:08
  • OK, I am doing this, I will post my code as an answer as everyone can have an example and tell me if I correctly used mocks ... – Romain Masclef Sep 17 '15 at 09:25

2 Answers2

6

it's almost not possible to do functional tests on a shared bundle, because you can't rely on the Symfony2 distribution. I think in this case the best thing to do is properly Unit Test your bundle. – olaurendeau

Here is the test class related to the listener (100% coverage on the class) :

<?php

namespace Kali\AuditLoggerBundle\Tests\Controller;

use Kali\AuditLoggerBundle\Entity\AuditLog;
use Kali\AuditLoggerBundle\EventListener\AuditLoggerListener;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
 * Class AuditLoggerListenerTest
 * @package Kali\AuditLoggerBundle\Tests\Controller
 */
class AuditLoggerListenerTest extends WebTestCase
{
    protected static $container;

    /**
     * This method is called before the first test of this test class is run.
     *
     * @since Method available since Release 3.4.0
     */
    public static function setUpBeforeClass()
    {
        self::$container = static::createClient()->getContainer();
    }

/*
 * ===========================================================================
 * TESTS ON AUDITABLE ENTITIES
 * ===========================================================================
 */
    /**
     * test prepersist function
     */
    public function testPrePersistWithAuditableEntity()
    {
        //Mock all the needed objects
        $token          =   $this->mockToken();
        $tokenStorage   =   $this->mockTokenStorage();
        $eventManager   =   $this->mockEventManager();
        $entityManager  =   $this->mockEntityManager();
        $entity         =   $this->mockEntity();
        $lifeCycleEvent =   $this->mockEvent('LifecycleEventArgs');

        //assert the methods that must be called or not
        $token          ->  expects($this->once())->method('getUsername');
        $tokenStorage   ->  expects($this->once())->method('getToken')->willReturn($token);
        $eventManager   ->  expects($this->once())->method('removeEventListener');
        $entityManager  ->  expects($this->once())->method('getEventManager')->willReturn($eventManager);
        $entityManager  ->  expects($this->once())->method('persist');
        $lifeCycleEvent ->  expects($this->never())->method('getEntityChangeSet');
        $lifeCycleEvent ->  expects($this->once())->method('getEntityManager')->willReturn($entityManager);
        $lifeCycleEvent ->  expects($this->once())->method('getEntity')->willReturn($entity);

        //instanciate the listener
        $listener = new AuditLoggerListener(
            new AuditLog(),
            self::$container->get('jms_serializer'),//Yes this is not really good to do that
            $tokenStorage
        );
        // call the function to test
        $listener->prePersist($lifeCycleEvent);
    }

    /**
     * test preUpdate function
     */
    public function testPreUpdateWithAuditableEntity()
    {
        //Mock all the needed objects
        $token          =   $this->mockToken();
        $tokenStorage   =   $this->mockTokenStorage();
        $eventManager   =   $this->mockEventManager();
        $entityManager  =   $this->mockEntityManager();
        $entity         =   $this->mockEntity();
        $lifeCycleEvent =   $this->mockEvent('PreUpdateEventArgs');

        //assert the methods that must be called or not
        $token          ->  expects($this->once())->method('getUsername');
        $tokenStorage   ->  expects($this->once())->method('getToken')->willReturn($token);
        $eventManager   ->  expects($this->once())->method('removeEventListener');
        $entityManager  ->  expects($this->once())->method('getEventManager')->willReturn($eventManager);
        $entityManager  ->  expects($this->once())->method('persist');
        $lifeCycleEvent ->  expects($this->once())->method('getEntityChangeSet');
        $lifeCycleEvent ->  expects($this->once())->method('getEntityManager')->willReturn($entityManager);
        $lifeCycleEvent ->  expects($this->once())->method('getEntity')->willReturn($entity);

        //instanciate the listener
        $listener = new AuditLoggerListener(
            new AuditLog(),
            self::$container->get('jms_serializer'),//Yes this is not really good to do that
            $tokenStorage
        );
        // call the function to test
        $listener->preUpdate($lifeCycleEvent);
    }

    /**
     * test PreRemove function
     */
    public function testPreRemoveWithAuditableEntity()
    {
        //Mock all the needed objects
        $token          =   $this->mockToken();
        $tokenStorage   =   $this->mockTokenStorage();
        $eventManager   =   $this->mockEventManager();
        $entityManager  =   $this->mockEntityManager();
        $entity         =   $this->mockEntity();
        $lifeCycleEvent =   $this->mockEvent('LifecycleEventArgs');

        //assert the methods that must be called or not
        $token          ->  expects($this->once())->method('getUsername');
        $tokenStorage   ->  expects($this->once())->method('getToken')->willReturn($token);
        $eventManager   ->  expects($this->once())->method('removeEventListener');
        $entityManager  ->  expects($this->once())->method('getEventManager')->willReturn($eventManager);
        $entityManager  ->  expects($this->once())->method('persist');
        $lifeCycleEvent ->  expects($this->never())->method('getEntityChangeSet');
        $lifeCycleEvent ->  expects($this->once())->method('getEntityManager')->willReturn($entityManager);
        $lifeCycleEvent ->  expects($this->once())->method('getEntity')->willReturn($entity);

        //instanciate the listener
        $listener = new AuditLoggerListener(
            new AuditLog(),
            self::$container->get('jms_serializer'),//Yes this is not really good to do that
            $tokenStorage
        );
        // call the function to test
        $listener->preRemove($lifeCycleEvent);
    }

/*
 * ===========================================================================
 * TESTS ON NON AUDITABLE ENTITIES
 * ===========================================================================
 */
    /**
     * test prepersit function
     */
    public function testPrePersistWithNonAuditableEntity()
    {
        //Mock all the needed objects
        $token          =   $this->mockToken();
        $tokenStorage   =   $this->mockTokenStorage();
        $eventManager   =   $this->mockEventManager();
        $entityManager  =   $this->mockEntityManager();
        $entity         =   new AuditLog();//this entity is non Auditable
        $lifeCycleEvent =   $this->mockEvent('LifecycleEventArgs');

        //assert the methods that must be called or not
        $token          ->  expects($this->never())->method('getUsername');
        $tokenStorage   ->  expects($this->never())->method('getToken')->willReturn($token);
        $eventManager   ->  expects($this->once())->method("removeEventListener");
        $entityManager  ->  expects($this->never())->method('persist');
        $entityManager  ->  expects($this->once())->method('getEventManager')->willReturn($eventManager);
        $lifeCycleEvent ->  expects($this->never())->method('getEntityChangeSet');
        $lifeCycleEvent ->  expects($this->once())->method('getEntityManager')->willReturn($entityManager);
        $lifeCycleEvent ->  expects($this->once())->method('getEntity')->willReturn($entity);

        $listener = new AuditLoggerListener(
            new AuditLog(),
            self::$container->get('jms_serializer'),
            $tokenStorage
        );

        $listener->prePersist($lifeCycleEvent);
    }

    /**
     * test prepersit function
     */
    public function testPreUpdateWithNonAuditableEntity()
    {
        //Mock all the needed objects
        $token          =   $this->mockToken();
        $tokenStorage   =   $this->mockTokenStorage();
        $eventManager   =   $this->mockEventManager();
        $entityManager  =   $this->mockEntityManager();
        $entity         =   new AuditLog();//this entity is non Auditable
        $lifeCycleEvent =   $this->mockEvent('PreUpdateEventArgs');

        //assert the methods that must be called or not
        $token          ->  expects($this->never())->method('getUsername');
        $tokenStorage   ->  expects($this->never())->method('getToken')->willReturn($token);
        $eventManager   ->  expects($this->once())->method("removeEventListener");
        $entityManager  ->  expects($this->never())->method('persist');
        $entityManager  ->  expects($this->once())->method('getEventManager')->willReturn($eventManager);
        $lifeCycleEvent ->  expects($this->never())->method('getEntityChangeSet');
        $lifeCycleEvent ->  expects($this->once())->method('getEntityManager')->willReturn($entityManager);
        $lifeCycleEvent ->  expects($this->once())->method('getEntity')->willReturn($entity);

        $listener = new AuditLoggerListener(
            new AuditLog(),
            self::$container->get('jms_serializer'),
            $tokenStorage
        );

        $listener->preUpdate($lifeCycleEvent);
    }

    /**
     * test preRemove function
     */
    public function testPreRemoveWithNonAuditableEntity()
    {
        //Mock all the needed objects
        $token          =   $this->mockToken();
        $tokenStorage   =   $this->mockTokenStorage();
        $eventManager   =   $this->mockEventManager();
        $entityManager  =   $this->mockEntityManager();
        $entity         =   new AuditLog();//this entity is non Auditable
        $lifeCycleEvent =   $this->mockEvent('LifecycleEventArgs');

        //assert the methods that must be called or not
        $token          ->  expects($this->never())->method('getUsername');
        $tokenStorage   ->  expects($this->never())->method('getToken')->willReturn($token);
        $eventManager   ->  expects($this->once())->method("removeEventListener");
        $entityManager  ->  expects($this->never())->method('persist');
        $entityManager  ->  expects($this->once())->method('getEventManager')->willReturn($eventManager);
        $lifeCycleEvent ->  expects($this->never())->method('getEntityChangeSet');
        $lifeCycleEvent ->  expects($this->once())->method('getEntityManager')->willReturn($entityManager);
        $lifeCycleEvent ->  expects($this->once())->method('getEntity')->willReturn($entity);

        $listener = new AuditLoggerListener(
            new AuditLog(),
            self::$container->get('jms_serializer'),
            $tokenStorage
        );

        $listener->preRemove($lifeCycleEvent);
    }

/*
 * ===========================================================================
 * MOCKS
 * ===========================================================================
 */

    /**
     * Mock a Token object
     *
     * @return \PHPUnit_Framework_MockObject_MockObject
     */
    private function mockToken()
    {
        $token = $this->getMock(
            'Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken',
            ['getUsername'],
            [],
            '',
            false
        );

        return $token;
    }

    /**
     * Mock a TokenStorage object
     *
     * @return \PHPUnit_Framework_MockObject_MockObject
     */
    private function mockTokenStorage()
    {
        //mock tokenStorage
        $tokenStorage = $this->getMock(
            'Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage',
            ['getToken'],
            [],
            '',
            false
        );

        return $tokenStorage;
    }

    /**
     * Mock an EventManager Object
     *
     * @return \PHPUnit_Framework_MockObject_MockObject
     */
    private function mockEventManager()
    {
        //mock the event manager
        $eventManager = $this->getMock(
            '\Doctrine\Common\EventManager',
            ['removeEventListener'],
            [],
            '',
            false
        );

        return $eventManager;
    }

    /**
     * Mock an EntityManager
     *
     * @return \PHPUnit_Framework_MockObject_MockObject
     */
    private function mockEntityManager()
    {
        //mock the entityManager
        $emMock = $this->getMock(
            '\Doctrine\ORM\EntityManager',
            ['getEventManager', 'persist', 'update', 'remove', 'flush'],
            [],
            '',
            false
        );

        return $emMock;
    }

    /**
     * Mock an Entity Object
     *
     * @return \PHPUnit_Framework_MockObject_MockObject
     */
    private function mockEntity()
    {
        $entity = $this->getMockBuilder('stdClass')
                       ->setMethods(['getName', 'getType'])
                       ->getMock();

        $entity->expects($this->any())
               ->method('getName')
               ->will($this->returnValue('toto'));
        $entity->expects($this->any())
               ->method('getType')
               ->will($this->returnValue('chien'));

        return $entity;
    }

    /**
     * mock a lifeCycleEventArgs Object
     *
     * @param $eventType
     *
     * @return \PHPUnit_Framework_MockObject_MockObject
     */
    private function mockEvent($eventType)
    {
        $lifeCycleEvent = $this->getMock(
            '\Doctrine\ORM\Event\\'.$eventType,
            ['getEntityManager', 'getEntity', 'getEntityChangeSet'],
            [],
            '',
            false
        );

        return $lifeCycleEvent;
    }
}

If you have anything to say about this, please, leave a comment :) (for example I can refactor "mock all needed objects" part into a function)

Romain Masclef
  • 121
  • 1
  • 8
4

Unit testing a php class means just testing the code contained by this class without any external interaction. So you should mock all external services : see phpunit mock documentation https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.mock-objects

For example if your class looks like that :

<?php
class AuditLogListener 
{
    ...
    function postPersist($event)
    {
        $animal = new Animal();
        $em = $event->getEm();
        $em->persist($animal);
    }
    ...
}

Your test should look like this :

<?php
class AuditLogListenerTest
{
    private $em;
    ...
    function testPostPersist()
    {
        $em = $this->getMockBuilder('stdClass')
                 ->setMethods(array('persist'))
                 ->getMock();

        $em->expects($this->once())
                 ->method('persist')
                 ->with($this->isInstanceOf('Animal'));

        $event = $this->getMockBuilder('stdClass')
                 ->setMethods(array('getEm'))
                 ->getMock();

        $event->expects($this->once())
                 ->method('getEm')
                 ->will($this->returnValue($em));

        $listener = new AuditLogListener();
        $listener->postPersist($event);
    }
    ...
}

There are more easy to use mock frameworks like prophecy (https://github.com/phpspec/prophecy) but they may need more time to handle them.

olaurendeau
  • 659
  • 4
  • 12
  • Thank you for your answer, i was not sure about this method ... So if I understand, I need to mock both the listener and the LifeCycleEvent object in order to test the response of the functions.I need to see what's inside a LifeCycleEvent ... Again, thank you for clarifying this. – Romain Masclef Sep 16 '15 at 14:19
  • It's quite easy to define. More or less you have to mock everything except the class you want to test. If you want to test the listener and verify that he use the object from the lifecycle event and do something with it, then don't mock your listener, mock the rest. In my example if you don't call the "persist" method the test will fail. – olaurendeau Sep 16 '15 at 14:26
  • I forgot something ... the entity manager actually needs to persist a new AuditLog entity having those informations : - username, - date, - action ('insert', 'update', 'remove') - entityClass (which here must be Animal ... this is kind of a problem) - entityValue (a json representation of the entity) - entityChange (if this is an update, it is equal to the getEntityChangeSet function of LifeCycleEventArgs, which I mocked) Maybe I should post my listener code and service.yml in order for you to understand ? – Romain Masclef Sep 16 '15 at 17:16
  • The chaining for building the $event variable is not quite correct. The chaining needs to end at getMock() so that the $event object is a mock. Then $event->expects()... This caught me up for a bit before I realized it needed to be broken out so that the call to postPersist($event) could work. – David Baucum Apr 28 '16 at 13:13
  • Indeed @DavidBaucum thanks for your comment, I've updated the answer – olaurendeau Apr 28 '16 at 14:52