Skip to content

Commit d3763f6

Browse files
committed
unsetting result on overriding
1 parent 1d7dc23 commit d3763f6

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

Container.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public function set($id, $value)
5757
throw new Exception("Cannot override locked key {$id}");
5858
}
5959
$this->definitions[$id] = $value;
60+
// unset on override
61+
unset($this->objects[$id]);
6062
}
6163

6264
/**
@@ -72,7 +74,7 @@ public function get($id)
7274
return null;
7375
}
7476

75-
if (method_exists($this->definitions[$id], '__invoke')) {
77+
if (method_exists($this->definitions[$id], "__invoke")) {
7678
if (isset($this->raws[$this->definitions[$id]])) {
7779
return $this->definitions[$id];
7880
}

tests/ContainerTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function testGetFactory()
215215
$this->assertNotSame($container->factory("closure"), $container->factory("closure"));
216216
}
217217

218-
public function testOverriding()
218+
public function testOverridingValueWithValue()
219219
{
220220
$container = new Container();
221221
$container->set("param", "value");
@@ -224,6 +224,41 @@ public function testOverriding()
224224
$this->assertSame("other value", $container->get("param"));
225225
}
226226

227+
public function testOverridingClosureWithValue()
228+
{
229+
$container = new Container();
230+
$container->set("random", function () {
231+
return rand(1, 9);
232+
});
233+
$this->assertInternalType("int", $container->get("random"));
234+
$container->set("random", "a");
235+
$this->assertSame("a", $container->get("random"));
236+
}
237+
238+
public function testOverridingValueWithClosure()
239+
{
240+
$container = new Container();
241+
$container->set("random", "a");
242+
$this->assertSame("a", $container->get("random"));
243+
$container->set("random", function () {
244+
return rand(1, 9);
245+
});
246+
$this->assertInternalType("int", $container->get("random"));
247+
}
248+
249+
public function testOverridingClosureWithClosure()
250+
{
251+
$container = new Container();
252+
$container->set("random", function () {
253+
return rand(1, 9);
254+
});
255+
$this->assertInternalType("int", $container->get("random"));
256+
$container->set("random", function () {
257+
return "pi=" . (3 + 0.14);
258+
});
259+
$this->assertSame("pi=3.14", $container->get("random"));
260+
}
261+
227262
public function testOverridingLockedValue()
228263
{
229264
$container = new Container();

0 commit comments

Comments
 (0)