@@ -30,12 +30,12 @@ trait WebTestAssertionsTrait
3030{
3131 public static function assertResponseIsSuccessful (string $ message = '' ): void
3232 {
33- self ::assertThat (static ::getResponse (), new ResponseConstraint \ResponseIsSuccessful (), $ message );
33+ self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseIsSuccessful (), $ message );
3434 }
3535
3636 public static function assertResponseStatusCodeSame (int $ expectedCode , string $ message = '' ): void
3737 {
38- self ::assertThat (static ::getResponse (), new ResponseConstraint \ResponseStatusCodeSame ($ expectedCode ), $ message );
38+ self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseStatusCodeSame ($ expectedCode ), $ message );
3939 }
4040
4141 public static function assertResponseRedirects (string $ expectedLocation = null , int $ expectedCode = null , string $ message = '' ): void
@@ -48,94 +48,94 @@ public static function assertResponseRedirects(string $expectedLocation = null,
4848 $ constraint = LogicalAnd::fromConstraints ($ constraint , new ResponseConstraint \ResponseStatusCodeSame ($ expectedCode ));
4949 }
5050
51- self ::assertThat (static ::getResponse (), $ constraint , $ message );
51+ self ::assertThat (self ::getResponse (), $ constraint , $ message );
5252 }
5353
5454 public static function assertResponseHasHeader (string $ headerName , string $ message = '' ): void
5555 {
56- self ::assertThat (static ::getResponse (), new ResponseConstraint \ResponseHasHeader ($ headerName ), $ message );
56+ self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseHasHeader ($ headerName ), $ message );
5757 }
5858
5959 public static function assertResponseNotHasHeader (string $ headerName , string $ message = '' ): void
6060 {
61- self ::assertThat (static ::getResponse (), new LogicalNot (new ResponseConstraint \ResponseHasHeader ($ headerName )), $ message );
61+ self ::assertThat (self ::getResponse (), new LogicalNot (new ResponseConstraint \ResponseHasHeader ($ headerName )), $ message );
6262 }
6363
6464 public static function assertResponseHeaderSame (string $ headerName , string $ expectedValue , string $ message = '' ): void
6565 {
66- self ::assertThat (static ::getResponse (), new ResponseConstraint \ResponseHeaderSame ($ headerName , $ expectedValue ), $ message );
66+ self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseHeaderSame ($ headerName , $ expectedValue ), $ message );
6767 }
6868
6969 public static function assertResponseHeaderNotSame (string $ headerName , string $ expectedValue , string $ message = '' ): void
7070 {
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 );
7272 }
7373
7474 public static function assertResponseHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
7575 {
76- self ::assertThat (static ::getResponse (), new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain ), $ message );
76+ self ::assertThat (self ::getResponse (), new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain ), $ message );
7777 }
7878
7979 public static function assertResponseNotHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
8080 {
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 );
8282 }
8383
8484 public static function assertResponseCookieValueSame (string $ name , string $ expectedValue , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
8585 {
86- self ::assertThat (static ::getResponse (), LogicalAnd::fromConstraints (
86+ self ::assertThat (self ::getResponse (), LogicalAnd::fromConstraints (
8787 new ResponseConstraint \ResponseHasCookie ($ name , $ path , $ domain ),
8888 new ResponseConstraint \ResponseCookieValueSame ($ name , $ expectedValue , $ path , $ domain )
8989 ), $ message );
9090 }
9191
9292 public static function assertBrowserHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
9393 {
94- self ::assertThat (static ::getClient (), new BrowserKitConstraint \BrowserHasCookie ($ name , $ path , $ domain ), $ message );
94+ self ::assertThat (self ::getClient (), new BrowserKitConstraint \BrowserHasCookie ($ name , $ path , $ domain ), $ message );
9595 }
9696
9797 public static function assertBrowserNotHasCookie (string $ name , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
9898 {
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 );
100100 }
101101
102102 public static function assertBrowserCookieValueSame (string $ name , string $ expectedValue , bool $ raw = false , string $ path = '/ ' , string $ domain = null , string $ message = '' ): void
103103 {
104- self ::assertThat (static ::getClient (), LogicalAnd::fromConstraints (
104+ self ::assertThat (self ::getClient (), LogicalAnd::fromConstraints (
105105 new BrowserKitConstraint \BrowserHasCookie ($ name , $ path , $ domain ),
106106 new BrowserKitConstraint \BrowserCookieValueSame ($ name , $ expectedValue , $ raw , $ path , $ domain )
107107 ), $ message );
108108 }
109109
110110 public static function assertSelectorExists (string $ selector , string $ message = '' ): void
111111 {
112- self ::assertThat (static ::getCrawler (), new DomCrawlerConstraint \CrawlerSelectorExists ($ selector ), $ message );
112+ self ::assertThat (self ::getCrawler (), new DomCrawlerConstraint \CrawlerSelectorExists ($ selector ), $ message );
113113 }
114114
115115 public static function assertSelectorNotExists (string $ selector , string $ message = '' ): void
116116 {
117- self ::assertThat (static ::getCrawler (), new LogicalNot (new DomCrawlerConstraint \CrawlerSelectorExists ($ selector )), $ message );
117+ self ::assertThat (self ::getCrawler (), new LogicalNot (new DomCrawlerConstraint \CrawlerSelectorExists ($ selector )), $ message );
118118 }
119119
120120 public static function assertSelectorTextContains (string $ selector , string $ text , string $ message = '' ): void
121121 {
122- self ::assertThat (static ::getCrawler (), LogicalAnd::fromConstraints (
122+ self ::assertThat (self ::getCrawler (), LogicalAnd::fromConstraints (
123123 new DomCrawlerConstraint \CrawlerSelectorExists ($ selector ),
124124 new DomCrawlerConstraint \CrawlerSelectorTextContains ($ selector , $ text )
125125 ), $ message );
126126 }
127127
128128 public static function assertSelectorTextSame (string $ selector , string $ text , string $ message = '' ): void
129129 {
130- self ::assertThat (static ::getCrawler (), LogicalAnd::fromConstraints (
130+ self ::assertThat (self ::getCrawler (), LogicalAnd::fromConstraints (
131131 new DomCrawlerConstraint \CrawlerSelectorExists ($ selector ),
132132 new DomCrawlerConstraint \CrawlerSelectorTextSame ($ selector , $ text )
133133 ), $ message );
134134 }
135135
136136 public static function assertSelectorTextNotContains (string $ selector , string $ text , string $ message = '' ): void
137137 {
138- self ::assertThat (static ::getCrawler (), LogicalAnd::fromConstraints (
138+ self ::assertThat (self ::getCrawler (), LogicalAnd::fromConstraints (
139139 new DomCrawlerConstraint \CrawlerSelectorExists ($ selector ),
140140 new LogicalNot (new DomCrawlerConstraint \CrawlerSelectorTextContains ($ selector , $ text ))
141141 ), $ message );
@@ -153,23 +153,23 @@ public static function assertPageTitleContains(string $expectedTitle, string $me
153153
154154 public static function assertInputValueSame (string $ fieldName , string $ expectedValue , string $ message = '' ): void
155155 {
156- self ::assertThat (static ::getCrawler (), LogicalAnd::fromConstraints (
156+ self ::assertThat (self ::getCrawler (), LogicalAnd::fromConstraints (
157157 new DomCrawlerConstraint \CrawlerSelectorExists ("input[name= \"$ fieldName \"] " ),
158158 new DomCrawlerConstraint \CrawlerSelectorAttributeValueSame ("input[name= \"$ fieldName \"] " , 'value ' , $ expectedValue )
159159 ), $ message );
160160 }
161161
162162 public static function assertInputValueNotSame (string $ fieldName , string $ expectedValue , string $ message = '' ): void
163163 {
164- self ::assertThat (static ::getCrawler (), LogicalAnd::fromConstraints (
164+ self ::assertThat (self ::getCrawler (), LogicalAnd::fromConstraints (
165165 new DomCrawlerConstraint \CrawlerSelectorExists ("input[name= \"$ fieldName \"] " ),
166166 new LogicalNot (new DomCrawlerConstraint \CrawlerSelectorAttributeValueSame ("input[name= \"$ fieldName \"] " , 'value ' , $ expectedValue ))
167167 ), $ message );
168168 }
169169
170170 public static function assertRequestAttributeValueSame (string $ name , string $ expectedValue , string $ message = '' ): void
171171 {
172- self ::assertThat (static ::getRequest (), new ResponseConstraint \RequestAttributeValueSame ($ name , $ expectedValue ), $ message );
172+ self ::assertThat (self ::getRequest (), new ResponseConstraint \RequestAttributeValueSame ($ name , $ expectedValue ), $ message );
173173 }
174174
175175 public static function assertRouteSame ($ expectedRoute , array $ parameters = [], string $ message = '' ): void
@@ -183,21 +183,27 @@ public static function assertRouteSame($expectedRoute, array $parameters = [], s
183183 $ constraint = LogicalAnd::fromConstraints ($ constraint , ...$ constraints );
184184 }
185185
186- self ::assertThat (static ::getRequest (), $ constraint , $ message );
186+ self ::assertThat (self ::getRequest (), $ constraint , $ message );
187187 }
188188
189- private static function getClient (): KernelBrowser
189+ private static function getClient (KernelBrowser $ newClient = null ): ? KernelBrowser
190190 {
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__ ));
193199 }
194200
195- return static :: $ client ;
201+ return $ client ;
196202 }
197203
198204 private static function getCrawler (): Crawler
199205 {
200- if (!$ crawler = static ::getClient ()->getCrawler ()) {
206+ if (!$ crawler = self ::getClient ()->getCrawler ()) {
201207 static ::fail ('A client must have a crawler to make assertions. Did you forget to make an HTTP request? ' );
202208 }
203209
@@ -206,7 +212,7 @@ private static function getCrawler(): Crawler
206212
207213 private static function getResponse (): Response
208214 {
209- if (!$ response = static ::getClient ()->getResponse ()) {
215+ if (!$ response = self ::getClient ()->getResponse ()) {
210216 static ::fail ('A client must have an HTTP Response to make assertions. Did you forget to make an HTTP request? ' );
211217 }
212218
@@ -215,7 +221,7 @@ private static function getResponse(): Response
215221
216222 private static function getRequest (): Request
217223 {
218- if (!$ request = static ::getClient ()->getRequest ()) {
224+ if (!$ request = self ::getClient ()->getRequest ()) {
219225 static ::fail ('A client must have an HTTP Request to make assertions. Did you forget to make an HTTP request? ' );
220226 }
221227
0 commit comments