* * 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\BrowserKit\Exception\InvalidArgumentException; use Symfony\Component\BrowserKit\Exception\LogicException; use Symfony\Component\BrowserKit\Exception\RuntimeException; 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 $history; protected CookieJar $cookieJar; protected array $server = []; protected Request $internalRequest; protected object $request; protected Response $internalResponse; protected object $response; protected Crawler $crawler; protected bool $useHtml5Parser = true; protected bool $insulated = false; protected ?string $redirect; protected bool $followRedirects = true; protected bool $followMetaRefresh = false; private int $maxRedirects = -1; private int $redirectCount = 0; private array $redirects = []; private bool $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): void { $this->followRedirects = $followRedirects; } /** * Sets whether to automatically follow meta refresh redirects or not. */ public function followMetaRefresh(bool $followMetaRefresh = true): void { $this->followMetaRefresh = $followMetaRefresh; } /** * Returns whether client automatically follows redirects or not. */ public function isFollowingRedirects(): bool { return $this->followRedirects; } /** * Sets the maximum number of redirects that crawler can follow. */ public function setMaxRedirects(int $maxRedirects): void { $this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects; $this->followRedirects = -1 !== $this->maxRedirects; } /** * Returns the maximum number of redirects that crawler can follow. */ public function getMaxRedirects(): int { return $this->maxRedirects; } /** * Sets the insulated flag. * * @throws LogicException When Symfony Process Component is not installed */ public function insulate(bool $insulated = true): void { if ($insulated && !class_exists(\Symfony\Component\Process\Process::class)) { throw new LogicException('Unable to isolate requests as the Symfony Process Component is not installed. Try running "composer require symfony/process".'); } $this->insulated = $insulated; } /** * Sets server parameters. */ public function setServerParameters(array $server): void { $this->server = array_merge([ 'HTTP_USER_AGENT' => 'Symfony BrowserKit', ], $server); } /** * Sets single server parameter. */ public function setServerParameter(string $key, string $value): void { $this->server[$key] = $value; } /** * Gets single server parameter for specified key. */ public function getServerParameter(string $key, mixed $default = ''): mixed { 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']); } } /** * Converts the request parameters into a JSON string and uses it as request content. */ public function jsonRequest(string $method, string $uri, array $parameters = [], array $server = [], bool $changeHistory = true): Crawler { $content = json_encode($parameters); $this->setServerParameter('CONTENT_TYPE', 'application/json'); $this->setServerParameter('HTTP_ACCEPT', 'application/json'); try { return $this->request($method, $uri, [], [], $server, $content, $changeHistory); } finally { unset($this->server['CONTENT_TYPE']); unset($this->server['HTTP_ACCEPT']); } } /** * Returns the History instance. */ public function getHistory(): History { return $this->history; } /** * Returns the CookieJar instance. */ public function getCookieJar(): CookieJar { return $this->cookieJar; } /** * Returns the current Crawler instance. */ public function getCrawler(): Crawler { return $this->crawler ?? throw new BadMethodCallException(sprintf('The "request()" method must be called before "%s()".', __METHOD__)); } /** * Sets whether parsing should be done using "masterminds/html5". * * @return $this */ public function useHtml5Parser(bool $useHtml5Parser): static { $this->useHtml5Parser = $useHtml5Parser; return $this; } /** * Returns the current BrowserKit Response instance. */ public function getInternalResponse(): Response { return $this->internalResponse ?? throw new BadMethodCallException(sprintf('The "request()" method must be called before "%s()".', __METHOD__)); } /** * Returns the current origin response instance. * * The origin response is the response instance that is returned * by the code that handles requests. * * @see doRequest() */ public function getResponse(): object { return $this->response ?? throw new BadMethodCallException(sprintf('The "request()" method must be called before "%s()".', __METHOD__)); } /** * Returns the current BrowserKit Request instance. */ public function getInternalRequest(): Request { return $this->internalRequest ?? throw new BadMethodCallException(sprintf('The "request()" method must be called before "%s()".', __METHOD__)); } /** * Returns the current origin Request instance. * * The origin request is the request instance that is sent * to the code that handles requests. * * @see doRequest() */ public function getRequest(): object { return $this->request ?? throw new BadMethodCallException(sprintf('The "request()" method must be called before "%s()".', __METHOD__)); } /** * Clicks on a given link. * * @param array $serverParameters An array of server parameters */ public function click(Link $link, array $serverParameters = []): Crawler { if ($link instanceof Form) { return $this->submit($link, [], $serverParameters); } return $this->request($link->getMethod(), $link->getUri(), [], [], $serverParameters); } /** * 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 * @param array $serverParameters An array of server parameters */ public function clickLink(string $linkText, array $serverParameters = []): Crawler { $crawler = $this->crawler ?? throw new BadMethodCallException(sprintf('The "request()" method must be called before "%s()".', __METHOD__)); return $this->click($crawler->selectLink($linkText)->link(), $serverParameters); } /** * Submits a form. * * @param array $values An array of form field values * @param array $serverParameters An array of server parameters */ public function submit(Form $form, array $values = [], array $serverParameters = []): Crawler { $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