Skip to content

Commit 60cecdb

Browse files
author
Bl00D4NGEL
committed
fix: clear caching when setting container
1 parent f642340 commit 60cecdb

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Facade.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ public static function swapMock(): Mockery\MockInterface
136136
{
137137
$accessor = static::getFacadeAccessor();
138138
$mock = static::createMock();
139-
self::$resolvedInstances[$accessor] = $mock;
139+
static::$resolvedInstances[$accessor] = $mock;
140140

141141
return $mock;
142142
}
143143

144144
/**
145-
* Set the container instance.
145+
* Set the container instance. Reset any cache instances in the process.
146146
*
147147
* @codeCoverageIgnore
148148
*
@@ -151,6 +151,8 @@ public static function swapMock(): Mockery\MockInterface
151151
*/
152152
public static function setContainer(ContainerInterface $container): void
153153
{
154+
static::clear();
155+
154156
static::$container = $container;
155157
}
156158

tests/Unit/FacadeTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,21 @@ public function testSwapMock(): void
161161
$result = ServiceFacade::greeting('World'); // @phpstan-ignore-line
162162
$this->assertEquals('Hello World!', $result);
163163
}
164+
165+
public function testSettingContainerClearsFacadeCache(): void
166+
{
167+
$firstService = new Service();
168+
$firstContainer = new Container($firstService);
169+
ServiceFacade::setContainer($firstContainer);
170+
$firstRoot = ServiceFacade::getFacadeRoot();
171+
$this->assertSame($firstRoot, $firstService);
172+
173+
$secondService = new Service();
174+
$secondContainer = new Container($secondService);
175+
ServiceFacade::setContainer($secondContainer);
176+
$secondRoot = ServiceFacade::getFacadeRoot();
177+
$this->assertSame($secondRoot, $secondService);
178+
179+
$this->assertNotSame($firstRoot, $secondRoot);
180+
}
164181
}

0 commit comments

Comments
 (0)