1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JDesrosiers\Silex\Provider\Test; |
4
|
|
|
|
5
|
|
|
use JDesrosiers\Silex\Provider\JsonSchemaServiceProvider; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
7
|
|
|
use Silex\Application; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Symfony\Component\HttpKernel\Client; |
10
|
|
|
|
11
|
|
|
class DescribedByTest extends PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
private $app; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
$this->app = new Application(); |
18
|
|
|
$this->app["debug"] = true; |
19
|
|
|
|
20
|
|
|
$this->app->register(new JsonSchemaServiceProvider()); |
21
|
|
|
|
22
|
|
|
$this->client = new Client($this->app); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testDescribedByProfile() |
26
|
|
|
{ |
27
|
|
|
$this->app["json-schema.correlationMechanism"] = "profile"; |
28
|
|
|
|
29
|
|
|
$this->app->get("/foo", function (Application $app) { |
30
|
|
|
$app["json-schema.describedBy"] = "/schema/foo"; |
31
|
|
|
|
32
|
|
|
return $app->json(new \stdClass()); |
33
|
|
|
}); |
34
|
|
|
|
35
|
|
|
$headers = array( |
36
|
|
|
"HTTP_ACCEPT" => "application/json", |
37
|
|
|
); |
38
|
|
|
$this->client->request("GET", "/foo", array(), array(), $headers); |
39
|
|
|
$response = $this->client->getResponse(); |
40
|
|
|
|
41
|
|
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); |
42
|
|
|
$this->assertEquals("application/json; profile=\"/schema/foo\"", $response->headers->get("Content-Type")); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testDescribedByLink() |
46
|
|
|
{ |
47
|
|
|
$this->app["json-schema.correlationMechanism"] = "link"; |
48
|
|
|
|
49
|
|
|
$this->app->get("/foo", function (Application $app) { |
50
|
|
|
$app["json-schema.describedBy"] = "/schema/foo"; |
51
|
|
|
|
52
|
|
|
return $app->json(new \stdClass()); |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
$headers = array( |
56
|
|
|
"HTTP_ACCEPT" => "application/json", |
57
|
|
|
); |
58
|
|
|
$this->client->request("GET", "/foo", array(), array(), $headers); |
59
|
|
|
$response = $this->client->getResponse(); |
60
|
|
|
|
61
|
|
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); |
62
|
|
|
$this->assertEquals("</schema/foo>; rel=\"describedby\"", $response->headers->get("Link")); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testInvalidCorrelationMechanism() |
66
|
|
|
{ |
67
|
|
|
$this->app["json-schema.correlationMechanism"] = "foo"; |
68
|
|
|
|
69
|
|
|
$this->app->get("/foo", function (Application $app) { |
70
|
|
|
$app["json-schema.describedBy"] = "/schema/foo"; |
71
|
|
|
|
72
|
|
|
return $app->json(new \stdClass()); |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
$this->app->error(function (\Exception $e, $code) { |
|
|
|
|
76
|
|
|
$errorMessage = "json-schema.correlationMechanism must be either \"profile\" or \"link\""; |
77
|
|
|
$this->assertEquals($errorMessage, $e->getMessage()); |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
$headers = array( |
81
|
|
|
"HTTP_ACCEPT" => "application/json", |
82
|
|
|
); |
83
|
|
|
$this->client->request("GET", "/foo", array(), array(), $headers); |
84
|
|
|
$response = $this->client->getResponse(); |
85
|
|
|
|
86
|
|
|
$this->assertEquals(Response::HTTP_SERVICE_UNAVAILABLE, $response->getStatusCode()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testNoContent() |
90
|
|
|
{ |
91
|
|
|
$this->app->get("/foo", function (Application $app) { |
92
|
|
|
$app["json-schema.describedBy"] = "/schema/foo"; |
93
|
|
|
|
94
|
|
|
return Response::create("", Response::HTTP_NO_CONTENT); |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
$headers = array( |
98
|
|
|
"HTTP_ACCEPT" => "application/json", |
99
|
|
|
); |
100
|
|
|
$this->client->request("GET", "/foo", array(), array(), $headers); |
101
|
|
|
$response = $this->client->getResponse(); |
102
|
|
|
|
103
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testNoDescribedBy() |
107
|
|
|
{ |
108
|
|
|
$this->app->get("/foo", function (Application $app) { |
109
|
|
|
return $app->json(new \stdClass()); |
110
|
|
|
}); |
111
|
|
|
|
112
|
|
|
$headers = array( |
113
|
|
|
"HTTP_ACCEPT" => "application/json", |
114
|
|
|
); |
115
|
|
|
$this->client->request("GET", "/foo", array(), array(), $headers); |
116
|
|
|
$response = $this->client->getResponse(); |
117
|
|
|
|
118
|
|
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); |
119
|
|
|
$this->assertEquals("application/json", $response->headers->get("Content-Type")); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: