TearDown database after a phpUnitTest on a WebTestCase using DataFixtures

天涯浪子 提交于 2019-12-08 04:21:40

问题


I have the following phpUnit functional Test:

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

As you can see I load the following Fixture:

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');
  }

}

But I want to be able after my database changes happen to be able to tear down completely the database and recreate it for the next test. Do you have ant idea how to colpletely tear down the database?


回答1:


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
}


来源:https://stackoverflow.com/questions/51449639/teardown-database-after-a-phpunittest-on-a-webtestcase-using-datafixtures

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