CUrlTest::providerAsset2()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 15
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 27
rs 8.8571
1
<?php
2
3
namespace Anax\Url;
4
5
/**
6
 * A helper to create urls.
7
 *
8
 */
9
class CUrlTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * Provider for various siteUrls
13
     *
14
     * @return array
15
     */
16
    public function providerSiteUrl()
17
    {
18
        return [
19
            [
20
                "http://dbwebb.se",
21
                "http://dbwebb.se",
22
                "http://dbwebb.se",
23
            ],
24
            [
25
                "http://dbwebb.se/",
26
                "http://dbwebb.se/",
27
                "http://dbwebb.se",
28
            ],
29
            [
30
                "//dbwebb.se",
31
                "//dbwebb.se",
32
                "//dbwebb.se",
33
            ],
34
        ];
35
    }
36
37
38
39
    /**
40
     * Test
41
     *
42
     * @param string $route the route part
43
     *
44
     * @return void
45
     *
46
     * @dataProvider providerSiteUrl
47
     *
48
     */
49 View Code Duplication
    public function testCreateAsSiteUrl($siteUrl, $route, $result)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $url = new \Anax\Url\CUrl();
52
53
        $res = $url->setSiteUrl($siteUrl);
54
        $this->assertInstanceOf(get_class($url), $res, "setSiteUrl did not return this.");
55
56
        $res = $url->create($route);
57
        $this->assertEquals($result, $res, "Created url did not match expected.");
58
    }
59
60
61
62
    /**
63
     * Provider for routes
64
     *
65
     * @return array
66
     */
67
    public function providerRoute()
68
    {
69
        $siteUrl = "http://dbwebb.se";
70
        $baseUrl = $siteUrl;
71
        $scriptName = "index.php";
72
        $urlType = \Anax\Url\CUrl::URL_APPEND;
73
74
        return [
75
            [
76
                $siteUrl,
77
                $baseUrl,
78
                $scriptName,
79
                $urlType,
80
                "",
81
                "$baseUrl/$scriptName",
82
            ],
83
            [
84
                $siteUrl,
85
                $baseUrl,
86
                $scriptName,
87
                $urlType,
88
                "/",
89
                "$siteUrl",
90
            ],
91
            [
92
                $siteUrl,
93
                $baseUrl,
94
                $scriptName,
95
                $urlType,
96
                "/someother/path",
97
                "$siteUrl/someother/path",
98
            ],
99
            [
100
                $siteUrl,
101
                $baseUrl,
102
                $scriptName,
103
                $urlType,
104
                "controller",
105
                "$baseUrl/$scriptName/controller",
106
            ],
107
            [
108
                $siteUrl,
109
                $baseUrl,
110
                $scriptName,
111
                $urlType,
112
                "controller/action",
113
                "$baseUrl/$scriptName/controller/action",
114
            ],
115
            [
116
                $siteUrl,
117
                $baseUrl,
118
                $scriptName,
119
                $urlType,
120
                "controller/action/arg1",
121
                "$baseUrl/$scriptName/controller/action/arg1",
122
            ],
123
            [
124
                $siteUrl,
125
                $baseUrl,
126
                $scriptName,
127
                $urlType,
128
                "controller/action/arg1/arg2",
129
                "$baseUrl/$scriptName/controller/action/arg1/arg2",
130
            ],
131
        ];
132
    }
133
134
135
136
    /**
137
     * Test
138
     *
139
     * @param string $route the route part
140
     *
141
     * @return void
142
     *
143
     * @dataProvider providerRoute
144
     *
145
     */
146
    public function testCreateUrlAppend($siteUrl, $baseUrl, $scriptName, $urlType, $route, $result)
147
    {
148
        $url = new \Anax\Url\CUrl();
149
150
        $res = $url->setSiteUrl($siteUrl);
151
        $this->assertInstanceOf(get_class($url), $res, "setSiteUrl did not return this.");
152
153
        $res = $url->setBaseUrl($baseUrl);
154
        $this->assertInstanceOf(get_class($url), $res, "setBaseUrl did not return this.");
155
156
        $res = $url->setScriptName($scriptName);
157
        $this->assertInstanceOf(get_class($url), $res, "setScriptName did not return this.");
158
159
        $res = $url->setUrlType($urlType);
160
        $this->assertInstanceOf(get_class($url), $res, "setUrlType did not return this.");
161
162
        $res = $url->create($route);
163
        $this->assertEquals($result, $res, "Created url did not match expected.");
164
    }
165
166
167
    /**
168
     * Provider for routes
169
     *
170
     * @return array
171
     */
172
    public function providerRoute2()
173
    {
174
        $siteUrl = "http://dbwebb.se";
175
        $baseUrl = $siteUrl . "/kod-exempel/anax-mvc";
176
        $urlType = \Anax\Url\CUrl::URL_CLEAN;
177
178
        return [
179
            [
180
                $siteUrl,
181
                $baseUrl,
182
                $urlType,
183
                "",
184
                "$baseUrl",
185
            ],
186
            [
187
                $siteUrl,
188
                $baseUrl,
189
                $urlType,
190
                "/",
191
                "$siteUrl",
192
            ],
193
            [
194
                $siteUrl,
195
                $baseUrl,
196
                $urlType,
197
                "/someother/path",
198
                "$siteUrl/someother/path",
199
            ],
200
            [
201
                $siteUrl,
202
                $baseUrl,
203
                $urlType,
204
                "controller",
205
                "$baseUrl/controller",
206
            ],
207
            [
208
                $siteUrl,
209
                $baseUrl,
210
                $urlType,
211
                "controller/action",
212
                "$baseUrl/controller/action",
213
            ],
214
            [
215
                $siteUrl,
216
                $baseUrl,
217
                $urlType,
218
                "controller/action/arg1",
219
                "$baseUrl/controller/action/arg1",
220
            ],
221
            [
222
                $siteUrl,
223
                $baseUrl,
224
                $urlType,
225
                "controller/action/arg1/arg2",
226
                "$baseUrl/controller/action/arg1/arg2",
227
            ],
228
        ];
229
    }
