1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JDesrosiers\Resourceful\Controller\Test; |
4
|
|
|
|
5
|
|
|
use JDesrosiers\Resourceful\Controller\CreateResourceController; |
6
|
|
|
use JDesrosiers\Resourceful\FileCache\FileCache; |
7
|
|
|
use JDesrosiers\Silex\Provider\JsonSchemaServiceProvider; |
8
|
|
|
use PHPUnit_Framework_TestCase; |
9
|
|
|
use Silex\Application; |
10
|
|
|
use Silex\Provider\RoutingServiceProvider; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Symfony\Component\HttpKernel\Client; |
13
|
|
|
|
14
|
|
|
class CreateResourceControllerTest extends PHPUnit_Framework_TestCase |
15
|
|
|
{ |
16
|
|
|
private $app; |
17
|
|
|
private $service; |
18
|
|
|
private $client; |
19
|
|
|
|
20
|
|
|
public function setUp() |
21
|
|
|
{ |
22
|
|
|
$this->app = new Application(); |
23
|
|
|
$this->app["debug"] = true; |
24
|
|
|
|
25
|
|
|
$this->app->register(new RoutingServiceProvider()); |
26
|
|
|
$this->app->register(new JsonSchemaServiceProvider()); |
27
|
|
|
$this->app["uniqid"] = function () { |
28
|
|
|
return uniqid(); |
29
|
|
|
}; |
30
|
|
|
|
31
|
|
|
$this->app["schemaService"] = new FileCache(__DIR__); |
32
|
|
|
|
33
|
|
|
$this->service = $this->getMockBuilder("Doctrine\Common\Cache\Cache")->getMock(); |
34
|
|
|
$this->app->get("/foo/{id}")->bind("/schema/foo"); |
35
|
|
|
$this->app->post("/foo/", new CreateResourceController($this->service, "/schema/foo")); |
36
|
|
|
$this->app["json-schema.schema-store"]->add("/schema/foo", $this->app["schemaService"]->fetch("/schema/foo")); |
37
|
|
|
|
38
|
|
|
$this->client = new Client($this->app); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCreate() |
42
|
|
|
{ |
43
|
|
|
$foo = new \stdClass(); |
44
|
|
|
$foo->id = "4ee8e29d45851"; |
45
|
|
|
|
46
|
|
|
$this->app["uniqid"] = $foo->id; |
47
|
|
|
|
48
|
|
|
$headers = [ |
49
|
|
|
"HTTP_ACCEPT" => "application/json", |
50
|
|
|
"CONTENT_TYPE" => "application/json" |
51
|
|
|
]; |
52
|
|
|
$this->client->request("POST", "/foo/", [], [], $headers, "{}"); |
53
|
|
|
$response = $this->client->getResponse(); |
54
|
|
|
|
55
|
|
|
$this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode()); |
56
|
|
|
$this->assertEquals("application/json", $response->headers->get("Content-Type")); |
57
|
|
|
$this->assertEquals("/foo/$foo->id", $response->headers->get("Location")); |
58
|
|
|
$this->assertJsonStringEqualsJsonString("{\"id\":\"$foo->id\"}", $response->getContent()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testBadRequest() |
62
|
|
|
{ |
63
|
|
|
$this->app->error(function (\Exception $e, $code) { |
|
|
|
|
64
|
|
|
$errorMessage = '[{"code":303,"dataPath":"\/illegalField","schemaPath":"\/additionalProperties","message":"Additional properties not allowed"}]'; |
65
|
|
|
$this->assertEquals($errorMessage, $e->getMessage()); |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
$headers = [ |
69
|
|
|
"HTTP_ACCEPT" => "application/json", |
70
|
|
|
"CONTENT_TYPE" => "application/json" |
71
|
|
|
]; |
72
|
|
|
$this->client->request("POST", "/foo/", [], [], $headers, '{"illegalField":"illegal"}'); |
73
|
|
|
$response = $this->client->getResponse(); |
74
|
|
|
|
75
|
|
|
$this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testInvalidJson() |
79
|
|
|
{ |
80
|
|
|
$this->app->error(function (\Exception $e, $code) { |
|
|
|
|
81
|
|
|
$this->assertEquals("Invalid JSON: Syntax error", $e->getMessage()); |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
$headers = [ |
85
|
|
|
"HTTP_ACCEPT" => "application/json", |
86
|
|
|
"CONTENT_TYPE" => "application/json" |
87
|
|
|
]; |
88
|
|
|
$this->client->request("POST", "/foo/", [], [], $headers, 'invalid json'); |
89
|
|
|
$response = $this->client->getResponse(); |
90
|
|
|
|
91
|
|
|
$this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testSaveError() |
95
|
|
|
{ |
96
|
|
|
$foo = new \stdClass(); |
97
|
|
|
$foo->id = "4ee8e29d45851"; |
98
|
|
|
|
99
|
|
|
$this->service->method("save") |
100
|
|
|
->willReturn(false); |
101
|
|
|
|
102
|
|
|
$this->app->error(function (\Exception $e, $code) { |
|
|
|
|
103
|
|
|
$this->assertEquals("Failed to save resource", $e->getMessage()); |
104
|
|
|
}); |
105
|
|
|
|
106
|
|
|
$headers = [ |
107
|
|
|
"HTTP_ACCEPT" => "application/json", |
108
|
|
|
"CONTENT_TYPE" => "application/json" |
109
|
|
|
]; |
110
|
|
|
$this->client->request("POST", "/foo/", [], [], $headers, "{}"); |
111
|
|
|
$response = $this->client->getResponse(); |
112
|
|
|
|
113
|
|
|
$this->assertEquals(Response::HTTP_SERVICE_UNAVAILABLE, $response->getStatusCode()); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.