-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContainerTest.php
178 lines (150 loc) · 4.69 KB
/
ContainerTest.php
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
<?php
/**
* Created by IntelliJ IDEA.
* Author: flashytime
* Date: 2018/7/7 17:29
*/
namespace Flashytime\Container\Tests;
use Flashytime\Container\Container;
use Flashytime\Container\NotFoundException;
class ContainerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Container
*/
public $container;
public function setUp()
{
$this->container = new Container();
}
public function testClosure()
{
//hello world
$this->container->set('hello', function () {
return 'Hello World!';
});
$this->assertSame('Hello World!', $this->container->get('hello'));
//container passes self to Closure
$this->container->set('name', function () {
return 'Mocha';
});
$this->container->set('mocha', function ($container) {
return sprintf('Hello %s!', $container->get('name'));
});
$this->assertSame('Hello Mocha!', $this->container->get('mocha'));
}
public function testInstanceOf()
{
//foo instanceof Foo
$this->container->set('foo', 'Flashytime\Container\Tests\Foo');
$this->assertTrue($this->container->has('foo'));
$foo = $this->container->get('foo');
$this->assertTrue($foo instanceof Foo);
$this->assertInstanceOf(Foo::class, $foo);
//foo2 instanceof Foo
$this->container->set('foo2', function () {
return new Foo();
});
$foo2 = $this->container->get('foo2');
$this->assertInstanceOf(Foo::class, $foo2);
//foo3 instanceof Foo
$this->container->set('foo3', new Foo());
$foo3 = $this->container->get('foo3');
$this->assertInstanceOf(Foo::class, $foo3);
//Foo instanceof FooInterface
$this->container->set(FooInterface::class, Foo::class);
$this->assertInstanceOf(FooInterface::class, $this->container->get(FooInterface::class));
$this->assertInstanceOf(Foo::class, $this->container->get(FooInterface::class));
}
public function testWithParameters()
{
//closure with one parameter
$this->container->set('one', function ($container, $name) {
return sprintf('Hello %s!', $name);
});
$mocha = $this->container->get('one', ['Mocha']);
$this->assertSame('Hello Mocha!', $mocha);
//closure with two parameters
$this->container->set('two', function ($container, $name, $age) {
return sprintf('My name is %s, I am %d.', $name, $age);
});
$mocha = $this->container->get('two', ['Mocha', 18]);
$this->assertSame('My name is Mocha, I am 18.', $mocha);
//class with parameters
$this->container->set('foo', Foo::class);
$foo = $this->container->get('foo', ['Mocha', 18]);
$this->assertSame('Mocha', $foo->getName());
$this->assertSame(18, $foo->getAge());
}
public function testSingleton()
{
//singleton
$this->container->setSingleton('foo', Foo::class);
$first = $this->container->get('foo');
$second = $this->container->get('foo');
$this->assertSame($first, $second);
$this->assertTrue($first === $second);
//not singleton
$this->container->set('foo', Foo::class);
$first = $this->container->get('foo');
$second = $this->container->get('foo');
$this->assertNotSame($first, $second);
$this->assertFalse($first === $second);
}
public function testDependencyInjection()
{
$this->container->set(FooInterface::class, Foo::class);
$this->container->set('bar', Bar::class);
$bar = $this->container->get('bar');
$this->assertInstanceOf(Bar::class, $bar);
$this->assertInstanceOf(FooInterface::class, $bar->getFoo());
$this->assertInstanceOf(Foo::class, $bar->getFoo());
}
public function testNotFound()
{
$this->expectException(NotFoundException::class);
$this->container->get('unknown');
}
}
//tests classes
interface FooInterface
{
}
class Foo implements FooInterface
{
private $name;
private $age;
public function __construct($name = null, $age = 0)
{
$this->name = $name;
$this->age = $age;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setAge($age)
{
$this->age = $age;
}
public function getAge()
{
return $this->age;
}
}
class Bar
{
public $foo;
public function __construct(FooInterface $foo)
{
$this->foo = $foo;
}
public function getFoo()
{
return $this->foo;
}
}