TearDown database after a phpUnitTest on a WebTestCase using DataFixtures

一笑奈何 提交于 2019-12-08 04:25:32

As you are using Doctrine DataFixtures, you can implement this logic in your test:

<?php
namespace Tests\AppBundle\Controller;

Use Doctrine\Common\DataFixtures\Purger\ORMPurger;


/**
 * @testtype Functional
 */
class DefaultControllerTest extends BasicHttpController
{
    private $em;

    /**
     * {@inheritdoc}
     */
    public function setUp()
    {
        $client = static::createClient();
        $container = $client->getContainer();
        $doctrine = $container->get('doctrine');
        $this->em = $doctrine->getManager();

        $fixture = new YourFixture();
        $fixture->load($entityManager);
    }

    private function truncateEntities()
    {
        $purger = new ORMPurger($this->em);
        $purger->purge();
    }

    public function tearDown()
    {
        $this->truncateEntities(); 
    }

    // your tests
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!