Skip to content

Commit 45e60fe

Browse files
committed
Merge branch 'hotfix/55' into develop
Forward port #55
2 parents b0bf194 + 79bf689 commit 45e60fe

File tree

4 files changed

+103
-7
lines changed

4 files changed

+103
-7
lines changed

.travis.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ cache:
99

1010
env:
1111
global:
12-
- COMPOSER_ARGS="--no-interaction --ignore-platform-reqs"
12+
- COMPOSER_ARGS="--no-interaction"
1313
- COVERAGE_DEPS="satooshi/php-coveralls"
14+
- LEGACY_DEPS="phpunit/phpunit doctrine/instantiator"
1415

1516
matrix:
1617
include:
@@ -64,10 +65,11 @@ before_install:
6465
- travis_retry composer self-update
6566

6667
install:
67-
- if [[ $TEST_COVERAGE == 'true' ]]; then travis_retry composer require --dev $COMPOSER_ARGS $COVERAGE_DEPS ; fi
68+
- travis_retry composer install $COMPOSER_ARGS --ignore-platform-reqs
69+
- if [[ $TRAVIS_PHP_VERSION =~ ^5.6 ]]; then travis_retry composer update $COMPOSER_ARGS --with-dependencies $LEGACY_DEPS ; fi
6870
- if [[ $DEPS == 'latest' ]]; then travis_retry composer update $COMPOSER_ARGS ; fi
6971
- if [[ $DEPS == 'lowest' ]]; then travis_retry composer update --prefer-lowest --prefer-stable $COMPOSER_ARGS ; fi
70-
- travis_retry composer install $COMPOSER_ARGS
72+
- if [[ $TEST_COVERAGE == 'true' ]]; then travis_retry composer require --dev $COMPOSER_ARGS $COVERAGE_DEPS ; fi
7173
- composer show
7274

7375
script:

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ All notable changes to this project will be documented in this file, in reverse
3636

3737
### Fixed
3838

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

4344
## 1.2.2 - 2016-10-11
4445

src/Listener/RenderErrorListener.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace ZF\ApiProblem\Listener;
88

9+
use Exception;
10+
use Throwable;
911
use Zend\EventManager\EventManagerInterface;
1012
use Zend\EventManager\AbstractListenerAggregate;
1113
use Zend\Mvc\MvcEvent;
@@ -64,7 +66,7 @@ public function onRenderError(MvcEvent $e)
6466
$details = false;
6567

6668
$exception = $e->getParam('exception');
67-
if ($exception instanceof \Exception
69+
if (($exception instanceof Throwable || $exception instanceof Exception)
6870
&& ! $exception instanceof ViewExceptionInterface
6971
) {
7072
$code = $exception->getCode();

test/Listener/RenderErrorListenerTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
namespace ZFTest\ApiProblem\Listener;
88

99
use PHPUnit_Framework_TestCase as TestCase;
10+
use RuntimeException;
11+
use TypeError;
1012
use Zend\Http\Request;
1113
use Zend\Http\Response;
1214
use Zend\Mvc\Application;
@@ -56,4 +58,93 @@ public function testOnRenderErrorCreatesAnApiProblemResponse()
5658
$this->assertContains('www.w3.org', $content['describedBy']);
5759
$this->assertContains('accept', $content['detail']);
5860
}
61+
62+
public function testOnRenderErrorCreatesAnApiProblemResponseFromException()
63+
{
64+
$response = new Response();
65+
$request = new Request();
66+
$request->getHeaders()->addHeaderLine('Accept', 'application/json');
67+
68+
$event = new MvcEvent();
69+
$event->setError(Application::ERROR_EXCEPTION);
70+
$event->setParam('exception', new RuntimeException('exception', 400));
71+
$event->setRequest($request);
72+
$event->setResponse($response);
73+
74+
$this->listener->setDisplayExceptions(true);
75+
$this->listener->onRenderError($event);
76+
77+
$this->assertTrue($event->propagationIsStopped());
78+
$this->assertSame($response, $event->getResponse());
79+
80+
$this->assertEquals(400, $response->getStatusCode());
81+
$headers = $response->getHeaders();
82+
$this->assertTrue($headers->has('Content-Type'));
83+
$this->assertEquals(ApiProblem::CONTENT_TYPE, $headers->get('content-type')->getFieldValue());
84+
$content = json_decode($response->getContent(), true);
85+
$this->assertArrayHasKey('status', $content);
86+
$this->assertArrayHasKey('title', $content);
87+
$this->assertArrayHasKey('describedBy', $content);
88+
$this->assertArrayHasKey('detail', $content);
89+
$this->assertArrayHasKey('details', $content);
90+
91+
$this->assertEquals(400, $content['status']);
92+
$this->assertEquals('Unexpected error', $content['title']);
93+
$this->assertContains('www.w3.org', $content['describedBy']);
94+
$this->assertEquals('exception', $content['detail']);
95+
96+
$this->assertInternalType('array', $content['details']);
97+
$details = $content['details'];
98+
$this->assertArrayHasKey('code', $details);
99+
$this->assertArrayHasKey('message', $details);
100+
$this->assertArrayHasKey('trace', $details);
101+
$this->assertEquals(400, $details['code']);
102+
$this->assertEquals('exception', $details['message']);
103+
}
104+
105+
/**
106+
* @requires PHP 7.0
107+
*/
108+
public function testOnRenderErrorCreatesAnApiProblemResponseFromThrowable()
109+
{
110+
$response = new Response();
111+
$request = new Request();
112+
$request->getHeaders()->addHeaderLine('Accept', 'application/json');
113+
114+
$event = new MvcEvent();
115+
$event->setError(Application::ERROR_EXCEPTION);
116+
$event->setParam('exception', new TypeError('throwable', 400));
117+
$event->setRequest($request);
118+
$event->setResponse($response);
119+
120+
$this->listener->setDisplayExceptions(true);
121+
$this->listener->onRenderError($event);
122+
123+
$this->assertTrue($event->propagationIsStopped());
124+
$this->assertSame($response, $event->getResponse());
125+
126+
$this->assertEquals(400, $response->getStatusCode());
127+
$headers = $response->getHeaders();
128+
$this->assertTrue($headers->has('Content-Type'));
129+
$this->assertEquals(ApiProblem::CONTENT_TYPE, $headers->get('content-type')->getFieldValue());
130+
$content = json_decode($response->getContent(), true);
131+
$this->assertArrayHasKey('status', $content);
132+
$this->assertArrayHasKey('title', $content);
133+
$this->assertArrayHasKey('describedBy', $content);
134+
$this->assertArrayHasKey('detail', $content);
135+
$this->assertArrayHasKey('details', $content);
136+
137+
$this->assertEquals(400, $content['status']);
138+
$this->assertEquals('Unexpected error', $content['title']);
139+
$this->assertContains('www.w3.org', $content['describedBy']);
140+
$this->assertEquals('throwable', $content['detail']);
141+
142+
$this->assertInternalType('array', $content['details']);
143+
$details = $content['details'];
144+
$this->assertArrayHasKey('code', $details);
145+
$this->assertArrayHasKey('message', $details);
146+
$this->assertArrayHasKey('trace', $details);
147+
$this->assertEquals(400, $details['code']);
148+
$this->assertEquals('throwable', $details['message']);
149+
}
59150
}

0 commit comments

Comments
 (0)