1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace 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
|
|
View Code Duplication |
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
|
|
|
|
65
|
|
|
return $this->mink->getSession()->getPage(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
public function setHttpHeader($name, $value) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$client = $this->mink->getSession()->getDriver()->getClient(); |
|
|
|
|
71
|
|
|
// Use for goutte Driver only |
72
|
|
|
if (method_exists($client, 'setHeader')) { |
73
|
|
|
$client->setHeader($name, $value); |
74
|
|
|
} else { |
75
|
|
|
// Browserkit |
76
|
|
|
$client->setServerParameter($name, $value); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getHttpHeaders() |
81
|
|
|
{ |
82
|
|
|
return array_change_key_case( |
83
|
|
|
$this->mink->getSession()->getResponseHeaders(), |
84
|
|
|
CASE_LOWER |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getHttpHeader($name) |
89
|
|
|
{ |
90
|
|
|
$name = strtolower($name); |
91
|
|
|
$headers = $this->getHttpHeaders(); |
92
|
|
|
|
93
|
|
|
if (isset($headers[$name])) { |
94
|
|
|
if (is_array($headers[$name])) { |
95
|
|
|
$value = implode(', ', $headers[$name]); |
96
|
|
|
} |
97
|
|
|
else { |
98
|
|
|
$value = $headers[$name]; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
else { |
102
|
|
|
throw new \OutOfBoundsException( |
103
|
|
|
"The header '$name' doesn't exist" |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
return $value; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.