Skip to content

Commit

Permalink
[WebBundle] added some black magic to remove the boiler plate code ne…
Browse files Browse the repository at this point in the history
…eded in end user functional tests
  • Loading branch information
fabpot committed Jun 10, 2010
1 parent e3c2e40 commit defa307
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Foundation/Test/WebTestCase.php
Expand Up @@ -40,7 +40,7 @@ public function createClient(array $server = array())
/**
* Creates a Kernel.
*
* @return Symfony\Foundation\Kernel A Kernel instance
* @return Symfony\Components\HttpKernel\HttpKernelInterface A HttpKernelInterface instance
*/
abstract protected function createKernel();
}
62 changes: 62 additions & 0 deletions src/Symfony/Framework/WebBundle/Test/WebTestCase.php
@@ -0,0 +1,62 @@
<?php

namespace Symfony\Framework\WebBundle\Test;

use Symfony\Foundation\Test\WebTestCase as BaseWebTestCase;
use Symfony\Components\Finder\Finder;

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* WebTestCase is the base class for functional tests.
*
* @package Symfony
* @subpackage Framework_WebBundle
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
abstract class WebTestCase extends BaseWebTestCase
{
/**
* Creates a Kernel.
*
* If you run tests with the PHPUnit CLI tool, everything will work as expected.
* If not, override this method in your test classes.
*
* @return Symfony\Components\HttpKernel\HttpKernelInterface A HttpKernelInterface instance
*/
protected function createKernel()
{
// black magic below, you have been warned!
$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.'/'.dirname($matches[1]);
}

$finder = new Finder();
$finder->name('*Kernel.php')->in($dir);
if (!count($finder)) {
throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
}

$file = current(iterator_to_array($finder));
$class = $file->getBasename('.php');
unset($finder);

require_once $file;

return new $class('test', true);
}
}

0 comments on commit defa307

Please sign in to comment.