diff --git a/src/Symfony/Component/BrowserKit/AbstractBrowser.php b/src/Symfony/Component/BrowserKit/AbstractBrowser.php index fd6dc27a0d4f6..f58a8756bb48b 100644 --- a/src/Symfony/Component/BrowserKit/AbstractBrowser.php +++ b/src/Symfony/Component/BrowserKit/AbstractBrowser.php @@ -11,12 +11,6 @@ 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. * @@ -30,710 +24,6 @@ * * @author Fabien Potencier */ -abstract class AbstractBrowser +abstract class AbstractBrowser extends Client { - 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) - * @param History $history A History instance to store the browser history - * @param CookieJar $cookieJar A CookieJar instance to store the cookies - */ - 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. - * - * @param bool $followRedirect Whether to follow redirects - */ - public function followRedirects($followRedirect = true) - { - $this->followRedirects = (bool) $followRedirect; - } - - /** - * 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. - * - * @param int $maxRedirects - */ - public function setMaxRedirects($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($insulated = true) - { - if ($insulated && !class_exists('Symfony\\Component\\Process\\Process')) { - throw new \LogicException('Unable to isolate requests as the Symfony Process Component is not installed.'); - } - - $this->insulated = (bool) $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. - * - * @param string $key A key of the parameter - * @param string $value A value of the parameter - */ - public function setServerParameter($key, $value) - { - $this->server[$key] = $value; - } - - /** - * Gets single server parameter for specified key. - * - * @param string $key A key of the parameter to get - * @param string $default A default value when key is undefined - * - * @return string A value of the parameter - */ - public function getServerParameter($key, $default = '') - { - return isset($this->server[$key]) ? $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) { - @trigger_error(sprintf('Calling the "%s()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.', __METHOD__), E_USER_DEPRECATED); - // 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) { - @trigger_error(sprintf('Calling the "%s()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.', __METHOD__), E_USER_DEPRECATED); - // 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) { - @trigger_error(sprintf('Calling the "%s()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.', __METHOD__), E_USER_DEPRECATED); - // 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) { - @trigger_error(sprintf('Calling the "%s()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.', __METHOD__), E_USER_DEPRECATED); - // 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) { - @trigger_error(sprintf('Calling the "%s()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.', __METHOD__), E_USER_DEPRECATED); - // 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 Form $form A Form instance - * @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 = []*/) - { - if (\func_num_args() < 3 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface) { - @trigger_error(sprintf('The "%s()" method will have a new "array $serverParameters = []" argument in version 5.0, not defining it is deprecated since Symfony 4.2.', __METHOD__), E_USER_DEPRECATED); - } - - $form->setValues($values); - $serverParameters = 2 < \func_num_args() ? func_get_arg(2) : []; - - 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