|
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 ArrayIterator; |
|
17
|
|
|
use League\Uri\Exceptions\TemplateCanNotBeExpanded; |
|
18
|
|
|
use League\Uri\UriTemplate\VariableBag; |
|
19
|
|
|
use PHPUnit\Framework\TestCase; |
|
20
|
|
|
use function var_export; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @coversDefaultClass \League\Uri\UriTemplate\VariableBag |
|
24
|
|
|
*/ |
|
25
|
|
|
final class VariableBagTest extends TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @covers ::assign |
|
29
|
|
|
* @covers ::__construct |
|
30
|
|
|
* @covers ::all |
|
31
|
|
|
* @covers ::normalizeValue |
|
32
|
|
|
* |
|
33
|
|
|
* @param array<string, string|array<string>> $expected |
|
34
|
|
|
* |
|
35
|
|
|
* @dataProvider provideValidIterable |
|
36
|
|
|
*/ |
|
37
|
|
|
public function testItCanBeInstantiatedWithAnIterable(iterable $iterable, array $expected): void |
|
38
|
|
|
{ |
|
39
|
|
|
$bag = new VariableBag($iterable); |
|
40
|
|
|
|
|
41
|
|
|
self::assertEquals($expected, $bag->all()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function provideValidIterable(): iterable |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
|
|
'array' => [ |
|
48
|
|
|
'iterable' => ['name' => 'value'], |
|
49
|
|
|
'expected' => ['name' => 'value'], |
|
50
|
|
|
], |
|
51
|
|
|
'iterable' => [ |
|
52
|
|
|
'iterable' => new ArrayIterator(['name' => 'value']), |
|
53
|
|
|
'expected' => ['name' => 'value'], |
|
54
|
|
|
], |
|
55
|
|
|
'empty array' => [ |
|
56
|
|
|
'iterable' => [], |
|
57
|
|
|
'expected' => [], |
|
58
|
|
|
], |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @covers ::assign |
|
64
|
|
|
* @covers ::normalizeValue |
|
65
|
|
|
* @covers ::fetch |
|
66
|
|
|
* |
|
67
|
|
|
* @param mixed $value the value to be assign to the name |
|
68
|
|
|
* @param string|array<string> $expected |
|
69
|
|
|
* |
|
70
|
|
|
* @dataProvider provideValidAssignParameters |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testItCanAssignNameAndValuesToTheBag(string $name, $value, $expected): void |
|
73
|
|
|
{ |
|
74
|
|
|
$bag = new VariableBag(); |
|
75
|
|
|
$bag->assign($name, $value); |
|
76
|
|
|
|
|
77
|
|
|
self::assertSame($expected, $bag->fetch($name)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function provideValidAssignParameters(): iterable |
|
81
|
|
|
{ |
|
82
|
|
|
return [ |
|
83
|
|
|
'string' => [ |
|
84
|
|
|
'name' => 'foo', |
|
85
|
|
|
'value' => 'bar', |
|
86
|
|
|
'expected' => 'bar', |
|
87
|
|
|
], |
|
88
|
|
|
'integer' => [ |
|
89
|
|
|
'name' => 'foo', |
|
90
|
|
|
'value' => 12, |
|
91
|
|
|
'expected' => '12', |
|
92
|
|
|
], |
|
93
|
|
|
'bool' => [ |
|
94
|
|
|
'name' => 'foo', |
|
95
|
|
|
'value' => false, |
|
96
|
|
|
'expected' => '0', |
|
97
|
|
|
], |
|
98
|
|
|
'list' => [ |
|
99
|
|
|
'name' => 'foo', |
|
100
|
|
|
'value' => ['bar', true, 42], |
|
101
|
|
|
'expected' => ['bar', '1', '42'], |
|
102
|
|
|
], |
|
103
|
|
|
'empty string' => [ |
|
104
|
|
|
'name' => 'foo', |
|
105
|
|
|
'value' => '', |
|
106
|
|
|
'expected' => '', |
|
107
|
|
|
], |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @covers ::assign |
|
113
|
|
|
* @covers ::normalizeValue |
|
114
|
|
|
* @covers ::__construct |
|
115
|
|
|
*/ |
|
116
|
|
|
public function testItWillFailToAssignUnsupportedType(): void |
|
117
|
|
|
{ |
|
118
|
|
|
self::expectException(\TypeError::class); |
|
119
|
|
|
|
|
120
|
|
|
new VariableBag(['name' => new \stdClass()]); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @covers ::assign |
|
125
|
|
|
* @covers ::normalizeValue |
|
126
|
|
|
* @covers ::__construct |
|
127
|
|
|
*/ |
|
128
|
|
|
public function testItWillFailToAssignNestedList(): void |
|
129
|
|
|
{ |
|
130
|
|
|
self::expectException(TemplateCanNotBeExpanded::class); |
|
131
|
|
|
|
|
132
|
|
|
new VariableBag(['name' => ['foo' => ['bar' => 'baz']]]); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @covers ::__set_state |
|
137
|
|
|
*/ |
|
138
|
|
|
public function testSetState(): void |
|
139
|
|
|
{ |
|
140
|
|
|
$bag = new VariableBag(['foo' => 'bar', 'yolo' => 42, 'list' => [1, 2, 'three']]); |
|
141
|
|
|
|
|
142
|
|
|
self::assertEquals($bag, eval('return '.var_export($bag, true).';')); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function testItCanReplaceItsValueWithThatOfAnotherInstance(): void |
|
146
|
|
|
{ |
|
147
|
|
|
$bag = new VariableBag([ |
|
148
|
|
|
'foo' => 'bar', |
|
149
|
|
|
'list' => [1, 2, 'three'], |
|
150
|
|
|
]); |
|
151
|
|
|
|
|
152
|
|
|
$defaultBag = new VariableBag([ |
|
153
|
|
|
'foo' => 'bar', |
|
154
|
|
|
'yolo' => 42, |
|
155
|
|
|
'list' => 'this is a list', |
|
156
|
|
|
]); |
|
157
|
|
|
|
|
158
|
|
|
$expected = new VariableBag([ |
|
159
|
|
|
'foo' => 'bar', |
|
160
|
|
|
'yolo' => 42, |
|
161
|
|
|
'list' => [1, 2, 'three'], |
|
162
|
|
|
]); |
|
163
|
|
|
|
|
164
|
|
|
self::assertEquals($expected, $bag->replace($defaultBag)); |
|
165
|
|
|
self::assertEquals($defaultBag, $defaultBag->replace($bag)); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|