3
3
namespace Zenstruck \Browser ;
4
4
5
5
use Behat \Mink \Session ;
6
+ use PHPUnit \Framework \Assert as PHPUnit ;
6
7
use Symfony \Component \VarDumper \VarDumper ;
7
8
use Zenstruck \Browser \Response \HtmlResponse ;
8
9
use Zenstruck \Browser \Response \JsonResponse ;
@@ -15,6 +16,9 @@ class Response
15
16
{
16
17
private Session $ session ;
17
18
19
+ /**
20
+ * @internal
21
+ */
18
22
final public function __construct (Session $ session )
19
23
{
20
24
$ this ->session = $ session ;
@@ -25,6 +29,9 @@ final public function statusCode(): int
25
29
return $ this ->session ->getStatusCode ();
26
30
}
27
31
32
+ /**
33
+ * @internal
34
+ */
28
35
final public static function createFor (Session $ session ): self
29
36
{
30
37
$ contentType = (string ) $ session ->getResponseHeader ('content-type ' );
@@ -49,21 +56,60 @@ final public function body(): string
49
56
return $ this ->session ->getPage ()->getContent ();
50
57
}
51
58
59
+ final public function assertJson (): JsonResponse
60
+ {
61
+ if (!$ this instanceof JsonResponse) {
62
+ PHPUnit::fail ('Not a json response. ' );
63
+ }
64
+
65
+ return $ this ;
66
+ }
67
+
68
+ final public function assertXml (): XmlResponse
69
+ {
70
+ if (!$ this instanceof XmlResponse) {
71
+ PHPUnit::fail ('Not an xml response. ' );
72
+ }
73
+
74
+ return $ this ;
75
+ }
76
+
77
+ final public function assertHtml (): HtmlResponse
78
+ {
79
+ if (!$ this instanceof HtmlResponse) {
80
+ PHPUnit::fail ('Not an html response. ' );
81
+ }
82
+
83
+ return $ this ;
84
+ }
85
+
86
+ /**
87
+ * @internal
88
+ */
52
89
final public function raw (): string
53
90
{
54
91
return "{$ this ->rawMetadata ()}\n{$ this ->rawBody ()}" ;
55
92
}
56
93
94
+ /**
95
+ * @internal
96
+ */
57
97
final public function isSuccessful (): bool
58
98
{
59
99
return $ this ->statusCode () >= 200 && $ this ->statusCode () < 300 ;
60
100
}
61
101
102
+ /**
103
+ * @internal
104
+ */
62
105
final public function isRedirect (): bool
63
106
{
64
107
return $ this ->statusCode () >= 300 && $ this ->statusCode () < 400 ;
65
108
}
66
109
110
+ /**
111
+ * @internal
112
+ */
67
113
public function dump (?string $ selector = null ): void
68
114
{
69
115
if (null !== $ selector ) {
@@ -73,11 +119,17 @@ public function dump(?string $selector = null): void
73
119
VarDumper::dump ($ this ->raw ());
74
120
}
75
121
122
+ /**
123
+ * @internal
124
+ */
76
125
final protected function session (): Session
77
126
{
78
127
return $ this ->session ;
79
128
}
80
129
130
+ /**
131
+ * @internal
132
+ */
81
133
protected function rawMetadata (): string
82
134
{
83
135
$ ret = "URL: {$ this ->session ->getCurrentUrl ()} ( {$ this ->statusCode ()}) \n\n" ;
@@ -91,6 +143,9 @@ protected function rawMetadata(): string
91
143
return $ ret ;
92
144
}
93
145
146
+ /**
147
+ * @internal
148
+ */
94
149
protected function rawBody (): string
95
150
{
96
151
return $ this ->body ();
0 commit comments