* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\BrowserKit; use Symfony\Component\BrowserKit\Exception\BadMethodCallException; use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\DomCrawler\Form; use Symfony\Component\DomCrawler\Link; use Symfony\Component\Process\PhpProcess; /** * Simulates a browser. * * To make the actual request, you need to implement the doRequest() method. * * If you want to be able to run requests in their own process (insulated flag), * you need to also implement the getScript() method. * * @author Fabien Potencier */ abstract class AbstractBrowser { protected $history; protected $cookieJar; protected $server = []; protected $internalRequest; protected $request; protected $internalResponse; protected $response; protected $crawler; protected $insulated = false; protected $redirect; protected $followRedirects = true; protected $followMetaRefresh = false; private $maxRedirects = -1; private $redirectCount = 0; private $redirects = []; private $isMainRequest = true; /** * @param array $server The server parameters (equivalent of $_SERVER) */ public function __construct(array $server = [], History $history = null, CookieJar $cookieJar = null) { $this->setServerParameters($server); $this->history = $history ?: new History(); $this->cookieJar = $cookieJar ?: new CookieJar(); } /** * Sets whether to automatically follow redirects or not. */ public function followRedirects(bool $followRedirects = true) { $this->followRedirects = $followRedirects; } /** * Sets whether to automatically follow meta refresh redirects or not. */ public function followMetaRefresh(bool $followMetaRefresh = true) { $this->followMetaRefresh = $followMetaRefresh; } /** * Returns whether client automatically follows redirects or not. * * @return bool */ public function isFollowingRedirects() { return $this->followRedirects; } /** * Sets the maximum number of redirects that crawler can follow. */ public function setMaxRedirects(int $maxRedirects) { $this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects; $this->followRedirects = -1 != $this->maxRedirects; } /** * Returns the maximum number of redirects that crawler can follow. * * @return int */ public function getMaxRedirects() { return $this->maxRedirects; } /** * Sets the insulated flag. * * @param bool $insulated Whether to insulate the requests or not * * @throws \RuntimeException When Symfony Process Component is not installed */ public function insulate(bool $insulated = true) { if ($insulated && !class_exists(\Symfony\Component\Process\Process::class)) { throw new \LogicException('Unable to isolate requests as the Symfony Process Component is not installed.'); } $this->insulated = $insulated; } /** * Sets server parameters. * * @param array $server An array of server parameters */ public function setServerParameters(array $server) { $this->server = array_merge([ 'HTTP_USER_AGENT' => 'Symfony BrowserKit', ], $server); } /** * Sets single server parameter. */ public function setServerParameter(string $key, string $value) { $this->server[$key] = $value; } /** * Gets single server parameter for specified key. * * @param mixed $default A default value when key is undefined * * @return mixed A value of the parameter */ public function getServerParameter(string $key, $default = '') { return $this->server[$key] ?? $default; } public function xmlHttpRequest(string $method, string $uri, array $parameters = [], array $files = [], array $server = [], string $content = null, bool $changeHistory = true): Crawler { $this->setServerParameter('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest'); try { return $this->request($method, $uri, $parameters, $files, $server, $content, $changeHistory); } finally { unset($this->server['HTTP_X_REQUESTED_WITH']); } } /** * Returns the History instance. * * @return History A History instance */ public function getHistory() { return $this->history; } /** * Returns the CookieJar instance. * * @return CookieJar A CookieJar instance */ public function getCookieJar() { return $this->cookieJar; } /** * Returns the current Crawler instance. * * @return Crawler A Crawler instance */ public function getCrawler() { if (null === $this->crawler) { throw new BadMethodCallException(sprintf('The "request()" method must be called before "%s()".', __METHOD__)); } return $this->crawler; } /** * Returns the current BrowserKit Response instance. * * @return Response A BrowserKit Response instance */ public function getInternalResponse() { if (null === $this->internalResponse) { throw new BadMethodCallException(sprintf('The "request()" method must be called before "%s()".', __METHOD__)); } return $this->internalResponse; } /** * Returns the current origin response instance. * * The origin response is the response instance that is returned * by the code that handles requests. * * @return object A response instance * * @see doRequest() */ public function getResponse() { if (null === $this->response) { throw new BadMethodCallException(sprintf('The "request()" method must be called before "%s()".', __METHOD__)); } return $this->response; } /** * Returns the current BrowserKit Request instance. * * @return Request A BrowserKit Request instance */ public function getInternalRequest() { if (null === $this->internalRequest) { throw new BadMethodCallException(sprintf('The "request()" method must be called before "%s()".', __METHOD__)); } return $this->internalRequest; } /** * Returns the current origin Request instance. * * The origin request is the request instance that is sent * to the code that handles requests. * * @return object A Request instance * * @see doRequest() */ public function getRequest() { if (null === $this->request) { throw new BadMethodCallException(sprintf('The "request()" method must be called before "%s()".', __METHOD__)); } return $this->request; } /** * Clicks on a given link. * * @return Crawler */ public function click(Link $link) { if ($link instanceof Form) { return $this->submit($link); } return $this->request($link->getMethod(), $link->getUri()); } /** * Clicks the first link (or clickable image) that contains the given text. * * @param string $linkText The text of the link or the alt attribute of the clickable image */ public function clickLink(string $linkText): Crawler { if (null === $this->crawler) { throw new BadMethodCallException(sprintf('The "request()" method must be called before "%s()".', __METHOD__)); } return $this->click($this->crawler->selectLink($linkText)->link()); } /** * Submits a form. * * @param array $values An array of form field values * @param array $serverParameters An array of server parameters * * @return Crawler */ public function submit(Form $form, array $values = [], array $serverParameters = []) { $form->setValues($values); return $this->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles(), $serverParameters); } /** * Finds the first form that contains a button with the given content and * uses it to submit the given form field values. * * @param string $button The text content, id, value or name of the form