1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JDesrosiers\Resourceful\JsonErrorHandler\Test; |
4
|
|
|
|
5
|
|
|
use JDesrosiers\Resourceful\JsonErrorHandler\JsonErrorHandler; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
7
|
|
|
use Silex\Application; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Symfony\Component\HttpKernel\Client; |
10
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
11
|
|
|
|
12
|
|
|
class JsonErrorHandlerTest extends PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
protected $app; |
15
|
|
|
protected $client; |
16
|
|
|
|
17
|
|
|
public function setUp() |
18
|
|
|
{ |
19
|
|
|
$this->app = new Application(); |
20
|
|
|
$this->app["debug"] = true; |
21
|
|
|
|
22
|
|
|
$this->app->error(new JsonErrorHandler($this->app)); |
23
|
|
|
|
24
|
|
|
$this->client = new Client($this->app); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testHandleError() |
28
|
|
|
{ |
29
|
|
|
$this->app->get("/foo", function () { |
30
|
|
|
throw new NotFoundHttpException("Not Found", null, 4); |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
$headers = [ |
34
|
|
|
"HTTP_ACCEPT" => "application/json", |
35
|
|
|
]; |
36
|
|
|
$this->client->request("GET", "/foo", [], [], $headers); |
37
|
|
|
$response = $this->client->getResponse(); |
38
|
|
|
$content = json_decode($response->getContent()); |
39
|
|
|
|
40
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); |
41
|
|
|
$this->assertEquals("application/json", $response->headers->get("Content-Type")); |
42
|
|
|
$this->assertEquals(4, $content->code); |
43
|
|
|
$this->assertEquals("Not Found", $content->message); |
44
|
|
|
$this->assertInternalType("string", $content->trace); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|