|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of project-quality-inspector. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Alexandre GESLIN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace ProjectQualityInspector\Rule; |
|
12
|
|
|
|
|
13
|
|
|
use ProjectQualityInspector\Application\ProcessHelper; |
|
14
|
|
|
use ProjectQualityInspector\Exception\ExpectationFailedException; |
|
15
|
|
|
use GuzzleHttp\Psr7\Request; |
|
16
|
|
|
use GuzzleHttp\Psr7\Response; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class CurlRule |
|
20
|
|
|
* |
|
21
|
|
|
* @package ProjectQualityInspector\Rule |
|
22
|
|
|
*/ |
|
23
|
|
|
class CurlRule extends AbstractRule |
|
24
|
|
|
{ |
|
25
|
|
|
private $client; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(array $config, $baseDir) |
|
28
|
|
|
{ |
|
29
|
|
|
parent::__construct($config, $baseDir); |
|
30
|
|
|
|
|
31
|
|
|
$this->client = new \GuzzleHttp\Client(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritdoc |
|
36
|
|
|
*/ |
|
37
|
|
|
public function evaluate() |
|
38
|
|
|
{ |
|
39
|
|
|
$expectationsFailedExceptions = []; |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
foreach ($this->config['queries'] as $query) { |
|
42
|
|
|
try { |
|
43
|
|
|
$this->expectsResponseMatches($query); |
|
44
|
|
|
} catch (ExpectationFailedException $e) { |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param array $query |
|
52
|
|
|
*/ |
|
53
|
|
|
public function expectsResponseMatches($query) |
|
54
|
|
|
{ |
|
55
|
|
|
$request = $query['request']; |
|
56
|
|
|
$expectedResponse = $query['expectedResponse']; |
|
57
|
|
|
$request['url'] = ($request['url']) ? $request['url'] : $this->config['base-url']; |
|
58
|
|
|
$request['method'] = ($request['method']) ? $request['method'] : 'GET'; |
|
59
|
|
|
|
|
60
|
|
|
$response = $this->client->send(new Request($request['method'], $request['url'])); |
|
61
|
|
|
|
|
62
|
|
|
try { |
|
63
|
|
|
$this->expectsHeadersMatches($expectedResponse, $response); |
|
|
|
|
|
|
64
|
|
|
$this->addAssertion($request['url']); |
|
65
|
|
|
} catch (ExpectationFailedException $e) { |
|
66
|
|
|
$expectationsFailedExceptions[] = $e; |
|
|
|
|
|
|
67
|
|
|
$this->addAssertion($request['url'], [['message' => $e->getMessage() . $e->getReason(), 'type' => 'expectsHeadersMatches']]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (count($expectationsFailedExceptions)) { |
|
71
|
|
|
$this->throwRuleViolationException($expectationsFailedExceptions); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param array $expectedResponse |
|
77
|
|
|
* @param array $response |
|
78
|
|
|
* |
|
79
|
|
|
* @throws ExpectationFailedException |
|
80
|
|
|
*/ |
|
81
|
|
|
private function expectsHeadersMatches($expectedResponse, Response $response) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->expectsStatusCodeMatches($expectedResponse, $response); |
|
84
|
|
|
|
|
85
|
|
|
foreach ($expectedResponse['headers'] as $expectedHeader => $expectedHeaderValue) { |
|
86
|
|
|
if (!isset($response->getheaders()[$expectedHeader])) { |
|
87
|
|
|
$message = sprintf('there is no expected header "%s" in response of url', $expectedHeader); |
|
88
|
|
|
throw new ExpectationFailedException($mergedBranches, $message); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($response->getheaders()[$expectedHeader][0] != $expectedHeaderValue) { |
|
92
|
|
|
$message = sprintf('the expected "%s" header\'s value should be "%s". Current value is %s', $expectedHeader, $expectedHeaderValue, $response->getheaders()[$expectedHeader][0]); |
|
93
|
|
|
throw new ExpectationFailedException($mergedBranches, $message); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param array $expectedResponse |
|
100
|
|
|
* @param array $response |
|
101
|
|
|
* |
|
102
|
|
|
* @throws ExpectationFailedException |
|
103
|
|
|
*/ |
|
104
|
|
|
private function expectsStatusCodeMatches($expectedResponse, Response $response) |
|
105
|
|
|
{ |
|
106
|
|
|
if (isset($expectedResponse['statusCode']) && $expectedResponse['statusCode'] != $response->getStatusCode()) { |
|
107
|
|
|
$message = sprintf('the expected status code should be "%s". Current value is "%s"', $expectedResponse['statusCode'], $response->getStatusCode()); |
|
108
|
|
|
throw new ExpectationFailedException($mergedBranches, $message); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.