HasContentType::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 HasContentType extends Constraint
10
{
11
    /**
12
     * @var string
13
     */
14
    private $contentType;
15
16
    /**
17
     * HasContentType constructor.
18
     *
19
     * @param string $contentType
20
     */
21 10
    public function __construct($contentType)
22
    {
23 10
        $this->contentType = $contentType;
24 10
    }
25
26 7
    protected function matches($other): bool
27
    {
28 7
        if ($other instanceof ResponseInterface) {
29 6
            return $other->hasHeader('Content-Type')
30 6
                && ($other->getHeaderLine('Content-Type') === $this->contentType);
31
        }
32 1
        return false;
33
    }
34
35 1
    public function toString(): string
36
    {
37 1
        return \sprintf("has expected Content-Type '%s'", $this->contentType);
38
    }
39
40
    /**
41
     * @return string
42
     */
43 1
    protected function failureDescription($other): string
44
    {
45 1
        $contentType = $other instanceof ResponseInterface ? $other->getHeaderLine('Content-Type') : (string) $other;
46 1
        return 'a HTTP Response with Content-Type \'' . $contentType . '\' ' . $this->toString();
47
    }
48
}
49