Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c2c2853

Browse filesBrowse files
committed
Avoid nullable values in some Client's methods
1 parent 9e82562 commit c2c2853
Copy full SHA for c2c2853

File tree

Expand file treeCollapse file tree

4 files changed

+93
-7
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+93
-7
lines changed

‎src/Symfony/Component/BrowserKit/Client.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/BrowserKit/Client.php
+31-5Lines 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

+16Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/BrowserKit/Tests/ClientTest.php
+44Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Client.php
+2-2Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.