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\Template; |
18
|
|
|
use League\Uri\UriTemplate\VariableBag; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
use function var_export; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @coversDefaultClass \League\Uri\UriTemplate\Template |
24
|
|
|
*/ |
25
|
|
|
final class TemplateTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @covers ::createFromString |
29
|
|
|
* @covers ::__construct |
30
|
|
|
* @covers ::toString |
31
|
|
|
* |
32
|
|
|
* @dataProvider providesValidNotation |
33
|
|
|
*/ |
34
|
|
|
public function testItCanBeInstantiatedWithAValidNotation(string $notation): void |
35
|
|
|
{ |
36
|
|
|
$template = Template::createFromString($notation); |
37
|
|
|
self::assertSame($notation, $template->toString()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function providesValidNotation(): iterable |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
'complex template' => ['http://example.com{+path}{/segments}{?query,more*,foo[]*}'], |
44
|
|
|
'template without expression' => ['foobar'], |
45
|
|
|
|
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @covers ::createFromString |
51
|
|
|
* |
52
|
|
|
* @dataProvider providesInvalidNotation |
53
|
|
|
*/ |
54
|
|
|
public function testItFailsToInstantiatedWithAnInvalidNotation(string $notation): void |
55
|
|
|
{ |
56
|
|
|
self::expectException(SyntaxError::class); |
57
|
|
|
|
58
|
|
|
Template::createFromString($notation); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function providesInvalidNotation(): iterable |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
['fooba{r'], |
65
|
|
|
['fooba}r'], |
66
|
|
|
['fooba}{r'], |
67
|
|
|
['{foo{bar'], |
68
|
|
|
['{foo}}bar'], |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @covers ::__set_state |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function testSetState(): void |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$notation = '{foo}{bar}'; |
78
|
|
|
|
79
|
|
|
$template = Template::createFromString($notation); |
80
|
|
|
|
81
|
|
|
self::assertEquals($template, eval('return '.var_export($template, true).';')); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @covers ::variableNames |
86
|
|
|
* |
87
|
|
|
* @dataProvider expectedVariableNames |
88
|
|
|
*/ |
89
|
|
|
public function testGetVariableNames(string $template, array $expected): void |
90
|
|
|
{ |
91
|
|
|
self::assertSame($expected, Template::createFromString($template)->variableNames()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public function expectedVariableNames(): iterable |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
[ |
98
|
|
|
'template' => '', |
99
|
|
|
'expected' => [], |
100
|
|
|
], |
101
|
|
|
[ |
102
|
|
|
'template' => '{foo}{bar}', |
103
|
|
|
'expected' => ['foo', 'bar'], |
104
|
|
|
], |
105
|
|
|
[ |
106
|
|
|
'template' => '{foo}{foo:2}{+foo}', |
107
|
|
|
'expected' => ['foo'], |
108
|
|
|
], |
109
|
|
|
[ |
110
|
|
|
'template' => '{bar}{foo}', |
111
|
|
|
'expected' => ['bar', 'foo'], |
112
|
|
|
], |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @covers ::createFromString |
118
|
|
|
*/ |
119
|
|
|
public function testExpandAcceptsOnlyStringAndStringableObject(): void |
120
|
|
|
{ |
121
|
|
|
self::expectException(\TypeError::class); |
122
|
|
|
|
123
|
|
|
Template::createFromString(new \stdClass()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @dataProvider providesExpansion |
128
|
|
|
*/ |
129
|
|
|
public function testItCanExpandVariables(string $notation, array $variables, string $expected): void |
130
|
|
|
{ |
131
|
|
|
self::assertSame($expected, Template::createFromString($notation)->expand(new VariableBag($variables))); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function providesExpansion(): iterable |
135
|
|
|
{ |
136
|
|
|
return [ |
137
|
|
|
'with variables' => [ |
138
|
|
|
'notation' => 'foobar{var}', |
139
|
|
|
'variables' => ['var' => 'yolo'], |
140
|
|
|
'expected' => 'foobaryolo', |
141
|
|
|
], |
142
|
|
|
'with no variables' => [ |
143
|
|
|
'notation' => 'foobar', |
144
|
|
|
'variables' => [], |
145
|
|
|
'expected' => 'foobar', |
146
|
|
|
], |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.