TearDown database after a phpUnitTest on a WebTestCase

125
July 21, 2018, at 05:00 AM

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?

READ ALSO
Adding y plots to a matlib using input on python

Adding y plots to a matlib using input on python

I've spent 18 hours on this so I think it's time I ask the expertsI've only started to learn python so I can't use indexes or anything yet, just arrays and lists

139
How do you access nested json data? [duplicate]

How do you access nested json data? [duplicate]

This question already has an answer here:

157
Iterating through class names and hiding some in vanilla js

Iterating through class names and hiding some in vanilla js

Stumped and I am sure there is an easy explanation to this

140
Login with facebook crash the app if using FaceBook Lite

Login with facebook crash the app if using FaceBook Lite

In my app I'm using facebook SDK to perform a login and register methods

129