Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes problem with rebinding singletons. #60

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Http/FakeHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function withMiddleware(string ...$middleware): self
public function withoutMiddleware(string ...$middleware): self
{
foreach ($middleware as $name) {
$this->container->removeBinding($name);
$this->container->bindSingleton(
$name,
new class implements MiddlewareInterface {
Expand Down
1 change: 1 addition & 0 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public function makeApp(array $env = []): AbstractKernel
$environment = new Environment($env);

$app = $this->createAppInstance();
$app->getContainer()->removeBinding(EnvironmentInterface::class);
$app->getContainer()->bindSingleton(EnvironmentInterface::class, $environment);

foreach ($this->beforeInit as $callback) {
Expand Down
3 changes: 3 additions & 0 deletions src/Traits/InteractsWithCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public function getRegisteredBootloaders(): array
*/
public function mockContainer(string $alias, ?string $interface = null): \Mockery\MockInterface
{
$this->getContainer()->removeBinding($alias);

$this->getContainer()->bindSingleton(
$alias,
$mock = \Mockery::mock($interface ?? $alias)
Expand All @@ -152,6 +154,7 @@ public function withEnvironment(array $env): self
array_merge($current, $env)
);

$this->getContainer()->removeBinding(EnvironmentInterface::class);
$this->getContainer()->bind(EnvironmentInterface::class, $this->environment);

return $this;
Expand Down
1 change: 1 addition & 0 deletions src/Traits/InteractsWithEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ trait InteractsWithEvents
{
public function fakeEventDispatcher(array $eventsToFake = []): FakeEventDispatcher
{
$this->getContainer()->removeBinding(EventDispatcherInterface::class);
$this->getContainer()->bindSingleton(
EventDispatcherInterface::class,
$dispatcher = $this->getContainer()->make(
Expand Down
1 change: 1 addition & 0 deletions src/Traits/InteractsWithExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ trait InteractsWithExceptions
{
protected function withoutExceptionHandling(): void
{
$this->getContainer()->removeBinding(ExceptionHandlerInterface::class);
$this->getContainer()->bind(
ExceptionHandlerInterface::class,
new class implements ExceptionHandlerInterface {
Expand Down
1 change: 1 addition & 0 deletions src/Traits/InteractsWithMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function fakeMailer(): FakeMailer
}
}

$container->removeBinding(MailerInterface::class);
$container->bindSingleton(
MailerInterface::class,
$mailer = new FakeMailer()
Expand Down
1 change: 1 addition & 0 deletions src/Traits/InteractsWithQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function fakeQueue(): FakeQueueManager
}
}

$container->removeBinding(QueueConnectionProviderInterface::class);
$container->bindSingleton(
QueueConnectionProviderInterface::class,
$manager = $container->get(FakeQueueManager::class)
Expand Down
7 changes: 4 additions & 3 deletions src/Traits/InteractsWithScaffolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ public function mockScaffolder(string $command, array $args, \Closure $expected)

$this->getConsole()->run($command, new ArrayInput($args), $output = new BufferedOutput());

$originalFiles !== null
? $this->getContainer()->bind(FilesInterface::class, $originalFiles)
: $this->getContainer()->removeBinding(FilesInterface::class);
$this->getContainer()->removeBinding(FilesInterface::class);
if ($originalFiles !== null) {
$this->getContainer()->bind(FilesInterface::class, $originalFiles);
}

return $output->fetch();
}
Expand Down