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->get("/foo", function (Application $app) { |
28
|
|
|
$app["json-schema.describedBy"] = "/schema/foo"; |
29
|
|
|
|
30
|
|
|
return $app->json(new \stdClass()); |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
$headers = array( |
34
|
|
|
"HTTP_ACCEPT" => "application/json", |
35
|
|
|
); |
36
|
|
|
$this->client->request("GET", "/foo", array(), array(), $headers); |
37
|
|
|
$response = $this->client->getResponse(); |
38
|
|
|
|
39
|
|
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); |
40
|
|
|
$this->assertEquals("application/json; profile=\"/schema/foo\"", $response->headers->get("Content-Type")); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testDescribedByLink() |
44
|
|
|
{ |
45
|
|
|
$this->app["json-schema.correlationMechanism"] = "link"; |
46
|
|
|
|
47
|
|
|
$this->app->get("/foo", function (Application $app) { |
48
|
|
|
$app["json-schema.describedBy"] = "/schema/foo"; |
49
|
|
|
|
50
|
|
|
return $app->json(new \stdClass()); |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
$headers = array( |
54
|
|
|
"HTTP_ACCEPT" => "application/json", |
55
|
|
|
); |
56
|
|
|
$this->client->request("GET", "/foo", array(), array(), $headers); |
57
|
|
|
$response = $this->client->getResponse(); |
58
|
|
|
|
59
|
|
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); |
60
|
|
|
$this->assertEquals("</schema/foo>; rel=\"describedBy\"", $response->headers->get("Link")); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testInvalidCorrelationMechanism() |
64
|
|
|
{ |
65
|
|
|
$this->app["json-schema.correlationMechanism"] = "foo"; |
66
|
|
|
|
67
|
|
|
$this->app->get("/foo", function (Application $app) { |
68
|
|
|
$app["json-schema.describedBy"] = "/schema/foo"; |
69
|
|
|
|
70
|
|
|
return $app->json(new \stdClass()); |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
$this->app->error(function (\Exception $e, $code) { |
|
|
|
|
74
|
|
|
$errorMessage = "json-schema.correlationMechanism must be either \"profile\" or \"link\""; |
75
|
|
|
$this->assertEquals($errorMessage, $e->getMessage()); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
$headers = array( |
79
|
|
|
"HTTP_ACCEPT" => "application/json", |
80
|
|
|
); |
81
|
|
|
$this->client->request("GET", "/foo", array(), array(), $headers); |
82
|
|
|
$response = $this->client->getResponse(); |
83
|
|
|
|
84
|
|
|
$this->assertEquals(Response::HTTP_SERVICE_UNAVAILABLE, $response->getStatusCode()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testNoContent() |
88
|
|
|
{ |
89
|
|
|
$this->app->get("/foo", function (Application $app) { |
90
|
|
|
$app["json-schema.describedBy"] = "/schema/foo"; |
91
|
|
|
|
92
|
|
|
return Response::create("", Response::HTTP_NO_CONTENT); |
93
|
|
|
}); |
94
|
|
|
|
95
|
|
|
$headers = array( |
96
|
|
|
"HTTP_ACCEPT" => "application/json", |
97
|
|
|
); |
98
|
|
|
$this->client->request("GET", "/foo", array(), array(), $headers); |
99
|
|
|
$response = $this->client->getResponse(); |
100
|
|
|
|
101
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testNoDescribedBy() |
105
|
|
|
{ |
106
|
|
|
$this->app->get("/foo", function (Application $app) { |
107
|
|
|
return $app->json(new \stdClass()); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
$headers = array( |
111
|
|
|
"HTTP_ACCEPT" => "application/json", |
112
|
|
|
); |
113
|
|
|
$this->client->request("GET", "/foo", array(), array(), $headers); |
114
|
|
|
$response = $this->client->getResponse(); |
115
|
|
|
|
116
|
|
|
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); |
117
|
|
|
$this->assertEquals("application/json", $response->headers->get("Content-Type")); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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: