You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #26509 [BrowserKit] Avoid nullable values in some Client's methods (ossinkine)
This PR was merged into the 4.1-dev branch.
Discussion
----------
[BrowserKit] Avoid nullable values in some Client's methods
| Q | A
| ------------- | ---
| Branch? | master | Bug fix? | yes/no
| New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks? | no <!-- see https://symfony.com/bc -->
| Deprecations? | yes <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass? | yes <!-- please add some, will be required by reviewers -->
| License | MIT
<!--
Write a short README entry for your feature/bugfix here (replace this comment block.)
This will help people understand your PR and can be used as a start of the Doc PR.
Additionally:
- Bug fixes must be submitted against the lowest branch where they apply
(lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against the master branch.
-->
I suggest the some methods in `Client` should not return `null` for simplify usage.
If you are trying to get response from client when `request` method was not called, it seems an exception should be throwed.
Commits
-------
c2c2853 Avoid nullable values in some Client's methods
@@ -183,20 +184,30 @@ public function getCookieJar()
183
184
/**
184
185
* Returns the current Crawler instance.
185
186
*
186
-
* @return Crawler|null A Crawler instance
187
+
* @return Crawler A Crawler instance
187
188
*/
188
189
publicfunctiongetCrawler()
189
190
{
191
+
if (null === $this->crawler) {
192
+
@trigger_error(sprintf('Calling the "%s()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.', __METHOD__), E_USER_DEPRECATED);
193
+
// throw new BadMethodCallException(sprintf('The "request()" method must be called before the "%s()" one', __METHOD__));
194
+
}
195
+
190
196
return$this->crawler;
191
197
}
192
198
193
199
/**
194
200
* Returns the current BrowserKit Response instance.
195
201
*
196
-
* @return Response|null A BrowserKit Response instance
202
+
* @return Response A BrowserKit Response instance
197
203
*/
198
204
publicfunctiongetInternalResponse()
199
205
{
206
+
if (null === $this->internalResponse) {
207
+
@trigger_error(sprintf('Calling the "%s()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.', __METHOD__), E_USER_DEPRECATED);
208
+
// throw new BadMethodCallException(sprintf('The "request()" method must be called before the "%s()" one', __METHOD__));
209
+
}
210
+
200
211
return$this->internalResponse;
201
212
}
202
213
@@ -206,22 +217,32 @@ public function getInternalResponse()
206
217
* The origin response is the response instance that is returned
207
218
* by the code that handles requests.
208
219
*
209
-
* @return object|null A response instance
220
+
* @return object A response instance
210
221
*
211
222
* @see doRequest()
212
223
*/
213
224
publicfunctiongetResponse()
214
225
{
226
+
if (null === $this->response) {
227
+
@trigger_error(sprintf('Calling the "%s()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.', __METHOD__), E_USER_DEPRECATED);
228
+
// throw new BadMethodCallException(sprintf('The "request()" method must be called before the "%s()" one', __METHOD__));
229
+
}
230
+
215
231
return$this->response;
216
232
}
217
233
218
234
/**
219
235
* Returns the current BrowserKit Request instance.
220
236
*
221
-
* @return Request|null A BrowserKit Request instance
237
+
* @return Request A BrowserKit Request instance
222
238
*/
223
239
publicfunctiongetInternalRequest()
224
240
{
241
+
if (null === $this->internalRequest) {
242
+
@trigger_error(sprintf('Calling the "%s()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.', __METHOD__), E_USER_DEPRECATED);
243
+
// throw new BadMethodCallException(sprintf('The "request()" method must be called before the "%s()" one', __METHOD__));
244
+
}
245
+
225
246
return$this->internalRequest;
226
247
}
227
248
@@ -231,12 +252,17 @@ public function getInternalRequest()
231
252
* The origin request is the request instance that is sent
232
253
* to the code that handles requests.
233
254
*
234
-
* @return object|null A Request instance
255
+
* @return object A Request instance
235
256
*
236
257
* @see doRequest()
237
258
*/
238
259
publicfunctiongetRequest()
239
260
{
261
+
if (null === $this->request) {
262
+
@trigger_error(sprintf('Calling the "%s()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.', __METHOD__), E_USER_DEPRECATED);
263
+
// throw new BadMethodCallException(sprintf('The "request()" method must be called before the "%s()" one', __METHOD__));
Copy file name to clipboardExpand all lines: src/Symfony/Component/BrowserKit/Tests/ClientTest.php
+44Lines changed: 44 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -94,6 +94,16 @@ public function testGetRequest()
94
94
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
95
95
}
96
96
97
+
/**
98
+
* @group legacy
99
+
* @expectedDeprecation Calling the "Symfony\Component\BrowserKit\Client::getRequest()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.
100
+
*/
101
+
publicfunctiontestGetRequestNull()
102
+
{
103
+
$client = newTestClient();
104
+
$this->assertNull($client->getRequest());
105
+
}
106
+
97
107
publicfunctiontestGetRequestWithXHR()
98
108
{
99
109
$client = newTestClient();
@@ -124,6 +134,16 @@ public function testGetResponse()
124
134
$this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getResponse(), '->getCrawler() returns the Response of the last request');
125
135
}
126
136
137
+
/**
138
+
* @group legacy
139
+
* @expectedDeprecation Calling the "Symfony\Component\BrowserKit\Client::getResponse()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.
140
+
*/
141
+
publicfunctiontestGetResponseNull()
142
+
{
143
+
$client = newTestClient();
144
+
$this->assertNull($client->getResponse());
145
+
}
146
+
127
147
publicfunctiontestGetInternalResponse()
128
148
{
129
149
$client = newTestClient();
@@ -135,6 +155,16 @@ public function testGetInternalResponse()
* @expectedDeprecation Calling the "Symfony\Component\BrowserKit\Client::getInternalResponse()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.
@@ -153,6 +183,16 @@ public function testGetCrawler()
153
183
$this->assertSame($crawler, $client->getCrawler(), '->getCrawler() returns the Crawler of the last request');
154
184
}
155
185
186
+
/**
187
+
* @group legacy
188
+
* @expectedDeprecation Calling the "Symfony\Component\BrowserKit\Client::getCrawler()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.
189
+
*/
190
+
publicfunctiontestGetCrawlerNull()
191
+
{
192
+
$client = newTestClient();
193
+
$this->assertNull($client->getCrawler());
194
+
}
195
+
156
196
publicfunctiontestRequestHttpHeaders()
157
197
{
158
198
$client = newTestClient();
@@ -720,6 +760,10 @@ public function testInternalRequest()
* @expectedDeprecation Calling the "Symfony\Component\BrowserKit\Client::getInternalRequest()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0.
0 commit comments