1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sanpi\Behatch\HttpCall\Request; |
4
|
|
|
|
5
|
|
|
use Behat\Mink\Mink; |
6
|
|
|
|
7
|
|
|
class BrowserKit |
8
|
|
|
{ |
9
|
|
|
protected $mink; |
10
|
|
|
|
11
|
|
|
public function __construct(Mink $mink) |
12
|
|
|
{ |
13
|
|
|
$this->mink = $mink; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function getMethod() |
17
|
|
|
{ |
18
|
|
|
return $this->getRequest() |
19
|
|
|
->getMethod(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getUri() |
23
|
|
|
{ |
24
|
|
|
return $this->getRequest() |
25
|
|
|
->getUri(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getServer() |
29
|
|
|
{ |
30
|
|
|
return $this->getRequest() |
31
|
|
|
->getServer(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getParameters() |
35
|
|
|
{ |
36
|
|
|
return $this->getRequest() |
37
|
|
|
->getParameters(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function getRequest() |
41
|
|
|
{ |
42
|
|
|
$client = $this->mink->getSession()->getDriver()->getClient(); |
|
|
|
|
43
|
|
|
// BC layer for BrowserKit 2.2.x and older |
44
|
|
|
if (method_exists($client, 'getInternalRequest')) { |
45
|
|
|
$request = $client->getInternalRequest(); |
46
|
|
|
} else { |
47
|
|
|
$request = $client->getRequest(); |
48
|
|
|
} |
49
|
|
|
return $request; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getContent() |
53
|
|
|
{ |
54
|
|
|
return $this->mink->getSession()->getPage()->getContent(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function send($method, $url, $parameters = [], $files = [], $content = null, $headers = []) |
58
|
|
|
{ |
59
|
|
|
$client = $this->mink->getSession()->getDriver()->getClient(); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$client->followRedirects(false); |
62
|
|
|
$client->request($method, $url, $parameters, $files, $headers, $content); |
63
|
|
|
$client->followRedirects(true); |
64
|
|
|
$this->resetHttpHeaders(); |
65
|
|
|
|
66
|
|
|
return $this->mink->getSession()->getPage(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setHttpHeader($name, $value) |
70
|
|
|
{ |
71
|
|
|
$client = $this->mink->getSession()->getDriver()->getClient(); |
|
|
|
|
72
|
|
|
// Goutte\Client |
73
|
|
|
if (method_exists($client, 'setHeader')) { |
74
|
|
|
$client->setHeader($name, $value); |
75
|
|
|
} else { |
76
|
|
|
// Symfony\Component\BrowserKit\Client |
77
|
|
|
|
78
|
|
|
/* taken from Behat\Mink\Driver\BrowserKitDriver::setRequestHeader */ |
79
|
|
|
$contentHeaders = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true); |
80
|
|
|
$name = str_replace('-', '_', strtoupper($name)); |
81
|
|
|
|
82
|
|
|
// CONTENT_* are not prefixed with HTTP_ in PHP when building $_SERVER |
83
|
|
|
if (!isset($contentHeaders[$name])) { |
84
|
|
|
$name = 'HTTP_' . $name; |
85
|
|
|
} |
86
|
|
|
/* taken from Behat\Mink\Driver\BrowserKitDriver::setRequestHeader */ |
87
|
|
|
|
88
|
|
|
$client->setServerParameter($name, $value); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getHttpHeaders() |
93
|
|
|
{ |
94
|
|
|
return array_change_key_case( |
95
|
|
|
$this->mink->getSession()->getResponseHeaders(), |
96
|
|
|
CASE_LOWER |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getHttpHeader($name) |
101
|
|
|
{ |
102
|
|
|
$name = strtolower($name); |
103
|
|
|
$headers = $this->getHttpHeaders(); |
104
|
|
|
|
105
|
|
|
if (isset($headers[$name])) { |
106
|
|
|
if (is_array($headers[$name])) { |
107
|
|
|
$value = implode(', ', $headers[$name]); |
108
|
|
|
} |
109
|
|
|
else { |
110
|
|
|
$value = $headers[$name]; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
else { |
114
|
|
|
throw new \OutOfBoundsException( |
115
|
|
|
"The header '$name' doesn't exist" |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
return $value; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function resetHttpHeaders() |
122
|
|
|
{ |
123
|
|
|
$client = $this->mink->getSession()->getDriver()->getClient(); |
|
|
|
|
124
|
|
|
|
125
|
|
|
// At this point, `$client` is not supposed to be Goutte\Client |
126
|
|
|
// Otherwise, this code is overridden on HttpCall\Request\Goutte |
127
|
|
|
/** @var \Symfony\Component\BrowserKit\Client $client */ |
128
|
|
|
$client->setServerParameters([]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: