Completed
Push — master ( 7d4a72...efe314 )
by Jason
04:42
created

DescribedByTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 109
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A testDescribedByProfile() 0 17 1
A testDescribedByLink() 0 19 1
A testInvalidCorrelationMechanism() 0 23 1
A testNoContent() 0 16 1
A testNoDescribedBy() 0 15 1
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);
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $code is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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