WebTestCase.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Test;
  11. use Symfony\Bundle\FrameworkBundle\Client;
  12. use Symfony\Component\Finder\Finder;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\HttpKernelInterface;
  15. /**
  16. * WebTestCase is the base class for functional tests.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. abstract class WebTestCase extends \PHPUnit_Framework_TestCase
  21. {
  22. static protected $class;
  23. static 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. static protected function createClient(array $options = array(),
  33. array $server = array('HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'))
  34. {
  35. static::$kernel = static::createKernel($options);
  36. static::$kernel->boot();
  37. $client = static::$kernel->getContainer()->get('test.client');
  38. $client->setServerParameters($server);
  39. return $client;
  40. }
  41. /**
  42. * Finds the directory where the phpunit.xml(.dist) is stored.
  43. *
  44. * If you run tests with the PHPUnit CLI tool, everything will work as expected.
  45. * If not, override this method in your test classes.
  46. *
  47. * @return string The directory where phpunit.xml(.dist) is stored
  48. */
  49. static protected function getPhpUnitXmlDir()
  50. {
  51. if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
  52. throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
  53. }
  54. $dir = static::getPhpUnitCliConfigArgument();
  55. if ($dir === null &&
  56. (file_exists(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml') ||
  57. file_exists(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) {
  58. $dir = getcwd();
  59. }
  60. // Can't continue
  61. if ($dir === null) {
  62. throw new \RuntimeException('Unable to guess the Kernel directory.');
  63. }
  64. if (!is_dir($dir)) {
  65. $dir = dirname($dir);
  66. }
  67. return $dir;
  68. }
  69. /**
  70. * Finds the value of configuration flag from cli
  71. *
  72. * PHPUnit will use the last configuration argument on the command line, so this only returns
  73. * the last configuration argument
  74. *
  75. * @return string The value of the phpunit cli configuration option
  76. */
  77. static private function getPhpUnitCliConfigArgument()
  78. {
  79. $dir = null;
  80. $reversedArgs = array_reverse($_SERVER['argv']);
  81. foreach ($reversedArgs as $argIndex=>$testArg) {
  82. if ($testArg === '-c' || $testArg === '--configuration') {
  83. $dir = realpath($reversedArgs[$argIndex - 1]);
  84. break;
  85. } elseif (strpos($testArg, '--configuration=') === 0) {
  86. $argPath = substr($testArg, strlen('--configuration='));
  87. $dir = realpath($argPath);
  88. break;
  89. }
  90. }
  91. return $dir;
  92. }
  93. /**
  94. * Attempts to guess the kernel location.
  95. *
  96. * When the Kernel is located, the file is required.
  97. *
  98. * @return string The Kernel class name
  99. */
  100. static protected function getKernelClass()
  101. {
  102. $dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : static::getPhpUnitXmlDir();
  103. $finder = new Finder();
  104. $finder->name('*Kernel.php')->depth(0)->in($dir);
  105. if (!count($finder)) {
  106. throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
  107. }
  108. $file = current(iterator_to_array($finder));
  109. $class = $file->getBasename('.php');
  110. require_once $file;
  111. return $class;
  112. }
  113. /**
  114. * Creates a Kernel.
  115. *
  116. * Available options:
  117. *
  118. * * environment
  119. * * debug
  120. *
  121. * @param array $options An array of options
  122. *
  123. * @return HttpKernelInterface A HttpKernelInterface instance
  124. */
  125. static protected function createKernel(array $options = array())
  126. {
  127. if (null === static::$class) {
  128. static::$class = static::getKernelClass();
  129. }
  130. return new static::$class(
  131. isset($options['environment']) ? $options['environment'] : 'test',
  132. isset($options['debug']) ? $options['debug'] : true
  133. );
  134. }
  135. /**
  136. * Shuts the kernel down if it was used in the test.
  137. */
  138. protected function tearDown()
  139. {
  140. if (null !== static::$kernel) {
  141. static::$kernel->shutdown();
  142. }
  143. }
  144. }