Passed
Push — master ( 3ba553...c661e4 )
by Pouya
01:29
created

ConvertToAbsolutePathTest::dataForConvert()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 74
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 61
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 74
rs 8.8509

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace unit;
4
5
use http\Client;
6
use PHPUnit\Framework\TestCase;
7
use seosazi\PathConverter\ConvertToAbsolutePath;
8
9
class ConvertToAbsolutePathTest extends TestCase
10
{
11
    /** @var ConvertToAbsolutePath */
12
    private $address;
13
14
    public function setUp(): void
15
    {
16
        parent::setUp(); // TODO: Change the autogenerated stub
17
        $this->setAddress(
18
            new ConvertToAbsolutePath('http://example.com/some/fake/path/page.html')
19
        );
20
    }
21
22
    public function testConstruct()
23
    {
24
        $this->expectException(new ConvertToAbsolutePath('/some/fake/path/page.html'));
0 ignored issues
show
Bug introduced by
new seosazi\PathConverte...e/fake/path/page.html') of type seosazi\PathConverter\ConvertToAbsolutePath is incompatible with the type string expected by parameter $exception of PHPUnit\Framework\TestCase::expectException(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
        $this->expectException(/** @scrutinizer ignore-type */ new ConvertToAbsolutePath('/some/fake/path/page.html'));
Loading history...
25
    }
26
27
    /**
28
     * @dataProvider dataForTestUpToLastDir
29
     * @param $path
30
     * @param $result
31
     */
32
    public function testUpToLastDir($path, $result)
33
    {
34
        $this->assertSame($this->getAddress()->upToLastDir($path), $result);
35
    }
36
37
    public function dataForTestUpToLastDir()
38
    {
39
        return [
40
            'page path with file name'  => [
41
                'http://example.com/some/fake/path/page.html',
42
                'http://example.com/some/fake/path/'
43
            ],
44
            'page path with any file name type 1' => [
45
                'http://example.com/some/fake/',
46
                'http://example.com/some/fake/'
47
            ],
48
            'page path with any file name type 2' => [
49
                'http://example.com/some/fake',
50
                'http://example.com/some/'
51
            ],
52
            'home page' => [
53
                'http://example.com',
54
                'http://example.com/'
55
            ]
56
        ];
57
    }
58
59
    /**
60
     * @dataProvider dataForOnlySitePath
61
     * @param $path
62
     * @param $result
63
     */
64
    public function testOnlySitePath($path, $result)
65
    {
66
        $this->assertSame($this->getAddress()->onlySitePath($path), $result);
67
    }
68
69
    public function dataForOnlySitePath()
70
    {
71
        return [
72
            'page path with file name'  => [
73
                'http://example.com/some/fake/path/page.html',
74
                'http://example.com'
75
            ],
76
            'page path with any file name type 1' => [
77
                'http://example.com/some/fake/',
78
                'http://example.com'
79
            ],
80
            'page path with any file name type 2' => [
81
                'http://example.com/some/fake',
82
                'http://example.com'
83
            ],
84
            'home page 1' => [
85
                'http://example.com',
86
                'http://example.com'
87
            ],
88
            'home page 2' => [
89
                'example.com/some/fake',
90
                ''
91
            ]
92
        ];
93
    }
94
95
96
    /**
97
     * @dataProvider dataForConvert
98
     * @param $pagePath
99
     * @param $baseTag
100
     * @param $path
101
     * @param $result
102
     */
103
    public function testConvert($pagePath, $baseTag, $path, $result)
104
    {
105
        if (isset($pagePath)) {
106
            $this->getAddress()->setPagePath($pagePath);
107
        }
108
        if (isset($baseTag)) {
109
            $this->getAddress()->setBaseTag($baseTag);
110
        }
111
        $this->assertSame($this->getAddress()->convert($path), $result);
112
    }
113
114
    public function dataForConvert()
115
    {
116
        return [
117
            'normal page with base 1'  => [
118
                'https://www.php.net/manual/en/function.parse-url.php',
119
                'https://www.php.net/manual/en/function.parse-url.php',
120
                '/images/[email protected]',
121
                'https://www.php.net/images/[email protected]'
122
            ],
123
            'normal page without base'  => [
124
                'https://kafshnice.ir/product-category/shoe/for-men/',
125
                null,
126
                'https://kafshnice.ir/wp-content/uploads/2020/01/2020-01-23_6-11-45-99x75.png',
127
                'https://kafshnice.ir/wp-content/uploads/2020/01/2020-01-23_6-11-45-99x75.png'
128
            ],
129
            'normal page without base 2'  => [
130
                'https://roadmap.sh/backend',
131
                null,
132
                '/_next/static/hxwb-QfpHEYaFAmytdA9D/pages/%5Broadmap%5D.js',
133
                'https://roadmap.sh/_next/static/hxwb-QfpHEYaFAmytdA9D/pages/%5Broadmap%5D.js'
134
            ],
135
            'normal page without base 3'  => [
136
                'https://bastam.bankmellat.ir/bastam/shahab_service',
137
                null,
138
                '/bastam/resources/images/browserSupport/war_icon.png',
139
                'https://bastam.bankmellat.ir/bastam/resources/images/browserSupport/war_icon.png'
140
            ],
141
            'normal page with base 2'  => [
142
                'https://bastam.bankmellat.ir/bastam/shahab_service',
143
                '/bastam/resources/images/',
144
                'browserSupport/war_icon.png',
145
                'https://bastam.bankmellat.ir/bastam/resources/images/browserSupport/war_icon.png'
146
            ],
147
            'normal page with base 3'  => [
148
                'https://bastam.bankmellat.ir/bastam/shahab_service',
149
                'bastam/resources/images',
150
                'browserSupport/war_icon.png',
151
                'https://bastam.bankmellat.ir/bastam/resources/images/browserSupport/war_icon.png'
152
            ],
153
            'javascript' => [
154
                'https://bastam.bankmellat.ir/bastam/shahab_service',
155
                null,
156
                'javascript:void(0)',
157
                ''
158
            ],
159
            'WithoutScheme' => [
160
                'https://bastam.bankmellat.ir/bastam/shahab_service',
161
                null,
162
                '//bastam.bankmellat.ir/bastam/resources/images/browserSupport/war_icon.png',
163
                'http://bastam.bankmellat.ir/bastam/resources/images/browserSupport/war_icon.png'
164
            ],
165
            'QueryOrFragment' => [
166
                'https://bastam.bankmellat.ir/bastam/shahab_service',
167
                null,
168
                '?ex=151.0_5',
169
                'https://bastam.bankmellat.ir/bastam/shahab_service?ex=151.0_5'
170
            ],
171
            'WithPointSlash' => [
172
                'https://bastam.bankmellat.ir/bastam/shahab_service',
173
                null,
174
                './browserSupport/war_icon.png',
175
                'https://bastam.bankmellat.ir/bastam/browserSupport/war_icon.png'
176
            ],
177
            'with ../' => [
178
                'https://www.jquery-az.com/html/test/demo.php?ex=151.0_5',
179
                null,
180
                '../../banana.jpg',
181
                'https://www.jquery-az.com/banana.jpg'
182
            ],
183
            'empty path ' => [
184
                'https://www.jquery-az.com/html/test/demo.php',
185
                null,
186
                '',
187
                'https://www.jquery-az.com/html/test/demo.php',
188
            ]
189
        ];
190
    }
191
192
    /**
193
     * @dataProvider dataForGetDomain
194
     * @param $pagePath
195
     * @param $baseTag
196
     * @param $result
197
     */
198
    public function testGetDomain($pagePath, $baseTag, $result)
199
    {
200
        if (isset($pagePath)) {
201
            $this->getAddress()->setPagePath($pagePath);
202
        }
203
        if (isset($baseTag)) {
204
            $this->getAddress()->setBaseTag($baseTag);
205
        }
206
        $this->assertSame($this->getAddress()->getDomain(), $result);
207
        $this->assertSame($this->getAddress()->getDomain(), $result);
208
    }
209
210
    public function dataForGetDomain()
211
    {
212
        return [
213
            'normal page with base 1'  => [
214
                'https://www.php.net/manual/en/function.parse-url.php',
215
                'https://www.php.net/manual/en/function.parse-url.php',
216
                'www.php.net/'
217
            ],
218
            'normal page without base'  => [
219
                'https://kafshnice.ir/product-category/shoe/for-men/',
220
                null,
221
                'kafshnice.ir/'
222
            ],
223
            'normal page with base 2'  => [
224
                'https://bastam.bankmellat.ir/bastam/shahab_service',
225
                '/bastam/resources/images/',
226
                'bastam.bankmellat.ir/'
227
            ]
228
        ];
229
    }
230
231
    /**
232
     * @dataProvider dataForGetScheme
233
     * @param $pagePath
234
     * @param $baseTag
235
     * @param $result
236
     */
237
    public function testGetScheme($pagePath, $baseTag, $result)
238
    {
239
        if (isset($pagePath)) {
240
            $this->getAddress()->setPagePath($pagePath);
241
        }
242
        if (isset($baseTag)) {
243
            $this->getAddress()->setBaseTag($baseTag);
244
        }
245
        $this->assertSame($this->getAddress()->getScheme(), $result);
246
        $this->assertSame($this->getAddress()->getScheme(), $result);
247
    }
248
249
    public function dataForGetScheme()
250
    {
251
        return [
252
            'normal page with base 1'  => [
253
                'https://www.php.net/manual/en/function.parse-url.php',
254
                'http://www.php.net/manual/en/function.parse-url.php',
255
                'http'
256
            ],
257
            'normal page without base'  => [
258
                'https://kafshnice.ir/product-category/shoe/for-men/',
259
                null,
260
                'https'
261
            ],
262
            'normal page with base 2'  => [
263
                'https://bastam.bankmellat.ir/bastam/shahab_service',
264
                '/bastam/resources/images/',
265
                'https'
266
            ]
267
        ];
268
    }
269
270
    /**
271
     * @dataProvider dataForGetBaseTagParsing
272
     * @param $baseTag
273
     * @param $result
274
     */
275
    public function testGetBaseTagParsing($baseTag, $result)
276
    {
277
        if (isset($baseTag)) {
278
            $this->getAddress()->setBaseTag($baseTag);
279
        }
280
        $this->assertSame($this->getAddress()->getBaseTagParsing(), $result);
281
        $this->assertSame($this->getAddress()->getBaseTagParsing(), $result);
282
    }
283
284
    public function dataForGetBaseTagParsing()
285
    {
286
        return [
287
            'normal page with base tag null'  => [
288
                null,
289
                []
290
            ],
291
            'normal page with base tag'  => [
292
                'https://bastam.bankmellat.ir/bastam/shahab_service',
293
                parse_url('https://bastam.bankmellat.ir/bastam/shahab_service')
294
            ]
295
        ];
296
    }
297
298
    /**
299
     * @dataProvider dataForGetPagePathParsing
300
     * @param $pagePath
301
     * @param $result
302
     */
303
    public function testGetPagePathParsing($pagePath, $result)
304
    {
305
        if (isset($pagePath)) {
306
            $this->getAddress()->setPagePath($pagePath);
307
        }
308
        $this->assertSame($this->getAddress()->getPagePathParsing(), $result);
309
        $this->assertSame($this->getAddress()->getPagePathParsing(), $result);
310
    }
311
312
    public function dataForGetPagePathParsing()
313
    {
314
        return [
315
            'normal page with base tag null'  => [
316
                null,
317
                parse_url('http://example.com/some/fake/path/page.html')
318
            ],
319
            'normal page with base tag'  => [
320
                'https://bastam.bankmellat.ir/bastam/shahab_service',
321
                parse_url('https://bastam.bankmellat.ir/bastam/shahab_service')
322
            ]
323
        ];
324
    }
325
326
    /**
327
     * @dataProvider dataForCheckPathIsAbsoluteOrForAnotherApp
328
     * @param $path
329
     * @param $result
330
     */
331
    public function testCheckPathIsAbsoluteOrForAnotherApp($path, $result)
332
    {
333
        $this->assertSame($this->getAddress()->checkPathIsAbsoluteOrForAnotherApp($path), $result);
334
    }
335
336
    public function dataForCheckPathIsAbsoluteOrForAnotherApp()
337
    {
338
        return [
339
            'tel'  => [
340
                'tel:1-562-867-5309',
341
                ''
342
            ],
343
            'whatsapp'  => [
344
                'whatsapp://send?text=WHATEVER_LINK_OR_TEXT_YOU_WANT_TO_SEND',
345
                ''
346
            ],
347
            'services'  => [
348
                'services://send?text=WHATEVER_LINK_OR_TEXT_YOU_WANT_TO_SEND',
349
                ''
350
            ],
351
            'correct path'  => [
352
                'https://bastam.bankmellat.ir/bastam/shahab_service',
353
                'https://bastam.bankmellat.ir/bastam/shahab_service'
354
            ]
355
        ];
356
    }
357
358
    /**
359
     * @dataProvider dataForIsPathStartWithPointSlash
360
     * @param $path
361
     * @param $result
362
     */
363
    public function testIsPathStartWithPointSlash($path, $result)
364
    {
365
        if (isset($pagePath)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pagePath seems to never exist and therefore isset should always be false.
Loading history...
366
            $this->getAddress()->setPagePath($pagePath);
367
        }
368
        $this->assertSame($this->getAddress()->isPathStartWithPointSlash($path), $result);
369
    }
370
371
    public function dataForIsPathStartWithPointSlash()
372
    {
373
        return [
374
            'true'  => [
375
                './bastam/shahab_service',
376
                true
377
            ],
378
            'false 1'  => [
379
                'https://bastam.bankmellat.ir/bastam/shahab_service',
380
                false
381
            ],
382
            'false 2'  => [
383
                '/bastam/shahab_service',
384
                false
385
            ],
386
            'false 3'  => [
387
                '../bastam/shahab_service',
388
                false
389
            ]
390
        ];
391
    }
392
393
    /**
394
     * @dataProvider dataForIsPathStartWithTwoPointSlash
395
     * @param $path
396
     * @param $result
397
     */
398
    public function testIsPathStartWithTwoPointSlash($path, $result)
399
    {
400
        if (isset($pagePath)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pagePath seems to never exist and therefore isset should always be false.
Loading history...
401
            $this->getAddress()->setPagePath($pagePath);
402
        }
403
        $this->assertSame($this->getAddress()->isPathStartWithTwoPointSlash($path), $result);
404
    }
405
406
    public function dataForIsPathStartWithTwoPointSlash()
407
    {
408
        return [
409
            'true'  => [
410
                './bastam/shahab_service',
411
                false
412
            ],
413
            'false 1'  => [
414
                'https://bastam.bankmellat.ir/bastam/shahab_service',
415
                false
416
            ],
417
            'false 2'  => [
418
                '/bastam/shahab_service',
419
                false
420
            ],
421
            'false 3'  => [
422
                '../bastam/shahab_service',
423
                true
424
            ]
425
        ];
426
    }
427
428
    /**
429
     * @return ConvertToAbsolutePath
430
     */
431
    public function getAddress(): ConvertToAbsolutePath
432
    {
433
        return $this->address;
434
    }
435
436
    /**
437
     * @param ConvertToAbsolutePath $address
438
     */
439
    public function setAddress(ConvertToAbsolutePath $address): void
440
    {
441
        $this->address = $address;
442
    }
443
}
444