Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10032

TearDown database after a phpUnitTest on a WebTestCase using DataFixtures

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','[email protected]','ROLE_USER','+3021456742324','Acme Products','Soft Engineer');
    $this->createUser('Jackie','Chan','jchan','thesimplepasswd','[email protected]','ROLE_ADMIN','+302141232324','Holywood','Actor');
    $this->createUser('Chuck','Norris','chuck_norris','unhackablepasswd','[email protected]','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?

Upvotes: 1

Views: 1637

Answers (1)

fgamess
fgamess

Reputation: 1324

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
}

Upvotes: 5

Related Questions