Created
October 25, 2015 16:11
-
-
Save fchris82/77aa5e5c063c2eccf9e9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Diginin\TestingBundle\Behat\Cli; | |
use Behat\Testwork\Cli\Controller; | |
use Symfony\Bundle\FrameworkBundle\Console\Application; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\ArrayInput; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\HttpKernel\Kernel; | |
/** | |
* Class DatabaseMigrationController | |
* | |
* Minden tesztfuttatás előtt aktualizálja a teszt adatbázist a migrációs fájlok futtatásával. | |
* | |
* @package Diginin\TestingBundle\Behat\Cli | |
*/ | |
class DatabaseMigrationController implements Controller | |
{ | |
/** | |
* @var Kernel | |
*/ | |
protected $kernel; | |
/** | |
* @var Application | |
*/ | |
protected $application; | |
/** | |
* @var OutputInterface | |
*/ | |
protected $output; | |
public function __construct(Kernel $kernel) | |
{ | |
$this->kernel = $kernel; | |
} | |
protected function getApplication() | |
{ | |
if (!$this->application) { | |
$this->application = new Application($this->kernel); | |
$this->application->setAutoExit(false); | |
} | |
return $this->application; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function configure(Command $command) | |
{ | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$this->output = $output; | |
try { | |
// Kapcsolódunk. | |
/** @type \Doctrine\DBAL\Connection $db */ | |
$db = $this->kernel->getContainer()->get('doctrine')->getManager()->getConnection(); | |
// Ellenőrizzük, hogy egyáltalán létezik-e az adatbázis | |
$db->connect(); | |
} catch (\PDOException $e) { | |
$this->createDatabase(); | |
} | |
// Minden futással frissítjük az adatbázist, már ha szükséges. | |
$this->databaseMigrate(); | |
// Lezárjuk az adatbázist és felszabadítunk memóriát | |
$db->close(); | |
} | |
protected function createDatabase() | |
{ | |
$this->runConsole('doctrine:database:drop', ['--force' => true]); | |
$this->runConsole('doctrine:database:create'); | |
} | |
protected function databaseMigrate() | |
{ | |
$this->runConsole('doctrine:migrations:migrate', ['--no-interaction' => true]); | |
} | |
/** | |
* @param string $command | |
* @param array $options | |
* | |
* @return int | |
* | |
* @throws \Exception | |
*/ | |
protected function runConsole($command, Array $options = []) | |
{ | |
$this->output->writeln(sprintf('<comment> > Command </comment> <info><fg=blue>%s</fg=blue></info>', $command)); | |
$options['-e'] = 'test'; | |
$options['-q'] = null; | |
$options = array_merge($options, ['command' => $command]); | |
return $this->getApplication()->run(new ArrayInput($options), $this->output); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment