|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JDesrosiers\Resourceful\Controller\Test; |
|
4
|
|
|
|
|
5
|
|
|
use JDesrosiers\Resourceful\Controller\PutResourceController; |
|
6
|
|
|
use JDesrosiers\Resourceful\FileCache\FileCache; |
|
7
|
|
|
use JDesrosiers\Resourceful\Resourceful; |
|
8
|
|
|
use JDesrosiers\Silex\Provider\JsonSchemaServiceProvider; |
|
9
|
|
|
use PHPUnit_Framework_TestCase; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
use Symfony\Component\HttpKernel\Client; |
|
12
|
|
|
|
|
13
|
|
|
class PutResourceControllerTest extends PHPUnit_Framework_TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
private $app; |
|
16
|
|
|
private $service; |
|
17
|
|
|
private $client; |
|
18
|
|
|
|
|
19
|
|
|
public function setUp() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->app = new Resourceful(); |
|
22
|
|
|
$this->app["debug"] = true; |
|
23
|
|
|
$this->app->register(new JsonSchemaServiceProvider()); |
|
24
|
|
|
|
|
25
|
|
|
$this->app["schemaService"] = new FileCache(__DIR__); |
|
26
|
|
|
|
|
27
|
|
|
$this->service = $this->getMockBuilder("Doctrine\Common\Cache\Cache")->getMock(); |
|
28
|
|
|
$this->app->put("/foo/{id}", new PutResourceController($this->service, "/schema/foo")); |
|
29
|
|
|
$this->app["json-schema.schema-store"]->add("/schema/foo", $this->app["schemaService"]->fetch("/schema/foo")); |
|
30
|
|
|
|
|
31
|
|
|
$this->client = new Client($this->app); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testCreate() |
|
35
|
|
|
{ |
|
36
|
|
|
$foo = new \stdClass(); |
|
37
|
|
|
$foo->id = "4ee8e29d45851"; |
|
38
|
|
|
|
|
39
|
|
|
$this->service->method("contains") |
|
40
|
|
|
->with("/foo/$foo->id") |
|
41
|
|
|
->willReturn(false); |
|
42
|
|
|
|
|
43
|
|
|
$headers = [ |
|
44
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
45
|
|
|
"CONTENT_TYPE" => "application/json" |
|
46
|
|
|
]; |
|
47
|
|
|
$this->client->request("PUT", "/foo/$foo->id", [], [], $headers, "{\"id\":\"$foo->id\"}"); |
|
48
|
|
|
$response = $this->client->getResponse(); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode()); |
|
51
|
|
|
$this->assertEquals("application/json", $response->headers->get("Content-Type")); |
|
52
|
|
|
$this->assertEquals("PUT", $response->headers->get("Allow")); |
|
53
|
|
|
$this->assertJsonStringEqualsJsonString("{\"id\":\"$foo->id\"}", $response->getContent()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testBadRequest() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->service->method("contains") |
|
59
|
|
|
->with("/foo/4ee8e29d45851") |
|
60
|
|
|
->willReturn(true); |
|
61
|
|
|
|
|
62
|
|
|
$this->app->error(function (\Exception $e, $code) { |
|
|
|
|
|
|
63
|
|
|
$errorMessage = '[{"code":303,"dataPath":"\/illegalField","schemaPath":"\/additionalProperties","message":"Additional properties not allowed"}]'; |
|
64
|
|
|
$this->assertEquals($errorMessage, $e->getMessage()); |
|
65
|
|
|
}); |
|
66
|
|
|
|
|
67
|
|
|
$headers = [ |
|
68
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
69
|
|
|
"CONTENT_TYPE" => "application/json" |
|
70
|
|
|
]; |
|
71
|
|
|
$data = '{"id":"4ee8e29d45851","illegalField":"illegal"}'; |
|
72
|
|
|
$this->client->request("PUT", "/foo/4ee8e29d45851", [], [], $headers, $data); |
|
73
|
|
|
$response = $this->client->getResponse(); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
|
76
|
|
|
$this->assertFalse($response->headers->has("Allow")); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testInvalidJson() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->service->method("contains") |
|
82
|
|
|
->with("/foo/4ee8e29d45851") |
|
83
|
|
|
->willReturn(true); |
|
84
|
|
|
|
|
85
|
|
|
$this->app->error(function (\Exception $e, $code) { |
|
|
|
|
|
|
86
|
|
|
$this->assertEquals("Invalid JSON: Syntax error", $e->getMessage()); |
|
87
|
|
|
}); |
|
88
|
|
|
|
|
89
|
|
|
$headers = [ |
|
90
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
91
|
|
|
"CONTENT_TYPE" => "application/json" |
|
92
|
|
|
]; |
|
93
|
|
|
$data = 'invalid json'; |
|
94
|
|
|
$this->client->request("PUT", "/foo/4ee8e29d45851", [], [], $headers, $data); |
|
95
|
|
|
$response = $this->client->getResponse(); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
|
98
|
|
|
$this->assertFalse($response->headers->has("Allow")); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testUpdate() |
|
102
|
|
|
{ |
|
103
|
|
|
$foo = new \stdClass(); |
|
104
|
|
|
$foo->id = "4ee8e29d45851"; |
|
105
|
|
|
|
|
106
|
|
|
$this->service->method("contains") |
|
107
|
|
|
->with("/foo/$foo->id") |
|
108
|
|
|
->willReturn(true); |
|
109
|
|
|
|
|
110
|
|
|
$headers = [ |
|
111
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
112
|
|
|
"CONTENT_TYPE" => "application/json" |
|
113
|
|
|
]; |
|
114
|
|
|
$this->client->request("PUT", "/foo/$foo->id", [], [], $headers, "{\"id\":\"$foo->id\"}"); |
|
115
|
|
|
$response = $this->client->getResponse(); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); |
|
118
|
|
|
$this->assertEquals("application/json", $response->headers->get("Content-Type")); |
|
119
|
|
|
$this->assertEquals("PUT", $response->headers->get("Allow")); |
|
120
|
|
|
$this->assertFalse($response->headers->has("Location")); |
|
121
|
|
|
$this->assertJsonStringEqualsJsonString("{\"id\":\"$foo->id\"}", $response->getContent()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function testSaveError() |
|
125
|
|
|
{ |
|
126
|
|
|
$foo = new \stdClass(); |
|
127
|
|
|
$foo->id = "4ee8e29d45851"; |
|
128
|
|
|
|
|
129
|
|
|
$this->service->method("save") |
|
130
|
|
|
->willReturn(false); |
|
131
|
|
|
|
|
132
|
|
|
$this->app->error(function (\Exception $e, $code) { |
|
|
|
|
|
|
133
|
|
|
$this->assertEquals("Failed to save resource", $e->getMessage()); |
|
134
|
|
|
}); |
|
135
|
|
|
|
|
136
|
|
|
$headers = [ |
|
137
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
138
|
|
|
"CONTENT_TYPE" => "application/json" |
|
139
|
|
|
]; |
|
140
|
|
|
$this->client->request("PUT", "/foo/$foo->id", [], [], $headers, "{\"id\":\"$foo->id\"}"); |
|
141
|
|
|
$response = $this->client->getResponse(); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertEquals(Response::HTTP_SERVICE_UNAVAILABLE, $response->getStatusCode()); |
|
144
|
|
|
$this->assertFalse($response->headers->has("Allow")); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function testIdsMatch() |
|
148
|
|
|
{ |
|
149
|
|
|
$this->service->method("contains") |
|
150
|
|
|
->with("/foo/4ee8e29d45851") |
|
151
|
|
|
->willReturn(true); |
|
152
|
|
|
|
|
153
|
|
|
$this->app->error(function (\Exception $e, $code) { |
|
|
|
|
|
|
154
|
|
|
$errorMessage = "The `id` in the body must match the `id` in the URI"; |
|
155
|
|
|
$this->assertEquals($errorMessage, $e->getMessage()); |
|
156
|
|
|
}); |
|
157
|
|
|
|
|
158
|
|
|
$headers = [ |
|
159
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
160
|
|
|
"CONTENT_TYPE" => "application/json" |
|
161
|
|
|
]; |
|
162
|
|
|
$this->client->request("PUT", "/foo/4ee8e29d45851", [], [], $headers, '{"id":"bar"}'); |
|
163
|
|
|
$response = $this->client->getResponse(); |
|
164
|
|
|
|
|
165
|
|
|
$this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
|
166
|
|
|
$this->assertFalse($response->headers->has("Allow")); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.