KernelBrowser.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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;
  11. use Symfony\Bundle\FrameworkBundle\Test\TestBrowserToken;
  12. use Symfony\Component\BrowserKit\Cookie;
  13. use Symfony\Component\BrowserKit\CookieJar;
  14. use Symfony\Component\BrowserKit\History;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\HttpKernelBrowser;
  19. use Symfony\Component\HttpKernel\KernelInterface;
  20. use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
  21. use Symfony\Component\Security\Core\User\UserInterface;
  22. /**
  23. * Simulates a browser and makes requests to a Kernel object.
  24. *
  25. * @author Fabien Potencier <fabien@symfony.com>
  26. */
  27. class KernelBrowser extends HttpKernelBrowser
  28. {
  29. private $hasPerformedRequest = false;
  30. private $profiler = false;
  31. private $reboot = true;
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function __construct(KernelInterface $kernel, array $server = [], History $history = null, CookieJar $cookieJar = null)
  36. {
  37. parent::__construct($kernel, $server, $history, $cookieJar);
  38. }
  39. /**
  40. * Returns the container.
  41. *
  42. * @return ContainerInterface
  43. */
  44. public function getContainer()
  45. {
  46. return $this->kernel->getContainer();
  47. }
  48. /**
  49. * Returns the kernel.
  50. *
  51. * @return KernelInterface
  52. */
  53. public function getKernel()
  54. {
  55. return $this->kernel;
  56. }
  57. /**
  58. * Gets the profile associated with the current Response.
  59. *
  60. * @return HttpProfile|false|null A Profile instance
  61. */
  62. public function getProfile()
  63. {
  64. if (null === $this->response || !$this->kernel->getContainer()->has('profiler')) {
  65. return false;
  66. }
  67. return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
  68. }
  69. /**
  70. * Enables the profiler for the very next request.
  71. *
  72. * If the profiler is not enabled, the call to this method does nothing.
  73. */
  74. public function enableProfiler()
  75. {
  76. if ($this->kernel->getContainer()->has('profiler')) {
  77. $this->profiler = true;
  78. }
  79. }
  80. /**
  81. * Disables kernel reboot between requests.
  82. *
  83. * By default, the Client reboots the Kernel for each request. This method
  84. * allows to keep the same kernel across requests.
  85. */
  86. public function disableReboot()
  87. {
  88. $this->reboot = false;
  89. }
  90. /**
  91. * Enables kernel reboot between requests.
  92. */
  93. public function enableReboot()
  94. {
  95. $this->reboot = true;
  96. }
  97. /**
  98. * @param UserInterface $user
  99. */
  100. public function loginUser($user, string $firewallContext = 'main'): self
  101. {
  102. if (!interface_exists(UserInterface::class)) {
  103. throw new \LogicException(sprintf('"%s" requires symfony/security-core to be installed.', __METHOD__));
  104. }
  105. if (!$user instanceof UserInterface) {
  106. throw new \LogicException(sprintf('The first argument of "%s" must be instance of "%s", "%s" provided.', __METHOD__, UserInterface::class, \is_object($user) ? \get_class($user) : \gettype($user)));
  107. }
  108. $token = new TestBrowserToken($user->getRoles(), $user, $firewallContext);
  109. $token->setAuthenticated(true);
  110. $session = $this->getContainer()->get('session');
  111. $session->set('_security_'.$firewallContext, serialize($token));
  112. $session->save();
  113. $cookie = new Cookie($session->getName(), $session->getId());
  114. $this->getCookieJar()->set($cookie);
  115. return $this;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. *
  120. * @param Request $request A Request instance
  121. *
  122. * @return Response A Response instance
  123. */
  124. protected function doRequest($request)
  125. {
  126. // avoid shutting down the Kernel if no request has been performed yet
  127. // WebTestCase::createClient() boots the Kernel but do not handle a request
  128. if ($this->hasPerformedRequest && $this->reboot) {
  129. $this->kernel->shutdown();
  130. } else {
  131. $this->hasPerformedRequest = true;
  132. }
  133. if ($this->profiler) {
  134. $this->profiler = false;
  135. $this->kernel->boot();
  136. $this->kernel->getContainer()->get('profiler')->enable();
  137. }
  138. return parent::doRequest($request);
  139. }
  140. /**
  141. * {@inheritdoc}
  142. *
  143. * @param Request $request A Request instance
  144. *
  145. * @return Response A Response instance
  146. */
  147. protected function doRequestInProcess($request)
  148. {
  149. $response = parent::doRequestInProcess($request);
  150. $this->profiler = false;
  151. return $response;
  152. }
  153. /**
  154. * Returns the script to execute when the request must be insulated.
  155. *
  156. * It assumes that the autoloader is named 'autoload.php' and that it is
  157. * stored in the same directory as the kernel (this is the case for the
  158. * Symfony Standard Edition). If this is not your case, create your own
  159. * client and override this method.
  160. *
  161. * @param Request $request A Request instance
  162. *
  163. * @return string The script content
  164. */
  165. protected function getScript($request)
  166. {
  167. $kernel = var_export(serialize($this->kernel), true);
  168. $request = var_export(serialize($request), true);
  169. $errorReporting = error_reporting();
  170. $requires = '';
  171. foreach (get_declared_classes() as $class) {
  172. if (0 === strpos($class, 'ComposerAutoloaderInit')) {
  173. $r = new \ReflectionClass($class);
  174. $file = \dirname($r->getFileName(), 2).'/autoload.php';
  175. if (is_file($file)) {
  176. $requires .= 'require_once '.var_export($file, true).";\n";
  177. }
  178. }
  179. }
  180. if (!$requires) {
  181. throw new \RuntimeException('Composer autoloader not found.');
  182. }
  183. $requires .= 'require_once '.var_export((new \ReflectionObject($this->kernel))->getFileName(), true).";\n";
  184. $profilerCode = '';
  185. if ($this->profiler) {
  186. $profilerCode = '$kernel->getContainer()->get(\'profiler\')->enable();';
  187. }
  188. $code = <<<EOF
  189. <?php
  190. error_reporting($errorReporting);
  191. $requires
  192. \$kernel = unserialize($kernel);
  193. \$kernel->boot();
  194. $profilerCode
  195. \$request = unserialize($request);
  196. EOF;
  197. return $code.$this->getHandleScript();
  198. }
  199. }