forked from nelmio/alice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParameterBagTest.php
169 lines (134 loc) · 4.28 KB
/
ParameterBagTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Nelmio\Alice;
use Nelmio\Alice\Throwable\Exception\ParameterNotFoundException;
use PHPUnit\Framework\TestCase;
use ReflectionObject;
use stdClass;
/**
* @covers \Nelmio\Alice\ParameterBag
*/
class ParameterBagTest extends TestCase
{
public function testReadAccessorsReturnPropertiesValues(): void
{
$parameters = [
'foo' => 'bar',
'ping' => 'pong',
];
$bag = new ParameterBag($parameters);
static::assertTrue($bag->has('foo'));
static::assertTrue($bag->has('ping'));
static::assertFalse($bag->has('bar'));
static::assertFalse($bag->has('pong'));
static::assertEquals('bar', $bag->get('foo'));
static::assertEquals('pong', $bag->get('ping'));
$this->assertBagSize(2, $bag);
}
public function testThrowsAnExceptionWhenATryingToGetAnInexistingParameter(): void
{
$this->expectException(ParameterNotFoundException::class);
$this->expectExceptionMessage('Could not find the parameter "foo".');
$bag = new ParameterBag();
$bag->get('foo');
}
public function testIsImmutable(): void
{
$bag = new ParameterBag([
'foo' => $std = new stdClass(),
]);
// Mutate injected object
$std->foo = 'bar';
// Mutate retrieved object
$bag->get('foo')->foo = 'baz';
static::assertEquals(
new ParameterBag([
'foo' => new stdClass(),
]),
$bag
);
}
/**
* @depends \Nelmio\Alice\ParameterTest::testIsImmutable
*/
public function testWithersReturnNewModifiedObject(): void
{
$bag = new ParameterBag(['foo' => 'bar']);
$newBag = $bag->with(new Parameter('ping', 'pong'));
static::assertEquals(new ParameterBag(['foo' => 'bar']), $bag);
static::assertEquals(new ParameterBag(['foo' => 'bar', 'ping' => 'pong']), $newBag);
}
public function testIfTwoParametersWithTheSameKeyAreAddedThenTheNewerOneWillBeDiscarded(): void
{
$bag = (new ParameterBag([
'foo' => 'bar',
'ping' => 'pong',
]))
->with(new Parameter('ping', 'boo'))
->with(new Parameter('he', 'ho'))
;
static::assertEquals('bar', $bag->get('foo'));
static::assertEquals('pong', $bag->get('ping'));
static::assertEquals('ho', $bag->get('he'));
$this->assertBagSize(3, $bag);
}
public function testIsTraversable(): void
{
$params = [
'foo' => 'bar',
'ping' => 'pong',
];
$bag = new ParameterBag($params);
$traversed = [];
foreach ($bag as $key => $param) {
$traversed[$key] = $param;
}
static::assertSame($params, $traversed);
}
public function testIsCountable(): void
{
$bag = new ParameterBag();
static::assertCount(0, $bag);
$bag = $bag
->with(new Parameter('foo', 'bar'))
->with(new Parameter('ping', 'pong'))
;
static::assertCount(2, $bag);
}
public function testCanRemoveElements(): void
{
$bag = (new ParameterBag(['foo' => 'bar']))->without('foo')->without('foo');
static::assertEquals(new ParameterBag(), $bag);
}
private function assertBagSize(int $size, ParameterBag $bag): void
{
$reflectionObject = new ReflectionObject($bag);
$paramReflection = $reflectionObject->getProperty('parameters');
$paramReflection->setAccessible(true);
static::assertCount($size, $paramReflection->getValue($bag));
}
public function testToArray(): void
{
$bag = new ParameterBag();
static::assertEquals([], $bag->toArray());
$bag = new ParameterBag([
'foo' => 'bar',
'baz' => $std = new stdClass(),
]);
static::assertEquals(
[
'foo' => 'bar',
'baz' => $std,
],
$bag->toArray()
);
}
}