Skip to content

Commit 1b87590

Browse files
committed
Adds unit tests for #55
Adds two new test cases for #55, one each for testing exceptions and throwables (the latter depends on PHP 7.0).
1 parent 4050eb3 commit 1b87590

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

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 || $exception instanceof \Throwable)
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)