Client.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace Symfony\Components\HttpKernel\Test;
  3. use Symfony\Components\HttpKernel\HttpKernelInterface;
  4. use Symfony\Components\HttpKernel\Request;
  5. use Symfony\Components\BrowserKit\Client as BaseClient;
  6. use Symfony\Components\BrowserKit\Request as DomRequest;
  7. use Symfony\Components\BrowserKit\Response as DomResponse;
  8. use Symfony\Components\BrowserKit\History;
  9. use Symfony\Components\BrowserKit\CookieJar;
  10. /*
  11. * This file is part of the Symfony package.
  12. *
  13. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  14. *
  15. * For the full copyright and license information, please view the LICENSE
  16. * file that was distributed with this source code.
  17. */
  18. /**
  19. * Client simulates a browser and makes requests to a Kernel object.
  20. *
  21. * @package Symfony
  22. * @subpackage Components_HttpKernel
  23. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  24. */
  25. class Client extends BaseClient
  26. {
  27. protected $kernel;
  28. protected $test;
  29. protected $testers;
  30. /**
  31. * Constructor.
  32. *
  33. * @param Symfony\Components\HttpKernel\HttpKernelInterface $kernel An HttpKernel instance
  34. * @param array $server The server parameters (equivalent of $_SERVER)
  35. * @param Symfony\Components\BrowserKit\History $history A History instance to store the browser history
  36. * @param Symfony\Components\BrowserKit\CookieJar $cookieJar A CookieJar instance to store the cookies
  37. */
  38. public function __construct(HttpKernelInterface $kernel, array $server = array(), History $history = null, CookieJar $cookieJar = null)
  39. {
  40. $this->kernel = $kernel;
  41. $this->testers = array();
  42. parent::__construct($server, $history, $cookieJar);
  43. $this->followRedirects = false;
  44. }
  45. /**
  46. * Sets the \PHPUnit_Framework_TestCase instance associated with this client.
  47. *
  48. * @param \PHPUnit_Framework_TestCase $test A \PHPUnit_Framework_TestCase instance
  49. */
  50. public function setTestCase(\PHPUnit_Framework_TestCase $test)
  51. {
  52. $this->test = $test;
  53. }
  54. /**
  55. * Returns true if the tester is defined.
  56. *
  57. * @param string $name The tester alias
  58. *
  59. * @return Boolean true if the tester is defined, false otherwise
  60. */
  61. public function hasTester($name)
  62. {
  63. return isset($this->testers[$name]);
  64. }
  65. /**
  66. * Adds an tester object for this client.
  67. *
  68. * @param string $name The tester alias
  69. * @param Symfony\Foundation\Test\TesterInterface $tester A Tester instance
  70. */
  71. public function addTester($name, TesterInterface $tester)
  72. {
  73. $this->testers[$name] = $tester;
  74. }
  75. /**
  76. * Gets an tester by name.
  77. *
  78. * @param string $name The tester alias
  79. *
  80. * @return Symfony\Foundation\Test\TesterInterface An Tester instance
  81. */
  82. public function getTester($name)
  83. {
  84. if (!isset($this->testers[$name])) {
  85. throw new \InvalidArgumentException(sprintf('Tester "%s" does not exist.', $name));
  86. }
  87. return $this->testers[$name];
  88. }
  89. /**
  90. * Makes a request.
  91. *
  92. * @param Symfony\Components\HttpKernel\Request $request A Request instance
  93. *
  94. * @param Symfony\Components\HttpKernel\Response $response A Response instance
  95. */
  96. protected function doRequest($request)
  97. {
  98. return $this->kernel->handle($request);
  99. }
  100. /**
  101. * Returns the script to execute when the request must be insulated.
  102. *
  103. * @param Symfony\Components\HttpKernel\Request $request A Request instance
  104. */
  105. protected function getScript($request)
  106. {
  107. $kernel = serialize($this->kernel);
  108. $request = serialize($request);
  109. $r = new \ReflectionClass('\\Symfony\\Foundation\\UniversalClassLoader');
  110. $requirePath = $r->getFileName();
  111. $symfonyPath = realpath(__DIR__.'/../../../..');
  112. return <<<EOF
  113. <?php
  114. require_once '$requirePath';
  115. \$loader = new Symfony\Foundation\UniversalClassLoader();
  116. \$loader->registerNamespaces(array('Symfony' => '$symfonyPath'));
  117. \$loader->register();
  118. \$kernel = unserialize('$kernel');
  119. echo serialize(\$kernel->handle(unserialize('$request')));
  120. EOF;
  121. }
  122. /**
  123. * Converts the BrowserKit request to a HttpKernel request.
  124. *
  125. * @param Symfony\Components\BrowserKit\Request $request A Request instance
  126. *
  127. * @return Symfony\Components\HttpKernel\Request A Request instance
  128. */
  129. protected function filterRequest(DomRequest $request)
  130. {
  131. $uri = $request->getUri();
  132. if (preg_match('#^https?\://([^/]+)/(.*)$#', $uri, $matches)) {
  133. $uri = '/'.$matches[2];
  134. }
  135. return Request::create($uri, $request->getMethod(), $request->getParameters(), $request->getFiles(), $request->getCookies(), $request->getServer());
  136. }
  137. /**
  138. * Converts the HttpKernel response to a BrowserKit response.
  139. *
  140. * @param Symfony\Components\HttpKernel\Response $response A Response instance
  141. *
  142. * @return Symfony\Components\BrowserKit\Response A Response instance
  143. */
  144. protected function filterResponse($response)
  145. {
  146. return new DomResponse($response->getContent(), $response->getStatusCode(), $response->headers->all(), $response->getCookies());
  147. }
  148. /**
  149. * Called when a method does not exit.
  150. *
  151. * It forwards assert* methods.
  152. *
  153. * @param string $method The method name to execute
  154. * @param array $arguments An array of arguments to pass to the method
  155. */
  156. public function __call($method, $arguments)
  157. {
  158. if ('assert' !== substr($method, 0, 6)) {
  159. throw new \BadMethodCallException(sprintf('Method %s::%s is not defined.', get_class($this), $method));
  160. }
  161. // standard PHPUnit assert?
  162. if (method_exists($this->test, $method)) {
  163. return call_user_func_array(array($this->test, $method), $arguments);
  164. }
  165. if (!preg_match('/^assert([A-Z].+?)([A-Z].+)$/', $method, $matches)) {
  166. throw new \BadMethodCallException(sprintf('Method %s::%s is not defined.', get_class($this), $method));
  167. }
  168. // registered tester object?
  169. $name = strtolower($matches[1]);
  170. if (!$this->hasTester($name)) {
  171. throw new \BadMethodCallException(sprintf('Method %s::%s is not defined (assert object "%s" is not defined).', get_class($this), $method, $name));
  172. }
  173. $tester = $this->getTester($name);
  174. $tester->setTestCase($this->test);
  175. return call_user_func_array(array($tester, 'assert'.$matches[2]), $arguments);
  176. }
  177. }