Skip to content

Instantly share code, notes, and snippets.

@buxx
Forked from wowo/ModelTestCase.php
Created June 11, 2012 17:25
Show Gist options
  • Save buxx/2911431 to your computer and use it in GitHub Desktop.
Save buxx/2911431 to your computer and use it in GitHub Desktop.
Model Test Case, which loads fixtures and builds database before each test
<?php
/*
* Adapt to your AppKernel.php path
*/
require_once(__DIR__ . "../../../../../../app/AppKernel.php");
/**
* @see http://haulynjason.net/weblog/2012/01/fully-isolated-tests-in-symfony2/
*/
class ModelTestCase extends \PHPUnit_Framework_TestCase
{
protected $_kernel;
protected $_application;
protected $_container;
public function __construct()
{
$this->_kernel = new \AppKernel("test", true);
$this->_kernel->boot();
$this->_container = $this->_kernel->getContainer();
parent::__construct();
}
protected function get($service)
{
return $this->_container->get($service);
}
public function setUp()
{
$this->_application = new \Symfony\Bundle\FrameworkBundle\Console\Application($this->_kernel);
$this->_application->setAutoExit(false);
$this->runConsole("doctrine:schema:drop", array("--force" => true));
$this->runConsole("doctrine:schema:create");
$this->runConsole("cache:warmup");
$this->runConsole("doctrine:fixtures:load");
}
protected function runConsole($command, Array $options = array())
{
$options["-e"] = "test";
$options["-q"] = null;
$options = array_merge($options, array('command' => $command));
return $this->_application->run(new \Symfony\Component\Console\Input\ArrayInput($options));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment