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