230
231
232
233
    /**
234
     * Test
235
     *
236
     *
237
     * @return void
238
     *
239
     * @dataProvider providerRoute2
240
     *
241
     */
242
    public function testCreateUrlAppend2($siteUrl, $baseUrl, $urlType, $route, $result)
243
    {
244
        $url = new \Anax\Url\CUrl();
245
246
        $res = $url->setSiteUrl($siteUrl);
247
        $this->assertInstanceOf(get_class($url), $res, "setSiteUrl did not return this.");
248
249
        $res = $url->setBaseUrl($baseUrl);
250
        $this->assertInstanceOf(get_class($url), $res, "setBaseUrl did not return this.");
251
252
        $res = $url->setUrlType($urlType);
253
        $this->assertInstanceOf(get_class($url), $res, "setUrlType did not return this.");
254
255
        $res = $url->create($route);
256
        $this->assertEquals($result, $res, "Created url did not match expected.");
257
    }
258
259
260
261
    /**
262
     * Provider for asset
263
     *
264
     * @return array
265
     */
266
    public function providerAsset()
267
    {
268
        $staticSiteUrl = "http://dbwebb.se";
269
        $staticBaseUrl = $staticSiteUrl . "/kod-exempel/anax-mvc";
270
271
        return [
272
            [
273
                $staticSiteUrl,
274
                $staticBaseUrl,
275
                "http://dbwebb.se/css/style.css",
276
                "http://dbwebb.se/css/style.css",
277
            ],
278
            [
279
                $staticSiteUrl,
280
                $staticBaseUrl,
281
                "//dbwebb.se/css/style.css",
282
                "//dbwebb.se/css/style.css",
283
            ],
284
            [
285
                $staticSiteUrl,
286
                $staticBaseUrl,
287
                "/css/style.css",
288
                "$staticSiteUrl/css/style.css",
289
            ],
290
            [
291
                $staticSiteUrl,
292
                $staticBaseUrl,
293
                "css/style.css",
294
                "$staticBaseUrl/css/style.css",
295
            ],
296
        ];
297
    }
298
299
300
301
    /**
302
     * Test
303
     *
304
     * @return void
305
     *
306
     * @dataProvider providerAsset
307
     *
308
     */
309
    public function testCreateAsset($staticSiteUrl, $staticBaseUrl, $asset, $result)
310
    {
311
        $url = new \Anax\Url\CUrl();
312
313
        $res = $url->setStaticSiteUrl($staticSiteUrl);
314
        $this->assertInstanceOf(get_class($url), $res, "setStaticSiteUrl did not return this.");
315
316
        $res = $url->setStaticBaseUrl($staticBaseUrl);
317
        $this->assertInstanceOf(get_class($url), $res, "setStaticBaseUrl did not return this.");
318
319
        $res = $url->asset($asset);
320
        $this->assertEquals($result, $res, "Created url did not match expected.");
321
    }
322
323
324
325
    /**
326
     * Provider for asset
327
     *
328
     * @return array
329
     */
330
    public function providerAsset2()
331
    {
332
        $baseUrl = "http://dbwebb.se/kod-exempel/anax-mvc";
333
334
        return [
335
            [
336
                $baseUrl,
337
                "http://dbwebb.se/css/style.css",
338
                "http://dbwebb.se/css/style.css",
339
            ],
340
            [
341
                $baseUrl,
342
                "//dbwebb.se/css/style.css",
343
                "//dbwebb.se/css/style.css",
344
            ],
345
            [
346
                $baseUrl,
347
                "/css/style.css",
348
                "/css/style.css",
349
            ],
350
            [
351
                $baseUrl,
352
                "css/style.css",
353
                "$baseUrl/css/style.css",
354
            ],
355
        ];
356
    }
357
358
359
360
    /**
361
     * Test
362
     *
363
     * @return void
364
     *
365
     * @dataProvider providerAsset2
366
     *
367
     */
368 View Code Duplication
    public function testCreateAsset2($baseUrl, $asset, $result)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
369
    {
370
        $url = new \Anax\Url\CUrl();
371
372
        $res = $url->setBaseUrl($baseUrl);
373
        $this->assertInstanceOf(get_class($url), $res, "setBaseUrl did not return this.");
374
375
        $res = $url->asset($asset);
376
        $this->assertEquals($result, $res, "Created url did not match expected.");
377
    }
378
379
380
381
    /**
382
     * Test
383
     *
384
     * @expectedException Exception
385
     *
386
     * @return void
387
     *
388
     */
389
    public function testWrongUrlType()
390
    {
391
        $url = new \Anax\Url\CUrl();
392
393
        $res = $url->setUrlType('NO_SUCH_TYPE');
0 ignored issues
show
Unused Code introduced by
$res is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
394
    }
395
}
396