使用 DataFixtures 在 WebTestCase 上执行 phpUnitTest 后拆卸数据库

TearDown database after a phpUnitTest on a WebTestCase using DataFixtures

我有以下 phpUnit 功能测试:

namespace Tests\AppBundle\Controller;

/**
* @testtype Functional
*/
class DefaultControllerTest extends BasicHttpController
{

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

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

    /**
    * {@inheritdoc}
    */
    public function tearDown()
    {
        //Database is being destroyed here....
    }

    public function testIndex()
    {
        $client = static::createClient();

        $crawler = $client->request('GET', '/');
        $response=$client->getResponse();
        $this->assertEquals(302, $response->getStatusCode());
        $this->assertEquals('/login',$response->headers->get('Location'));

        //@todo Create Dummy Users
        $this->checkPanelAfterSucessfullLogin($crawler); //How I can create some user?
    }
}

如您所见,我加载了以下夹具:

namespace AppBundle\DataFixtures\Test;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\Common\Persistence\ObjectManager;

class DummyUserFixtures extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{

  /**
  * @var ContainerInterface
  */
  private $container=null;

  /**
  * Generic function that creates a user with provided information.
  * @param $name {String} The user's name
  * @param $surname {String} The user's surname
  * @param $username {String} The user's username
  * @param $password {String} The user's password
  * @param $email {String} The user's recovery email
  * @param $role {String} The user's system role
  * @param $phone {String | null} The user's phone number
  * @param $organization {String|null} The user's organization
  * @param $occupation {String|null} The user's occupation
  *
  * @return AppBundle\Entity\User
  */
  private function createUser($name,$surname,$username,$password,$email,$role,$phone=null,$organization=null,$occupation=null)
  {
    $fosUserManager=$this->container->get('fos_user.user_manager');
      /**
      * @var AppBundle\Entity\User
      */
      $user=$fosUserManager->createUser();
      $user->setUsername($username);
      $user->setEmail($email);
      $user->setPlainPassword($password);
      $user->setEnabled(true);
      $user->setRoles(array($role));

      $user->setName($name);
      $user->setSurname($surname);

      if($phone){
        $user->setPhone($phone);
      }

      if($organization){
        $user->setOrganization($organization);
      }

      if($occupation){
        $user->serOccupation($occupation);
      }

      $fosUserManager->updateUser($user, true);

      return $user;
  }

  public function setContainer(ContainerInterface $container = null)
  {
    $this->container = $container;
  }

  /**
  * {@inheritDoc}
  */
  public function load(ObjectManager $manager)
  {
    $this->createUser('John','Doe','jdoe','simplepasswd','jdoe@example.com','ROLE_USER','+3021456742324','Acme Products','Soft Engineer');
    $this->createUser('Jackie','Chan','jchan','thesimplepasswd','jackiechan@example.com','ROLE_ADMIN','+302141232324','Holywood','Actor');
    $this->createUser('Chuck','Norris','chuck_norris','unhackablepasswd','chucknorris@example.com','ROLE_SUPERADMIN',null,'Universe','Master');
  }

}

但我希望能够在我的数据库发生更改后能够完全拆除数据库并为下一次测试重新创建它。你知道如何完全拆除数据库吗?

当你使用 Doctrine DataFixtures 时,你可以在你的测试中实现这个逻辑:

<?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
}