Symfony2 phpunit: how to delete and copy a database before running tests?

In my symfony2 application, I use phpunit to check that the response from each route is 200.

Before running the tests, I want to dump the test database and copy my production database under the test name (i.e. I want to reuse the test database).

I found out about public static function setUpBeforeClass()

but I lost how to drop and copy the database.

How can i do this?

My class:

<?php

namespace AppBundle\Tests\Controller;

use AppBundle\FoodMeUpParameters;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

require_once __DIR__.'/../../../../app/AppKernel.php';


class ApplicationAvailabilityFunctionalTest extends WebTestCase
{

    public static function setUpBeforeClass()
    {

    }

    /**
     * @dataProvider routeProvider
     * @param $route
     */
    public function testAllRoutesAreLoaded($route)
    {
        $listedRoutes = $this->getListedRoutes();

        $this->assertArrayHasKey($route, $listedRoutes);
    }
}

      

+3


source to share


2 answers


This is PHP code for backing up MySQL database. -routines includes your views, functions, stored proc, etc.

<?php
$schemaFile = "schema.sql";
$mysqlArgs  = "-u {$dbUser} -h {$dbHost} {$dbName}";

//if you don't have a pass and still pass in arg -p it will interrupt and prompt
if (isset($dbPassword) && strlen(trim($dbPassword)) > 0) { 
    $mysqlArgs .= " -p{$dbPassword}";
}

$mysqlDumpCommand = "mysqldump {$mysqlArgs} --routines";
$schemaDump       = "{$mysqlDumpCommand} > {$schemaFile}";
system($schemaDump);

      



Then the import code, assuming the same code $mysqlArgs

as above.

$mysqlImportCommand = "mysql {$mysqlArgs}";
$import             = "{$mysqlImportCommand} < {$schemaFile}";
system($import);

      

0


source


My 5c. Usually you don't need to use setUpBeforeClass

because it populates the DB for the whole package and therefore poses a risk of test dependency, while unit tests need to be independent. Use the setUp

and methods instead tearDown

. Now, saying: you said you just want to check that the answer is code 200. Why would you want to populate the entire DB for this in the first place when you can just mock the expected responses from your models?



0


source







All Articles