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