Avoid nullable values in some Client's methods

This commit is contained in:
Gocha Ossinkine 2018-03-13 15:42:54 +05:00
parent 9e82562948
commit c2c285355b
4 changed files with 93 additions and 7 deletions

View File

@ -11,6 +11,7 @@
namespace Symfony\Component\BrowserKit; namespace Symfony\Component\BrowserKit;
use Symfony\Component\BrowserKit\Exception\BadMethodCallException;
use Symfony\Component\DomCrawler\Crawler; use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\DomCrawler\Link; use Symfony\Component\DomCrawler\Link;
use Symfony\Component\DomCrawler\Form; use Symfony\Component\DomCrawler\Form;
@ -183,20 +184,30 @@ abstract class Client
/** /**
* Returns the current Crawler instance. * Returns the current Crawler instance.
* *
* @return Crawler|null A Crawler instance * @return Crawler A Crawler instance
*/ */
public function getCrawler() 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);
// throw new BadMethodCallException(sprintf('The "request()" method must be called before the "%s()" one', __METHOD__));
}
return $this->crawler; return $this->crawler;
} }
/** /**
* Returns the current BrowserKit Response instance. * Returns the current BrowserKit Response instance.
* *
* @return Response|null A BrowserKit Response instance * @return Response A BrowserKit Response instance
*/ */
public function getInternalResponse() 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; return $this->internalResponse;
} }
@ -206,22 +217,32 @@ abstract class Client
* The origin response is the response instance that is returned * The origin response is the response instance that is returned
* by the code that handles requests. * by the code that handles requests.
* *
* @return object|null A response instance * @return object A response instance
* *
* @see doRequest() * @see doRequest()
*/ */
public function getResponse() 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; return $this->response;
} }
/** /**
* Returns the current BrowserKit Request instance. * Returns the current BrowserKit Request instance.
* *
* @return Request|null A BrowserKit Request instance * @return Request A BrowserKit Request instance
*/ */
public function getInternalRequest() 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; return $this->internalRequest;
} }
@ -231,12 +252,17 @@ abstract class Client
* The origin request is the request instance that is sent * The origin request is the request instance that is sent
* to the code that handles requests. * to the code that handles requests.
* *
* @return object|null A Request instance * @return object A Request instance
* *
* @see doRequest() * @see doRequest()
*/ */
public function getRequest() 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; return $this->request;
} }

View File

@ -0,0 +1,16 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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
{
}

View File

@ -94,6 +94,16 @@ class ClientTest extends TestCase
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request'); $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() public function testGetRequestWithXHR()
{ {
$client = new TestClient(); $client = new TestClient();
@ -124,6 +134,16 @@ class ClientTest extends TestCase
$this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getResponse(), '->getCrawler() returns the Response of the last request'); $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() public function testGetInternalResponse()
{ {
$client = new TestClient(); $client = new TestClient();
@ -135,6 +155,16 @@ class ClientTest extends TestCase
$this->assertInstanceOf('Symfony\Component\BrowserKit\Tests\SpecialResponse', $client->getResponse()); $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() public function testGetContent()
{ {
$json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}'; $json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
@ -153,6 +183,16 @@ class ClientTest extends TestCase
$this->assertSame($crawler, $client->getCrawler(), '->getCrawler() returns the Crawler of the last request'); $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() public function testRequestHttpHeaders()
{ {
$client = new TestClient(); $client = new TestClient();
@ -720,6 +760,10 @@ class ClientTest extends TestCase
$this->assertInstanceOf('Symfony\Component\BrowserKit\Request', $client->getInternalRequest()); $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() public function testInternalRequestNull()
{ {
$client = new TestClient(); $client = new TestClient();

View File

@ -25,8 +25,8 @@ use Symfony\Component\HttpFoundation\Response;
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
* *
* @method Request|null getRequest() A Request instance * @method Request getRequest() A Request instance
* @method Response|null getResponse() A Response instance * @method Response getResponse() A Response instance
*/ */
class Client extends BaseClient class Client extends BaseClient
{ {