Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit e302410

Browse files
committed
Add tests
1 parent 9314370 commit e302410

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/ServiceManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ public function get($name)
166166

167167
$object = $this->doCreate($name);
168168

169-
if ($this->sharedByDefault ^ isset($this->shared[$name])) {
169+
if (($this->sharedByDefault && !isset($this->shared[$name]))
170+
|| (isset($this->shared[$name]) && $this->shared[$name])) {
170171
$this->services[$name] = $object;
171172
}
172173

test/ServiceManagerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,35 @@ function ($container, $name, $callback) {
103103
);
104104
$this->assertEquals('bar', $instance->foo);
105105
}
106+
107+
public function cacheProvider()
108+
{
109+
return [
110+
[true, true, true],
111+
[true, false, false],
112+
[false, false, false],
113+
[false, true, true]
114+
];
115+
}
116+
117+
/**
118+
* @dataProvider cacheProvider
119+
*/
120+
public function testCacheability($sharedByDefault, $serviceShared, $shouldBeSameInstance)
121+
{
122+
$serviceManager = new ServiceManager([
123+
'shared_by_default' => $sharedByDefault,
124+
'factories' => [
125+
stdClass::class => InvokableFactory::class,
126+
],
127+
'shared' => [
128+
stdClass::class => $serviceShared
129+
]
130+
]);
131+
132+
$a = $serviceManager->get(stdClass::class);
133+
$b = $serviceManager->get(stdClass::class);
134+
135+
$this->assertEquals($shouldBeSameInstance, $a === $b);
136+
}
106137
}

0 commit comments

Comments
 (0)