Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Trigger deprecation notices for request, response decorator method in…
Browse files Browse the repository at this point in the history
…vocations

This patch updates the codebase to trigger deprecation notices when
invoking the various custom methods in the request and response
decorators. The notices detail why they are triggered, how to avoid
them, and link to relevant migration documentation.

Some tests needed updates. In particular, any tests that might produce
Stratigility HTTP message artifacts were updated to expect the
deprecation notices and skip over them if discovered. In a few cases,
methods from the messages were exercised by tests, and these were
updated to no longer do so.

A few expectations were also made on the Stratigility HTTP message
types; these were updated to the looser PSR-7 message types.
  • Loading branch information
weierophinney committed Sep 29, 2016
1 parent 08cb6b3 commit 227406f
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 12 deletions.
21 changes: 21 additions & 0 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ public function __construct(
*/
public function getCurrentRequest()
{
trigger_error(sprintf(
'%s is now deprecated. The request passed to your method is the current '
. 'request now. %s will no longer be available starting in Stratigility 2.0.0. '
. 'Please see https://docs.zendframework.com/migration/to-v2/#original-request-response-and-uri '
. 'for full details.',
__CLASS__,
\Zend\Stratigility\Middleware\OriginalMessages::class,
__METHOD__
), E_USER_DEPRECATED);

return $this->psrRequest;
}

Expand All @@ -76,6 +86,17 @@ public function getCurrentRequest()
*/
public function getOriginalRequest()
{
trigger_error(sprintf(
'%s is now deprecated. Please register %s as your outermost middleware, '
. 'and pull the original request via the request "originalRequest" '
. 'attribute. %s will no longer be available starting in Stratigility 2.0.0. '
. 'Please see https://docs.zendframework.com/migration/to-v2/#original-request-response-and-uri '
. 'for full details.',
__CLASS__,
\Zend\Stratigility\Middleware\OriginalMessages::class,
__METHOD__
), E_USER_DEPRECATED);

return $this->originalRequest;
}

Expand Down
37 changes: 37 additions & 0 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public function __construct(PsrResponseInterface $response)
*/
public function getOriginalResponse()
{
trigger_error(sprintf(
'%s is now deprecated. Please register %s as your outermost middleware, '
. 'and pull the original response via the request "originalResponse" '
. 'attribute. %s will no longer be available starting in Stratigility 2.0.0. '
. 'Please see https://docs.zendframework.com/migration/to-v2/#original-request-response-and-uri '
. 'for full details.',
__CLASS__,
\Zend\Stratigility\Middleware\OriginalMessages::class,
__METHOD__
), E_USER_DEPRECATED);
return $this->psrResponse;
}

Expand All @@ -67,6 +77,15 @@ public function getOriginalResponse()
*/
public function write($data)
{
trigger_error(sprintf(
'%s is now deprecated; use $response->getBody()->write(). '
. '%s will no longer be available starting in Stratigility 2.0.0. '
. 'Please see https://docs.zendframework.com/migration/to-v2/#deprecated-functionality '
. 'for full details.',
__CLASS__,
__METHOD__
), E_USER_DEPRECATED);

if ($this->complete) {
throw $this->responseIsAlreadyCompleted(__METHOD__);
}
Expand All @@ -89,6 +108,15 @@ public function write($data)
*/
public function end($data = null)
{
trigger_error(sprintf(
'%s is now deprecated; use $response->getBody()->write(). '
. '%s will no longer be available starting in Stratigility 2.0.0. '
. 'Please see https://docs.zendframework.com/migration/to-v2/#deprecated-functionality '
. 'for full details.',
__CLASS__,
__METHOD__
), E_USER_DEPRECATED);

if ($this->complete) {
return $this;
}
Expand All @@ -111,6 +139,15 @@ public function end($data = null)
*/
public function isComplete()
{
trigger_error(sprintf(
'%s is now deprecated; use $response->getBody()->write(). '
. '%s will no longer be available starting in Stratigility 2.0.0. '
. 'Please see https://docs.zendframework.com/migration/to-v2/#deprecated-functionality '
. 'for full details.',
__CLASS__,
__METHOD__
), E_USER_DEPRECATED);

return $this->complete;
}

Expand Down
51 changes: 40 additions & 11 deletions test/FinalHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use Exception;
use PHPUnit_Framework_TestCase as TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\ServerRequest as PsrRequest;
use Zend\Diactoros\Response as PsrResponse;
Expand All @@ -22,6 +24,8 @@

class FinalHandlerTest extends TestCase
{
public $errorHandler;

/**
* @var Escaper
*/
Expand All @@ -44,13 +48,39 @@ class FinalHandlerTest extends TestCase

public function setUp()
{
$this->restoreErrorHandler();
$this->errorHandler = function ($errno, $errstr) {
if (false !== strstr($errstr, Request::class . ' is now deprecated')) {
return true;
}
if (false !== strstr($errstr, Response::class . ' is now deprecated')) {
return true;
}

return false;
};
set_error_handler($this->errorHandler, E_USER_DEPRECATED);

$psrRequest = new PsrRequest([], [], 'http://example.com/', 'GET', 'php://memory');
$this->escaper = new Escaper();
$this->request = new Request($psrRequest);
$this->response = new Response(new PsrResponse());
$this->final = new FinalHandler();
}

public function tearDown()
{
$this->restoreErrorHandler();
}

public function restoreErrorHandler()
{
if ($this->errorHandler) {
restore_error_handler();
$this->errorHandler = null;
}
}

public function testInvokingWithErrorAndNoStatusCodeSetsStatusTo500()
{
$error = 'error';
Expand Down Expand Up @@ -246,15 +276,16 @@ public function testShouldNotMarkStratigilityResponseAsCompleteWhenHandlingError
{
$error = new Exception('Exception message', 501);

$body = $this->prophesize(StreamInterface::class);
$body->write('Not Implemented')->shouldBeCalled();

$response = $this->prophesize('Zend\Stratigility\Http\Response');
$response->getStatusCode()->willReturn(200);
$response->withStatus(501, '')->will(function () use ($response) {
return $response->reveal();
});
$response->getReasonPhrase()->willReturn('Not Implemented');
$response->write('Not Implemented')->will(function () use ($response) {
return $response->reveal();
});
$response->getBody()->will([$body, 'reveal']);

$final = new FinalHandler([], new Response(new PsrResponse()));
$this->assertSame($response->reveal(), $final(
Expand All @@ -281,7 +312,7 @@ public function testShouldNotDecoratePsrResponseAsStratigilityCompletedResponseW
$error
);

$this->assertInstanceOf('Zend\Stratigility\Http\Response', $test);
$this->assertInstanceOf(ResponseInterface::class, $test);
$this->assertSame(501, $test->getStatusCode());
$this->assertSame('Not Implemented', $test->getReasonPhrase());

Expand All @@ -297,6 +328,7 @@ public function testShouldNotMarkStratigilityResponseAsCompleteWhenCreating404s(
{
$body = $this->prophesize('Psr\Http\Message\StreamInterface');
$body->getSize()->willReturn(0)->shouldBeCalledTimes(2);
$body->write("Cannot GET /foo\n")->shouldBeCalled();

$response = $this->prophesize('Zend\Stratigility\Http\Response');
$response->getBody()->will(function () use ($body) {
Expand All @@ -305,14 +337,10 @@ public function testShouldNotMarkStratigilityResponseAsCompleteWhenCreating404s(
$response->withStatus(404)->will(function () use ($response) {
return $response->reveal();
});
$response
->write("Cannot GET /foo\n")
->will(function () use ($response) {
return $response->reveal();
})
->shouldBeCalled();
$response->getBody()->will([$body, 'reveal']);

$request = $this->prophesize('Zend\Diactoros\ServerRequest');
$request->getAttribute('originalRequest', false)->willReturn(false);
$request->getUri()->willReturn('/foo');
$request->getMethod()->willReturn('GET');

Expand All @@ -331,6 +359,7 @@ public function testShouldNotDecoratePsrResponseAsStratigilityCompletedResponseW
$response = new PsrResponse();

$request = $this->prophesize('Zend\Diactoros\ServerRequest');
$request->getAttribute('originalRequest', false)->willReturn(false);
$request->getUri()->willReturn('/foo');
$request->getMethod()->willReturn('GET');

Expand All @@ -339,7 +368,7 @@ public function testShouldNotDecoratePsrResponseAsStratigilityCompletedResponseW
$request->reveal(),
$response
);
$this->assertInstanceOf('Zend\Stratigility\Http\Response', $test);
$this->assertInstanceOf(ResponseInterface::class, $test);
$this->assertSame(404, $test->getStatusCode());

$body = $test->getBody();
Expand Down
21 changes: 21 additions & 0 deletions test/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,34 @@

class RequestTest extends TestCase
{
public $errorHandler;

public function setUp()
{
$this->restoreErrorHandler();
$this->errorHandler = function ($errno, $errstr) {
return (false !== strstr($errstr, Request::class . ' is now deprecated'));
};
set_error_handler($this->errorHandler, E_USER_DEPRECATED);

$psrRequest = new PsrRequest([], [], 'http://example.com/', 'GET', 'php://memory');
$this->original = $psrRequest;
$this->request = new Request($this->original);
}

public function tearDown()
{
$this->restoreErrorHandler();
}

public function restoreErrorHandler()
{
if ($this->errorHandler) {
restore_error_handler();
$this->errorHandler = null;
}
}

public function testCallingSetUriSetsUriInRequestAndOriginalRequestInClone()
{
$url = 'http://example.com/foo';
Expand Down
21 changes: 21 additions & 0 deletions test/Http/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,33 @@

class ResponseTest extends TestCase
{
public $errorHandler;

public function setUp()
{
$this->restoreErrorHandler();
$this->errorHandler = function ($errno, $errstr) {
return (false !== strstr($errstr, Response::class . ' is now deprecated'));
};
set_error_handler($this->errorHandler, E_USER_DEPRECATED);

$this->original = new PsrResponse();
$this->response = new Response($this->original);
}

public function tearDown()
{
$this->restoreErrorHandler();
}

public function restoreErrorHandler()
{
if ($this->errorHandler) {
restore_error_handler();
$this->errorHandler = null;
}
}

public function testIsNotCompleteByDefault()
{
$this->assertFalse($this->response->isComplete());
Expand Down
30 changes: 30 additions & 0 deletions test/MiddlewarePipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,27 @@

class MiddlewarePipeTest extends TestCase
{
public $errorHandler;

public $deprecationsSuppressed = false;

public function setUp()
{
$this->deprecationsSuppressed = false;

$this->restoreErrorHandler();
$this->errorHandler = function ($errno, $errstr) {
if (false !== strstr($errstr, RequestDecorator::class . ' is now deprecated')) {
return true;
}
if (false !== strstr($errstr, ResponseDecorator::class . ' is now deprecated')) {
return true;
}

return false;
};
set_error_handler($this->errorHandler, E_USER_DEPRECATED);

$this->request = new Request([], [], 'http://example.com/', 'GET', 'php://memory');
$this->response = new Response();
$this->middleware = new MiddlewarePipe();
Expand All @@ -37,6 +53,15 @@ public function tearDown()
if (false !== $this->deprecationsSuppressed) {
restore_error_handler();
}
$this->restoreErrorHandler();
}

public function restoreErrorHandler()
{
if ($this->errorHandler) {
restore_error_handler();
$this->errorHandler = null;
}
}

/**
Expand Down Expand Up @@ -511,6 +536,11 @@ public function testOmittingFinalHandlerDuringInvocationRaisesDeprecationNotice(
$triggered = false;

$this->deprecationsSuppressed = set_error_handler(function ($errno, $errstr) use (&$triggered) {
if (false !== strstr($errstr, ResponseDecorator::class)) {
// ignore response decorator deprecation message
return true;
}

$this->assertContains(MiddlewarePipe::class . '()', $errstr);
$triggered = true;
return true;
Expand Down
2 changes: 1 addition & 1 deletion test/NextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function testSlashAndPathGetResetBeforeExecutingNextMiddleware()
$next($req, $res);
});
$route3 = new Route('/foo/baz', function ($req, $res, $next) {
$res->end('done');
$res->getBody()->write('done');
return $res;
});

Expand Down

0 comments on commit 227406f

Please sign in to comment.