1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Pitchart\Phlunit\Checks; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\Assert; |
7
|
|
|
use Pitchart\Phlunit\Checks\HttpResponse\HttpHeaderCheck; |
8
|
|
|
use Pitchart\Phlunit\Checks\Mixin\ConstraintCheck; |
9
|
|
|
use Pitchart\Phlunit\Checks\Mixin\TypeCheck; |
10
|
|
|
use Pitchart\Phlunit\Checks\Mixin\WithMessage; |
11
|
|
|
use Pitchart\Phlunit\Constraint\HttpResponse\HasContentType; |
12
|
|
|
use Pitchart\Phlunit\Constraint\HttpResponse\HasHeader; |
13
|
|
|
use Pitchart\Phlunit\Constraint\HttpResponse\HasReason; |
14
|
|
|
use Pitchart\Phlunit\Constraint\HttpResponse\HasStatusCode; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ResponseCheck |
19
|
|
|
* |
20
|
|
|
* @package Pitchart\Phlunit\Checks |
21
|
|
|
* |
22
|
|
|
* @author Julien VITTE <[email protected]> |
23
|
|
|
* |
24
|
|
|
* @implements FluentCheck<ResponseInterface> |
25
|
|
|
*/ |
26
|
|
|
class ResponseCheck implements FluentCheck |
27
|
|
|
{ |
28
|
1 |
|
use TypeCheck, ConstraintCheck, WithMessage; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ResponseInterface |
32
|
|
|
*/ |
33
|
|
|
private $value; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* ResponseCheck constructor. |
37
|
|
|
* |
38
|
|
|
* @param ResponseInterface $value |
39
|
|
|
*/ |
40
|
13 |
|
public function __construct(ResponseInterface $value) |
41
|
|
|
{ |
42
|
13 |
|
$this->value = $value; |
43
|
13 |
|
} |
44
|
|
|
|
45
|
3 |
|
public function hasHeader(string $header): self |
46
|
|
|
{ |
47
|
3 |
|
Assert::assertThat($this->value, new HasHeader($header), $this->message); |
48
|
3 |
|
$this->resetMessage(); |
49
|
3 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function whoseHeader(string $header): HttpHeaderCheck |
53
|
|
|
{ |
54
|
1 |
|
$this->hasHeader($header); |
55
|
1 |
|
return new HttpHeaderCheck($this->value, $header); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param int|string $value |
60
|
|
|
* |
61
|
|
|
* @return ResponseCheck |
62
|
|
|
*/ |
63
|
2 |
|
public function hasStatus($value): self |
64
|
|
|
{ |
65
|
2 |
|
Assert::assertThat($this->value, new HasStatusCode($value), $this->message); |
66
|
2 |
|
$this->resetMessage(); |
67
|
2 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
public function hasReason(string $reason): self |
71
|
|
|
{ |
72
|
2 |
|
Assert::assertThat($this->value, new HasReason($reason), $this->message); |
73
|
2 |
|
$this->resetMessage(); |
74
|
2 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function isJson(): self |
78
|
|
|
{ |
79
|
1 |
|
Assert::assertThat($this->value, new HasContentType('application/json'), $this->message); |
80
|
1 |
|
$this->resetMessage(); |
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function isHtml(): self |
85
|
|
|
{ |
86
|
1 |
|
Assert::assertThat($this->value, new HasContentType('text/html'), $this->message); |
87
|
1 |
|
$this->resetMessage(); |
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function isXml(): self |
92
|
|
|
{ |
93
|
1 |
|
Assert::assertThat($this->value, new HasContentType('application/xml'), $this->message); |
94
|
1 |
|
$this->resetMessage(); |
95
|
1 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
public function asJson(): JsonCheck |
99
|
|
|
{ |
100
|
1 |
|
$content = $this->value->getBody()->getContents(); |
101
|
1 |
|
Assert::assertJson($content); |
102
|
1 |
|
return new JsonCheck($content); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
public function asXml(): XmlCheck |
106
|
|
|
{ |
107
|
1 |
|
$content = $this->value->getBody()->getContents(); |
108
|
1 |
|
return new XmlCheck($content); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|