Issues (3)

src/Test/SwaggerServiceProviderTest.php (1 issue)

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 13 and the first side effect is on line 9.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace JDesrosiers\Silex\Provider\Test;
4
5
use JDesrosiers\Silex\Provider\SwaggerServiceProvider;
6
use Silex\Application;
7
use Symfony\Component\HttpKernel\Client;
8
9
require_once __DIR__ . "/../../vendor/autoload.php";
10
11
date_default_timezone_set("GMT");
12
13
class SwaggerServiceProviderTest extends \PHPUnit_Framework_TestCase
14
{
15
    protected $app;
16
17
    public function setUp()
18
    {
19
        $this->app = new Application();
20
        $this->app->register(new SwaggerServiceProvider(), array(
21
            "swagger.srcDir" => __DIR__ . "/../../vendor/zircote/swagger-php/library",
22
            "swagger.servicePath" => __DIR__,
23
            "swagger.excludePath" => __DIR__ . "/Exclude",
24
        ));
25
    }
26
27
    public function dataProviderApiDocs()
28
    {
29
        $configs = array(
30
            "swagger.basePath" => "http://localhost:8888",
31
            "swagger.apiVersion" => "1.1",
32
            "swagger.swaggerVersion" => "9.9.9",
33
        );
34
35
        return array(
36
            array("/api-docs", array(), "/foo", "/api-docs/foo", "/api-docs/baz"),
37
            array("/api-docs", array(), "/foo/bar", "/api-docs/foo-bar", "/api-docs/baz"),
38
            array("/api-docs", $configs, "/fooBarDefaults", "/api-docs/fooBarDefaults", "/api-docs/baz"),
39
            array("/api/api-docs", array(), "/foo", "/api/api-docs/foo", "/api/api-docs/baz"),
40
            array("/api/api-docs", array(), "/foo/bar", "/api/api-docs/foo-bar", "/api/api-docs/baz"),
41
            array("/api/api-docs", $configs, "/fooBarDefaults", "/api/api-docs/fooBarDefaults", "/api/api-docs/baz"),
42
        );
43
    }
44
45
    /**
46
     * @dataProvider dataProviderApiDocs
47
     */
48
    public function testApiDocs($apiDocPath, $options, $resource, $resourcePath, $excludePath)
49
    {
50
        $resourceList = array(
51
            "apiVersion" => isset($options["swagger.apiVersion"]) ? $options["swagger.apiVersion"] : "0.1",
52
            "swaggerVersion" => isset($options["swagger.swaggerVersion"]) ? $options["swagger.swaggerVersion"] : "1.2",
53
            "apis" => array(
54
                array(
55
                    "path" => "/foo",
56
                ),
57
                array(
58
                    "path" => "/foo-bar",
59
                ),
60
                array(
61
                    "path" => "/fooBarDefaults",
62
                ),
63
            ),
64
        );
65
66
        if (isset($options["swagger.basePath"])) {
67
            $resourceList["basePath"] = $options["swagger.basePath"];
68
        }
69
70
        // Ensure that swagger.basePath, .apiVersion and .swaggerVersion options are respected.
71
        foreach ($options as $key => $value) {
72
            $this->app[$key] = $value;
73
        }
74
75
        $options = array(
76
            "output" => "json",
77
            "json_pretty_print" => $this->app["swagger.prettyPrint"],
78
            "defaultBasePath" => $this->app["swagger.basePath"],
79
            "defaultApiVersion" => $this->app["swagger.apiVersion"],
80
            "defaultSwaggerVersion" => $this->app["swagger.swaggerVersion"],
81
        );
82
        $expectedResponse = $this->app["swagger"]->getResource($resource, $options);
83
84
        $this->app["swagger.apiDocPath"] = $apiDocPath;
85
86
        // Test resource list
87
        $client = new Client($this->app);
88
        $client->request("GET", $apiDocPath);
89
        $response = $client->getResponse();
90
        $this->assertEquals(200, $response->getStatusCode());
91
        $this->assertEquals("application/json", $response->headers->get("Content-Type"));
92
        $this->assertEquals($resourceList, json_decode($response->getContent(), true));
93
94
        // Test resource
95
        $client = new Client($this->app);
96
        $client->request("GET", $resourcePath);
97
        $response = $client->getResponse();
98
        $this->assertEquals(200, $response->getStatusCode());
99
        $this->assertEquals("application/json", $response->headers->get("Content-Type"));
100
        $this->assertEquals($expectedResponse, $response->getContent());
101
102
        // Test excluded resource
103
        $client = new Client($this->app);
104
        $client->request("GET", $excludePath);
105
        $response = $client->getResponse();
106
        $this->assertEquals(404, $response->getStatusCode());
107
    }
108
109
    public function dataProviderCaching()
110
    {
111
        return array(
112
            array(array("public" => true, "max_age" => "6"), array("Cache-Control" => "max-age=6, public")),
113
            array(array("public" => false, "s_maxage" => "6"), array("Cache-Control" => "private, s-maxage=6")),
114
            array(array("last_modified" => new \DateTime("Thu, 11 Jul 2013 07:22:49 GMT")), array("Cache-Control" => "private, must-revalidate", "Last-Modified" => "Thu, 11 Jul 2013 07:22:49 GMT")),
115
        );
116
    }
117
118
    /**
119
     * @dataProvider dataProviderCaching
120
     */
121
    public function testCaching($cache, $expectedHeaders)
122
    {
123
        $this->app["swagger.cache"] = $cache;
124
125
        $client = new Client($this->app);
126
        $client->request("GET", "/api/api-docs");
127
        $response = $client->getResponse();
128
129
        $this->assertEquals(200, $response->getStatusCode());
130
        foreach ($expectedHeaders as $header => $value) {
131
            $this->assertEquals($value, $response->headers->get($header));
132
        }
133
    }
134
135
    public function testNotModified()
136
    {
137
        $client = new Client($this->app);
138
        $client->request("GET", "/api/api-docs");
139
        $response = $client->getResponse();
140
141
        $client = new Client($this->app, array("HTTP_IF_NONE_MATCH" => $response->headers->get("ETag")));
142
        $client->request("GET", "/api/api-docs");
143
        $response = $client->getResponse();
144
145
        $this->assertEquals(304, $response->getStatusCode());
146
        $this->assertFalse($response->headers->has("Content-Type"));
147
        $this->assertEquals("", $response->getContent());
148
    }
149
150
    public function testModified()
151
    {
152
        $client = new Client($this->app, array("HTTP_IF_NONE_MATCH" => '"49fe5e81e4d90156fbef0a3ae347777f"'));
153
        $client->request("GET", "/api/api-docs");
154
        $response = $client->getResponse();
155
156
        $this->assertEquals(200, $response->getStatusCode());
157
        $this->assertTrue(strlen($response->getContent()) > 0);
158
        $this->assertEquals("application/json", $response->headers->get("Content-Type"));
159
    }
160
161
    public function testLogging()
162
    {
163
        $this->app["swagger.excludePath"] = array();
164
        $this->app["swagger.servicePath"] = __DIR__ . "/../../../../../vendor/zircote/swagger-php";
165
        $realPath = realpath($this->app["swagger.servicePath"]);
166
167
        $this->app["logger"] = $this->getMock("Psr\Log\LoggerInterface");
168
        $this->app["logger"]->expects($this->any())
169
            ->method("notice")
170
            ->with("Skipping files in \"$realPath/tests\" add your \"vendor\" directory to the exclude paths");
171
172
        $client = new Client($this->app);
173
        $client->request("GET", "/api/api-docs");
174
        $client->getResponse();
175
    }
176
}
177