小编Rom*_*lef的帖子

如何在没有任何实体的情况下测试学说 EventListener/Subscriber

我创建了一个 AuditLoggerBundle*,它有一个使用 Doctrine 事件(prePersist、preUpdate 和 preRemove)的服务,以便在 audit_log 表(AuditLog 实体)中创建一个新条目。

该捆绑包与我的其他捆绑包配合良好,但我想对其进行单元测试并对其进行功能测试

问题是,为了对函数进行功能测试AuditLoggerListener,我需要至少有两个可以持久化、更新等的“假”实体。

在这个包中,我不知道如何做到这一点,因为我只有一个 AuditLog 实体,我需要使用两个实体(仅在测试中使用)。

  1. 第一个实体将是“可审计的”(如果我对这个实体进行持久化、更新或删除,我必须在 audit_log 中有一个新条目)。
  2. 第二个将是“不可审计的”(当我对此实体执行持久化、更新或删除时,我不能在 audit_log 表中有新条目)。*
  3. 这两个实体可以与唯一的 EntityClass 相关,但不能是 AuditLog 的实例

这就是我看到持久功能测试的方式:

<?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
Run Code Online (Sandbox Code Playgroud)

所以我的问题是我的包中没有任何 Animal 实体,我只需要这个来测试包,所以它必须只在测试数据库中创建,而不是在生产环境中创建(当我执行 app/console …

php event-listener subscriber symfony doctrine-orm

4
推荐指数
1
解决办法
4773
查看次数

标签 统计

doctrine-orm ×1

event-listener ×1

php ×1

subscriber ×1

symfony ×1