PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/nattaphat/hgis
PHP | 173 lines | 86 code | 23 blank | 64 comment | 11 complexity | 24e65583f68ca9b6e7d1a4e845f29893 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. protected static $class;
  22. protected static $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. protected static 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. * @throws \RuntimeException
  51. */
  52. protected static function getPhpUnitXmlDir()
  53. {
  54. if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
  55. throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
  56. }
  57. $dir = static::getPhpUnitCliConfigArgument();
  58. if ($dir === null &&
  59. (is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml') ||
  60. is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) {
  61. $dir = getcwd();
  62. }
  63. // Can't continue
  64. if ($dir === null) {
  65. throw new \RuntimeException('Unable to guess the Kernel directory.');
  66. }
  67. if (!is_dir($dir)) {
  68. $dir = dirname($dir);
  69. }
  70. return $dir;
  71. }
  72. /**
  73. * Finds the value of the CLI configuration option.
  74. *
  75. * PHPUnit will use the last configuration argument on the command line, so this only returns
  76. * the last configuration argument.
  77. *
  78. * @return string The value of the PHPUnit cli configuration option
  79. */
  80. private static function getPhpUnitCliConfigArgument()
  81. {
  82. $dir = null;
  83. $reversedArgs = array_reverse($_SERVER['argv']);
  84. foreach ($reversedArgs as $argIndex => $testArg) {
  85. if (preg_match('/^-[^ \-]*c$/', $testArg) || $testArg === '--configuration') {
  86. $dir = realpath($reversedArgs[$argIndex - 1]);
  87. break;
  88. } elseif (strpos($testArg, '--configuration=') === 0) {
  89. $argPath = substr($testArg, strlen('--configuration='));
  90. $dir = realpath($argPath);
  91. break;
  92. }
  93. }
  94. return $dir;
  95. }
  96. /**
  97. * Attempts to guess the kernel location.
  98. *
  99. * When the Kernel is located, the file is required.
  100. *
  101. * @return string The Kernel class name
  102. *
  103. * @throws \RuntimeException
  104. */
  105. protected static function getKernelClass()
  106. {
  107. $dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : static::getPhpUnitXmlDir();
  108. $finder = new Finder();
  109. $finder->name('*Kernel.php')->depth(0)->in($dir);
  110. $results = iterator_to_array($finder);
  111. if (!count($results)) {
  112. 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.');
  113. }
  114. $file = current($results);
  115. $class = $file->getBasename('.php');
  116. require_once $file;
  117. return $class;
  118. }
  119. /**
  120. * Creates a Kernel.
  121. *
  122. * Available options:
  123. *
  124. * * environment
  125. * * debug
  126. *
  127. * @param array $options An array of options
  128. *
  129. * @return HttpKernelInterface A HttpKernelInterface instance
  130. */
  131. protected static function createKernel(array $options = array())
  132. {
  133. if (null === static::$class) {
  134. static::$class = static::getKernelClass();
  135. }
  136. return new static::$class(
  137. isset($options['environment']) ? $options['environment'] : 'test',
  138. isset($options['debug']) ? $options['debug'] : true
  139. );
  140. }
  141. /**
  142. * Shuts the kernel down if it was used in the test.
  143. */
  144. protected function tearDown()
  145. {
  146. if (null !== static::$kernel) {
  147. static::$kernel->shutdown();
  148. }
  149. }
  150. }