ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PimpleTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Pimple.
5  *
6  * Copyright (c) 2009 Fabien Potencier
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is furnished
13  * to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all
16  * copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 namespace Pimple\Tests;
28 
30 
35 {
36  public function testWithString()
37  {
38  $pimple = new Container();
39  $pimple['param'] = 'value';
40 
41  $this->assertEquals('value', $pimple['param']);
42  }
43 
44  public function testWithClosure()
45  {
46  $pimple = new Container();
47  $pimple['service'] = function () {
48  return new Fixtures\Service();
49  };
50 
51  $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $pimple['service']);
52  }
53 
55  {
56  $pimple = new Container();
57  $pimple['service'] = $pimple->factory(function () {
58  return new Fixtures\Service();
59  });
60 
61  $serviceOne = $pimple['service'];
62  $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceOne);
63 
64  $serviceTwo = $pimple['service'];
65  $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceTwo);
66 
67  $this->assertNotSame($serviceOne, $serviceTwo);
68  }
69 
71  {
72  $pimple = new Container();
73  $pimple['service'] = function () {
74  return new Fixtures\Service();
75  };
76  $pimple['container'] = function ($container) {
77  return $container;
78  };
79 
80  $this->assertNotSame($pimple, $pimple['service']);
81  $this->assertSame($pimple, $pimple['container']);
82  }
83 
84  public function testIsset()
85  {
86  $pimple = new Container();
87  $pimple['param'] = 'value';
88  $pimple['service'] = function () {
89  return new Fixtures\Service();
90  };
91 
92  $pimple['null'] = null;
93 
94  $this->assertTrue(isset($pimple['param']));
95  $this->assertTrue(isset($pimple['service']));
96  $this->assertTrue(isset($pimple['null']));
97  $this->assertFalse(isset($pimple['non_existent']));
98  }
99 
100  public function testConstructorInjection()
101  {
102  $params = array('param' => 'value');
103  $pimple = new Container($params);
104 
105  $this->assertSame($params['param'], $pimple['param']);
106  }
107 
113  {
114  $pimple = new Container();
115  echo $pimple['foo'];
116  }
117 
119  {
120  $pimple = new Container();
121  $pimple['foo'] = null;
122  $this->assertNull($pimple['foo']);
123  }
124 
125  public function testUnset()
126  {
127  $pimple = new Container();
128  $pimple['param'] = 'value';
129  $pimple['service'] = function () {
130  return new Fixtures\Service();
131  };
132 
133  unset($pimple['param'], $pimple['service']);
134  $this->assertFalse(isset($pimple['param']));
135  $this->assertFalse(isset($pimple['service']));
136  }
137 
141  public function testShare($service)
142  {
143  $pimple = new Container();
144  $pimple['shared_service'] = $service;
145 
146  $serviceOne = $pimple['shared_service'];
147  $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceOne);
148 
149  $serviceTwo = $pimple['shared_service'];
150  $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceTwo);
151 
152  $this->assertSame($serviceOne, $serviceTwo);
153  }
154 
158  public function testProtect($service)
159  {
160  $pimple = new Container();
161  $pimple['protected'] = $pimple->protect($service);
162 
163  $this->assertSame($service, $pimple['protected']);
164  }
165 
167  {
168  $pimple = new Container();
169  $pimple['global_function'] = 'strlen';
170  $this->assertSame('strlen', $pimple['global_function']);
171  }
172 
173  public function testRaw()
174  {
175  $pimple = new Container();
176  $pimple['service'] = $definition = $pimple->factory(function () { return 'foo'; });
177  $this->assertSame($definition, $pimple->raw('service'));
178  }
179 
180  public function testRawHonorsNullValues()
181  {
182  $pimple = new Container();
183  $pimple['foo'] = null;
184  $this->assertNull($pimple->raw('foo'));
185  }
186 
187  public function testFluentRegister()
188  {
189  $pimple = new Container();
190  $this->assertSame($pimple, $pimple->register($this->getMock('Pimple\ServiceProviderInterface')));
191  }
192 
198  {
199  $pimple = new Container();
200  $pimple->raw('foo');
201  }
202 
206  public function testExtend($service)
207  {
208  $pimple = new Container();
209  $pimple['shared_service'] = function () {
210  return new Fixtures\Service();
211  };
212  $pimple['factory_service'] = $pimple->factory(function () {
213  return new Fixtures\Service();
214  });
215 
216  $pimple->extend('shared_service', $service);
217  $serviceOne = $pimple['shared_service'];
218  $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceOne);
219  $serviceTwo = $pimple['shared_service'];
220  $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceTwo);
221  $this->assertSame($serviceOne, $serviceTwo);
222  $this->assertSame($serviceOne->value, $serviceTwo->value);
223 
224  $pimple->extend('factory_service', $service);
225  $serviceOne = $pimple['factory_service'];
226  $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceOne);
227  $serviceTwo = $pimple['factory_service'];
228  $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $serviceTwo);
229  $this->assertNotSame($serviceOne, $serviceTwo);
230  $this->assertNotSame($serviceOne->value, $serviceTwo->value);
231  }
232 
234  {
235  if (extension_loaded('pimple')) {
236  $this->markTestSkipped('Pimple extension does not support this test');
237  }
238  $pimple = new Container();
239 
240  $pimple['foo'] = $pimple->factory(function () { return; });
241  $pimple['foo'] = $pimple->extend('foo', function ($foo, $pimple) { return; });
242  unset($pimple['foo']);
243 
244  $p = new \ReflectionProperty($pimple, 'values');
245  $p->setAccessible(true);
246  $this->assertEmpty($p->getValue($pimple));
247 
248  $p = new \ReflectionProperty($pimple, 'factories');
249  $p->setAccessible(true);
250  $this->assertCount(0, $p->getValue($pimple));
251  }
252 
258  {
259  $pimple = new Container();
260  $pimple->extend('foo', function () {});
261  }
262 
263  public function testKeys()
264  {
265  $pimple = new Container();
266  $pimple['foo'] = 123;
267  $pimple['bar'] = 123;
268 
269  $this->assertEquals(array('foo', 'bar'), $pimple->keys());
270  }
271 
274  {
275  $pimple = new Container();
276  $pimple['invokable'] = new Fixtures\Invokable();
277 
278  $this->assertInstanceOf('Pimple\Tests\Fixtures\Service', $pimple['invokable']);
279  }
280 
283  {
284  $pimple = new Container();
285  $pimple['non_invokable'] = new Fixtures\NonInvokable();
286 
287  $this->assertInstanceOf('Pimple\Tests\Fixtures\NonInvokable', $pimple['non_invokable']);
288  }
289 
296  {
297  $pimple = new Container();
298  $pimple->factory($service);
299  }
300 
307  {
308  $pimple = new Container();
309  $pimple->protect($service);
310  }
311 
318  {
319  $pimple = new Container();
320  $pimple['foo'] = $service;
321  $pimple->extend('foo', function () {});
322  }
323 
330  {
331  $pimple = new Container();
332  $pimple['foo'] = function () {};
333  $pimple->extend('foo', $service);
334  }
335 
340  {
341  return array(
342  array(123),
343  array(new Fixtures\NonInvokable()),
344  );
345  }
346 
350  public function serviceDefinitionProvider()
351  {
352  return array(
353  array(function ($value) {
354  $service = new Fixtures\Service();
355  $service->value = $value;
356 
357  return $service;
358  }),
359  array(new Fixtures\Invokable()),
360  );
361  }
362 
364  {
365  $pimple = new Container();
366  $pimple['foo'] = function () {
367  return 'foo';
368  };
369  $foo = $pimple['foo'];
370 
371  $pimple['bar'] = function () {
372  return 'bar';
373  };
374  $this->assertSame('bar', $pimple['bar']);
375  }
376 
382  {
383  $pimple = new Container();
384  $pimple['foo'] = function () {
385  return 'foo';
386  };
387  $foo = $pimple['foo'];
388 
389  $pimple['foo'] = function () {
390  return 'bar';
391  };
392  }
393 
395  {
396  $pimple = new Container();
397  $pimple['foo'] = function () {
398  return 'foo';
399  };
400  $foo = $pimple['foo'];
401 
402  unset($pimple['foo']);
403  $pimple['foo'] = function () {
404  return 'bar';
405  };
406  $this->assertSame('bar', $pimple['foo']);
407  }
408 
409  public function testExtendingService()
410  {
411  $pimple = new Container();
412  $pimple['foo'] = function () {
413  return 'foo';
414  };
415  $pimple['foo'] = $pimple->extend('foo', function ($foo, $app) {
416  return "$foo.bar";
417  });
418  $pimple['foo'] = $pimple->extend('foo', function ($foo, $app) {
419  return "$foo.baz";
420  });
421  $this->assertSame('foo.bar.baz', $pimple['foo']);
422  }
423 
425  {
426  $pimple = new Container();
427  $pimple['foo'] = function () {
428  return 'foo';
429  };
430  $pimple['bar'] = function () {
431  return 'bar';
432  };
433  $foo = $pimple['foo'];
434 
435  $pimple['bar'] = $pimple->extend('bar', function ($bar, $app) {
436  return "$bar.baz";
437  });
438  $this->assertSame('bar.baz', $pimple['bar']);
439  }
440 }
testExtend($service)
serviceDefinitionProvider
Definition: PimpleTest.php:206
testShare($service)
serviceDefinitionProvider
Definition: PimpleTest.php:141
testProtectFailsForInvalidServiceDefinitions($service)
badServiceDefinitionProvider Callable is not a Closure or invokable object.
Definition: PimpleTest.php:306
serviceDefinitionProvider()
Provider for service definitions.
Definition: PimpleTest.php:350
testExtendFailsForKeysNotContainingServiceDefinitions($service)
badServiceDefinitionProvider Identifier "foo" does not contain an object definition.
Definition: PimpleTest.php:317
$container
Definition: wac.php:13
testExtendingServiceAfterOtherServiceFreeze()
Definition: PimpleTest.php:424
settingAnInvokableObjectShouldTreatItAsFactory()
Definition: PimpleTest.php:273
testFactoryFailsForInvalidServiceDefinitions($service)
badServiceDefinitionProvider Service definition is not a Closure or invokable object.
Definition: PimpleTest.php:295
testOverridingServiceAfterFreeze()
Cannot override frozen service "foo".
Definition: PimpleTest.php:381
settingNonInvokableObjectShouldTreatItAsParameter()
Definition: PimpleTest.php:282
testExtendValidatesKeyIsPresent()
Identifier "foo" is not defined.
Definition: PimpleTest.php:257
Container main class.
Definition: Container.php:34
testOffsetGetValidatesKeyIsPresent()
Identifier "foo" is not defined.
Definition: PimpleTest.php:112
testRawValidatesKeyIsPresent()
Identifier "foo" is not defined.
Definition: PimpleTest.php:197
badServiceDefinitionProvider()
Provider for invalid service definitions.
Definition: PimpleTest.php:339
testProtect($service)
serviceDefinitionProvider
Definition: PimpleTest.php:158
testExtendFailsForInvalidServiceDefinitions($service)
badServiceDefinitionProvider Extension service definition is not a Closure or invokable object...
Definition: PimpleTest.php:329