Passed
Push — codeclean ( dc06b1...eb864a )
by Paweł
02:30
created

HelpersTest::testCheckDomainValids()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 22
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 26
rs 9.568
1
<?php
2
3
namespace Wszetko\Sitemap\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Wszetko\Sitemap\Helpers\Url;
7
8
class HelpersTest extends TestCase
9
{
10
    public function testCheckDomainValids()
11
    {
12
        $testCaseValid = [
13
            'a',
14
            'a.b',
15
            'localhost',
16
            'example.',
17
            'example.c',
18
            'example.co',
19
            'example.com',
20
            'example.info',
21
            'example.com.pl',
22
            'example.co.uk',
23
            'example1.com',
24
            '1example.com',
25
            'ex-am-ple.com',
26
            'żółw.pl',
27
            'sub.example.com',
28
            'sub.example.com',
29
            'sub.sub.example.com',
30
            'xn--fsqu00a.xn--0zwm56d'
31
        ];
32
33
        foreach ($testCaseValid as $test) {
34
            $this->assertTrue(Url::checkDomain($test),
35
                "Test 'CheckDomain' for '$test' should return TRUE. FALSE was returned.");
36
        }
37
    }
38
39
    public function testCheckDomainInvalids()
40
    {
41
        $testCaseInvalid = [
42
            '',
43
            ' ',
44
            '-',
45
            '.',
46
            '..',
47
            '0',
48
            'alert(',
49
            '<script',
50
            '.example.com',
51
            'example-.com',
52
            'example.com/',
53
            'example..com',
54
            'example.com ',
55
            ' example.com',
56
            'ex ample.com'
57
        ];
58
59
        foreach ($testCaseInvalid as $test) {
60
            $this->assertFalse(Url::checkDomain($test),
61
                "Test 'CheckDomain' for '$test' should return FALSE. TRUE was returned.");
62
        }
63
    }
64
65
    public function testNormalizeUrlValids()
66
    {
67
        $testCasesValid = [
68
            'https://example.com',
69
            'https://example.com/',
70
            'http://example.com',
71
            'ftp://example.com',
72
            'https://1.2.3.4',
73
            'https://example.com:80',
74
            'https://example.com:8080',
75
            'https://[email protected]',
76
            'https://user:[email protected]',
77
            'https://example.com/path',
78
            'https://example.com/path.html',
79
            'https://example.com/path/path',
80
            'https://example.com/path/path.php',
81
            'https://example.com?param=value',
82
            'https://example.com?param=value&param2=value',
83
            'https://example.com#anchor',
84
            'https://example.com?param=value#anchor',
85
            'https://example.com/path?param=value#anchor',
86
            'https://user:[email protected]:8080/path/path.html?param=value&param2=value#anchor'
87
        ];
88
89
        foreach ($testCasesValid as $test) {
90
            $this->assertEquals($test, Url::normalizeUrl($test),
91
                "Test 'NormalizeUrl' for '$test' should return '$test'.");
92
        }
93
    }
94
95
    public function testNormalizeUrlInvalids()
96
    {
97
        $testCasesInvalid = [
98
            'http://',
99
            'http://.',
100
            'http://..',
101
            'http://../',
102
            'http://?',
103
            'http://??',
104
            'http://??/',
105
            'http://#',
106
            'http://##',
107
            'http://##/',
108
            'http://foo.bar?q=Spaces should be encoded',
109
            '//',
110
            '//a',
111
            '///a',
112
            '///',
113
            'http:///a',
114
            'foo.com',
115
            'rdar://1234',
116
            'h://test',
117
            'http:// shouldfail.com',
118
            ':// should fail',
119
            'http://foo.bar/foo(bar)baz quux',
120
            'ftps://foo.bar/',
121
            'http://-error-.invalid/',
122
            'http://-a.b.co',
123
            'http://a.b-.co',
124
            'http://0.0.0.0',
125
            'http://3628126748',
126
            'http://.www.foo.bar/',
127
            'http://www.foo.bar./',
128
            'http://.www.foo.bar./',
129
            'https://user@[email protected]'
130
        ];
131
132
        foreach ($testCasesInvalid as $test) {
133
            $this->assertFalse(false, Url::normalizeUrl($test), "Test 'NormalizeUrl' for '$test' should return FALSE.");
0 ignored issues
show
Bug introduced by
It seems like Wszetko\Sitemap\Helpers\Url::normalizeUrl($test) can also be of type false; however, parameter $message of PHPUnit\Framework\Assert::assertFalse() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

133
            $this->assertFalse(false, /** @scrutinizer ignore-type */ Url::normalizeUrl($test), "Test 'NormalizeUrl' for '$test' should return FALSE.");
Loading history...
Unused Code introduced by
The call to PHPUnit\Framework\Assert::assertFalse() has too many arguments starting with 'Test 'NormalizeUrl' for...' should return FALSE.'. ( Ignorable by Annotation )

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

133
            $this->/** @scrutinizer ignore-call */ 
134
                   assertFalse(false, Url::normalizeUrl($test), "Test 'NormalizeUrl' for '$test' should return FALSE.");

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
134
        }
135
    }
136
137
    public function testNormalizeUrlNormalized()
138
    {
139
        $testCaseNormalized = [
140
            'https://żółw.pl' => 'https://xn--w-uga1v8h.pl',
141
            'https://例如.中国' => 'https://xn--fsqu6v.xn--fiqs8s',
142
            'http://مثال.إختبار' => 'http://xn--mgbh0fb.xn--kgbechtv',
143
            'https://user:pass@zółw.pl' => 'https://user:[email protected]',
144
            'https://example.com/żółty' => 'https://example.com/żółty',
145
        ];
146
147
        foreach ($testCaseNormalized as $test => $result) {
148
            $this->assertEquals($result, Url::normalizeUrl($test),
149
                "Test 'NormalizeUrl' for '$test' should return '$result'.");
150
        }
151
    }
152
}
153