Skip to content

Commit

Permalink
Run fake http requests in scope
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed Jan 25, 2024
1 parent 2bedf48 commit 500759f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Attribute/TestScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Attribute;

#[Attribute(flags: Attribute::TARGET_METHOD)]
#[Attribute(flags: Attribute::TARGET_METHOD|Attribute::TARGET_CLASS)]
final class TestScope
{
public function __construct(
Expand Down
22 changes: 17 additions & 5 deletions src/Http/FakeHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Stream;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
Expand All @@ -16,7 +17,9 @@
use Spiral\Auth\TokenStorageInterface;
use Spiral\Auth\Transport\HeaderTransport;
use Spiral\Auth\TransportRegistry;
use Spiral\Core\Container;
use Spiral\Core\Attribute\Proxy;
use Spiral\Core\BinderInterface;
use Spiral\Core\InvokerInterface;
use Spiral\Http\Http;
use Spiral\Session\SessionInterface;
use Spiral\Testing\Auth\FakeActorProvider;
Expand All @@ -32,12 +35,21 @@ class FakeHttp

private ?object $actor = null;
private ?SessionInterface $session = null;
private BinderInterface $binder;
private ContainerInterface $container;

public function __construct(
private readonly Container $container,
ContainerInterface $container,
private readonly FileFactory $fileFactory,
private readonly \Closure $scope,
) {
$this->binder = $container
->get(InvokerInterface::class)
->invoke(static fn (#[Proxy] BinderInterface $binder): BinderInterface => $binder);

$this->container = $container
->get(InvokerInterface::class)
->invoke(static fn (#[Proxy] ContainerInterface $container): ContainerInterface => $container);
}

public function withActor(object $actor): self
Expand Down Expand Up @@ -129,7 +141,7 @@ public function flushSession(): self
public function withMiddleware(string ...$middleware): self
{
foreach ($middleware as $name) {
$this->container->removeBinding($name);
$this->binder->removeBinding($name);
}

return $this;
Expand All @@ -138,8 +150,8 @@ public function withMiddleware(string ...$middleware): self
public function withoutMiddleware(string ...$middleware): self
{
foreach ($middleware as $name) {
$this->container->removeBinding($name);
$this->container->bindSingleton(
$this->binder->removeBinding($name);
$this->binder->bindSingleton(
$name,
new class implements MiddlewareInterface {
public function process(
Expand Down
32 changes: 28 additions & 4 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,25 @@ protected function tearDownTraits(): void

protected function runTest(): mixed
{
$attribute = $this->getTestAttributes(TestScope::class)[0] ?? null;
if ($attribute === null) {
$scope = $this->getTestScope();
if ($scope === null) {
return parent::runTest();
}

return $this->app->getContainer()->runScope(
new Scope($attribute->scope, $attribute->bindings),
$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);

return parent::runTest();
},
);

$this->app = $previousApp;

return $result;
}

private function runTraitSetUpOrTearDown(string $method): void
Expand All @@ -263,4 +269,22 @@ private function runTraitSetUpOrTearDown(string $method): void
$ref = $parent;
}
}

private function getTestScope(): ?TestScope
{
$attribute = $this->getTestAttributes(TestScope::class)[0] ?? null;
if ($attribute !== null) {
return $attribute;
}

try {
foreach ((new \ReflectionClass($this))->getAttributes(TestScope::class) as $attr) {
return $attr->newInstance();
}
} catch (\Throwable) {
return null;
}

return null;
}
}

0 comments on commit 500759f

Please sign in to comment.