Completed
Pull Request — master (#178)
by ignace nyamagana
03:19
created

DataTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 145
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testDefaultConstructor() 0 7 1
A testCreateFromString() 0 4 1
A validUrlProvider() 0 25 1
A testCreateFromStringFailed() 0 5 1
A invalidUrlProvider() 0 6 1
A testCreateFromStringFailedWithWrongComponent() 0 5 1
A invalidComponentProvider() 0 8 1
A testCreateFromComponentsFailedWithInvalidArgumentException() 0 5 1
A testCreateFromComponentsFailedInvalidMediatype() 0 5 1
A testCreateFromComponentsFailedWithException() 0 5 1
A testWithPath() 0 6 1
A testSyntaxError() 0 5 1
1
<?php
2
3
/**
4
 * League.Uri (https://uri.thephpleague.com)
5
 *
6
 * (c) Ignace Nyamagana Butera <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LeagueTest\Uri;
13
14
use League\Uri\Exceptions\SyntaxError;
15
use League\Uri\Uri;
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * @group data
20
 * @group uri
21
 * @coversDefaultClass League\Uri\Uri
22
 */
23
class DataTest extends TestCase
24
{
25
    /**
26
     * @covers ::formatDataPath
27
     */
28
    public function testDefaultConstructor(): void
29
    {
30
        self::assertSame(
31
            'data:text/plain;charset=us-ascii,',
32
            (string) Uri::createFromString('data:')
33
        );
34
    }
35
36
    /**
37
     * @covers ::formatPath
38
     * @covers ::formatDataPath
39
     * @covers ::assertValidPath
40
     * @covers ::assertValidState
41
     *
42
     * @dataProvider validUrlProvider
43
     */
44
    public function testCreateFromString(string $uri, string $path): void
45
    {
46
        self::assertSame($path, Uri::createFromString($uri)->getPath());
47
    }
48
49
    public function validUrlProvider(): array
50
    {
51
        return [
52
            'simple string' => [
53
                'uri' => 'data:text/plain;charset=us-ascii,Bonjour%20le%20monde%21',
54
                'path' => 'text/plain;charset=us-ascii,Bonjour%20le%20monde%21',
55
            ],
56
            'string without mimetype' => [
57
                'uri' => 'data:,Bonjour%20le%20monde%21',
58
                'path' => 'text/plain;charset=us-ascii,Bonjour%20le%20monde%21',
59
            ],
60
            'string without parameters' => [
61
                'uri' => 'data:text/plain,Bonjour%20le%20monde%21',
62
                'path' => 'text/plain;charset=us-ascii,Bonjour%20le%20monde%21',
63
            ],
64
            'empty string' => [
65
                'uri' => 'data:,',
66
                'path' => 'text/plain;charset=us-ascii,',
67
            ],
68
            'binary data' => [
69
                'uri' => 'data:image/gif;charset=binary;base64,R0lGODlhIAAgAIABAP8AAP///yH+EUNyZWF0ZWQgd2l0aCBHSU1QACH5BAEKAAEALAAAAAAgACAAAAI5jI+py+0Po5y02ouzfqD7DwJUSHpjSZ4oqK7m5LJw/Ep0Hd1dG/OuvwKihCVianbbKJfMpvMJjWYKADs=',
70
                'path' => 'image/gif;charset=binary;base64,R0lGODlhIAAgAIABAP8AAP///yH+EUNyZWF0ZWQgd2l0aCBHSU1QACH5BAEKAAEALAAAAAAgACAAAAI5jI+py+0Po5y02ouzfqD7DwJUSHpjSZ4oqK7m5LJw/Ep0Hd1dG/OuvwKihCVianbbKJfMpvMJjWYKADs=',
71
            ],
72
        ];
73
    }
74
75
    /**
76
     * @covers ::formatDataPath
77
     * @covers ::assertValidPath
78
     * @covers ::assertValidState
79
     *
80
     * @dataProvider invalidUrlProvider
81
     */
82
    public function testCreateFromStringFailed(string $uri): void
83
    {
84
        self::expectException(SyntaxError::class);
85
        Uri::createFromString($uri);
86
    }
87
88
    public function invalidUrlProvider(): array
89
    {
90
        return [
91
            'invalid data' => ['data:image/png;base64,°28'],
92
        ];
93
    }
94
95
96
    /**
97
     * @covers ::formatDataPath
98
     * @covers ::assertValidPath
99
     * @covers ::assertValidState
100
     *
101
     * @dataProvider invalidComponentProvider
102
     */
103
    public function testCreateFromStringFailedWithWrongComponent(string $uri): void
104
    {
105
        self::expectException(SyntaxError::class);
106
        Uri::createFromString($uri);
107
    }
108
109
    public function invalidComponentProvider(): array
110
    {
111
        return [
112
            'invalid data' => ['data:image/png;base64,zzz28'],
113
            'invalid mime type' => ['data:image_png;base64,zzz'],
114
            'invalid parameter' => ['data:image/png;base64;base64,zzz'],
115
        ];
116
    }
117
118
    /**
119
     * @covers ::assertValidPath
120
     * @covers ::formatDataPath
121
     * @covers ::assertValidState
122
     */
123
    public function testCreateFromComponentsFailedWithInvalidArgumentException(): void
124
    {
125
        self::expectException(SyntaxError::class);
126
        Uri::createFromString('data:image/png;base64,°28');
127
    }
128
129
    /**
130
     * @covers ::assertValidPath
131
     * @covers ::validateParameter
132
     * @covers ::formatDataPath
133
     * @covers ::assertValidState
134
     */
135
    public function testCreateFromComponentsFailedInvalidMediatype(): void
136
    {
137
        self::expectException(SyntaxError::class);
138
        Uri::createFromString('data:image/png;base64=toto;base64,dsqdfqfd');
139
    }
140
141
    public function testCreateFromComponentsFailedWithException(): void
142
    {
143
        self::expectException(SyntaxError::class);
144
        Uri::createFromString('data:text/plain;charset=us-ascii,Bonjour%20le%20monde%21#fragment');
145
    }
146
147
    /**
148
     * @covers ::assertValidPath
149
     * @covers ::formatDataPath
150
     * @covers ::assertValidState
151
     */
152
    public function testWithPath(): void
153
    {
154
        $path = 'text/plain;charset=us-ascii,Bonjour%20le%20monde%21';
155
        $uri = Uri::createFromString('data:'.$path);
156
        self::assertSame($uri, $uri->withPath($path));
157
    }
158
159
    /**
160
     * @covers ::assertValidState
161
     */
162
    public function testSyntaxError(): void
163
    {
164
        self::expectException(SyntaxError::class);
165
        Uri::createFromString('http:text/plain;charset=us-ascii,Bonjour%20le%20monde%21');
166
    }
167
}
168