@@ -30,12 +30,12 @@ trait WebTestAssertionsTrait
30
30
{
31
31
public static function assertResponseIsSuccessful (string $ message = '' ): void
32
32
{
33
- self ::assertThat (static ::getResponse (), new ResponseConstraint \ResponseIsSuccessful (), $ message );
33
+ self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseIsSuccessful (), $ message );
34
34
}
35
35
36
36
public static function assertResponseStatusCodeSame (int $ expectedCode , string $ message = '' ): void
37
37
{
38
- self ::assertThat (static ::getResponse (), new ResponseConstraint \ResponseStatusCodeSame ($ expectedCode ), $ message );
38
+ self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseStatusCodeSame ($ expectedCode ), $ message );
39
39
}
40
40
41
41
public static function assertResponseRedirects (string $ expectedLocation = null , int $ expectedCode = null , string $ message = '' ): void
@@ -48,94 +48,94 @@ public static function assertResponseRedirects(string $expectedLocation = null,
48
48
$ constraint = LogicalAnd::fromConstraints ($ constraint , new ResponseConstraint \ResponseStatusCodeSame ($ expectedCode ));
49
49
}
50
50
51
- self ::assertThat (static ::getResponse (), $ constraint , $ message );
51
+ self ::assertThat (self ::getResponse (), $ constraint , $ message );
52
52
}
53
53
54
54
public static function assertResponseHasHeader (string $ headerName , string $ message = '' ): void
55
55
{
56
- self ::assertThat (static ::getResponse (), new ResponseConstraint \ResponseHasHeader ($ headerName ), $ message );
56
+ self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseHasHeader ($ headerName ), $ message );
57
57
}
58
58
59
59
public static function assertResponseNotHasHeader (string $ headerName , string $ message = '' ): void
60
60
{
61
- self ::assertThat (static ::getResponse (), new LogicalNot (new ResponseConstraint \ResponseHasHeader ($ headerName )), $ message );
61
+ self ::assertThat (self ::getResponse (), new LogicalNot (new ResponseConstraint \ResponseHasHeader ($ headerName )), $ message );
62
62
}
63
63
64
64
public static function assertResponseHeaderSame (string $ headerName , string $ expectedValue , string $ message = '' ): void
65
65
{
66
- self ::assertThat (static ::getResponse (), new ResponseConstraint \ResponseHeaderSame ($ headerName , $ expectedValue ), $ message );
66
+ self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseHeaderSame ($ headerName , $ expectedValue ), $ message );
67
67
}
68
68
69
69
public static function assertResponseHeaderNotSame (string $ headerName , string $ expectedValue , string $ message = '' ): void
70
70
{
71
- self ::assertThat (static ::getResponse (), new LogicalNot (new ResponseConstraint \ResponseHeaderSame ($ headerName , $ expectedValue )), $ message );
71
+ self ::assertThat (self ::getResponse (), new LogicalNot (new ResponseConstraint \ResponseHeaderSame ($ headerName , $ expectedValue )), $ message );
72
72
}
73
73
74
74
public static function assertResponseHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
75
75
{
76
- self ::assertThat (static ::getResponse (), new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain ), $ message );
76
+ self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain ), $ message );
77
77
}
78
78
79
79
public static function assertResponseNotHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
80
80
{
81
- self ::assertThat (static ::getResponse (), new LogicalNot (new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain )), $ message );
81
+ self ::assertThat (self ::getResponse (), new LogicalNot (new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain )), $ message );
82
82
}
83
83
84
84
public static function assertResponseCookieValueSame (string $ name , string $ expectedValue , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
85
85
{
86
- self ::assertThat (static ::getResponse (), LogicalAnd::fromConstraints (
86
+ self ::assertThat (self ::getResponse (), LogicalAnd::fromConstraints (
87
87
new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain ),
88
88
new ResponseConstraint \ResponseCookieValueSame ($ name , $ expectedValue , $ path , $ domain )
89
89
), $ message );
90
90
}
91
91
92
92
public static function assertBrowserHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
93
93
{
94
- self ::assertThat (static ::getClient (), new BrowserKitConstraint \BrowserHasCookie ($ name , $ path , $ domain ), $ message );
94
+ self ::assertThat (self ::getClient (), new BrowserKitConstraint \BrowserHasCookie ($ name , $ path , $ domain ), $ message );
95
95
}
96
96
97
97
public static function assertBrowserNotHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
98
98
{
99
- self ::assertThat (static ::getClient (), new LogicalNot (new BrowserKitConstraint \BrowserHasCookie ($ name , $ path , $ domain )), $ message );
99
+ self ::assertThat (self ::getClient (), new LogicalNot (new BrowserKitConstraint \BrowserHasCookie ($ name , $ path , $ domain )), $ message );
100
100
}
101
101
102
102
public static function assertBrowserCookieValueSame (string $ name , string $ expectedValue , bool $ raw = false , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
103
103
{
104
- self ::assertThat (static ::getClient (), LogicalAnd::fromConstraints (
104
+ self ::assertThat (self ::getClient (), LogicalAnd::fromConstraints (
105
105
new BrowserKitConstraint \BrowserHasCookie ($ name , $ path , $ domain ),
106
106
new BrowserKitConstraint \BrowserCookieValueSame ($ name , $ expectedValue , $ raw , $ path , $ domain )
107
107
), $ message );
108
108
}
109
109
110
110
public static function assertSelectorExists (string $ selector , string $ message = '' ): void
111
111
{
112
- self ::assertThat (static ::getCrawler (), new DomCrawlerConstraint \CrawlerSelectorExists ($ selector ), $ message );
112
+ self ::assertThat (self ::getCrawler (), new DomCrawlerConstraint \CrawlerSelectorExists ($ selector ), $ message );
113
113
}
114
114
115
115
public static function assertSelectorNotExists (string $ selector , string $ message = '' ): void
116
116
{
117
- self ::assertThat (static ::getCrawler (), new LogicalNot (new DomCrawlerConstraint \CrawlerSelectorExists ($ selector )), $ message );
117
+ self ::assertThat (self ::getCrawler (), new LogicalNot (new DomCrawlerConstraint \CrawlerSelectorExists ($ selector )), $ message );
118
118
}
119
119
120
120
public static function assertSelectorTextContains (string $ selector , string $ text , string $ message = '' ): void
121
121
{
122
- self ::assertThat (static ::getCrawler (), LogicalAnd::fromConstraints (
122
+ self ::assertThat (self ::getCrawler (), LogicalAnd::fromConstraints (
123
123
new DomCrawlerConstraint \CrawlerSelectorExists ($ selector ),
124
124
new DomCrawlerConstraint \CrawlerSelectorTextContains ($ selector , $ text )
125
125
), $ message );
126
126
}
127
127
128
128
public static function assertSelectorTextSame (string $ selector , string $ text , string $ message = '' ): void
129
129
{
130
- self ::assertThat (static ::getCrawler (), LogicalAnd::fromConstraints (
130
+ self ::assertThat (self ::getCrawler (), LogicalAnd::fromConstraints (
131
131
new DomCrawlerConstraint \CrawlerSelectorExists ($ selector ),
132
132
new DomCrawlerConstraint \CrawlerSelectorTextSame ($ selector , $ text )
133
133
), $ message );
134
134
}
135
135
136
136
public static function assertSelectorTextNotContains (string $ selector , string $ text , string $ message = '' ): void
137
137
{
138
- self ::assertThat (static ::getCrawler (), LogicalAnd::fromConstraints (
138
+ self ::assertThat (self ::getCrawler (), LogicalAnd::fromConstraints (
139
139
new DomCrawlerConstraint \CrawlerSelectorExists ($ selector ),
140
140
new LogicalNot (new DomCrawlerConstraint \CrawlerSelectorTextContains ($ selector , $ text ))
141
141
), $ message );
@@ -153,23 +153,23 @@ public static function assertPageTitleContains(string $expectedTitle, string $me
153
153
154
154
public static function assertInputValueSame (string $ fieldName , string $ expectedValue , string $ message = '' ): void
155
155
{
156
- self ::assertThat (static ::getCrawler (), LogicalAnd::fromConstraints (
156
+ self ::assertThat (self ::getCrawler (), LogicalAnd::fromConstraints (
157
157
new DomCrawlerConstraint \CrawlerSelectorExists ("input[name= \"$ fieldName \"] " ),
158
158
new DomCrawlerConstraint \CrawlerSelectorAttributeValueSame ("input[name= \"$ fieldName \"] " , 'value ' , $ expectedValue )
159
159
), $ message );
160
160
}
161
161
162
162
public static function assertInputValueNotSame (string $ fieldName , string $ expectedValue , string $ message = '' ): void
163
163
{
164
- self ::assertThat (static ::getCrawler (), LogicalAnd::fromConstraints (
164
+ self ::assertThat (self ::getCrawler (), LogicalAnd::fromConstraints (
165
165
new DomCrawlerConstraint \CrawlerSelectorExists ("input[name= \"$ fieldName \"] " ),
166
166
new LogicalNot (new DomCrawlerConstraint \CrawlerSelectorAttributeValueSame ("input[name= \"$ fieldName \"] " , 'value ' , $ expectedValue ))
167
167
), $ message );
168
168
}
169
169
170
170
public static function assertRequestAttributeValueSame (string $ name , string $ expectedValue , string $ message = '' ): void
171
171
{
172
- self ::assertThat (static ::getRequest (), new ResponseConstraint \RequestAttributeValueSame ($ name , $ expectedValue ), $ message );
172
+ self ::assertThat (self ::getRequest (), new ResponseConstraint \RequestAttributeValueSame ($ name , $ expectedValue ), $ message );
173
173
}
174
174
175
175
public static function assertRouteSame ($ expectedRoute , array $ parameters = [], string $ message = '' ): void
@@ -183,21 +183,27 @@ public static function assertRouteSame($expectedRoute, array $parameters = [], s
183
183
$ constraint = LogicalAnd::fromConstraints ($ constraint , ...$ constraints );
184
184
}
185
185
186
- self ::assertThat (static ::getRequest (), $ constraint , $ message );
186
+ self ::assertThat (self ::getRequest (), $ constraint , $ message );
187
187
}
188
188
189
- private static function getClient (): KernelBrowser
189
+ private static function getClient (KernelBrowser $ newClient = null ): ? KernelBrowser
190
190
{
191
- if (!static ::$ client instanceof KernelBrowser) {
192
- static ::fail (\sprintf ('A client must be set to make assertions on it. Did you forget to call "%s::createClient"? ' , __CLASS__ ));
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__ ));
193
199
}
194
200
195
- return static :: $ client ;
201
+ return $ client ;
196
202
}
197
203
198
204
private static function getCrawler (): Crawler
199
205
{
200
- if (!$ crawler = static ::getClient ()->getCrawler ()) {
206
+ if (!$ crawler = self ::getClient ()->getCrawler ()) {
201
207
static ::fail ('A client must have a crawler to make assertions. Did you forget to make an HTTP request? ' );
202
208
}
203
209
@@ -206,7 +212,7 @@ private static function getCrawler(): Crawler
206
212
207
213
private static function getResponse (): Response
208
214
{
209
- if (!$ response = static ::getClient ()->getResponse ()) {
215
+ if (!$ response = self ::getClient ()->getResponse ()) {
210
216
static ::fail ('A client must have an HTTP Response to make assertions. Did you forget to make an HTTP request? ' );
211
217
}
212
218
@@ -215,7 +221,7 @@ private static function getResponse(): Response
215
221
216
222
private static function getRequest (): Request
217
223
{
218
- if (!$ request = static ::getClient ()->getRequest ()) {
224
+ if (!$ request = self ::getClient ()->getRequest ()) {
219
225
static ::fail ('A client must have an HTTP Request to make assertions. Did you forget to make an HTTP request? ' );
220
226
}
221
227
0 commit comments