WebTestCase.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Symfony\Bundle\FrameworkBundle\Test;
  3. use Symfony\Bundle\FrameworkBundle\Client;
  4. use Symfony\Component\Finder\Finder;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\HttpKernelInterface;
  7. use Symfony\Component\HttpKernel\Test\WebTestCase as BaseWebTestCase;
  8. /*
  9. * This file is part of the Symfony package.
  10. *
  11. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  12. *
  13. * For the full copyright and license information, please view the LICENSE
  14. * file that was distributed with this source code.
  15. */
  16. /**
  17. * WebTestCase is the base class for functional tests.
  18. *
  19. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20. */
  21. abstract class WebTestCase extends BaseWebTestCase
  22. {
  23. protected $kernel;
  24. /**
  25. * Creates a Client.
  26. *
  27. * @param array $options An array of options to pass to the createKernel class
  28. * @param array $server An array of server parameters
  29. *
  30. * @return Client A Client instance
  31. */
  32. public function createClient(array $options = array(), array $server = array())
  33. {
  34. $this->kernel = $this->createKernel($options);
  35. $this->kernel->boot();
  36. $client = $this->kernel->getContainer()->get('test.client');
  37. $client->setServerParameters($server);
  38. return $client;
  39. }
  40. /**
  41. * Creates a Kernel.
  42. *
  43. * If you run tests with the PHPUnit CLI tool, everything will work as expected.
  44. * If not, override this method in your test classes.
  45. *
  46. * Available options:
  47. *
  48. * * environment
  49. * * debug
  50. *
  51. * @param array $options An array of options
  52. *
  53. * @return HttpKernelInterface A HttpKernelInterface instance
  54. */
  55. protected function createKernel(array $options = array())
  56. {
  57. if (isset($_SERVER['KERNEL_DIR'])) {
  58. $dir = $_SERVER['KERNEL_DIR'];
  59. } else {
  60. // black magic below, you have been warned!
  61. $dir = getcwd();
  62. if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
  63. throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
  64. }
  65. // find the --configuration flag from PHPUnit
  66. $cli = implode(' ', $_SERVER['argv']);
  67. if (preg_match('/\-\-configuration[= ]+([^ ]+)/', $cli, $matches)) {
  68. $dir = $dir.'/'.$matches[1];
  69. } elseif (preg_match('/\-c +([^ ]+)/', $cli, $matches)) {
  70. $dir = $dir.'/'.$matches[1];
  71. } elseif (file_exists(getcwd().'/phpunit.xml') || file_exists(getcwd().'/phpunit.xml.dist')) {
  72. $dir = getcwd();
  73. } else {
  74. throw new \RuntimeException('Unable to guess the Kernel directory.');
  75. }
  76. if (!is_dir($dir)) {
  77. $dir = dirname($dir);
  78. }
  79. }
  80. $finder = new Finder();
  81. $finder->name('*Kernel.php')->in($dir);
  82. if (!count($finder)) {
  83. throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
  84. }
  85. $file = current(iterator_to_array($finder));
  86. $class = $file->getBasename('.php');
  87. unset($finder);
  88. require_once $file;
  89. return new $class(
  90. isset($options['environment']) ? $options['environment'] : 'test',
  91. isset($options['debug']) ? $options['debug'] : true
  92. );
  93. }
  94. }