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); + }, + ); + } }