Skip to content

Commit b08a2a5

Browse files
committed
added tests for exception conversion into appropriate http responses
1 parent 9ed11d0 commit b08a2a5

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed

src/RESTFul/Routing/Route.php

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ static function run($uri = null){
8686
}catch (HttpException $h){
8787
$body = $h->getBody();
8888
if($h instanceof InternalServerError){
89+
new ProgrammingError($body); // for logging purpose
8990
$body = null;
9091
}
9192
$httpResponse = new HTTPResponse($h->getCode(),$h->getMessage(),$body);

tests/RESTFul/ClientSide/TestRoute.php

+84
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,27 @@ function testExceptionFromServiceMethod(){
8585
$client = new Client();
8686
$request = $client->get(APP_DOMAIN.'/'.APP_PATH.'/test/route/exception');
8787

88+
$isException = false;
89+
try{
90+
$client->send($request);
91+
}catch (ServerErrorResponseException $e){
92+
$response = $e->getResponse();
93+
$this->assertEquals(500, $response->getStatusCode());
94+
$this->assertEquals("Internal Server Error", $response->getReasonPhrase());
95+
$this->assertEquals("", $response->getBody(true));
96+
97+
// validate log
98+
$this->assertContainsAndClearLog('Testing Exception in Service Method');
99+
100+
$isException = true;
101+
}
102+
$this->assertTrue($isException);
103+
}
104+
105+
function testPlatformExceptionFromServiceMethod(){
106+
$client = new Client();
107+
$request = $client->get(APP_DOMAIN.'/'.APP_PATH.'/test/route/exception/platform-exception');
108+
88109
$isException = false;
89110
try{
90111
$client->send($request);
@@ -102,6 +123,69 @@ function testExceptionFromServiceMethod(){
102123
$this->assertTrue($isException);
103124
}
104125

126+
function testInternalServerErrorFromServiceMethod(){
127+
$client = new Client();
128+
$request = $client->get(APP_DOMAIN.'/'.APP_PATH.'/test/route/exception/internal-server-error');
129+
130+
$isException = false;
131+
try{
132+
$client->send($request);
133+
}catch (ServerErrorResponseException $e){
134+
$response = $e->getResponse();
135+
$this->assertEquals(500, $response->getStatusCode());
136+
$this->assertEquals("Internal Server Error", $response->getReasonPhrase());
137+
$this->assertEquals("", $response->getBody(true));
138+
139+
// validate log
140+
$this->assertContainsAndClearLog('Testing Uncaught internalServerError in Service');
141+
142+
$isException = true;
143+
}
144+
$this->assertTrue($isException);
145+
}
146+
147+
function testDataNotFoundExceptionFromServiceMethod(){
148+
$client = new Client();
149+
$request = $client->get(APP_DOMAIN.'/'.APP_PATH.'/test/route/exception/data-not-found-exception');
150+
151+
$isException = false;
152+
try{
153+
$client->send($request);
154+
}catch (ClientErrorResponseException $e){
155+
$response = $e->getResponse();
156+
$this->assertEquals(404, $response->getStatusCode());
157+
$this->assertEquals("Not Found", $response->getReasonPhrase());
158+
$this->assertEquals("", $response->getBody(true));
159+
160+
// validate log
161+
$this->assertContainsAndClearLog('[H][404][::1][/test/route/exception/data-not-found-exception] Not Found');
162+
163+
$isException = true;
164+
}
165+
$this->assertTrue($isException);
166+
}
167+
168+
function testNoAccessExceptionFromServiceMethod(){
169+
$client = new Client();
170+
$request = $client->get(APP_DOMAIN.'/'.APP_PATH.'/test/route/exception/no-access-exception');
171+
172+
$isException = false;
173+
try{
174+
$client->send($request);
175+
}catch (ClientErrorResponseException $e){
176+
$response = $e->getResponse();
177+
$this->assertEquals(401, $response->getStatusCode());
178+
$this->assertEquals("Unauthorized", $response->getReasonPhrase());
179+
$this->assertEquals("", $response->getBody(true));
180+
181+
// validate log
182+
$this->assertContainsAndClearLog('[H][401][::1][/test/route/exception/no-access-exception] Unauthorized');
183+
184+
$isException = true;
185+
}
186+
$this->assertTrue($isException);
187+
}
188+
105189
function testPathParams(){
106190
$client = new Client();
107191
$request = $client->get(APP_DOMAIN.'/'.APP_PATH.'/test/route/myParam1/path/myParam2');

tests/RESTFul/Services/TestRoute.php

+39-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use PhpPlatform\RESTFul\HTTPResponse;
77
use PhpPlatform\Errors\Exceptions\Application\BadInputException;
88
use PhpPlatform\RESTFul\HTTPRequest;
9+
use PhpPlatform\Errors\Exceptions\Http\_5XX\InternalServerError;
10+
use PhpPlatform\Errors\Exceptions\Persistence\DataNotFoundException;
11+
use PhpPlatform\Errors\Exceptions\Persistence\NoAccessException;
912

1013
/**
1114
* @Path "/test/route"
@@ -45,12 +48,46 @@ function wrongResponse(){
4548
return array("this"=>array("is","JSON","from","array"));
4649
}
4750

51+
/**
52+
* @Path "/exception/platform-exception"
53+
* @GET
54+
*/
55+
function platformException(){
56+
throw new BadInputException("Testing Uncaught Bad Input Exception in Service");
57+
}
58+
59+
/**
60+
* @Path "/exception/internal-server-error"
61+
* @GET
62+
*/
63+
function internalServerError(){
64+
throw new InternalServerError("Testing Uncaught internalServerError in Service");
65+
}
66+
67+
/**
68+
* @Path "/exception/data-not-found-exception"
69+
* @GET
70+
*/
71+
function dataNotFoundException(){
72+
throw new DataNotFoundException();
73+
}
74+
75+
76+
/**
77+
* @Path "/exception/no-access-exception"
78+
* @GET
79+
*/
80+
function noAccessException(){
81+
throw new NoAccessException();
82+
}
83+
84+
4885
/**
4986
* @Path "/exception"
5087
* @GET
5188
*/
5289
function exception(){
53-
throw new BadInputException("Testing Uncaught Bad Input Exception in Service");
90+
throw new \Exception("Testing Exception in Service Method");
5491
}
5592

5693
/**
@@ -119,4 +156,5 @@ function testHTTPMethodDELETE($request){
119156
function testHTTPMethodDefault($request){
120157
return new HTTPResponse(200,'OK',"Default");
121158
}
159+
122160
}

0 commit comments

Comments
 (0)