8000 feature #26509 [BrowserKit] Avoid nullable values in some Client's me… · symfony/symfony@5511ddc · GitHub
[go: up one dir, main page]

Skip to content

Commit 5511ddc

Browse files
committed
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
2 parents bf120d0 + c2c2853 commit 5511ddc

File tree

4 files changed

+93
-7
lines changed

4 files changed

+93
-7
lines changed

src/Symfony/Component/BrowserKit/Client.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\BrowserKit;
1313

14+
use Symfony\Component\BrowserKit\Exception\BadMethodCallException;
1415
use Symfony\Component\DomCrawler\Crawler;
1516
use Symfony\Component\DomCrawler\Link;
1617
use Symfony\Component\DomCrawler\Form;
@@ -183,20 +184,30 @@ public function getCookieJar()
183184
/**
184185
* Returns the current Crawler instance.
185186
*
186-
* @return Crawler|null A Crawler instance
187+
* @return Crawler A Crawler instance
187188
*/
188189
public function getCrawler()
189190
{
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+
190196
return $this->crawler;
191197
}
192198

193199
/**
194200
* Returns the current BrowserKit Response instance.
195201
*
196-
* @return Response|null A BrowserKit Response instance
202+
* @return Response A BrowserKit Response instance
197203
*/
198204
public function getInternalResponse()
199205
{
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+
200211
return $this->internalResponse;
201212
}
202213

@@ -206,22 +217,32 @@ public function getInternalResponse()
206217
* The origin response is the response instance that is returned
207218
* by the code that handles requests.
208219
*
209-
* @return object|null A response instance
220+
* @return object A response instance
210221
*
211222
* @see doRequest()
212223
*/
213224
public function getResponse()
214225
{
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+
215231
return $this->response;
216232
}
217233

218234
/**
219235
* Returns the current BrowserKit Request instance.
220236
*
221-
* @return Request|null A BrowserKit Request instance
237+
* @return Request A BrowserKit Request instance
222238
*/
223239
public function getInternalRequest()
224240
{
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+
225246
return $this->internalRequest;
226247
}
227248

@@ -231,12 +252,17 @@ public function getInternalRequest()
231252
* The origin request is the request instance that is sent
232253
* to the code that handles requests.
233254
*
234-
* @return object|null A Request instance
255+
* @return object A Request instance
235256
*
236257
* @see doRequest()
237258
*/
238259
public function getRequest()
239260
{
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__));
264+
}
265+
240266
return $this->request;
241267
}
242268

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\BrowserKit\Exception;
13+
14+
class BadMethodCallException extends \BadMethodCallException
15+
{
16+
}

src/Symfony/Component/BrowserKit/Tests/ClientTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ public function testGetRequest()
9494
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
9595
}
9696

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+
public function testGetRequestNull()
102+
{
103+
$client = new TestClient();
104+
$this->assertNull($client->getRequest());
105+
}
106+
97107
public function testGetRequestWithXHR()
98108
{
99109
$client = new TestClient();
@@ -124,6 +134,16 @@ public function testGetResponse()
124134
$this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getResponse(), '->getCrawler() returns the Response of the last request');
125135
}
126136

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+
public function testGetResponseNull()
142+
{
143+
$client = new TestClient();
144+
$this->assertNull($client->getResponse());
145+
}
146+
127147
public function testGetInternalResponse()
128148
{
129149
$client = new TestClient();
@@ -135,6 +155,16 @@ public function testGetInternalResponse()
135155
$this->assertInstanceOf('Symfony\Component\BrowserKit\Tests\SpecialResponse', $client->getResponse());
136156
}
137157

158+
/**
159+
* @group legacy
160+
* @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.
161+
*/
162+
public function testGetInternalResponseNull()
163+
{
164+
$client = new TestClient();
165+
$this->assertNull($client->getInternalResponse());
166+
}
167+
138168
public function testGetContent()
139169
{
140170
$json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
@@ -153,6 +183,16 @@ public function testGetCrawler()
153183
$this->assertSame($crawler, $client->getCrawler(), '->getCrawler() returns the Crawler of the last request');
154184
}
155185

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+
public function testGetCrawlerNull()
191+
{
192+
$client = new TestClient();
193+
$this->assertNull($client->getCrawler());
194+
}
195+
156196
public function testRequestHttpHeaders()
157197
{
158198
$client = new TestClient();
@@ -720,6 +760,10 @@ public function testInternalRequest()
720760
$this->assertInstanceOf('Symfony\Component\BrowserKit\Request', $client->getInternalRequest());
721761
}
722762

763+
/**
764+
* @group legacy
765+
* @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.
766+
*/
723767
public function testInternalRequestNull()
724768
{
725769
$client = new TestClient();

src/Symfony/Component/HttpKernel/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
*
2626
* @author Fabien Potencier <fabien@symfony.com>
2727
*
28-
* @method Request|null getRequest() A Request instance
29-
* @method Response|null getResponse() A Response instance
28+
* @method Request getRequest() A Request instance
29+
* @method Response getResponse() A Response instance
3030
*/
3131
class Client extends BaseClient
3232
{

0 commit comments

Comments
 (0)
0