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

Commit

Permalink
Merge branch 'hotfix/55' into develop
Browse files Browse the repository at this point in the history
Forward port #55
  • Loading branch information
weierophinney committed Jul 24, 2017
2 parents b0bf194 + 79bf689 commit 45e60fe
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 7 deletions.
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ cache:

env:
global:
- COMPOSER_ARGS="--no-interaction --ignore-platform-reqs"
- COMPOSER_ARGS="--no-interaction"
- COVERAGE_DEPS="satooshi/php-coveralls"
- LEGACY_DEPS="phpunit/phpunit doctrine/instantiator"

matrix:
include:
Expand Down Expand Up @@ -64,10 +65,11 @@ before_install:
- travis_retry composer self-update

install:
- if [[ $TEST_COVERAGE == 'true' ]]; then travis_retry composer require --dev $COMPOSER_ARGS $COVERAGE_DEPS ; fi
- travis_retry composer install $COMPOSER_ARGS --ignore-platform-reqs
- if [[ $TRAVIS_PHP_VERSION =~ ^5.6 ]]; then travis_retry composer update $COMPOSER_ARGS --with-dependencies $LEGACY_DEPS ; fi
- if [[ $DEPS == 'latest' ]]; then travis_retry composer update $COMPOSER_ARGS ; fi
- if [[ $DEPS == 'lowest' ]]; then travis_retry composer update --prefer-lowest --prefer-stable $COMPOSER_ARGS ; fi
- travis_retry composer install $COMPOSER_ARGS
- if [[ $TEST_COVERAGE == 'true' ]]; then travis_retry composer require --dev $COMPOSER_ARGS $COVERAGE_DEPS ; fi
- composer show

script:
Expand Down
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- [#52](https://github.com/zfcampus/zf-api-problem/pull/52) updates the
`ApiProblemListener` to check for either exceptions or PHP 7 `Throwable`
instances when creating an `ApiProblem`.
- [#52](https://github.com/zfcampus/zf-api-problem/pull/52) and
[#55](https://github.com/zfcampus/zf-api-problem/pull/55) update the
`ApiProblemListener` and `RenderErrorListener` to check for either exceptions
or PHP 7 `Throwable` instances when creating an `ApiProblem` to return.

## 1.2.2 - 2016-10-11

Expand Down
4 changes: 3 additions & 1 deletion src/Listener/RenderErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace ZF\ApiProblem\Listener;

use Exception;
use Throwable;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\Mvc\MvcEvent;
Expand Down Expand Up @@ -64,7 +66,7 @@ public function onRenderError(MvcEvent $e)
$details = false;

$exception = $e->getParam('exception');
if ($exception instanceof \Exception
if (($exception instanceof Throwable || $exception instanceof Exception)
&& ! $exception instanceof ViewExceptionInterface
) {
$code = $exception->getCode();
Expand Down
91 changes: 91 additions & 0 deletions test/Listener/RenderErrorListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace ZFTest\ApiProblem\Listener;

use PHPUnit_Framework_TestCase as TestCase;
use RuntimeException;
use TypeError;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\Application;
Expand Down Expand Up @@ -56,4 +58,93 @@ public function testOnRenderErrorCreatesAnApiProblemResponse()
$this->assertContains('www.w3.org', $content['describedBy']);
$this->assertContains('accept', $content['detail']);
}

public function testOnRenderErrorCreatesAnApiProblemResponseFromException()
{
$response = new Response();
$request = new Request();
$request->getHeaders()->addHeaderLine('Accept', 'application/json');

$event = new MvcEvent();
$event->setError(Application::ERROR_EXCEPTION);
$event->setParam('exception', new RuntimeException('exception', 400));
$event->setRequest($request);
$event->setResponse($response);

$this->listener->setDisplayExceptions(true);
$this->listener->onRenderError($event);

$this->assertTrue($event->propagationIsStopped());
$this->assertSame($response, $event->getResponse());

$this->assertEquals(400, $response->getStatusCode());
$headers = $response->getHeaders();
$this->assertTrue($headers->has('Content-Type'));
$this->assertEquals(ApiProblem::CONTENT_TYPE, $headers->get('content-type')->getFieldValue());
$content = json_decode($response->getContent(), true);
$this->assertArrayHasKey('status', $content);
$this->assertArrayHasKey('title', $content);
$this->assertArrayHasKey('describedBy', $content);
$this->assertArrayHasKey('detail', $content);
$this->assertArrayHasKey('details', $content);

$this->assertEquals(400, $content['status']);
$this->assertEquals('Unexpected error', $content['title']);
$this->assertContains('www.w3.org', $content['describedBy']);
$this->assertEquals('exception', $content['detail']);

$this->assertInternalType('array', $content['details']);
$details = $content['details'];
$this->assertArrayHasKey('code', $details);
$this->assertArrayHasKey('message', $details);
$this->assertArrayHasKey('trace', $details);
$this->assertEquals(400, $details['code']);
$this->assertEquals('exception', $details['message']);
}

/**
* @requires PHP 7.0
*/
public function testOnRenderErrorCreatesAnApiProblemResponseFromThrowable()
{
$response = new Response();
$request = new Request();
$request->getHeaders()->addHeaderLine('Accept', 'application/json');

$event = new MvcEvent();
$event->setError(Application::ERROR_EXCEPTION);
$event->setParam('exception', new TypeError('throwable', 400));
$event->setRequest($request);
$event->setResponse($response);

$this->listener->setDisplayExceptions(true);
$this->listener->onRenderError($event);

$this->assertTrue($event->propagationIsStopped());
$this->assertSame($response, $event->getResponse());

$this->assertEquals(400, $response->getStatusCode());
$headers = $response->getHeaders();
$this->assertTrue($headers->has('Content-Type'));
$this->assertEquals(ApiProblem::CONTENT_TYPE, $headers->get('content-type')->getFieldValue());
$content = json_decode($response->getContent(), true);
$this->assertArrayHasKey('status', $content);
$this->assertArrayHasKey('title', $content);
$this->assertArrayHasKey('describedBy', $content);
$this->assertArrayHasKey('detail', $content);
$this->assertArrayHasKey('details', $content);

$this->assertEquals(400, $content['status']);
$this->assertEquals('Unexpected error', $content['title']);
$this->assertContains('www.w3.org', $content['describedBy']);
$this->assertEquals('throwable', $content['detail']);

$this->assertInternalType('array', $content['details']);
$details = $content['details'];
$this->assertArrayHasKey('code', $details);
$this->assertArrayHasKey('message', $details);
$this->assertArrayHasKey('trace', $details);
$this->assertEquals(400, $details['code']);
$this->assertEquals('throwable', $details['message']);
}
}

0 comments on commit 45e60fe

Please sign in to comment.