diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index 8b5614f075f2..68df87c4f698 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -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; @@ -183,20 +184,30 @@ public function getCookieJar() /** * Returns the current Crawler instance. * - * @return Crawler|null A Crawler instance + * @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); + // 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; } @@ -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; } @@ -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; } diff --git a/src/Symfony/Component/BrowserKit/Exception/BadMethodCallException.php b/src/Symfony/Component/BrowserKit/Exception/BadMethodCallException.php new file mode 100644 index 000000000000..8683b0a787d6 --- /dev/null +++ b/src/Symfony/Component/BrowserKit/Exception/BadMethodCallException.php @@ -0,0 +1,16 @@ + + * + * 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 +{ +} diff --git a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php index eec64feb2db7..e44143298955 100644 --- a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php @@ -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(); @@ -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(); @@ -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"]}'; @@ -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(); @@ -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(); diff --git a/src/Symfony/Component/HttpKernel/Client.php b/src/Symfony/Component/HttpKernel/Client.php index cc744b398c94..623ce34ec5d4 100644 --- a/src/Symfony/Component/HttpKernel/Client.php +++ b/src/Symfony/Component/HttpKernel/Client.php @@ -25,8 +25,8 @@ * * @author Fabien Potencier * - * @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 {