Created
May 27, 2019 12:22
-
-
Save mcustiel/bb2d1ba2f967c43573e9ace8eee4bbd6 to your computer and use it in GitHub Desktop.
PHPUnit listener for to run migrations on symfony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Tests\Repository\PHPUnit; | |
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand; | |
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; | |
use Doctrine\Bundle\MigrationsBundle\Command\MigrationsMigrateDoctrineCommand; | |
use Exception; | |
use PHPUnit_Framework_BaseTestListener; | |
use PHPUnit_Framework_Test; | |
use PHPUnit_Framework_TestSuite; | |
use Symfony\Bundle\FrameworkBundle\Console\Application; | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Symfony\Component\Console\Output\ConsoleOutput; | |
class IntegrationDbSuitesListener extends PHPUnit_Framework_BaseTestListener | |
{ | |
/** | |
* @var bool | |
*/ | |
private $dbCreated = false; | |
/** | |
* An error occurred. | |
* | |
* @param PHPUnit_Framework_Test $test | |
* @param Exception $e | |
* @param float $time | |
*/ | |
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) | |
{ | |
if ($this->dbCreated) { | |
$this->dropDatabase(); | |
SymfonyKernelHelper::ensureKernelShutdown(); | |
} | |
} | |
/** | |
* A test suite started. | |
* | |
* @param PHPUnit_Framework_TestSuite $suite | |
*/ | |
public function startTestSuite(PHPUnit_Framework_TestSuite $suite) | |
{ | |
if ($suite->getName() === 'Project Integration Test Suite') { | |
SymfonyKernelHelper::bootKernel(); | |
$this->createDatabase(); | |
$this->createSchema(); | |
} | |
} | |
/** | |
* A test suite ended. | |
* | |
* @param PHPUnit_Framework_TestSuite $suite | |
*/ | |
public function endTestSuite(PHPUnit_Framework_TestSuite $suite) | |
{ | |
if ($suite->getName() === 'Project Integration Test Suite') { | |
$this->dropDatabase(); | |
SymfonyKernelHelper::ensureKernelShutdown(); | |
} | |
} | |
private function createSchema() | |
{ | |
$application = new Application(SymfonyKernelHelper::getKernel()); | |
$command = new MigrationsMigrateDoctrineCommand(); | |
$application->add($command); | |
$input = new ArrayInput([ | |
'--quiet' => true, | |
]); | |
$input->setInteractive(false); | |
$command->run($input, new ConsoleOutput()); | |
} | |
private function createDatabase() | |
{ | |
$application = new Application(SymfonyKernelHelper::getKernel()); | |
$command = new CreateDatabaseDoctrineCommand(); | |
$application->add($command); | |
$input = new ArrayInput([ | |
'--quiet' => true, | |
]); | |
$input->setInteractive(false); | |
$command->run($input, new ConsoleOutput()); | |
$this->dbCreated = true; | |
} | |
private function dropDatabase() | |
{ | |
$application = new Application(SymfonyKernelHelper::getKernel()); | |
$command = new DropDatabaseDoctrineCommand(); | |
$application->add($command); | |
$input = new ArrayInput([ | |
'--force' => true, | |
'--quiet' => true, | |
]); | |
$input->setInteractive(false); | |
$command->run($input, new ConsoleOutput()); | |
$this->dbCreated = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment