Skip to content

Commit

Permalink
HttpFaker opens http scope on request
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Sep 13, 2024
1 parent 6fcd738 commit 1e0e224
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/Http/FakeHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class FakeHttp
private ?SessionInterface $session = null;
private BinderInterface $binder;

/**
* @param \Closure(\Closure $closure, array $bindings): mixed $scope Scope runner
*/
public function __construct(
#[Proxy] private readonly ContainerInterface $container,
private readonly FileFactory $fileFactory,
Expand Down Expand Up @@ -418,9 +421,7 @@ protected function handleRequest(ServerRequestInterface $request, array $binding
return $this->getHttp()->handle($request);
};

$scope = $this->scope;

return new TestResponse($scope($handler, $bindings));
return new TestResponse(($this->scope)($handler, $bindings));
}

protected function validateRequestData($data): void
Expand Down
14 changes: 10 additions & 4 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Spiral\Core\ConfigsInterface;
use Spiral\Core\Container;
use Spiral\Core\ContainerScope;
use Spiral\Core\Internal\Introspector;
use Spiral\Core\Scope;
use Spiral\Testing\Attribute\TestScope;

Expand Down Expand Up @@ -173,7 +174,7 @@ public function initApp(array $env = [], Container $container = new Container())
* @param array<string, string|array|callable|object> $bindings
* @throws \Throwable
*/
public function runScoped(Closure $callback, array $bindings = []): mixed
public function runScoped(Closure $callback, array $bindings = [], ?string $name = null): mixed
{
if ($this->environment) {
$bindings[EnvironmentInterface::class] = $this->environment;
Expand Down Expand Up @@ -234,7 +235,7 @@ protected function runTest(): mixed
}

$scopes = \is_array($scope->scope) ? $scope->scope : [$scope->scope];
$result = $this->runScopes($scopes, function (): mixed {
$result = self::runScopes($scopes, function (): mixed {
return parent::runTest();
}, $this->getContainer(), $scope->bindings);

Expand Down Expand Up @@ -280,18 +281,23 @@ private function getTestScope(): ?TestScope
return null;
}

private function runScopes(array $scopes, Closure $callback, Container $container, array $bindings): mixed
private static function runScopes(array $scopes, Closure $callback, Container $container, array $bindings): mixed
{
begin:
if ($scopes === []) {
return $container->runScope($bindings, $callback);
}


$scope = \array_shift($scopes);
if ($scopes !== null && \in_array($scope, Introspector::scopeNames(), true)) {
goto begin;
}

return $container->runScope(
new Scope($scope, []),
function (Container $container) use ($scopes, $callback, $bindings): mixed {
return $this->runScopes($scopes, $callback, $container, $bindings);
return self::runScopes($scopes, $callback, $container, $bindings);
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/InteractsWithHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final public function fakeHttp(): FakeHttp
return $this->getContainer()->get(FactoryInterface::class)->make(FakeHttp::class, [
'fileFactory' => $this->getFileFactory(),
'scope' => function (\Closure $closure, array $bindings = []) {
return $this->runScoped($closure, $bindings);
return self::runScopes(['http'], $closure, $this->getContainer(), $bindings);
}
]);
}
Expand Down

0 comments on commit 1e0e224

Please sign in to comment.