1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Pitchart\Phlunit\Constraint\HttpResponse; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\Constraint\Constraint; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
|
8
|
|
|
final class HasStatusCode extends Constraint |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var int |
12
|
|
|
*/ |
13
|
|
|
private $code; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param int|string $code |
17
|
|
|
*/ |
18
|
9 |
|
public function __construct($code) |
19
|
|
|
{ |
20
|
9 |
|
$this->code = (int) $code; |
21
|
9 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Returns a string representation of the constraint. |
25
|
|
|
* |
26
|
|
|
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
27
|
|
|
* |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
1 |
|
public function toString(): string |
31
|
|
|
{ |
32
|
1 |
|
return 'has the expected status code ' . $this->exporter()->export($this->code); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Evaluates the constraint for parameter $other. Returns true if the |
37
|
|
|
* constraint is met, false otherwise. |
38
|
|
|
* |
39
|
|
|
* @param mixed $other value or object to evaluate |
40
|
|
|
*/ |
41
|
6 |
|
protected function matches($other): bool |
42
|
|
|
{ |
43
|
6 |
|
if ($other instanceof ResponseInterface) { |
44
|
5 |
|
return $other->getStatusCode() === $this->code; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns the description of the failure |
52
|
|
|
* |
53
|
|
|
* The beginning of failure messages is "Failed asserting that" in most |
54
|
|
|
* cases. This method should return the second part of that sentence. |
55
|
|
|
* |
56
|
|
|
* @param mixed $other evaluated value or object |
57
|
|
|
* |
58
|
|
|
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
1 |
|
protected function failureDescription($other): string |
63
|
|
|
{ |
64
|
1 |
|
$statusCode = $other instanceof ResponseInterface ? $other->getStatusCode() : (string) $other; |
65
|
1 |
|
return 'a HTTP Response with status ' . $statusCode . ' ' . $this->toString(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|