From e05de9ee56e68422d19084ed2dfb9b887397e262 Mon Sep 17 00:00:00 2001 From: Amrouche Hamza Date: Wed, 29 May 2019 08:28:33 +0200 Subject: [PATCH] [BrowserKit] remove deprecated code --- .../Component/BrowserKit/AbstractBrowser.php | 702 ++++++++++++++++- src/Symfony/Component/BrowserKit/Client.php | 738 ------------------ src/Symfony/Component/BrowserKit/Response.php | 44 +- .../BrowserKit/Tests/AbstractBrowserTest.php | 65 +- .../BrowserKit/Tests/HttpBrowserTest.php | 19 - .../BrowserKit/Tests/ResponseTest.php | 9 - 6 files changed, 717 insertions(+), 860 deletions(-) delete mode 100644 src/Symfony/Component/BrowserKit/Client.php diff --git a/src/Symfony/Component/BrowserKit/AbstractBrowser.php b/src/Symfony/Component/BrowserKit/AbstractBrowser.php index f58a8756bb..57ba716d5c 100644 --- a/src/Symfony/Component/BrowserKit/AbstractBrowser.php +++ b/src/Symfony/Component/BrowserKit/AbstractBrowser.php @@ -11,6 +11,12 @@ 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. * @@ -24,6 +30,700 @@ namespace Symfony\Component\BrowserKit; * * @author Fabien Potencier */ -abstract class AbstractBrowser extends Client +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) + * @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) { + 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 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 = []) + { + $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