Skip to content

Instantly share code, notes, and snippets.

@lsmith77
Created December 25, 2010 13:31
Show Gist options
  • Save lsmith77/754879 to your computer and use it in GitHub Desktop.
Save lsmith77/754879 to your computer and use it in GitHub Desktop.
<?php
namespace Application\FooBundle\Test;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
class WebTestCase extends \Symfony\Bundle\FrameworkBundle\Test\WebTestCase
{
/**
* Avoid the issues with
*
* DOMDocument::loadHTML(): Namespace prefix fb is not defined in Entity
*
* when running on Debian machines (likely a libxml 2.6.x issue).
*
* It only sets libxml to use internal errors.
*
*/
public function __construct()
{
libxml_use_internal_errors(true);
}
/**
* Override the original createKernel method to accommodate the directory
* additional level in the app directory:
* app/main/MainKernel.php
* app/mobile/MobileKernel.php
* etc.
*
* @see Symfony\Bundle\FrameworkBundle\Test\WebTestCase
* @param array $options
* @return object
*/
protected function createKernel(array $options = array())
{
$dir = getcwd();
if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
}
// find the --configuration flag from PHPUnit
$cli = implode(' ', $_SERVER['argv']);
if (preg_match('/\-\-configuration[= ]+([^ ]+)/', $cli, $matches)) {
$dir = $dir.'/'.$matches[1];
} elseif (preg_match('/\-c +([^ ]+)/', $cli, $matches)) {
$dir = $dir.'/'.$matches[1];
} else {
throw new \RuntimeException('Unable to guess the Kernel directory.');
}
if (!is_dir($dir)) {
$dir = dirname($dir);
}
$appname = explode('\\', get_class($this));
$appname = $appname[1];
$class = $appname.'Kernel';
$file = $dir.'/'.strtolower($appname).'/'.$class.'.php';
require_once $file;
return new $class(
isset($options['environment']) ? $options['environment'] : 'test',
isset($options['debug']) ? $options['debug'] : true
);
}
protected function loadFixtures($classnames)
{
$kernel = $this->createKernel(array('environment' => 'test'));
$kernel->boot();
$em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
$connection = $em->getConnection();
if ($connection->getDriver() instanceOf \Doctrine\DBAL\Driver\PDOSqlite\Driver) {
$params = $connection->getParams();
$params['driver'] = 'pdo_sqlite';
$name = isset($params['path']) ? $params['path'] : $params['dbname'];
unset($params['dbname']);
$tmpConnection = \Doctrine\DBAL\DriverManager::getConnection($params);
$tmpConnection->getSchemaManager()->dropDatabase($name);
$metadatas = $em->getMetadataFactory()->getAllMetadata();
if (!empty($metadatas)) {
$schemaTool = new \Doctrine\ORM\Tools\SchemaTool($em);
$schemaTool->createSchema($metadatas);
}
$purger = null;
$append = true;
} else {
$purger = new ORMPurger();
$append = false;
}
$executor = new ORMExecutor($em, $purger);
$classnames = (array)$classnames;
foreach ($classnames as $classname) {
$namespace = explode('\\', $classname);
require_once $kernel->registerRootDir().'/tests/Fixtures/'.array_pop($namespace).'.php';
$loader = new Loader();
$loader->addFixture(new $classname());
$executor->execute($loader->getFixtures(), $append);
$append = true;
}
$connection->close();
}
protected function makeClient($authenticated = false)
{
$params = array();
if ($authenticated) {
$params = array('PHP_AUTH_USER' => 'test@liip.ch', 'PHP_AUTH_PW' => 'testtest12');
}
return $this->createClient(array('environment' => 'test'), $params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment