Skip to content

Commit d3e4df7

Browse files
committed
17191: refacor tests using dataprovider
1 parent fc582a2 commit d3e4df7

File tree

1 file changed

+70
-31
lines changed

1 file changed

+70
-31
lines changed

tests/framework/helpers/BaseUrlTest.php

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,64 +10,103 @@
1010
*/
1111
class BaseUrlTest extends TestCase
1212
{
13+
/** @dataProvider relativeTrueUrlProvider */
14+
public function testIsRelativeWillReturnTrue($url)
15+
{
16+
$this->assertTrue(BaseUrl::isRelative($url));
17+
}
1318

14-
public function testIsRelativeWithAbsoluteUrlWillReturnFalse()
19+
/** @dataProvider relativeFalseUrlProvider */
20+
public function testIsRelativeWillReturnFalse($url)
1521
{
16-
$this->assertFalse(BaseUrl::isRelative('https://acme.com/tnt-room=123'));
22+
$this->assertFalse(BaseUrl::isRelative($url));
1723
}
1824

19-
public function testUrlStartingWithDoubleSlashesWillReturnFalse()
25+
public function testEnsureSchemeWithRelativeUrlWillReturnInputUrl()
2026
{
21-
$this->assertFalse(BaseUrl::isRelative('//example.com'));
27+
$url = 'acme.com?name=bugs.bunny';
28+
$this->assertEquals('acme.com?name=bugs.bunny', BaseUrl::ensureScheme($url, 'https'));
2229
}
2330

24-
public function testIsRelativeWithRelativeUrlWillReturnTrue()
31+
public function testEnsureSchemeWithRelativeUrlWithAnotherUrlAsParamWillReturnInputUrl()
2532
{
26-
$this->assertTrue(
27-
BaseUrl::isRelative('acme.com/tnt-room=123')
33+
$this->assertEquals('acme.com/test?tnt-link=https://tnt.com/',
34+
BaseUrl::ensureScheme('acme.com/test?tnt-link=https://tnt.com/', 'https')
2835
);
2936
}
3037

31-
public function testIsRelativeWithRelativeUrlHavingHttpsUrlAsParamValueWillReturnTrue()
38+
public function testEnsureSchemeWithSchemeNotAStringWillReturnInputUrl()
3239
{
33-
$this->assertTrue(BaseUrl::isRelative(
34-
'acme.com/tnt-room-link=https://asd.com'
35-
));
40+
$url = 'acme.com?name=bugs.bunny';
41+
$this->assertEquals('acme.com?name=bugs.bunny', BaseUrl::ensureScheme($url, 123));
3642
}
3743

38-
public function testIsRelativeWithAbsoluteUrlHavingHttpsUrlAsParamValueWillReturnFalse()
44+
public function testEnsureSchemeWithProtocolRelativeUrlAndHttpsSchemeWillBeNormalized()
3945
{
40-
$this->assertFalse(
41-
BaseUrl::isRelative('https://acme.com/tnt-link=https://tnt.com')
42-
);
46+
$url = '//acme.com?characters/list';
47+
$this->assertEquals('https://acme.com?characters/list', BaseUrl::ensureScheme($url, 'https'));
4348
}
4449

45-
public function testIsRelativeWithA()
50+
public function testEnsureSchemeWithProtocolRelativeUrlAndEmptySchemeWillBeReturned()
4651
{
47-
$this->assertTrue(
48-
BaseUrl::isRelative('/name=bugs.bunny')
49-
);
52+
$url = '//acme.com?characters/list';
53+
$this->assertEquals('//acme.com?characters/list', BaseUrl::ensureScheme($url, ''));
5054
}
5155

52-
public function testIsRelativeWithFtpProtocolUrlWillReturnFalse()
56+
public function testAbsoluteUrlProtocolAndEmptySchemeWillCreateProtocolRelativeUrl()
5357
{
54-
$this->assertFalse(
55-
BaseUrl::isRelative('ftp://ftp.acme.com/tnt-suppliers.txt')
56-
);
58+
$url = 'https://acme.com?characters/list';
59+
$this->assertEquals('//acme.com?characters/list', BaseUrl::ensureScheme($url, ''));
5760
}
5861

59-
public function testIsRelativeWithHttpUrl()
62+
public function testEnsureSchemeWithAbsoluteUrlWithAnotherUrlAsParamWillReturnInputUrl()
6063
{
61-
$this->assertFalse(
62-
BaseUrl::isRelative('http://no-protection.acme.com')
63-
);
64+
$url = 'ss://acme.com/test?tnt-link=https://tnt.com/';
65+
$this->assertEquals('https://acme.com/test?tnt-link=https://tnt.com/', BaseUrl::ensureScheme($url, 'https'));
6466
}
6567

66-
public function testIsRelativeWithFileUrl()
68+
public function relativeTrueUrlProvider()
6769
{
68-
$this->assertFalse(
69-
BaseUrl::isRelative('file:///home/User/2ndFile.html')
70-
);
70+
return [
71+
'url url without protocol' => [
72+
'url' => 'acme.com/tnt-room=123',
73+
],
74+
'url without protocol and another url in a parameter value' => [
75+
'url' => 'acme.com?tnt-room-link=https://tnt.com',
76+
],
77+
'path only' => [
78+
'url' => '/path',
79+
],
80+
'path with param' => [
81+
'url' => '/path=/home/user',
82+
],
83+
];
7184
}
7285

86+
public function relativeFalseUrlProvider()
87+
{
88+
return [
89+
'url with https protocol' => [
90+
'url' => 'https://acme.com',
91+
],
92+
'url with https protocol and ending slash' => [
93+
'url' => 'https://acme.com/',
94+
],
95+
'url with https protocol and another url as param value' => [
96+
'url' => 'https://acme.com?tnt-link=https://tnt.com',
97+
],
98+
'url starting with two slashes' => [
99+
'url' => '//example.com',
100+
],
101+
'url with ftp protocol' => [
102+
'url' => 'ftp://ftp.acme.com/tnt-suppliers.txt',
103+
],
104+
'url with http protocol' => [
105+
'url' => 'http://no-protection.acme.com',
106+
],
107+
'file url' => [
108+
'url' => 'file:///home/User/2ndFile.html',
109+
]
110+
];
111+
}
73112
}

0 commit comments

Comments
 (0)