PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.5
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.5
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / mrclay / props-dic / test / Props / ContainerTest.php

ContainerTest.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.5, at vendor/mrclay/props-dic/test/Props/ContainerTest.php

325 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Props;
4
5 class ContainerTest extends \PHPUnit_Framework_TestCase
6 {
7 const TEST_CLASS = 'Props\ContainerTestObject';
8
9 public function testBasicInterop()
10 {
11 $di = new Container();
12 $this->assertInstanceOf('Psr\Container\ContainerInterface', $di);
13
14 $this->assertFalse($di->has('foo'));
15 $di->foo = 'bar';
16 $this->assertTrue($di->has('foo'));
17 }
18
19 /**
20 * @expectedException \Props\NotFoundException
21 */
22 public function testInteropNotFound()
23 {
24 $di = new Container();
25 $di->get('foo');
26 }
27
28 /**
29 * @expectedException \Psr\Container\ContainerExceptionInterface
30 */
31 public function testInteropException1()
32 {
33 $di = new Container();
34 $di->setFactory('foo', null);
35 }
36
37 /**
38 * @expectedException \Psr\Container\ContainerExceptionInterface
39 */
40 public function testInteropException2()
41 {
42 $di = new Container();
43 $di->setFactory('foo', function () {
44 throw new \Exception();
45 });
46 $di->foo;
47 }
48
49 public function testEmpty()
50 {
51 $di = new Container();
52 $this->assertFalse(isset($di->foo));
53 $this->assertFalse($di->has('foo'));
54 }
55
56 public function testValueSetRemovesFactory()
57 {
58 $di = new Container();
59 $di->foo = function () {
60 return 'Bar';
61 };
62 $di->foo = 'Foo';
63 $this->assertTrue(isset($di->foo));
64 $this->assertFalse($di->hasFactory('foo'));
65 }
66
67 public function testSetResolvable()
68 {
69 $di = new Container();
70 $di->foo = function () {
71 return new ContainerTestObject();
72 };
73
74 $this->assertTrue(isset($di->foo));
75 $this->assertTrue($di->has('foo'));
76 $this->assertTrue($di->hasFactory('foo'));
77 }
78
79 /**
80 * @expectedException \Props\NotFoundException
81 */
82 public function testReadMissingValue()
83 {
84 $di = new Container();
85 $di->foo;
86 }
87
88 /**
89 * @expectedException \Props\NotFoundException
90 */
91 public function testGetMissingValue()
92 {
93 $di = new Container();
94 $di->get('foo');
95 }
96
97 public function testGetNewUnresolvableValue()
98 {
99 $di = new Container();
100 $di->foo = 'Foo';
101
102 $this->setExpectedException('Props\NotFoundException');
103 $di->new_foo();
104 }
105
106 public function testSetAfterRead()
107 {
108 $di = new Container();
109
110 $di->foo = 'Foo';
111 $di->foo = 'Foo2';
112 $this->assertEquals('Foo2', $di->foo);
113 }
114
115 public function testHandlesNullValue()
116 {
117 $di = new Container();
118 $di->null = null;
119 $this->assertTrue(isset($di->null));
120 $this->assertTrue($di->has('null'));
121 $this->assertNull($di->null);
122 $this->assertNull($di->get('null'));
123 }
124
125 public function testFactoryReceivesContainer()
126 {
127 $di = new Container();
128 $di->foo = function () {
129 return func_get_args();
130 };
131 $foo = $di->foo;
132 $this->assertSame($foo[0], $di);
133 $this->assertEquals(count($foo), 1);
134 }
135
136 public function testGetResolvables()
137 {
138 $di = new Container();
139
140 $di->foo = function () {
141 return new ContainerTestObject();
142 };
143 $foo1 = $di->foo;
144 $foo2 = $di->foo;
145 $this->assertInstanceOf(self::TEST_CLASS, $foo1);
146 $this->assertSame($foo1, $foo2);
147
148 $foo3 = $di->new_foo();
149 $foo4 = $di->new_foo();
150 $this->assertInstanceOf(self::TEST_CLASS, $foo3);
151 $this->assertInstanceOf(self::TEST_CLASS, $foo4);
152 $this->assertNotSame($foo3, $foo4);
153 $this->assertNotSame($foo1, $foo3);
154 }
155
156 public function testKeyNamespace()
157 {
158 $di = new Container();
159 $di->foo = function () {
160 return new ContainerTestObject();
161 };
162 $di->new_foo = 'Foo';
163
164 $this->assertInstanceOf(self::TEST_CLASS, $di->new_foo());
165 $this->assertEquals('Foo', $di->new_foo);
166 }
167
168 public function testUnset()
169 {
170 $di = new Container();
171 $di->foo = 'Foo';
172
173 unset($di->foo);
174 $this->assertFalse(isset($di->foo));
175 }
176
177 public function testAccessUnsetValue()
178 {
179 $di = new Container();
180 $di->foo = 'Foo';
181 unset($di->foo);
182
183 $this->setExpectedException('Props\NotFoundException');
184 $di->foo;
185 }
186
187 public function testSetFactory()
188 {
189 $di = new Container();
190 $di->setFactory('foo', function () {
191 $obj = new ContainerTestObject();
192 $obj->bar = 'bar';
193 return $obj;
194 });
195
196 $foo = $di->foo;
197
198 $this->assertInstanceOf(self::TEST_CLASS, $foo);
199 $this->assertEquals('bar', $foo->bar);
200 }
201
202 public function testSetValue()
203 {
204 $di = new Container();
205 $di->setValue('foo', function () {});
206
207 $this->assertInstanceOf('Closure', $di->foo);
208 }
209
210 /**
211 * @expectedException \Props\NotFoundException
212 */
213 public function testCannotExtendValue()
214 {
215 $di = new Container();
216 $di->foo = 1;
217 $di->extend('foo', function ($value, Container $c) {
218 return $value + 1;
219 });
220 }
221
222 public function testExtend()
223 {
224 $di = new Container();
225 $di->key = 'count';
226
227 $di->counter = function (Container $c) {
228 static $i = 0;
229 $i++;
230 return (object)array(
231 $c->key => $i,
232 );
233 };
234
235 $c1 = $di->counter; // cached with $i = 1
236
237 $di->extend('counter', function ($value, Container $c) {
238 static $i = 0;
239 $i++;
240 $value->one = $i;
241 return $value;
242 });
243
244 $c2 = $di->counter; // because of extension, doesn't use original cached value
245 $this->assertEquals((object)array('count' => 2, 'one' => 1), $c2);
246 $this->assertNotSame($c1, $c2);
247
248 $di->key = 'total';
249
250 $c3 = $di->counter; // but caches repeat reads
251 $this->assertEquals((object)array('count' => 2, 'one' => 1), $c3);
252 $this->assertSame($c2, $c3);
253
254 $c4 = $di->new_counter();
255 $this->assertEquals((object)array('total' => 3, 'one' => 2), $c4);
256 $this->assertNotSame($c3, $c4);
257
258 $di->extend('counter', function ($value, Container $c) {
259 static $i = 0;
260 $i++;
261 $value->two = $i;
262 return $value;
263 });
264
265 $c5 = $di->counter; // going deep!
266 $this->assertEquals((object)array('total' => 4, 'one' => 3, 'two' => 1), $c5);
267
268 $c6 = $di->new_counter();
269 $this->assertEquals((object)array('total' => 5, 'one' => 4, 'two' => 2), $c6);
270 }
271
272 /**
273 * @expectedException \Props\NotFoundException
274 */
275 public function testGetFactoryForValue()
276 {
277 $di = new Container();
278 $di->key = 'count';
279 $di->getFactory('key');
280 }
281
282 /**
283 * @expectedException \Props\NotFoundException
284 */
285 public function testGetMissingFactory()
286 {
287 $di = new Container();
288 $di->getFactory('key');
289 }
290
291 public function testGetFactory()
292 {
293 $di = new Container();
294 $factory = function () {};
295 $di->foo = $factory;
296 $factory2 = $di->getFactory('foo');
297 $this->assertSame($factory, $factory2);
298 }
299
300 public function testGetKeys()
301 {
302 $di = new Container();
303 $di->foo = 'foo';
304 $di->bar = function () {};
305 $di->bar;
306 $this->assertEquals(array('foo', 'bar'), $di->getKeys());
307 }
308 }
309
310 class ContainerTestObject
311 {
312 public $calls;
313 public $args;
314
315 public function __construct()
316 {
317 $this->args = func_get_args();
318 }
319
320 public function __call($name, $args)
321 {
322 $this->calls[$name] = $args[0];
323 }
324 }
325