HeaderContains::matches()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
1
<?php declare(strict_types=1);
2
3
4
namespace Pitchart\Phlunit\Constraint\HttpResponse;
5
6
use PHPUnit\Framework\Constraint\Constraint;
7
use Psr\Http\Message\ResponseInterface;
8
9
class HeaderContains extends Constraint
10
{
11 1
    use WithHeaderResponseExporter;
12
    /**
13
     * @var string
14
     */
15
    private $header;
16
17
    /**
18
     * @var string
19
     */
20
    private $value;
21
22
    /**
23
     * HeaderIsEqualTo constructor.
24
     *
25
     * @param string $header
26
     * @param string $value
27
     */
28 13
    public function __construct(string $header, string $value)
29
    {
30 13
        $this->header = $header;
31 13
        $this->value = $value;
32 13
    }
33
34 10
    protected function matches($other): bool
35
    {
36 10
        if ($other instanceof ResponseInterface
37 10
            && $other->hasHeader($this->header)
38
        ) {
39 9
            return (bool) \preg_match('/' . \preg_quote($this->value) . '/i', $other->getHeaderLine($this->header));
40
        }
41 1
        return false;
42
    }
43
44
    /**
45
     * @return string
46
     */
47 1
    protected function failureDescription($other): string
48
    {
49 1
        return $this->exporter()->export($other) . ' ' . $this->toString();
50
    }
51
52
53 1
    public function toString(): string
54
    {
55 1
        return \sprintf(" contains value '%s' for header '%s'", $this->value, $this->header);
56
    }
57
}
58