Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[BrowserKit] Avoid nullable values in some Client's methods #26509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/Symfony/Component/BrowserKit/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\BrowserKit;

use Symfony\Component\BrowserKit\Exception\BadMethodCallException;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\DomCrawler\Link;
use Symfony\Component\DomCrawler\Form;
Expand Down Expand Up @@ -183,20 +184,30 @@ public function getCookieJar()
/**
* Returns the current Crawler instance.
*
* @return Crawler|null A Crawler instance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be reverted as it's still valid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null is returned only for BC. There are no cases when null might be returned.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fact is it's Crawler|null until 5.0

to me this is different then switching to new API for parameters (@param), we where want users to provide correct input beforehand.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fact is it's Crawler|null until 5.0

we don't document deprecated APIs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 sounds fair while thinking about it. Deprecation message should tell either way.

* @return Crawler A Crawler instance
*/
public function getCrawler()
{
if (null === $this->crawler) {
@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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the exception that would be thrown in 5.0 and comment it? That will help when we remove the deprecation notices. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabpot Please review

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was not clear enough. We cannot add the @throws annotation yet as that would be wrong, but to ease remving the deprecation notices, you can add a line like this bellow the trigger call:

// throw new BadMethodCallException('...');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

// throw new BadMethodCallException(sprintf('The "request()" method must be called before the "%s()" one', __METHOD__));
}

return $this->crawler;
}

/**
* Returns the current BrowserKit Response instance.
*
* @return Response|null A BrowserKit Response instance
* @return Response A BrowserKit Response instance
*/
public function getInternalResponse()
{
if (null === $this->internalResponse) {
@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);
// throw new BadMethodCallException(sprintf('The "request()" method must be called before the "%s()" one', __METHOD__));
}

return $this->internalResponse;
}

Expand All @@ -206,22 +217,32 @@ public function getInternalResponse()
* The origin response is the response instance that is returned
* by the code that handles requests.
*
* @return object|null A response instance
* @return object A response instance
*
* @see doRequest()
*/
public function getResponse()
{
if (null === $this->response) {
@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);
// throw new BadMethodCallException(sprintf('The "request()" method must be called before the "%s()" one', __METHOD__));
}

return $this->response;
}

/**
* Returns the current BrowserKit Request instance.
*
* @return Request|null A BrowserKit Request instance
* @return Request A BrowserKit Request instance
*/
public function getInternalRequest()
{
if (null === $this->internalRequest) {
@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);
// throw new BadMethodCallException(sprintf('The "request()" method must be called before the "%s()" one', __METHOD__));
}

return $this->internalRequest;
}

Expand All @@ -231,12 +252,17 @@ public function getInternalRequest()
* The origin request is the request instance that is sent
* to the code that handles requests.
*
* @return object|null A Request instance
* @return object A Request instance
*
* @see doRequest()
*/
public function getRequest()
{
if (null === $this->request) {
@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);
// throw new BadMethodCallException(sprintf('The "request()" method must be called before the "%s()" one', __METHOD__));
}

return $this->request;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\BrowserKit\Exception;

class BadMethodCallException extends \BadMethodCallException
{
}
44 changes: 44 additions & 0 deletions src/Symfony/Component/BrowserKit/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public function testGetRequest()
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
}

/**
* @group legacy
* @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.
*/
public function testGetRequestNull()
{
$client = new TestClient();
$this->assertNull($client->getRequest());
}

public function testGetRequestWithXHR()
{
$client = new TestClient();
Expand Down Expand Up @@ -124,6 +134,16 @@ public function testGetResponse()
$this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getResponse(), '->getCrawler() returns the Response of the last request');
}

/**
* @group legacy
* @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.
*/
public function testGetResponseNull()
{
$client = new TestClient();
$this->assertNull($client->getResponse());
}

public function testGetInternalResponse()
{
$client = new TestClient();
Expand All @@ -135,6 +155,16 @@ public function testGetInternalResponse()
$this->assertInstanceOf('Symfony\Component\BrowserKit\Tests\SpecialResponse', $client->getResponse());
}

/**
* @group legacy
* @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.
*/
public function testGetInternalResponseNull()
{
$client = new TestClient();
$this->assertNull($client->getInternalResponse());
}

public function testGetContent()
{
$json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
Expand All @@ -153,6 +183,16 @@ public function testGetCrawler()
$this->assertSame($crawler, $client->getCrawler(), '->getCrawler() returns the Crawler of the last request');
}

/**
* @group legacy
* @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.
*/
public function testGetCrawlerNull()
{
$client = new TestClient();
$this->assertNull($client->getCrawler());
}

public function testRequestHttpHeaders()
{
$client = new TestClient();
Expand Down Expand Up @@ -720,6 +760,10 @@ public function testInternalRequest()
$this->assertInstanceOf('Symfony\Component\BrowserKit\Request', $client->getInternalRequest());
}

/**
* @group legacy
* @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.
*/
public function testInternalRequestNull()
{
$client = new TestClient();
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpKernel/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
*
* @author Fabien Potencier <[email protected]>
*
* @method Request|null getRequest() A Request instance
* @method Response|null getResponse() A Response instance
* @method Request getRequest() A Request instance
* @method Response getResponse() A Response instance
*/
class Client extends BaseClient
{
Expand Down