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

WsTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateFromString() 0 4 1
A validUrlProvider() 0 29 1
A testConstructorThrowInvalidArgumentException() 0 5 1
A invalidUrlProvider() 0 8 1
A testModificationFailedWithEmptyAuthority() 0 8 1
A testPort() 0 4 1
A portProvider() 0 9 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 ws
20
 * @group uri
21
 * @coversDefaultClass League\Uri\Uri
22
 */
23
class WsTest extends TestCase
24
{
25
    /**
26
     *
27
     * @dataProvider validUrlProvider
28
     */
29
    public function testCreateFromString(string $input, string $expected): void
30
    {
31
        self::assertSame($expected, (string) Uri::createFromString($input));
32
    }
33
34
    public function validUrlProvider(): array
35
    {
36
        return [
37
            'with default port' => [
38
                'Ws://ExAmpLe.CoM:80/foo/bar?foo=bar',
39
                'ws://example.com/foo/bar?foo=bar',
40
            ],
41
            'with user info' => [
42
                'wss://login:[email protected]/',
43
                'wss://login:[email protected]/',
44
            ],
45
            'network path' => [
46
                '//ExAmpLe.CoM:21',
47
                '//example.com:21',
48
            ],
49
            'absolute path' => [
50
                '/path/to/my/file',
51
                '/path/to/my/file',
52
            ],
53
            'relative path' => [
54
                '.././path/../is/./relative',
55
                '.././path/../is/./relative',
56
            ],
57
            'empty string' => [
58
                '',
59
                '',
60
            ],
61
        ];
62
    }
63
64
    /**
65
     * @dataProvider invalidUrlProvider
66
     */
67
    public function testConstructorThrowInvalidArgumentException(string $uri): void
68
    {
69
        self::expectException(SyntaxError::class);
70
        Uri::createFromString($uri);
71
    }
72
73
    public function invalidUrlProvider(): array
74
    {
75
        return [
76
            ['wss:example.com'],
77
            ['wss:/example.com'],
78
            ['wss://example.com:80/foo/bar?foo=bar#content'],
79
        ];
80
    }
81
82
    public function testModificationFailedWithEmptyAuthority(): void
83
    {
84
        self::expectException(SyntaxError::class);
85
        Uri::createFromString('wss://example.com/path')
86
            ->withScheme(null)
87
            ->withHost(null)
88
            ->withPath('//toto');
89
    }
90
91
    /**
92
     * @dataProvider portProvider
93
     * @param ?int $port
0 ignored issues
show
Documentation introduced by
The doc-type ?int could not be parsed: Unknown type name "?int" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
94
     */
95
    public function testPort(string $uri, ?int $port): void
96
    {
97
        self::assertSame($port, Uri::createFromString($uri)->getPort());
98
    }
99
100
    public function portProvider(): array
101
    {
102
        return [
103
            ['ws://www.example.com:443/', 443],
104
            ['ws://www.example.com:80/', null],
105
            ['ws://www.example.com', null],
106
            ['//www.example.com:80/', 80],
107
        ];
108
    }
109
}
110