From fdf99043fb3709bfbe3de5e256b4b373a4ab7d66 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Sat, 17 Feb 2024 22:39:24 +0200 Subject: [PATCH] Add the ability to use array of scopes --- src/Attribute/TestScope.php | 2 +- src/TestCase.php | 28 +++++++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Attribute/TestScope.php b/src/Attribute/TestScope.php index 95d7e1a..1c63a50 100644 --- a/src/Attribute/TestScope.php +++ b/src/Attribute/TestScope.php @@ -10,7 +10,7 @@ final class TestScope { public function __construct( - public readonly string|\BackedEnum $scope, + public readonly string|\BackedEnum|array $scope, public readonly array $bindings = [], ) { } diff --git a/src/TestCase.php b/src/TestCase.php index de3e2d0..5a6dcb2 100644 --- a/src/TestCase.php +++ b/src/TestCase.php @@ -235,14 +235,12 @@ protected function runTest(): mixed $previousApp = $this->getApp(); - $result = $this->getApp()->getContainer()->runScope( - new Scope($scope->scope, $scope->bindings), - function (Container $container): mixed { - $this->initApp([...static::ENV, ...$this->getEnvVariablesFromConfig()], $container); + $scopes = \is_array($scope->scope) ? $scope->scope : [$scope->scope]; + $result = $this->runScopes($scopes, function (Container $container): mixed { + $this->initApp([...static::ENV, ...$this->getEnvVariablesFromConfig()], $container); - return parent::runTest(); - }, - ); + return parent::runTest(); + }, $this->getContainer(), $scope->bindings); $this->app = $previousApp; @@ -287,4 +285,20 @@ private function getTestScope(): ?TestScope return null; } + + private function runScopes(array $scopes, Closure $callback, Container $container, array $bindings): mixed + { + if ($scopes === []) { + return $container->runScope($bindings, $callback); + } + + $scope = \array_shift($scopes); + + return $container->runScope( + new Scope($scope, []), + function (Container $container) use ($scopes, $callback, $bindings): mixed { + return $this->runScopes($scopes, $callback, $container, $bindings); + }, + ); + } }