Passed
Push — master ( d7918c...3ba553 )
by Pouya
01:23
created

dataForIsPathStartWithTwoPointSlash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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