forked from thephpleague/uri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataTest.php
166 lines (150 loc) · 4.94 KB
/
DataTest.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Uri;
use League\Uri\Exceptions\SyntaxError;
use PHPUnit\Framework\TestCase;
/**
* @group data
* @group uri
* @coversDefaultClass \League\Uri\Uri
*/
final class DataTest extends TestCase
{
/**
* @covers ::formatDataPath
*/
public function testDefaultConstructor(): void
{
self::assertSame(
'data:text/plain;charset=us-ascii,',
(string) Uri::createFromString('data:')
);
}
/**
* @covers ::formatPath
* @covers ::formatDataPath
* @covers ::assertValidPath
* @covers ::assertValidState
*
* @dataProvider validUrlProvider
*/
public function testCreateFromString(string $uri, string $path): void
{
self::assertSame($path, Uri::createFromString($uri)->getPath());
}
public function validUrlProvider(): array
{
return [
'simple string' => [
'uri' => 'data:text/plain;charset=us-ascii,Bonjour%20le%20monde%21',
'path' => 'text/plain;charset=us-ascii,Bonjour%20le%20monde%21',
],
'string without mimetype' => [
'uri' => 'data:,Bonjour%20le%20monde%21',
'path' => 'text/plain;charset=us-ascii,Bonjour%20le%20monde%21',
],
'string without parameters' => [
'uri' => 'data:text/plain,Bonjour%20le%20monde%21',
'path' => 'text/plain;charset=us-ascii,Bonjour%20le%20monde%21',
],
'empty string' => [
'uri' => 'data:,',
'path' => 'text/plain;charset=us-ascii,',
],
'binary data' => [
'uri' => 'data:image/gif;charset=binary;base64,R0lGODlhIAAgAIABAP8AAP///yH+EUNyZWF0ZWQgd2l0aCBHSU1QACH5BAEKAAEALAAAAAAgACAAAAI5jI+py+0Po5y02ouzfqD7DwJUSHpjSZ4oqK7m5LJw/Ep0Hd1dG/OuvwKihCVianbbKJfMpvMJjWYKADs=',
'path' => 'image/gif;charset=binary;base64,R0lGODlhIAAgAIABAP8AAP///yH+EUNyZWF0ZWQgd2l0aCBHSU1QACH5BAEKAAEALAAAAAAgACAAAAI5jI+py+0Po5y02ouzfqD7DwJUSHpjSZ4oqK7m5LJw/Ep0Hd1dG/OuvwKihCVianbbKJfMpvMJjWYKADs=',
],
];
}
/**
* @covers ::formatDataPath
* @covers ::assertValidPath
* @covers ::assertValidState
*
* @dataProvider invalidUrlProvider
*/
public function testCreateFromStringFailed(string $uri): void
{
self::expectException(SyntaxError::class);
Uri::createFromString($uri);
}
public function invalidUrlProvider(): array
{
return [
'invalid data' => ['data:image/png;base64,°28'],
];
}
/**
* @covers ::formatDataPath
* @covers ::assertValidPath
* @covers ::assertValidState
*
* @dataProvider invalidComponentProvider
*/
public function testCreateFromStringFailedWithWrongComponent(string $uri): void
{
self::expectException(SyntaxError::class);
Uri::createFromString($uri);
}
public function invalidComponentProvider(): array
{
return [
'invalid data' => ['data:image/png;base64,zzz28'],
'invalid mime type' => ['data:image_png;base64,zzz'],
'invalid parameter' => ['data:image/png;base64;base64,zzz'],
];
}
/**
* @covers ::assertValidPath
* @covers ::formatDataPath
* @covers ::assertValidState
*/
public function testCreateFromComponentsFailedWithInvalidArgumentException(): void
{
self::expectException(SyntaxError::class);
Uri::createFromString('data:image/png;base64,°28');
}
/**
* @covers ::assertValidPath
* @covers ::validateParameter
* @covers ::formatDataPath
* @covers ::assertValidState
*/
public function testCreateFromComponentsFailedInvalidMediatype(): void
{
self::expectException(SyntaxError::class);
Uri::createFromString('data:image/png;base64=toto;base64,dsqdfqfd');
}
public function testCreateFromComponentsFailedWithException(): void
{
self::expectException(SyntaxError::class);
Uri::createFromString('data:text/plain;charset=us-ascii,Bonjour%20le%20monde%21#fragment');
}
/**
* @covers ::assertValidPath
* @covers ::formatDataPath
* @covers ::assertValidState
*/
public function testWithPath(): void
{
$path = 'text/plain;charset=us-ascii,Bonjour%20le%20monde%21';
$uri = Uri::createFromString('data:'.$path);
self::assertSame($uri, $uri->withPath($path));
}
/**
* @covers ::assertValidState
*/
public function testSyntaxError(): void
{
self::expectException(SyntaxError::class);
Uri::createFromString('http:text/plain;charset=us-ascii,Bonjour%20le%20monde%21');
}
}