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

Commit

Permalink
Merge branch 'hotfix/400'
Browse files Browse the repository at this point in the history
Close #402
Close #401
Fixes #400
  • Loading branch information
weierophinney committed Dec 7, 2016
2 parents 060e02f + ee14fe3 commit e8fdf76
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 1.0.4 - 2016-12-07

### Added

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- [#402](https://github.com/zendframework/zend-expressive/pull/402) fixes how
`Application::__invoke()` registers the error handler designed to swallow
deprecation notices, as introduced in 1.0.3. It now checks to see if another
error handler was previously registered, and, if so, creates a composite
handler that will delegate to the previous for all other errors.

## 1.0.3 - 2016-11-11

### Added
Expand Down
38 changes: 32 additions & 6 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,16 @@ public function __construct(
*
* If $out is not provided, uses the result of `getFinalHandler()`.
*
* @todo Remove logic for creating final handler for version 2.0.
* @todo Remove error handler for deprecation notice due to triggering
* error middleware for version 2.0.0.
* @todo Remove logic for creating final handler for version 2.0.0.
* @todo Remove swallowDeprecationNotices() invocation for version 2.0.0.
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable|null $out
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null)
{
set_error_handler(function ($errno, $errstr) {
return false !== strstr($errstr, 'error middleware is deprecated');
}, E_USER_DEPRECATED);
$this->swallowDeprecationNotices();

if (! $out && (null === ($out = $this->getFinalHandler($response)))) {
$response = $response instanceof StratigilityResponse
Expand Down Expand Up @@ -671,4 +668,33 @@ private function checkForDuplicateRoute($path, $methods = null)
);
}
}

/**
* Register an error handler to swallow deprecation notices due to error middleware usage.
*
* @todo Remove method for version 2.0.0.
* @return void
*/
private function swallowDeprecationNotices()
{
$handler = function ($errno, $errstr) {
if ($errno !== E_USER_DEPRECATED) {
return false;
}
return false !== strstr($errstr, 'error middleware is deprecated');
};

$previous = set_error_handler($handler);
if (! $previous) {
return;
}

set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use ($handler, $previous) {
if ($handler($errno, $errstr)) {
return true;
}

return $previous($errno, $errstr, $errfile, $errline, $errcontext);
});
}
}
39 changes: 39 additions & 0 deletions test/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@

class IntegrationTest extends TestCase
{
public $errorHandler;
public $response;

public function setUp()
{
$this->response = null;
$this->errorHandler = null;
}

public function tearDown()
{
if ($this->errorHandler) {
set_error_handler($this->errorHandler);
$this->errorHandler = null;
}
}

public function getEmitter()
Expand Down Expand Up @@ -67,4 +77,33 @@ public function testInjectedFinalHandlerCanEmitA404WhenNoMiddlewareMatches()
$this->assertInstanceOf(ResponseInterface::class, $this->response);
$this->assertEquals(404, $this->response->getStatusCode());
}

/**
* @todo Remove for version 2.0, as that version will remove error middleware support
* @group 400
*/
public function testErrorMiddlewareDeprecationErrorHandlerWillNotOverridePreviouslyRegisteredErrorHandler()
{
$triggered = false;
$this->errorHandler = set_error_handler(function ($errno, $errstr) use (&$triggered) {
$triggered = true;
return true;
});

$expected = new Response();
$middleware = function ($request, $response, $next) use ($expected) {
trigger_error('Triggered', E_USER_NOTICE);
return $expected;
};

$app = new Application(new FastRouteRouter(), null, null, $this->getEmitter());
$app->pipe($middleware);

$request = new ServerRequest([], [], 'https://example.com/foo', 'GET');
$response = clone $expected;

$app->run($request, $response);

$this->assertTrue($triggered);
}
}

0 comments on commit e8fdf76

Please sign in to comment.