forked from Respect/Config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstantiatorTest.php
More file actions
218 lines (192 loc) · 6.33 KB
/
InstantiatorTest.php
File metadata and controls
218 lines (192 loc) · 6.33 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
declare(strict_types=1);
namespace Respect\Config;
use DateTimeZone;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use stdClass;
use function date_default_timezone_set;
use function get_class;
#[CoversClass(Instantiator::class)]
final class InstantiatorTest extends TestCase
{
public function testStaticMethodConstructor(): void
{
$i = new Instantiator('DateTime');
$i->setParam('createFromFormat', [['Y-m-d', '2005-10-12']]);
$s = $i->getInstance();
$this->assertEquals('2005-10-12', $s->format('Y-m-d'));
}
public function testConstructorParamNames(): void
{
date_default_timezone_set('UTC');
$i = new Instantiator('DateTime');
$i->setParam('datetime', 'now');
$i->setParam('timezone', $tz = new DateTimeZone('UTC'));
$s = $i->getInstance();
$this->assertEquals('DateTime', $s::class);
$this->assertEquals($tz, $s->getTimezone());
}
public function testConstructorFull(): void
{
$i = new Instantiator('DateTime');
$i->setParam(
'__construct',
['now', $tz = new DateTimeZone('America/Sao_Paulo')],
);
$s = $i->getInstance();
$this->assertEquals('DateTime', $s::class);
$this->assertEquals($tz, $s->getTimezone());
}
public function testMethodNoParams(): void
{
$i = new Instantiator(__NAMESPACE__ . '\\TestClass');
$i->setParam('noParams', [
[],
]);
$s = $i->getInstance();
$this->assertTrue($s->ok);
}
public function testMethodWithObjectProperty(): void
{
$i = new Instantiator(__NAMESPACE__ . '\\TestClass');
$i->setParam('myProperty', 'bar');
$i->setParam('usingProperty', [
[],
]);
$testObject = $i->getInstance();
$this->assertTrue($testObject->myPropertyUsed);
}
public function testMethodSingleParam(): void
{
$i = new Instantiator(__NAMESPACE__ . '\\TestClass');
$i->setParam('oneParam', [
[true],
]);
$s = $i->getInstance();
$this->assertTrue($s->ok);
}
public function testMethodMultiParams(): void
{
$i = new Instantiator(__NAMESPACE__ . '\\TestClass');
$i->setParam('twoParams', [
[true, true],
]);
$s = $i->getInstance();
$this->assertTrue($s->ok);
}
public function testConstructorNullParams(): void
{
$i = new Instantiator(__NAMESPACE__ . '\\TestClass');
$i->setParam('__construct', [true]);
$s = $i->getInstance();
$this->assertTrue($s->ok);
}
public function testConstructorNullParamsFalse(): void
{
$i = new Instantiator(__NAMESPACE__ . '\\TestClass');
$i->setParam('__construct', [false]);
$s = $i->getInstance();
$this->assertFalse($s->ok);
}
public function testProperties(): void
{
$i = new Instantiator('stdClass');
$i->setParam('foo', 'bar');
$i->setParam('baz', 'bat');
$s = $i->getInstance();
$this->assertEquals('bar', $s->foo);
$this->assertEquals('bat', $s->baz);
}
public function testNestedInstantiators(): void
{
$i1 = new Instantiator('stdClass');
$i2 = new Instantiator('stdClass');
$i1->setParam('foo', $i2);
$s = $i1->getInstance();
$this->assertEquals('stdClass', get_class($s->foo));
}
public function testMagickInvoke(): void
{
$i1 = new Instantiator('stdClass');
$i2 = new Instantiator('stdClass');
$i1->setParam('foo', $i2);
$s = $i1();
$this->assertEquals('stdClass', get_class($s->foo));
}
public function testGetClassName(): void
{
$i = new Instantiator('DateTime');
$this->assertEquals('DateTime', $i->getClassName());
}
public function testGetParam(): void
{
$i = new Instantiator('stdClass');
$i->setParam('foo', 'bar');
$this->assertEquals('bar', $i->getParam('foo'));
}
public function testGetParams(): void
{
$i = new Instantiator('stdClass');
$i->setParam('foo', 'bar');
$i->setParam('baz', 'bat');
$this->assertEquals(['foo' => 'bar', 'baz' => 'bat'], $i->getParams());
}
public function testSetInstance(): void
{
$i = new Instantiator('stdClass');
$obj = new stdClass();
$obj->custom = true;
$i->setInstance($obj);
$this->assertSame($obj, $i->getInstance());
}
public function testConstructorWithInitialParams(): void
{
$i = new Instantiator('stdClass', ['foo' => 'bar', 'baz' => 'bat']);
$s = $i->getInstance();
$this->assertEquals('bar', $s->foo);
$this->assertEquals('bat', $s->baz);
}
public function testTrailingNullParamsAreStripped(): void
{
$i = new Instantiator(__NAMESPACE__ . '\\TestClass');
$i->setParam('foo', true);
$i->setParam('bar', null);
$i->setParam('baz', null);
$s = $i->getInstance();
$this->assertTrue($s->ok);
$this->assertNull($s->bar);
$this->assertNull($s->baz);
}
public function testMethodCallWithSingleNonArrayArg(): void
{
$i = new Instantiator(__NAMESPACE__ . '\\TestClass');
$i->setParam('oneParam', [true]);
$s = $i->getInstance();
$this->assertTrue($s->ok);
}
public function testMethodCallWithNullArg(): void
{
$i = new Instantiator(__NAMESPACE__ . '\\TestClass');
$i->setParam('noParams', [null]);
$s = $i->getInstance();
$this->assertTrue($s->ok);
}
public function testRefThrowsInInstantiator(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Ref can only be used with Autowire');
$i = new Instantiator(stdClass::class);
$i->setParam('foo', new Ref('some.key'));
$i->getInstance();
}
public function testStaticMethodReturningNonObject(): void
{
$i = new Instantiator(__NAMESPACE__ . '\\StaticNonObjectReturn');
$i->setParam('init', [[]]);
$s = $i->getInstance();
$this->assertInstanceOf(StaticNonObjectReturn::class, $s);
$this->assertTrue($s->ready);
}
}