13
13
14
14
use PHPUnit \Framework \Constraint \LogicalAnd ;
15
15
use PHPUnit \Framework \Constraint \LogicalNot ;
16
- use Symfony \Bundle \FrameworkBundle \KernelBrowser ;
17
- use Symfony \Component \BrowserKit \Test \Constraint as BrowserKitConstraint ;
18
16
use Symfony \Component \DomCrawler \Crawler ;
19
17
use Symfony \Component \DomCrawler \Test \Constraint as DomCrawlerConstraint ;
20
- use Symfony \Component \HttpFoundation \Request ;
21
- use Symfony \Component \HttpFoundation \Response ;
22
- use Symfony \Component \HttpFoundation \Test \Constraint as ResponseConstraint ;
23
18
24
19
/**
25
20
* Ideas borrowed from Laravel Dusk's assertions.
28
23
*/
29
24
trait WebTestAssertionsTrait
30
25
{
31
- public static function assertResponseIsSuccessful (string $ message = '' ): void
32
- {
33
- self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseIsSuccessful (), $ message );
34
- }
35
-
36
- public static function assertResponseStatusCodeSame (int $ expectedCode , string $ message = '' ): void
37
- {
38
- self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseStatusCodeSame ($ expectedCode ), $ message );
39
- }
40
-
41
- public static function assertResponseRedirects (string $ expectedLocation = null , int $ expectedCode = null , string $ message = '' ): void
42
- {
43
- $ constraint = new ResponseConstraint \ResponseIsRedirected ();
44
- if ($ expectedLocation ) {
45
- $ constraint = LogicalAnd::fromConstraints ($ constraint , new ResponseConstraint \ResponseHeaderSame ('Location ' , $ expectedLocation ));
46
- }
47
- if ($ expectedCode ) {
48
- $ constraint = LogicalAnd::fromConstraints ($ constraint , new ResponseConstraint \ResponseStatusCodeSame ($ expectedCode ));
49
- }
50
-
51
- self ::assertThat (self ::getResponse (), $ constraint , $ message );
52
- }
53
-
54
- public static function assertResponseHasHeader (string $ headerName , string $ message = '' ): void
55
- {
56
- self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseHasHeader ($ headerName ), $ message );
57
- }
58
-
59
- public static function assertResponseNotHasHeader (string $ headerName , string $ message = '' ): void
60
- {
61
- self ::assertThat (self ::getResponse (), new LogicalNot (new ResponseConstraint \ResponseHasHeader ($ headerName )), $ message );
62
- }
63
-
64
- public static function assertResponseHeaderSame (string $ headerName , string $ expectedValue , string $ message = '' ): void
65
- {
66
- self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseHeaderSame ($ headerName , $ expectedValue ), $ message );
67
- }
68
-
69
- public static function assertResponseHeaderNotSame (string $ headerName , string $ expectedValue , string $ message = '' ): void
70
- {
71
- self ::assertThat (self ::getResponse (), new LogicalNot (new ResponseConstraint \ResponseHeaderSame ($ headerName , $ expectedValue )), $ message );
72
- }
73
-
74
- public static function assertResponseHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
75
- {
76
- self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain ), $ message );
77
- }
78
-
79
- public static function assertResponseNotHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
80
- {
81
- self ::assertThat (self ::getResponse (), new LogicalNot (new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain )), $ message );
82
- }
83
-
84
- public static function assertResponseCookieValueSame (string $ name , string $ expectedValue , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
85
- {
86
- self ::assertThat (self ::getResponse (), LogicalAnd::fromConstraints (
87
- new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain ),
88
- new ResponseConstraint \ResponseCookieValueSame ($ name , $ expectedValue , $ path , $ domain )
89
- ), $ message );
90
- }
91
-
92
- public static function assertBrowserHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
93
- {
94
- self ::assertThat (self ::getClient (), new BrowserKitConstraint \BrowserHasCookie ($ name , $ path , $ domain ), $ message );
95
- }
96
-
97
- public static function assertBrowserNotHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
98
- {
99
- self ::assertThat (self ::getClient (), new LogicalNot (new BrowserKitConstraint \BrowserHasCookie ($ name , $ path , $ domain )), $ message );
100
- }
101
-
102
- public static function assertBrowserCookieValueSame (string $ name , string $ expectedValue , bool $ raw = false , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
103
- {
104
- self ::assertThat (self ::getClient (), LogicalAnd::fromConstraints (
105
- new BrowserKitConstraint \BrowserHasCookie ($ name , $ path , $ domain ),
106
- new BrowserKitConstraint \BrowserCookieValueSame ($ name , $ expectedValue , $ raw , $ path , $ domain )
107
- ), $ message );
108
- }
26
+ use BrowserKitAssertionsTrait;
109
27
110
28
public static function assertSelectorExists (string $ selector , string $ message = '' ): void
111
29
{
@@ -167,40 +85,6 @@ public static function assertInputValueNotSame(string $fieldName, string $expect
167
85
), $ message );
168
86
}
169
87
170
- public static function assertRequestAttributeValueSame (string $ name , string $ expectedValue , string $ message = '' ): void
171
- {
172
- self ::assertThat (self ::getRequest (), new ResponseConstraint \RequestAttributeValueSame ($ name , $ expectedValue ), $ message );
173
- }
174
-
175
- public static function assertRouteSame ($ expectedRoute , array $ parameters = [], string $ message = '' ): void
176
- {
177
- $ constraint = new ResponseConstraint \RequestAttributeValueSame ('_route ' , $ expectedRoute );
178
- $ constraints = [];
179
- foreach ($ parameters as $ key => $ value ) {
180
- $ constraints [] = new ResponseConstraint \RequestAttributeValueSame ($ key , $ value );
181
- }
182
- if ($ constraints ) {
183
- $ constraint = LogicalAnd::fromConstraints ($ constraint , ...$ constraints );
184
- }
185
-
186
- self ::assertThat (self ::getRequest (), $ constraint , $ message );
187
- }
188
-
189
- private static function getClient (KernelBrowser $ newClient = null ): ?KernelBrowser
190
- {
191
- static $ client ;
192
-
193
- if (0 < \func_num_args ()) {
194
- return $ client = $ newClient ;
195
- }
196
-
197
- if (!$ client instanceof KernelBrowser) {
198
- static ::fail (sprintf ('A client must be set to make assertions on it. Did you forget to call "%s::createClient()"? ' , __CLASS__ ));
199
- }
200
-
201
- return $ client ;
202
- }
203
-
204
88
private static function getCrawler (): Crawler
205
89
{
206
90
if (!$ crawler = self ::getClient ()->getCrawler ()) {
@@ -209,22 +93,4 @@ private static function getCrawler(): Crawler
209
93
210
94
return $ crawler ;
211
95
}
212
-
213
- private static function getResponse (): Response
214
- {
215
- if (!$ response = self ::getClient ()->getResponse ()) {
216
- static ::fail ('A client must have an HTTP Response to make assertions. Did you forget to make an HTTP request? ' );
217
- }
218
-
219
- return $ response ;
220
- }
221
-
222
- private static function getRequest (): Request
223
- {
224
- if (!$ request = self ::getClient ()->getRequest ()) {
225
- static ::fail ('A client must have an HTTP Request to make assertions. Did you forget to make an HTTP request? ' );
226
- }
227
-
228
- return $ request ;
229
- }
230
96
}
0 commit comments