/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php

https://github.com/Exercise/symfony · PHP · 169 lines · 86 code · 23 blank · 60 comment · 11 complexity · ee81beea53354f8bb44799f9d62bceea MD5 · raw file

  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\HttpKernel\HttpKernelInterface;
  14. /**
  15. * WebTestCase is the base class for functional tests.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. abstract class WebTestCase extends \PHPUnit_Framework_TestCase
  20. {
  21. static protected $class;
  22. static protected $kernel;
  23. /**
  24. * Creates a Client.
  25. *
  26. * @param array $options An array of options to pass to the createKernel class
  27. * @param array $server An array of server parameters
  28. *
  29. * @return Client A Client instance
  30. */
  31. static protected function createClient(array $options = array(), array $server = array())
  32. {
  33. if (null !== static::$kernel) {
  34. static::$kernel->shutdown();
  35. }
  36. static::$kernel = static::createKernel($options);
  37. static::$kernel->boot();
  38. $client = static::$kernel->getContainer()->get('test.client');
  39. $client->setServerParameters($server);
  40. return $client;
  41. }
  42. /**
  43. * Finds the directory where the phpunit.xml(.dist) is stored.
  44. *
  45. * If you run tests with the PHPUnit CLI tool, everything will work as expected.
  46. * If not, override this method in your test classes.
  47. *
  48. * @return string The directory where phpunit.xml(.dist) is stored
  49. */
  50. static protected function getPhpUnitXmlDir()
  51. {
  52. if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
  53. throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
  54. }
  55. $dir = static::getPhpUnitCliConfigArgument();
  56. if ($dir === null &&
  57. (is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml') ||
  58. is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) {
  59. $dir = getcwd();
  60. }
  61. // Can't continue
  62. if ($dir === null) {
  63. throw new \RuntimeException('Unable to guess the Kernel directory.');
  64. }
  65. if (!is_dir($dir)) {
  66. $dir = dirname($dir);
  67. }
  68. return $dir;
  69. }
  70. /**
  71. * Finds the value of configuration flag from cli
  72. *
  73. * PHPUnit will use the last configuration argument on the command line, so this only returns
  74. * the last configuration argument
  75. *
  76. * @return string The value of the phpunit cli configuration option
  77. */
  78. static private function getPhpUnitCliConfigArgument()
  79. {
  80. $dir = null;
  81. $reversedArgs = array_reverse($_SERVER['argv']);
  82. foreach ($reversedArgs as $argIndex => $testArg) {
  83. if ($testArg === '-c' || $testArg === '--configuration') {
  84. $dir = realpath($reversedArgs[$argIndex - 1]);
  85. break;
  86. } elseif (strpos($testArg, '--configuration=') === 0) {
  87. $argPath = substr($testArg, strlen('--configuration='));
  88. $dir = realpath($argPath);
  89. break;
  90. }
  91. }
  92. return $dir;
  93. }
  94. /**
  95. * Attempts to guess the kernel location.
  96. *
  97. * When the Kernel is located, the file is required.
  98. *
  99. * @return string The Kernel class name
  100. */
  101. static protected function getKernelClass()
  102. {
  103. $dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : static::getPhpUnitXmlDir();
  104. $finder = new Finder();
  105. $finder->name('*Kernel.php')->depth(0)->in($dir);
  106. $results = iterator_to_array($finder);
  107. if (!count($results)) {
  108. throw new \RuntimeException('Either set KERNEL_DIR in your phpunit.xml according to http://symfony.com/doc/current/book/testing.html#your-first-functional-test or override the WebTestCase::createKernel() method.');
  109. }
  110. $file = current($results);
  111. $class = $file->getBasename('.php');
  112. require_once $file;
  113. return $class;
  114. }
  115. /**
  116. * Creates a Kernel.
  117. *
  118. * Available options:
  119. *
  120. * * environment
  121. * * debug
  122. *
  123. * @param array $options An array of options
  124. *
  125. * @return HttpKernelInterface A HttpKernelInterface instance
  126. */
  127. static protected function createKernel(array $options = array())
  128. {
  129. if (null === static::$class) {
  130. static::$class = static::getKernelClass();
  131. }
  132. return new static::$class(
  133. isset($options['environment']) ? $options['environment'] : 'test',
  134. isset($options['debug']) ? $options['debug'] : true
  135. );
  136. }
  137. /**
  138. * Shuts the kernel down if it was used in the test.
  139. */
  140. protected function tearDown()
  141. {
  142. if (null !== static::$kernel) {
  143. static::$kernel->shutdown();
  144. }
  145. }
  146. }