comparison vendor/symfony/browser-kit/Client.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 5fb285c0d0e3
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
10 */ 10 */
11 11
12 namespace Symfony\Component\BrowserKit; 12 namespace Symfony\Component\BrowserKit;
13 13
14 use Symfony\Component\DomCrawler\Crawler; 14 use Symfony\Component\DomCrawler\Crawler;
15 use Symfony\Component\DomCrawler\Form;
15 use Symfony\Component\DomCrawler\Link; 16 use Symfony\Component\DomCrawler\Link;
16 use Symfony\Component\DomCrawler\Form;
17 use Symfony\Component\Process\PhpProcess; 17 use Symfony\Component\Process\PhpProcess;
18 18
19 /** 19 /**
20 * Client simulates a browser. 20 * Client simulates a browser.
21 * 21 *
28 */ 28 */
29 abstract class Client 29 abstract class Client
30 { 30 {
31 protected $history; 31 protected $history;
32 protected $cookieJar; 32 protected $cookieJar;
33 protected $server = array(); 33 protected $server = [];
34 protected $internalRequest; 34 protected $internalRequest;
35 protected $request; 35 protected $request;
36 protected $internalResponse; 36 protected $internalResponse;
37 protected $response; 37 protected $response;
38 protected $crawler; 38 protected $crawler;
40 protected $redirect; 40 protected $redirect;
41 protected $followRedirects = true; 41 protected $followRedirects = true;
42 42
43 private $maxRedirects = -1; 43 private $maxRedirects = -1;
44 private $redirectCount = 0; 44 private $redirectCount = 0;
45 private $redirects = array(); 45 private $redirects = [];
46 private $isMainRequest = true; 46 private $isMainRequest = true;
47 47
48 /** 48 /**
49 * @param array $server The server parameters (equivalent of $_SERVER) 49 * @param array $server The server parameters (equivalent of $_SERVER)
50 * @param History $history A History instance to store the browser history 50 * @param History $history A History instance to store the browser history
51 * @param CookieJar $cookieJar A CookieJar instance to store the cookies 51 * @param CookieJar $cookieJar A CookieJar instance to store the cookies
52 */ 52 */
53 public function __construct(array $server = array(), History $history = null, CookieJar $cookieJar = null) 53 public function __construct(array $server = [], History $history = null, CookieJar $cookieJar = null)
54 { 54 {
55 $this->setServerParameters($server); 55 $this->setServerParameters($server);
56 $this->history = $history ?: new History(); 56 $this->history = $history ?: new History();
57 $this->cookieJar = $cookieJar ?: new CookieJar(); 57 $this->cookieJar = $cookieJar ?: new CookieJar();
58 } 58 }
119 * 119 *
120 * @param array $server An array of server parameters 120 * @param array $server An array of server parameters
121 */ 121 */
122 public function setServerParameters(array $server) 122 public function setServerParameters(array $server)
123 { 123 {
124 $this->server = array_merge(array( 124 $this->server = array_merge([
125 'HTTP_USER_AGENT' => 'Symfony BrowserKit', 125 'HTTP_USER_AGENT' => 'Symfony BrowserKit',
126 ), $server); 126 ], $server);
127 } 127 }
128 128
129 /** 129 /**
130 * Sets single server parameter. 130 * Sets single server parameter.
131 * 131 *
250 * @param Form $form A Form instance 250 * @param Form $form A Form instance
251 * @param array $values An array of form field values 251 * @param array $values An array of form field values
252 * 252 *
253 * @return Crawler 253 * @return Crawler
254 */ 254 */
255 public function submit(Form $form, array $values = array()) 255 public function submit(Form $form, array $values = [])
256 { 256 {
257 $form->setValues($values); 257 $form->setValues($values);
258 258
259 return $this->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles()); 259 return $this->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles());
260 } 260 }
270 * @param string $content The raw body data 270 * @param string $content The raw body data
271 * @param bool $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload()) 271 * @param bool $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())
272 * 272 *
273 * @return Crawler 273 * @return Crawler
274 */ 274 */
275 public function request($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true) 275 public function request($method, $uri, array $parameters = [], array $files = [], array $server = [], $content = null, $changeHistory = true)
276 { 276 {
277 if ($this->isMainRequest) { 277 if ($this->isMainRequest) {
278 $this->redirectCount = 0; 278 $this->redirectCount = 0;
279 } else { 279 } else {
280 ++$this->redirectCount; 280 ++$this->redirectCount;
281 } 281 }
282 282
283 $originalUri = $uri;
284
283 $uri = $this->getAbsoluteUri($uri); 285 $uri = $this->getAbsoluteUri($uri);
284 286
285 $server = array_merge($this->server, $server); 287 $server = array_merge($this->server, $server);
286 288
287 if (isset($server['HTTPS'])) { 289 if (!empty($server['HTTP_HOST']) && null === parse_url($originalUri, PHP_URL_HOST)) {
290 $uri = preg_replace('{^(https?\://)'.preg_quote($this->extractHost($uri)).'}', '${1}'.$server['HTTP_HOST'], $uri);
291 }
292
293 if (isset($server['HTTPS']) && null === parse_url($originalUri, PHP_URL_SCHEME)) {
288 $uri = preg_replace('{^'.parse_url($uri, PHP_URL_SCHEME).'}', $server['HTTPS'] ? 'https' : 'http', $uri); 294 $uri = preg_replace('{^'.parse_url($uri, PHP_URL_SCHEME).'}', $server['HTTPS'] ? 'https' : 'http', $uri);
289 } 295 }
290 296
291 if (!$this->history->isEmpty()) { 297 if (!$this->history->isEmpty()) {
292 $server['HTTP_REFERER'] = $this->history->current()->getUri(); 298 $server['HTTP_REFERER'] = $this->history->current()->getUri();
351 $process->run(); 357 $process->run();
352 358
353 if (file_exists($deprecationsFile)) { 359 if (file_exists($deprecationsFile)) {
354 $deprecations = file_get_contents($deprecationsFile); 360 $deprecations = file_get_contents($deprecationsFile);
355 unlink($deprecationsFile); 361 unlink($deprecationsFile);
356 foreach ($deprecations ? unserialize($deprecations) : array() as $deprecation) { 362 foreach ($deprecations ? unserialize($deprecations) : [] as $deprecation) {
357 if ($deprecation[0]) { 363 if ($deprecation[0]) {
358 trigger_error($deprecation[1], E_USER_DEPRECATED); 364 @trigger_error($deprecation[1], E_USER_DEPRECATED);
359 } else { 365 } else {
360 @trigger_error($deprecation[1], E_USER_DEPRECATED); 366 @trigger_error($deprecation[1], E_USER_DEPRECATED);
361 } 367 }
362 } 368 }
363 } 369 }
495 } 501 }
496 } 502 }
497 503
498 $request = $this->internalRequest; 504 $request = $this->internalRequest;
499 505
500 if (in_array($this->internalResponse->getStatus(), array(301, 302, 303))) { 506 if (\in_array($this->internalResponse->getStatus(), [301, 302, 303])) {
501 $method = 'GET'; 507 $method = 'GET';
502 $files = array(); 508 $files = [];
503 $content = null; 509 $content = null;
504 } else { 510 } else {
505 $method = $request->getMethod(); 511 $method = $request->getMethod();
506 $files = $request->getFiles(); 512 $files = $request->getFiles();
507 $content = $request->getContent(); 513 $content = $request->getContent();
508 } 514 }
509 515
510 if ('GET' === strtoupper($method)) { 516 if ('GET' === strtoupper($method)) {
511 // Don't forward parameters for GET request as it should reach the redirection URI 517 // Don't forward parameters for GET request as it should reach the redirection URI
512 $parameters = array(); 518 $parameters = [];
513 } else { 519 } else {
514 $parameters = $request->getParameters(); 520 $parameters = $request->getParameters();
515 } 521 }
516 522
517 $server = $request->getServer(); 523 $server = $request->getServer();