|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JDesrosiers\Resourceful\CrudControllerProvider; |
|
4
|
|
|
|
|
5
|
|
|
use JDesrosiers\Resourceful\CrudControllerProvider\CrudControllerProvider; |
|
6
|
|
|
use JDesrosiers\Resourceful\Resourceful; |
|
7
|
|
|
use JDesrosiers\Resourceful\ResourcefulServiceProvider\ResourcefulServiceProvider; |
|
8
|
|
|
use JDesrosiers\Resourceful\SchemaControllerProvider\SchemaControllerProvider; |
|
9
|
|
|
use PHPUnit_Framework_TestCase; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
use Symfony\Component\HttpKernel\Client; |
|
12
|
|
|
|
|
13
|
|
|
class CrudControllerProviderTest 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
|
|
|
|
|
24
|
|
|
$this->app->register(new ResourcefulServiceProvider(), [ |
|
25
|
|
|
"resourceful.schemas" => $this->getMockBuilder("Doctrine\Common\Cache\Cache")->getMock(), |
|
26
|
|
|
]); |
|
27
|
|
|
|
|
28
|
|
|
$this->app->mount("/schema", new SchemaControllerProvider()); |
|
|
|
|
|
|
29
|
|
|
$this->app->flush(); |
|
30
|
|
|
|
|
31
|
|
|
$this->service = $this->getMockBuilder("Doctrine\Common\Cache\Cache")->getMock(); |
|
32
|
|
|
$this->app->mount("/foo", new CrudControllerProvider("foo", $this->service)); |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
$this->client = new Client($this->app); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testRetreive() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->service->method("contains") |
|
40
|
|
|
->with("/foo/4ee8e29d45851") |
|
41
|
|
|
->willReturn(true); |
|
42
|
|
|
|
|
43
|
|
|
$headers = [ |
|
44
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
45
|
|
|
]; |
|
46
|
|
|
$this->client->request("GET", "/foo/4ee8e29d45851", [], [], $headers); |
|
47
|
|
|
$response = $this->client->getResponse(); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); |
|
50
|
|
|
$this->assertEquals("application/json", $response->headers->get("Content-Type")); |
|
51
|
|
|
$this->assertEquals("</schema/foo>; rel=\"describedby\"", $response->headers->get("Link")); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testErrorHandling() |
|
55
|
|
|
{ |
|
56
|
|
|
$headers = [ |
|
57
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
58
|
|
|
]; |
|
59
|
|
|
$this->client->request("GET", "/foo/4ee8e29d45851", [], [], $headers); |
|
60
|
|
|
$response = $this->client->getResponse(); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode()); |
|
63
|
|
|
$this->assertEquals("application/json", $response->headers->get("Content-Type")); |
|
64
|
|
|
$this->assertEquals("</schema/error>; rel=\"describedby\"", $response->headers->get("Link")); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testUpdate() |
|
68
|
|
|
{ |
|
69
|
|
|
$foo = new \stdClass(); |
|
70
|
|
|
$foo->id = "4ee8e29d45851"; |
|
71
|
|
|
|
|
72
|
|
|
$this->service->method("contains") |
|
73
|
|
|
->with("/foo/$foo->id") |
|
74
|
|
|
->willReturn(true); |
|
75
|
|
|
|
|
76
|
|
|
$headers = [ |
|
77
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
78
|
|
|
"CONTENT_TYPE" => "application/json" |
|
79
|
|
|
]; |
|
80
|
|
|
$this->client->request("PUT", "/foo/$foo->id", [], [], $headers, "{\"id\":\"$foo->id\"}"); |
|
81
|
|
|
$response = $this->client->getResponse(); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); |
|
84
|
|
|
$this->assertEquals("application/json", $response->headers->get("Content-Type")); |
|
85
|
|
|
$this->assertEquals("</schema/foo>; rel=\"describedby\"", $response->headers->get("Link")); |
|
86
|
|
|
$this->assertFalse($response->headers->has("Location")); |
|
87
|
|
|
$this->assertJsonStringEqualsJsonString("{\"id\":\"$foo->id\"}", $response->getContent()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testDelete() |
|
91
|
|
|
{ |
|
92
|
|
|
$headers = [ |
|
93
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
94
|
|
|
]; |
|
95
|
|
|
$this->client->request("DELETE", "/foo/4ee8e29d45851", [], [], $headers); |
|
96
|
|
|
$response = $this->client->getResponse(); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode()); |
|
99
|
|
|
$this->assertFalse($response->headers->has("Content-Type")); |
|
100
|
|
|
$this->assertEquals("", $response->getContent()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testCreate() |
|
104
|
|
|
{ |
|
105
|
|
|
$foo = new \stdClass(); |
|
106
|
|
|
$foo->id = "4ee8e29d45851"; |
|
107
|
|
|
|
|
108
|
|
|
$this->app["uniqid"] = $foo->id; |
|
109
|
|
|
|
|
110
|
|
|
$headers = [ |
|
111
|
|
|
"HTTP_ACCEPT" => "application/json", |
|
112
|
|
|
"CONTENT_TYPE" => "application/json" |
|
113
|
|
|
]; |
|
114
|
|
|
$this->client->request("POST", "/foo/", [], [], $headers, "{}"); |
|
115
|
|
|
$response = $this->client->getResponse(); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode()); |
|
118
|
|
|
$this->assertEquals("application/json", $response->headers->get("Content-Type")); |
|
119
|
|
|
$this->assertEquals("</schema/foo>; rel=\"describedby\"", $response->headers->get("Link")); |
|
120
|
|
|
$this->assertEquals("/foo/$foo->id", $response->headers->get("Location")); |
|
121
|
|
|
$this->assertJsonStringEqualsJsonString("{\"id\":\"$foo->id\"}", $response->getContent()); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: