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

VarSpecifierTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 80
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testItCanBeInstantiatedWithAValidNotation() 0 4 1
A providesValidNotation() 0 13 1
A testItFailsToInstantiatedWithAnInvalidNotationString() 0 6 1
A providesInvalidNotation() 0 9 1
A testItCanReturnsTheVarSpecifierProperties() 0 14 1
A testSetState() 0 6 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
declare(strict_types=1);
13
14
namespace LeagueTest\Uri\UriTemplate;
15
16
use League\Uri\Exceptions\SyntaxError;
17
use League\Uri\UriTemplate\VarSpecifier;
18
use PHPUnit\Framework\TestCase;
19
use function var_export;
20
21
/**
22
 * @coversDefaultClass \League\Uri\UriTemplate\VarSpecifier
23
 */
24
final class VarSpecifierTest extends TestCase
25
{
26
    /**
27
     * @covers ::createFromString
28
     * @covers ::toString
29
     * @covers ::__construct
30
     *
31
     * @dataProvider providesValidNotation
32
     */
33
    public function testItCanBeInstantiatedWithAValidNotation(string $notation): void
34
    {
35
        self::assertSame($notation, VarSpecifier::createFromString($notation)->toString());
36
    }
37
38
    public function providesValidNotation(): iterable
39
    {
40
        return [
41
            'simple' => ['notation' => 'var'],
42
            'variable with underscore' => ['notation' => 'under_score'],
43
            'variable with digit' => ['notation' => 'd1g1t'],
44
            'variable with encoded' => ['notation' => 'semi%3Bcolon'],
45
            'variable with point' => ['notation' => 'p.int'],
46
            'with explode modifier' => ['notation' => 'var*'],
47
            'with position modifier' => ['notation' => 'var:5'],
48
            'complex variable' => ['notation' => 'und.er_sc0re%3B:5'],
49
        ];
50
    }
51
52
    /**
53
     * @covers ::createFromString
54
     *
55
     * @dataProvider providesInvalidNotation
56
     */
57
    public function testItFailsToInstantiatedWithAnInvalidNotationString(string $notation): void
58
    {
59
        self::expectException(SyntaxError::class);
60
61
        VarSpecifier::createFromString($notation);
62
    }
63
64
    public function providesInvalidNotation(): iterable
65
    {
66
        return [
67
            'using the explode modifier with the position notation' => ['notation' => 'var:*'],
68
            'with a position modifier ouf of bound' => ['notation' => 'var:10000'],
69
            'empty string' => ['notation' => ''],
70
            'unsupported character' => ['notation' => 'bébé'],
71
        ];
72
    }
73
74
    /**
75
     * @covers ::position
76
     * @covers ::modifier
77
     * @covers ::name
78
     */
79
    public function testItCanReturnsTheVarSpecifierProperties(): void
80
    {
81
        $varSpecifier = VarSpecifier::createFromString('und.er_sc0re%3B:5');
82
83
        self::assertSame('und.er_sc0re%3B', $varSpecifier->name());
84
        self::assertSame(':', $varSpecifier->modifier());
85
        self::assertSame(5, $varSpecifier->position());
86
87
        $varSpecifier = VarSpecifier::createFromString('und.er_sc0re%3B*');
88
89
        self::assertSame('und.er_sc0re%3B', $varSpecifier->name());
90
        self::assertSame('*', $varSpecifier->modifier());
91
        self::assertSame(0, $varSpecifier->position());
92
    }
93
94
    /**
95
     * @covers ::__set_state
96
     */
97
    public function testSetState(): void
98
    {
99
        $varSpecifier = VarSpecifier::createFromString('und.er_sc0re%3B*');
100
101
        self::assertEquals($varSpecifier, eval('return '.var_export($varSpecifier, true).';'));
102
    }
103
}
104