禁用软删除过滤器以进行硬删除记录不起作用

时间:2014-04-02 13:15:21

标签: symfony testing doctrine-orm phpunit functional-testing

我正在尝试在功能测试期间禁用“Soft Deleteable”过滤器,我按照以下步骤进行操作:

第一个选项:在我的测试中尝试在tearDown()处停用:

protected function tearDown() {
    parent::tearDown();

    $entityUser = $this->em->getRepository("UserSecurityBundle:User")->find($this->user->getUser()->getId());

    $filter = $this->em->getFilters()->disable('softdeleteable');

    $this->em->remove($entityUser);
    $this->em->flush();
    $this->em->close();
}

无效。

第二个选项:制作一个doctrine_test.yml并在config_test.yml中导入:

imports:
    - { resource: config.yml }
    - { resource: doctrine_test.yml }
    - { resource: security_test.yml }

在这种情况下,我删除doctrine.yml并加入config_prod.yml

无效。

这就是我的doctrine_test.yml部分的样子:

filters:
    softdeleteable:
        class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
        enabled: false

第三个选项:在setUp()中停用过滤器:

public function setUp() {
    static::$kernel = static::createKernel();
    static::$kernel->boot();
    $this->em = static::$kernel->getContainer()->get('doctrine')->getManager();

    $fixture = new LoadFeeData();
    $fixture->load($this->em);

    $this->em->getFilters()->disable('softdeleteable');
    $this->user = $this->createUser();

    parent::setUp();
}

无效。

有什么建议吗?溶液

2 个答案:

答案 0 :(得分:6)

经过一些研究,经过更多的测试,我找到了解决方案,见下文:

protected function tearDown() {
    parent::tearDown();

    $entityAccount = $this->em->getRepository("UserSecurityBundle:Account")->find(array("id" => $this->user->getId(), "user" => $this->user->getUser()->getId()));
    $entityUser = $entityAccount->getUser();

    $entityCompanyHasWtax = $this->em->getRepository("ApprovalBundle:CompanyHasWtax")->findOneBy(array("company" => $this->company, "wtax" => $this->fee, "user" => $this->user->getUser()->getId()));

    foreach ($this->em->getEventManager()->getListeners() as $eventName => $listeners) {
        foreach ($listeners as $listener) {
            if ($listener instanceof \Gedmo\SoftDeleteable\SoftDeleteableListener) {
                $this->em->getEventManager()->removeEventListener($eventName, $listener);
            }
        }
    }

    $this->em->remove($entityCompanyHasWtax);
    $this->em->remove($entityAccount);
    $this->em->remove($entityUser);

    $this->em->flush();
    $this->em->close();
}

Aparently Doctrine有一个错误,因为以这种方式禁用过滤器:

$this->em->getFilters()->disable('softdeleteable');

不工作,善待他人

答案 1 :(得分:-1)

虽然这个问题有点旧,但对某人有用:

创建自己的事件监听器可能是更好的解决方案:

class SoftDeleteableListener extends BaseSoftDeleteableListener
{

/**
 * @inheritdoc
 */
public function onFlush(EventArgs $args)
{
    $ea = $this->getEventAdapter($args);
    $om = $ea->getObjectManager();
    //return from event listener if you disabled filter: $em->getFilters()->disable('softdeleteable');
    if (!$om->getFilters()->isEnabled('softdeleteable')) {
        return;
    }

    parent::onFlush($args);
}

}

并添加您的配置:

gedmo.listener.softdeleteable:
    class: AppBundle\EventListener\SoftDeleteableListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ @annotation_reader ] ]

来源:https://github.com/Atlantic18/DoctrineExtensions/issues/1175