This repository was archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathBrowserKit.php
More file actions
144 lines (119 loc) · 3.83 KB
/
BrowserKit.php
File metadata and controls
144 lines (119 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Behatch\HttpCall\Request;
use Behat\Mink\Driver\Goutte\Client as GoutteClient;
use Behat\Mink\Mink;
use Symfony\Component\BrowserKit\Client as BrowserKitClient;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class BrowserKit
{
protected $mink;
public function __construct(Mink $mink)
{
$this->mink = $mink;
}
public function getMethod()
{
return $this->getRequest()
->getMethod();
}
public function getUri()
{
return $this->getRequest()
->getUri();
}
public function getServer()
{
return $this->getRequest()
->getServer();
}
public function getParameters()
{
return $this->getRequest()
->getParameters();
}
protected function getRequest()
{
$client = $this->mink->getSession()->getDriver()->getClient();
// BC layer for BrowserKit 2.2.x and older
if (method_exists($client, 'getInternalRequest')) {
$request = $client->getInternalRequest();
} else {
$request = $client->getRequest();
}
return $request;
}
public function getContent()
{
return $this->mink->getSession()->getPage()->getContent();
}
public function send($method, $url, $parameters = [], $files = [], $content = null, $headers = [])
{
foreach ($files as &$file) {
if (is_string($file)) {
$file = new UploadedFile($file, basename($file));
}
}
$client = $this->mink->getSession()->getDriver()->getClient();
$client->followRedirects(false);
$client->request($method, $url, $parameters, $files, $headers, $content);
$client->followRedirects(true);
$this->resetHttpHeaders();
return $this->mink->getSession()->getPage();
}
public function setHttpHeader($name, $value)
{
$client = $this->mink->getSession()->getDriver()->getClient();
// Goutte\Client
if (method_exists($client, 'setHeader')) {
$client->setHeader($name, $value);
} else {
// Symfony\Component\BrowserKit\Client
/* taken from Behat\Mink\Driver\BrowserKitDriver::setRequestHeader */
$contentHeaders = ['CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true];
$name = str_replace('-', '_', strtoupper($name));
// CONTENT_* are not prefixed with HTTP_ in PHP when building $_SERVER
if (!isset($contentHeaders[$name])) {
$name = 'HTTP_' . $name;
}
/* taken from Behat\Mink\Driver\BrowserKitDriver::setRequestHeader */
$client->setServerParameter($name, $value);
}
}
public function getHttpHeaders()
{
return array_change_key_case(
$this->mink->getSession()->getResponseHeaders(),
CASE_LOWER
);
}
public function getHttpHeader($name)
{
$values = $this->getHttpRawHeader($name);
return implode(', ', $values);
}
public function getHttpRawHeader($name)
{
$name = strtolower($name);
$headers = $this->getHttpHeaders();
if (isset($headers[$name])) {
$value = $headers[$name];
if (!is_array($headers[$name])) {
$value = [$headers[$name]];
}
} else {
throw new \OutOfBoundsException(
"The header '$name' doesn't exist"
);
}
return $value;
}
protected function resetHttpHeaders()
{
/** @var GoutteClient|BrowserKitClient $client */
$client = $this->mink->getSession()->getDriver()->getClient();
$client->setServerParameters([]);
if ($client instanceof GoutteClient) {
$client->restart();
}
}
}