1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overwatch\ServiceBundle\Expectation; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Overwatch\ExpectationBundle\Exception as Result; |
7
|
|
|
use Overwatch\ExpectationBundle\Expectation\ExpectationInterface; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* ToResolveToExpectation |
12
|
|
|
* Expectation classes are the actual runners of tests. |
13
|
|
|
* This is the runner for the "toRespondHttp" expectation. |
14
|
|
|
*/ |
15
|
|
|
class ToRespondHttpExpectation implements ExpectationInterface |
16
|
|
|
{ |
17
|
|
|
private $config; |
18
|
|
|
private $client; |
19
|
|
|
|
20
|
36 |
|
public function __construct($config, $client_options = []) |
|
|
|
|
21
|
|
|
{ |
22
|
36 |
|
$this->config = $config; |
23
|
36 |
|
$this->client = new Client($client_options); |
24
|
36 |
|
} |
25
|
|
|
|
26
|
9 |
|
public function run($actual, $expected = null) |
27
|
|
|
{ |
28
|
9 |
|
$actual = filter_var($actual, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED); |
29
|
|
|
|
30
|
9 |
|
if ($actual === false) { |
31
|
1 |
|
throw new \InvalidArgumentException('The actual value provided is not a valid URL'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
try { |
35
|
8 |
|
$response = $this->client->get( |
36
|
8 |
|
$actual, |
37
|
|
|
[ |
38
|
8 |
|
'allow_redirects' => false, |
39
|
8 |
|
'http_errors' => false, |
40
|
8 |
|
'timeout' => $this->config['timeout'] |
41
|
8 |
|
] |
42
|
8 |
|
); |
43
|
8 |
|
} catch (\Exception $e) { |
44
|
|
|
//Transform exception under certain circumstances, else re-throw. |
45
|
1 |
|
if ($e instanceof \GuzzleHttp\Exception\RequestException && !$e->hasResponse()) { |
46
|
1 |
|
throw new Result\ExpectationFailedException("Expected $actual to respond HTTP $expected, actually failed to respond", 0, $e); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
throw $e; |
50
|
|
|
} |
51
|
|
|
|
52
|
7 |
|
$result = $response->getStatusCode(); |
53
|
|
|
|
54
|
7 |
|
if ($this->isValidStatusCode($expected)) { |
55
|
2 |
|
if ((int) $expected !== $result) { |
56
|
1 |
|
throw new Result\ExpectationFailedException("Expected $actual to respond HTTP $expected, actually responded HTTP $result"); |
|
|
|
|
57
|
|
|
} |
58
|
1 |
|
} else { |
59
|
5 |
|
if (in_array($result, $this->config['unsatisfactory_codes'])) { |
60
|
1 |
|
throw new Result\ExpectationUnsatisfactoryException("$actual responded HTTP $result, which is configured as unsatisfactory"); |
|
|
|
|
61
|
4 |
|
} elseif (!in_array($result, $this->config['allowable_codes'])) { |
62
|
2 |
|
throw new Result\ExpectationFailedException("$actual responded HTTP $result, which is configured as a failed result"); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
return "Responded HTTP $result " . $response->getReasonPhrase(); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
7 |
|
private function isValidStatusCode($code) |
70
|
|
|
{ |
71
|
7 |
|
return in_array($code, array_keys(Response::$statusTexts)); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
This check marks parameter names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.