/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php
https://github.com/fernanDOTdo/symfony · PHP · 154 lines · 76 code · 21 blank · 57 comment · 8 complexity · aee27f458f4bd99f78e1bc1fa8782cd1 MD5 · raw file
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Bundle\FrameworkBundle\Test;
- use Symfony\Bundle\FrameworkBundle\Client;
- use Symfony\Component\Finder\Finder;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\HttpKernelInterface;
- use Symfony\Component\HttpKernel\Test\WebTestCase as BaseWebTestCase;
- /**
- * WebTestCase is the base class for functional tests.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- */
- abstract class WebTestCase extends BaseWebTestCase
- {
- protected $kernel;
- /**
- * Creates a Client.
- *
- * @param array $options An array of options to pass to the createKernel class
- * @param array $server An array of server parameters
- *
- * @return Client A Client instance
- */
- public function createClient(array $options = array(), array $server = array())
- {
- $this->kernel = $this->createKernel($options);
- $this->kernel->boot();
- $client = $this->kernel->getContainer()->get('test.client');
- $client->setServerParameters($server);
- return $client;
- }
- /**
- * Finds the directory where the phpunit.xml(.dist) is stored.
- *
- * If you run tests with the PHPUnit CLI tool, everything will work as expected.
- * If not, override this method in your test classes.
- *
- * @return string The directory where phpunit.xml(.dist) is stored
- */
- protected function getPhpUnitXmlDir()
- {
- $dir = null;
- if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
- throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
- }
- $dir = $this->getPhpUnitCliConfigArgument();
- if ($dir === null &&
- (file_exists(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml') ||
- file_exists(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) {
- $dir = getcwd();
- }
- // Can't continue
- if ($dir === null) {
- throw new \RuntimeException('Unable to guess the Kernel directory.');
- }
- if (!is_dir($dir)) {
- $dir = dirname($dir);
- }
- return $dir;
- }
- /**
- * Finds the value of configuration flag from cli
- *
- * PHPUnit will use the last configuration argument on the command line, so this only returns
- * the last configuration argument
- *
- * @return string The value of the phpunit cli configuration option
- */
- private function getPhpUnitCliConfigArgument()
- {
- $dir = null;
- $reversedArgs = array_reverse($_SERVER['argv']);
- foreach ($reversedArgs as $argIndex=>$testArg) {
- if ($testArg === '-c' || $testArg === '--configuration') {
- $dir = realpath($reversedArgs[$argIndex - 1]);
- break;
- } elseif (strpos($testArg, '--configuration=') === 0) {
- $argPath = substr($testArg, strlen('--configuration='));
- $dir = realpath($argPath);
- break;
- }
- }
- return $dir;
- }
- /**
- * Attempts to guess the kernel location.
- *
- * When the Kernel is located, the file is required.
- *
- * @return string The Kernel class name
- */
- protected function getKernelClass()
- {
- $dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : $this->getPhpUnitXmlDir();
- $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');
- require_once $file;
- return $class;
- }
- /**
- * Creates a Kernel.
- *
- * Available options:
- *
- * * environment
- * * debug
- *
- * @param array $options An array of options
- *
- * @return HttpKernelInterface A HttpKernelInterface instance
- */
- protected function createKernel(array $options = array())
- {
- $class = $this->getKernelClass();
- return new $class(
- isset($options['environment']) ? $options['environment'] : 'test',
- isset($options['debug']) ? $options['debug'] : true
- );
- }
- }