/
FragmentTest.php
120 lines (107 loc) · 4.04 KB
/
FragmentTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace LeagueTest\Uri\Components;
use League\Uri\Components\Exception;
use League\Uri\Components\Fragment;
use PHPUnit\Framework\TestCase;
/**
* @group fragment
*/
final class FragmentTest extends TestCase
{
/**
* @dataProvider getUriComponentProvider
* @param string $str
* @param string $encoded
*/
public function testGetUriComponent($str, $encoded)
{
self::assertSame($encoded, (new Fragment($str))->getUriComponent());
}
public function getUriComponentProvider()
{
$unreserved = 'a-zA-Z0-9.-_~!$&\'()*+,;=:@';
return [
'null' => [null, ''],
'empty' => ['', '#'],
'evaluate empty' => ['0', '#0'],
'hash' => ['#', '#%23'],
'toofan' => ['toofan', '#toofan'],
'notencoded' => ["azAZ0-9/?-._~!$&'()*+,;=:@", '#azAZ0-9/?-._~!$&\'()*+,;=:@'],
'encoded' => ['%^[]{}"<>\\', '#%25%5E%5B%5D%7B%7D%22%3C%3E%5C'],
'Percent encode spaces' => ['frag ment', '#frag%20ment'],
'Percent encode multibyte' => ['€', '#%E2%82%AC'],
"Don't encode something that's already encoded" => ['frag%20ment', '#frag%20ment'],
'Percent encode invalid percent encodings' => ['frag%2-ment', '#frag%252-ment'],
"Don't encode path segments" => ['frag/ment', '#frag/ment'],
"Don't encode unreserved chars or sub-delimiters" => [$unreserved, '#'.$unreserved],
'Encoded unreserved chars are not decoded' => ['fr%61gment', '#fr%61gment'],
];
}
public function testIsNull()
{
self::assertTrue((new Fragment(null))->isNull());
self::assertFalse((new Fragment(''))->isNull());
}
/**
* @dataProvider geValueProvider
* @param string|null $str
* @param string|null $expected
* @param int $enc_type
*/
public function testGetValue($str, $expected, $enc_type)
{
self::assertSame($expected, (new Fragment($str))->getContent($enc_type));
}
public function geValueProvider()
{
return [
[null, null, Fragment::RFC3987_ENCODING],
['', '', Fragment::RFC3987_ENCODING],
['0', '0', Fragment::RFC3987_ENCODING],
['azAZ0-9/?-._~!$&\'()*+,;=:@%^/[]{}\"<>\\', 'azAZ0-9/?-._~!$&\'()*+,;=:@%^/[]{}\"<>\\', Fragment::RFC3987_ENCODING],
['€', '€', Fragment::RFC3987_ENCODING],
['%E2%82%AC', '€', Fragment::RFC3987_ENCODING],
['frag ment', 'frag ment', Fragment::RFC3987_ENCODING],
['frag%20ment', 'frag ment', Fragment::RFC3987_ENCODING],
['frag%2-ment', 'frag%2-ment', Fragment::RFC3987_ENCODING],
['fr%61gment', 'fr%61gment', Fragment::RFC3987_ENCODING],
['frag+ment', 'frag%2Bment', Fragment::RFC1738_ENCODING],
];
}
/**
* @dataProvider getContentProvider
* @param string $input
* @param int $enc_type
* @param string $expected
*/
public function testGetContent($input, $enc_type, $expected)
{
self::assertSame($expected, (new Fragment($input))->getContent($enc_type));
}
public function getContentProvider()
{
return [
['€', Fragment::RFC3987_ENCODING, '€'],
['€', Fragment::RFC3986_ENCODING, '%E2%82%AC'],
['%E2%82%AC', Fragment::RFC3987_ENCODING, '€'],
['%E2%82%AC', Fragment::RFC3986_ENCODING, '%E2%82%AC'],
];
}
public function testInvalidEncodingTypeThrowException()
{
$this->expectException(Exception::class);
(new Fragment('host'))->getContent(-1);
}
public function testDebugInfo()
{
self::assertInternalType('array', (new Fragment('yolo'))->__debugInfo());
}
public function testPreserverDelimiter()
{
$fragment = new Fragment();
$altFragment = $fragment->withContent(null);
self::assertSame($fragment, $altFragment);
self::assertNull($altFragment->getContent());
self::assertSame('', $altFragment->__toString());
}
}