HttpStatusRule   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 19
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A validate() 0 6 2
1
<?php
2
3
namespace whm\Smoke\Rules\Http\Header;
4
5
use Psr\Http\Message\ResponseInterface;
6
use whm\Smoke\Http\Response;
7
use whm\Smoke\Rules\Rule;
8
use whm\Smoke\Rules\ValidationFailedException;
9
10
/**
11
 * This class validates, whether the response has a specific status.
12
 * Can be used to test, if your homepage has a 200 status or your error page sends a 404.
13
 */
14
class HttpStatusRule implements Rule
15
{
16
    protected $expectedStatus = 200;
17
18
    /**
19
     * @param int $expectedStatus The expected HTTP-status
20
     */
21
    public function init($expectedStatus = 200)
22
    {
23
        $this->expectedStatus = $expectedStatus;
24
    }
25
26
    public function validate(ResponseInterface $response)
27
    {
28
        if ($response->getStatusCode() !== $this->expectedStatus) {
29
            throw new ValidationFailedException('Status code ' . $response->getStatusCode() . ' found, ' . $this->expectedStatus . ' expected.');
30
        }
31
    }
32
}
33