PluginProbe ʕ •ᴥ•ʔ
Presto Player / 4.3.3
Presto Player v4.3.3
4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.4 4.2.3 4.2.2 4.2.0 4.2.1 trunk 1.10.0 1.10.1 1.10.2 1.11.0 1.12.0 1.13.0 1.14.0 1.14.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.13 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.14 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3-beta1 2.3.0 2.3.1 2.3.2 2.3.3 3.0.0 3.0.0-beta1 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1.0 3.1.1 3.1.2 3.1.3 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4
presto-player / vendor / level-2 / dice / tests / BasicTest.php
presto-player / vendor / level-2 / dice / tests Last commit date
TestData 2 years ago BasicTest.php 5 years ago CallTest.php 5 years ago ChainTest.php 5 years ago ConstructParamsTest.php 5 years ago CreateArgsTest.php 5 years ago DiceTest.php 5 years ago NamedInstancesTest.php 5 years ago NamespaceTest.php 5 years ago ShareInstancesTest.php 2 years ago SubstitutionsTest.php 5 years ago bootstrap.php 5 years ago
BasicTest.php
242 lines
1 <?php
2 /* @description Dice - A minimal Dependency Injection Container for PHP *
3 * @author Tom Butler tom@r.je *
4 * @copyright 2012-2018 Tom Butler <tom@r.je> | https:// r.je/dice.html *
5 * @license http:// www.opensource.org/licenses/bsd-license.php BSD License *
6 * @version 3.0 */
7
8
9
10
11 class BasicTest extends DiceTest {
12
13 public function testCreate() {
14 $this->getMockBuilder('TestCreate')->getMock();
15 $myobj = $this->dice->create('TestCreate');
16 $this->assertInstanceOf('TestCreate', $myobj);
17 }
18
19
20
21 public function testCreateInvalid() {
22 //"can't expect default exception". Not sure why.
23 $this->expectException('ErrorException');
24 try {
25 $this->dice->create('SomeClassThatDoesNotExist');
26 }
27 catch (Exception $e) {
28 throw new ErrorException('Error occurred');
29 }
30 }
31
32 public function testNoConstructor() {
33 $a = $this->dice->create('NoConstructor');
34 $this->assertInstanceOf('NoConstructor', $a);
35 }
36
37
38 public function testSetDefaultRule() {
39 $defaultBehaviour = [];
40 $defaultBehaviour['shared'] = true;
41 $dice = $this->dice->addRule('*', $defaultBehaviour);
42
43 $rule = $dice->getRule('*');
44 foreach ($defaultBehaviour as $name => $value) {
45 $this->assertEquals($rule[$name], $defaultBehaviour[$name]);
46 }
47 }
48
49
50 public function testDefaultRuleWorks() {
51 $defaultBehaviour = [];
52 $defaultBehaviour['shared'] = true;
53
54 $dice = $this->dice->addRule('*', $defaultBehaviour);
55
56 $rule = $dice->getRule('A');
57
58 $this->assertTrue($rule['shared']);
59
60 $a1 = $dice->create('A');
61 $a2 = $dice->create('A');
62
63 $this->assertSame($a1, $a2);
64 }
65
66
67 /*
68 * Object graph creation cannot be tested with mocks because the constructor need to be tested.
69 * You can't set 'expects' on the objects which are created making them redundant for that as well
70 * Need real classes to test with unfortunately.
71 */
72 public function testObjectGraphCreation() {
73 $a = $this->dice->create('A');
74 $this->assertInstanceOf('B', $a->b);
75 $this->assertInstanceOf('c', $a->b->c);
76 $this->assertInstanceOf('D', $a->b->c->d);
77 $this->assertInstanceOf('E', $a->b->c->e);
78 $this->assertInstanceOf('F', $a->b->c->e->f);
79 }
80
81 public function testSharedNamed() {
82 $rule = [];
83 $rule['shared'] = true;
84 $rule['instanceOf'] = 'A';
85
86 $dice = $this->dice->addRule('[A]', $rule);
87
88 $a1 = $dice->create('[A]');
89 $a2 = $dice->create('[A]');
90 $this->assertSame($a1, $a2);
91 }
92
93 public function testSharedRule() {
94 $shared = [];
95 $shared['shared'] = true;
96
97 $dice = $this->dice->addRule('MyObj', $shared);
98
99 $obj = $dice->create('MyObj');
100 $this->assertInstanceOf('MyObj', $obj);
101
102 $obj2 = $dice->create('MyObj');
103 $this->assertInstanceOf('MyObj', $obj2);
104
105 $this->assertSame($obj, $obj2);
106
107
108 //This check isn't strictly needed but it's nice to have that safety measure!
109 $obj->setFoo('bar');
110 $this->assertEquals($obj->getFoo(), $obj2->getFoo());
111 $this->assertEquals($obj->getFoo(), 'bar');
112 $this->assertEquals($obj2->getFoo(), 'bar');
113 }
114
115
116 public function testInterfaceRule() {
117 $rule = [];
118
119 $rule['shared'] = true;
120 $dice = $this->dice->addRule('interfaceTest', $rule);
121
122 $one = $dice->create('InterfaceTestClass');
123 $two = $dice->create('InterfaceTestClass');
124
125 $this->assertSame($one, $two);
126 }
127
128 public function testCyclicReferences() {
129 $rule = [];
130 $rule['shared'] = true;
131
132 $dice = $this->dice->addRule('CyclicB', $rule);
133
134 $a = $dice->create('CyclicA');
135
136 $this->assertInstanceOf('CyclicB', $a->b);
137 $this->assertInstanceOf('CyclicA', $a->b->a);
138
139 $this->assertSame($a->b, $a->b->a->b);
140 }
141
142 public function testInherit() {
143 $rule = ['shared' => true, 'inherit' => false];
144
145 $dice = $this->dice->addRule('ParentClass', $rule);
146 $obj1 = $dice->create('Child');
147 $obj2 = $dice->create('Child');
148 $this->assertNotSame($obj1, $obj2);
149 }
150
151 public function testSharedOverride() {
152
153 //Set everything to shared by default
154 $dice = $this->dice->addRule('*', ['shared' => true]);
155
156 $dice = $dice->addRule('A', ['shared' => false]);
157
158 $a1 = $dice->create('A');
159 $a2 = $dice->create('A');
160
161 $this->assertNotSame($a1, $a2);
162
163 }
164
165 public function testOptionalInterface() {
166
167 $optionalInterface = $this->dice->create('OptionalInterface');
168
169 $this->assertEquals(null, $optionalInterface->obj);
170 }
171
172
173 public function testScalarTypeHintWithShareInstances() {
174
175 $dice = $this->dice->addRule('ScalarTypeHint', ['shareInstances' => ['A']]);
176
177 $obj = $dice->create('ScalarTypeHint');
178
179 $this->assertInstanceOf('ScalarTypeHint', $obj);
180 }
181
182 public function testPassGlobals() {
183 //write to the global $_GET variable
184 $_GET['foo'] = 'bar';
185
186 $dice = $this->dice->addRule('CheckConstructorArgs',
187 [
188 'constructParams' => [
189 [\Dice\Dice::GLOBAL => '_GET']
190 ]
191 ]);
192
193 $obj = $dice->create('CheckConstructorArgs');
194
195 $this->assertEquals($_GET, $obj->arg1);
196 }
197
198 public function testPassConstantString() {
199 $dice = $this->dice->addRule('CheckConstructorArgs',
200 [
201 'constructParams' => [
202 [\Dice\Dice::CONSTANT => '\PDO::FETCH_ASSOC']
203 ]
204 ]);
205
206 $obj = $dice->create('CheckConstructorArgs');
207
208 $this->assertEquals(\PDO::FETCH_ASSOC, $obj->arg1);
209 }
210
211 public function testImmutability() {
212 $this->assertEquals([], $this->dice->getRule('Foo'));
213
214 $dice = $this->dice->addRule('Foo', ['shared' => true]);
215
216 $this->assertEquals([], $this->dice->getRule('Foo'));
217 }
218
219 public function testPassSelf() {
220 $dice = $this->dice->addRule('CheckConstructorArgs',
221 [
222 'constructParams' => [
223 [\Dice\Dice::INSTANCE => \Dice\Dice::SELF]
224 ]
225 ]);
226
227 $obj = $dice->create('CheckConstructorArgs');
228
229 $this->assertEquals($dice, $obj->arg1);
230 }
231
232
233 // Issue 180
234 public function testSlashNoSlash() {
235 $dice = $this->dice->addRule('\someclass', ['shared' => true]);
236
237 $b = $dice->create('\someotherclass');
238 $a = $dice->create('\someclass');
239
240 $this->assertSame($a, $b->obj);
241 }
242 }