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

https://github.com/fernanDOTdo/symfony · PHP · 154 lines · 76 code · 21 blank · 57 comment · 8 complexity · aee27f458f4bd99f78e1bc1fa8782cd1 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\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\HttpKernelInterface;
  15. use Symfony\Component\HttpKernel\Test\WebTestCase as BaseWebTestCase;
  16. /**
  17. * WebTestCase is the base class for functional tests.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.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. * Finds the directory where the phpunit.xml(.dist) is stored.
  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. * @return string The directory where phpunit.xml(.dist) is stored
  47. */
  48. protected function getPhpUnitXmlDir()
  49. {
  50. $dir = null;
  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 = $this->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. 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. protected function getKernelClass()
  101. {
  102. $dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : $this->getPhpUnitXmlDir();
  103. $finder = new Finder();
  104. $finder->name('*Kernel.php')->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. protected function createKernel(array $options = array())
  126. {
  127. $class = $this->getKernelClass();
  128. return new $class(
  129. isset($options['environment']) ? $options['environment'] : 'test',
  130. isset($options['debug']) ? $options['debug'] : true
  131. );
  132. }
  133. }