1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Deployee\Components\Container; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class ContainerTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function testSetGet() |
12
|
|
|
{ |
13
|
|
|
$container = new Container(['foo' => 1, 'bar' => function(){return 'foobar';}]); |
14
|
|
|
$container->set('barfoo', new \stdClass()); |
15
|
|
|
|
16
|
|
|
$this->assertSame(1, $container->get('foo')); |
17
|
|
|
$this->assertSame('foobar', $container->get('bar')); |
18
|
|
|
$this->assertInstanceOf(\stdClass::class, $container->get('barfoo')); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testSetFail() |
22
|
|
|
{ |
23
|
|
|
$container = new Container(['foo' => 1]); |
24
|
|
|
|
25
|
|
|
$this->expectException(ContainerException::class); |
26
|
|
|
$container->set('foo', 2); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testGetFail() |
30
|
|
|
{ |
31
|
|
|
$container = new Container(); |
32
|
|
|
$this->expectException(ContainerException::class); |
33
|
|
|
$container->get('foo'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testExtend() |
37
|
|
|
{ |
38
|
|
|
$object = new \stdClass(); |
39
|
|
|
$object->bar = 1; |
40
|
|
|
|
41
|
|
|
$container = new Container(['foo' => $object, 'addtofoo' => 5]); |
42
|
|
|
$container->extend('foo', function(\stdClass $foo, ContainerInterface $c){ |
43
|
|
|
return $foo->bar + $c->get('addtofoo'); |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
$extended = $container->get('foo'); |
47
|
|
|
$this->assertSame(6, $extended); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testReplaceObject() |
51
|
|
|
{ |
52
|
|
|
$objectOne = new \stdClass(); |
53
|
|
|
$objectOne->bar = 1; |
54
|
|
|
|
55
|
|
|
$objectTwo = new \stdClass(); |
56
|
|
|
$objectTwo->bar = 1337; |
57
|
|
|
|
58
|
|
|
$container = new Container(['object' => $objectOne]); |
59
|
|
|
$container->extend('object', function() use($objectTwo){ |
60
|
|
|
return $objectTwo; |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
$this->assertSame($objectTwo, $container->get('object')); |
64
|
|
|
} |
65
|
|
|
} |