|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JDesrosiers\Resourceful\Controller\Test; |
|
4
|
|
|
|
|
5
|
|
|
use JDesrosiers\Resourceful\Controller\GetResourceController; |
|
6
|
|
|
use JDesrosiers\Resourceful\Resourceful; |
|
7
|
|
|
use PHPUnit_Framework_TestCase; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Client; |
|
10
|
|
|
|
|
11
|
|
|
class GetResourceControllerTest extends PHPUnit_Framework_TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
private $app; |
|
14
|
|
|
private $service; |
|
15
|
|
|
private $client; |
|
16
|
|
|
|
|
17
|
|
|
public function setUp() |
|
18
|
|
|
{ |
|
19
|
|
|
$this->app = new Resourceful(); |
|
20
|
|
|
$this->app["debug"] = true; |
|
21
|
|
|
|
|
22
|
|
|
$this->service = $this->getMockBuilder("Doctrine\Common\Cache\Cache")->getMock(); |
|
23
|
|
|
$this->app->get("/foo/{id}", new GetResourceController($this->service)); |
|
24
|
|
|
|
|
25
|
|
|
$this->client = new Client($this->app); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testGet() |
|
29
|
|
|
{ |
|
30
|
|
|
$foo = new \stdClass(); |
|
31
|
|
|
$foo->id = "4ee8e29d45851"; |
|
32
|
|
|
|
|
33
|
|
|
$this->service->method("contains") |
|
34
|
|
|
->with("/foo/4ee8e29d45851") |
|
35
|
|
|
->willReturn(true); |
|
36
|
|
|
|
|
37
|
|
|
$this->service->method("fetch") |
|
38
|
|
|
->with("/foo/4ee8e29d45851") |
|
39
|
|
|
->willReturn($foo); |
|
40
|
|
|
|
|
41
|
|
|
$headers = [ |
|
42
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
43
|
|
|
]; |
|
44
|
|
|
$this->client->request("GET", "/foo/4ee8e29d45851", [], [], $headers); |
|
45
|
|
|
$response = $this->client->getResponse(); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); |
|
48
|
|
|
$this->assertEquals("application/json", $response->headers->get("Content-Type")); |
|
49
|
|
|
$this->assertEquals("GET", $response->headers->get("Allow")); |
|
50
|
|
|
$this->assertJsonStringEqualsJsonString('{"id":"4ee8e29d45851"}', $response->getContent()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testGetNotFound() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->service->method("contains") |
|
56
|
|
|
->with("/foo/4ee8e29d45851") |
|
57
|
|
|
->willReturn(false); |
|
58
|
|
|
|
|
59
|
|
|
$this->app->error(function (\Exception $e, $code) { |
|
|
|
|
|
|
60
|
|
|
$this->assertEquals("Not Found", $e->getMessage()); |
|
61
|
|
|
}); |
|
62
|
|
|
|
|
63
|
|
|
$headers = [ |
|
64
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
65
|
|
|
]; |
|
66
|
|
|
$this->client->request("GET", "/foo/4ee8e29d45851", [], [], $headers); |
|
67
|
|
|
$response = $this->client->getResponse(); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); |
|
70
|
|
|
$this->assertFalse($response->headers->has("Allow")); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testGetError() |
|
74
|
|
|
{ |
|
75
|
|
|
$this->service->method("contains") |
|
76
|
|
|
->with("/foo/4ee8e29d45851") |
|
77
|
|
|
->willReturn(true); |
|
78
|
|
|
|
|
79
|
|
|
$this->service->method("fetch") |
|
80
|
|
|
->with("/foo/4ee8e29d45851") |
|
81
|
|
|
->willReturn(false); |
|
82
|
|
|
|
|
83
|
|
|
$this->app->error(function (\Exception $e, $code) { |
|
|
|
|
|
|
84
|
|
|
$this->assertEquals("Failed to retrieve resource", $e->getMessage()); |
|
85
|
|
|
}); |
|
86
|
|
|
|
|
87
|
|
|
$headers = [ |
|
88
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
89
|
|
|
]; |
|
90
|
|
|
$this->client->request("GET", "/foo/4ee8e29d45851", [], [], $headers); |
|
91
|
|
|
$response = $this->client->getResponse(); |
|
92
|
|
|
|
|
93
|
|
|
$this->assertEquals(Response::HTTP_SERVICE_UNAVAILABLE, $response->getStatusCode()); |
|
94
|
|
|
$this->assertFalse($response->headers->has("Allow")); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.