123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- 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;
- /*
- * 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.
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.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;
- }
- /**
- * 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.
- *
- * Available options:
- *
- * * environment
- * * debug
- *
- * @param array $options An array of options
- *
- * @return HttpKernelInterface A HttpKernelInterface instance
- */
- protected function createKernel(array $options = array())
- {
- if (isset($_SERVER['KERNEL_DIR'])) {
- $dir = $_SERVER['KERNEL_DIR'];
- } else {
- // 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.'/'.$matches[1];
- } elseif (preg_match('/\-c +([^ ]+)/', $cli, $matches)) {
- $dir = $dir.'/'.$matches[1];
- } elseif (file_exists(getcwd().'/phpunit.xml') || file_exists(getcwd().'/phpunit.xml.dist')) {
- $dir = getcwd();
- } else {
- throw new \RuntimeException('Unable to guess the Kernel directory.');
- }
- if (!is_dir($dir)) {
- $dir = dirname($dir);
- }
- }
- $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(
- isset($options['environment']) ? $options['environment'] : 'test',
- isset($options['debug']) ? $options['debug'] : true
- );
- }
- }
|