CartServiceTest   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 291
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A dataProviderNotAcceptableReturns406() 0 8 1
A testNotAcceptableReturns406() 0 18 1
A testNoAcceptHeaders() 0 21 1
A testAcceptAny() 0 19 1
A dataProviderUnsupportedMediaTypeReturns415() 0 8 1
A testUnsupportedMediaTypeReturns415() 0 19 1
A dataProviderJmsCreateResponse() 0 16 1
A testJmsCreateResponse() 0 23 1
A dataProviderJmsDeserializeRequest() 0 13 1
A testJmsDeserializeRequest() 0 22 1
A dataProviderSymfonyCreateResponse() 0 14 1
A testSymfonyCreateResponse() 0 21 1
A dataProviderSymfonyDeserializeRequest() 0 13 1
B testSymfonyDeserializeRequest() 0 28 1
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 14 and the first side effect is on line 11.

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\Tests\Silex\Provider;
4
5
use JDesrosiers\Silex\Provider\ContentNegotiationServiceProvider;
6
use JDesrosiers\Silex\Provider\JmsSerializerServiceProvider;
7
use Silex\Application;
8
use Silex\Provider\SerializerServiceProvider;
9
use Symfony\Component\HttpKernel\Client;
10
11
require_once __DIR__ . "/../vendor/autoload.php";
12
require __DIR__ . "/Fixtures/Foo.php";
13
14
class CartServiceTest extends \PHPUnit_Framework_TestCase
15
{
16
    protected $app;
17
18
    public function setUp()
19
    {
20
        $this->app = new Application();
21
        $this->app["debug"] = true;
22
23
        $this->app->register(new ContentNegotiationServiceProvider(), array(
24
             "conneg.responseFormats" => array("json", "xml"),
25
             "conneg.requestFormats" => array("json", "xml"),
26
             "conneg.defaultFormat" => "json",
27
        ));
28
    }
29
30
    public function dataProviderNotAcceptableReturns406()
31
    {
32
        return array(
33
            array("text/html"),
34
            array("image/jpeg"),
35
            array("foo/bar"),
36
        );
37
    }
38
39
    /**
40
     * @dataProvider dataProviderNotAcceptableReturns406
41
     */
42
    public function testNotAcceptableReturns406($accept)
43
    {
44
        $this->app->get("/foo", function (Application $app) {
45
            return $app->json(array("foo" => "bar"));
46
        });
47
48
        $headers = array(
49
            "HTTP_ACCEPT" => $accept,
50
        );
51
52
        $client = new Client($this->app, $headers);
53
        $client->request("GET", "/foo");
54
55
        $response = $client->getResponse();
56
57
        $this->assertEquals("406", $response->getStatusCode());
58
        $this->assertEquals("text/html; charset=UTF-8", $response->headers->get("Content-Type"));
59
    }
60
61
    public function testNoAcceptHeaders()
62
    {
63
        $this->app->get("/foo", function (Application $app) {
64
            return $app->json(array("foo" => "bar"));
65
        });
66
67
        $headers = array(
68
            "HTTP_ACCEPT" => null,
69
            "HTTP_ACCEPT_CHARSET" => null,
70
            "HTTP_ACCEPT_LANGUAGE" => null,
71
        );
72
73
        $client = new Client($this->app, $headers);
74
        $client->request("GET", "/foo");
75
76
        $response = $client->getResponse();
77
78
        $this->assertEquals("200", $response->getStatusCode());
79
        $this->assertEquals('{"foo":"bar"}', $response->getContent());
80
        $this->assertEquals($response->headers->get("Content-Type"), "application/json");
81
    }
82
83
    public function testAcceptAny()
84
    {
85
        $this->app->get("/foo", function (Application $app) {
86
            return $app->json(array("foo" => "bar"));
87
        });
88
89
        $headers = array(
90
            "HTTP_ACCEPT" => "*/*",
91
        );
92
93
        $client = new Client($this->app, $headers);
94
        $client->request("GET", "/foo");
95
96
        $response = $client->getResponse();
97
98
        $this->assertEquals("200", $response->getStatusCode());
99
        $this->assertEquals('{"foo":"bar"}', $response->getContent());
100
        $this->assertEquals($response->headers->get("Content-Type"), "application/json");
101
    }
102
103
    public function dataProviderUnsupportedMediaTypeReturns415()
104
    {
105
        return array(
106
            array("application/json", "image/jpeg", "application/json"),
107
            array("application/xml", "image/jpeg", "text/xml; charset=UTF-8"),
108
            array("text/xml; charset=UTF-8", "image/jpeg", "text/xml; charset=UTF-8"),
109
        );
110
    }
111
112
    /**
113
     * @dataProvider dataProviderUnsupportedMediaTypeReturns415
114
     */
115
    public function testUnsupportedMediaTypeReturns415($accept, $contentType, $expectedContentType)
116
    {
117
        $this->app->post("/foo", function (Application $app) {
118
            return $app->json(array("foo" => "bar"));
119
        });
120
121
        $headers = array(
122
            "HTTP_ACCEPT" => $accept,
123
            "HTTP_CONTENT_TYPE" => $contentType,
124
        );
125
126
        $client = new Client($this->app, $headers);
127
        $client->request("POST", "/foo", array(), array(), $headers, "testdata");
128
129
        $response = $client->getResponse();
130
131
        $this->assertEquals("415", $response->getStatusCode());
132
        $this->assertEquals($expectedContentType, $response->headers->get("Content-Type"));
133
    }
134
135
    public function dataProviderJmsCreateResponse()
136
    {
137
        $xml = <<<XML
138
<?xml version="1.0" encoding="UTF-8"?>
139
<result>
140
  <entry><![CDATA[bar]]></entry>
141
</result>
142
143
XML;
144
145
        return array(
146
            array("application/json", "application/json", '{"foo":"bar"}'),
147
            array("text/xml", "text/xml; charset=UTF-8", $xml),
148
            array("application/xml", "text/xml; charset=UTF-8", $xml),
149
        );
150
    }
151
152
    /**
153
     * @dataProvider dataProviderJmsCreateResponse
154
     */
155
    public function testJmsCreateResponse($accept, $expectedContentType, $expectedContent)
156
    {
157
        $this->app->register(new JmsSerializerServiceProvider(), array(
158
            "serializer.srcDir" => __DIR__ . "/../vendor/jms/serializer/src",
159
        ));
160
161
        $this->app->get("/foo", function (Application $app) {
162
            return $app["conneg"]->createResponse(array("foo" => "bar"));
163
        });
164
165
        $headers = array(
166
            "HTTP_ACCEPT" => $accept,
167
        );
168
169
        $client = new Client($this->app, $headers);
170
        $client->request("GET", "/foo");
171
172
        $response = $client->getResponse();
173
174
        $this->assertEquals("200", $response->getStatusCode());
175
        $this->assertEquals($expectedContentType, $response->headers->get("Content-Type"));
176
        $this->assertEquals($expectedContent, $response->getContent());
177
    }
178
179
    public function dataProviderJmsDeserializeRequest()
180
    {
181
        $xml = <<<XML
182
<?xml version="1.0" encoding="UTF-8"?>
183
<request><![CDATA[foo]]></request>
184
XML;
185
186
        return array(
187
            array("application/json", '"foo"'),
188
            array("text/xml; charset=UTF-8", $xml),
189
            array("application/xml", $xml),
190
        );
191
    }
192
193
    /**
194
     * @dataProvider dataProviderJmsDeserializeRequest
195
     */
196
    public function testJmsDeserializeRequest($contentType, $content)
197
    {
198
        $this->app->register(new JmsSerializerServiceProvider(), array(
199
            "serializer.srcDir" => __DIR__ . "/../vendor/jms/serializer/src",
200
        ));
201
202
        $this->app->post("/foo", function (Application $app) {
203
            return print_r($app["conneg"]->deserializeRequest("string"), true);
204
        });
205
206
        $headers = array(
207
            "CONTENT_TYPE" => $contentType,
208
        );
209
210
        $client = new Client($this->app, $headers);
211
        $client->request("POST", "/foo", array(), array(), $headers, $content);
212
213
        $response = $client->getResponse();
214
215
        $this->assertEquals("200", $response->getStatusCode());
216
        $this->assertEquals("foo", $response->getContent());
217
    }
218
219
    public function dataProviderSymfonyCreateResponse()
220
    {
221
        $xml = <<<XML
222
<?xml version="1.0"?>
223
<response><foo>bar</foo></response>
224
225
XML;
226
227
        return array(
228
            array("application/json", "application/json", '{"foo":"bar"}'),
229
            array("text/xml", "text/xml; charset=UTF-8", $xml),
230
            array("application/xml", "text/xml; charset=UTF-8", $xml),
231
        );
232
    }
233
234
    /**
235
     * @dataProvider dataProviderSymfonyCreateResponse
236
     */
237
    public function testSymfonyCreateResponse($accept, $expectedContentType, $expectedContent)
238
    {
239
        $this->app->register(new SerializerServiceProvider());
240
241
        $this->app->get("/foo", function (Application $app) {
242
            return $app["conneg"]->createResponse(array("foo" => "bar"));
243
        });
244
245
        $headers = array(
246
            "HTTP_ACCEPT" => $accept,
247
        );
248
249
        $client = new Client($this->app, $headers);
250
        $client->request("GET", "/foo");
251
252
        $response = $client->getResponse();
253
254
        $this->assertEquals("200", $response->getStatusCode());
255
        $this->assertEquals($expectedContentType, $response->headers->get("Content-Type"));
256
        $this->assertEquals($expectedContent, $response->getContent());
257
    }
258
259
    public function dataProviderSymfonyDeserializeRequest()
260
    {
261
        $xml = <<<XML
262
<?xml version="1.0" encoding="UTF-8"?>
263
<Foo><foo><![CDATA[bar]]></foo></Foo>
264
XML;
265
266
        return array(
267
            array("application/json", '{"foo":"bar"}'),
268
            array("text/xml; charset=UTF-8", $xml),
269
            array("application/xml", $xml),
270
        );
271
    }
272
273
    /**
274
     * @dataProvider dataProviderSymfonyDeserializeRequest
275
     */
276
    public function testSymfonyDeserializeRequest($contentType, $content)
277
    {
278
        $this->app->register(new SerializerServiceProvider());
279
280
        $expectedContent = <<<CONTENT
281
JDesrosiers\Tests\Silex\Provider\Fixtures\Foo Object
282
(
283
    [foo:protected] => bar
284
)
285
286
CONTENT;
287
288
        $this->app->post("/foo", function (Application $app) {
289
            return print_r($app["conneg"]->deserializeRequest("JDesrosiers\Tests\Silex\Provider\Fixtures\Foo"), true);
290
        });
291
292
        $headers = array(
293
            "CONTENT_TYPE" => $contentType,
294
        );
295
296
        $client = new Client($this->app, $headers);
297
        $client->request("POST", "/foo", array(), array(), $headers, $content);
298
299
        $response = $client->getResponse();
300
301
        $this->assertEquals("200", $response->getStatusCode());
302
        $this->assertEquals($expectedContent, $response->getContent());
303
    }
304
}
305