Completed
Push — master ( 46fde5...13f008 )
by Nils
05:11
created

ExistsRule::doValidation()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Http\Header;
4
5
use whm\Smoke\Http\Response;
6
use whm\Smoke\Rules\StandardRule;
7
use whm\Smoke\Rules\ValidationFailedException;
8
9
/**
10
 * This rule checks if gzip compressions is activated.
11
 */
12
class ExistsRule extends StandardRule
13
{
14
    private $checkedHeaders;
15
16
    public function init(array $checkedHeaders)
17
    {
18
        $this->checkedHeaders = $checkedHeaders;
19
    }
20
21
    public function doValidation(Response $response)
22
    {
23
        foreach ($this->checkedHeaders as $headerConfig) {
24
            if (!$response->hasHeader($headerConfig['key'])) {
25
                throw new ValidationFailedException('Header not found (' . $headerConfig['key'] . ')');
26
            }
27
28
            $currentValue = $response->getHeader($headerConfig['key'])[0];
29
30
            if ($currentValue != $headerConfig['value']) {
31
                throw new ValidationFailedException('Header "' . $headerConfig['key'] . '" does not equal "' . $headerConfig['value'] . '". Current value is "' . $currentValue . '"');
32
            }
33
        }
34
    }
35
}
36