Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test doctrine EventListener/Subscriber without any entity

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 ...

like image 238
Romain Masclef Avatar asked Dec 25 '22 14:12

Romain Masclef


2 Answers

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)

like image 176
Romain Masclef Avatar answered Dec 27 '22 21:12

Romain Masclef


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.

like image 35
olaurendeau Avatar answered Dec 27 '22 20:12

olaurendeau