WebTestCase.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Symfony\Framework\WebBundle\Test;
  3. use Symfony\Foundation\Test\WebTestCase as BaseWebTestCase;
  4. use Symfony\Components\Finder\Finder;
  5. /*
  6. * This file is part of the Symfony package.
  7. *
  8. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  9. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. /**
  14. * WebTestCase is the base class for functional tests.
  15. *
  16. * @package Symfony
  17. * @subpackage Framework_WebBundle
  18. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  19. */
  20. abstract class WebTestCase extends BaseWebTestCase
  21. {
  22. /**
  23. * Creates a Kernel.
  24. *
  25. * If you run tests with the PHPUnit CLI tool, everything will work as expected.
  26. * If not, override this method in your test classes.
  27. *
  28. * @return Symfony\Components\HttpKernel\HttpKernelInterface A HttpKernelInterface instance
  29. */
  30. protected function createKernel()
  31. {
  32. // black magic below, you have been warned!
  33. $dir = getcwd();
  34. if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
  35. throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
  36. }
  37. // find the --configuration flag from PHPUnit
  38. $cli = implode(' ', $_SERVER['argv']);
  39. if (preg_match('/\-\-configuration[= ]+([^ ]+)/', $cli, $matches)) {
  40. $dir = $dir.'/'.dirname($matches[1]);
  41. }
  42. $finder = new Finder();
  43. $finder->name('*Kernel.php')->in($dir);
  44. if (!count($finder)) {
  45. throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
  46. }
  47. $file = current(iterator_to_array($finder));
  48. $class = $file->getBasename('.php');
  49. unset($finder);
  50. require_once $file;
  51. return new $class('test', true);
  52. }
  53. }