Skip to content
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
9 changes: 8 additions & 1 deletion src/Http/FakeHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Spiral\Auth\TokenStorageInterface;
use Spiral\Auth\Transport\HeaderTransport;
use Spiral\Auth\TransportRegistry;
use Spiral\Boot\FinalizerInterface;
use Spiral\Core\Attribute\Proxy;
use Spiral\Core\FactoryInterface;
use Spiral\Http\Http;
Expand Down Expand Up @@ -443,7 +444,13 @@ public function handleRequest(ServerRequestInterface $request, array $bindings =
->handle($request);
};

return new TestResponse(($this->scope)($handler, $bindings));
try {
return new TestResponse(($this->scope)($handler, $bindings));
} finally {
if ($this->container->has(FinalizerInterface::class)) {
$this->container->get(FinalizerInterface::class)->finalize(false);
}
}
}

protected function validateRequestData($data): void
Expand Down
30 changes: 30 additions & 0 deletions tests/src/Http/FakeHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Spiral\Testing\Tests\Http;

use PHPUnit\Framework\ExpectationFailedException;
use Spiral\Boot\FinalizerInterface;
use Spiral\Core\Internal\Introspector;
use Spiral\Testing\Attribute\TestScope;
use Spiral\Testing\Tests\App\Middleware\FailMiddleware;
use Spiral\Testing\Tests\Http\Stub\FakeFinalizer;
use Spiral\Testing\Tests\Http\Stub\StaticResultMiddleware;
use Spiral\Testing\Tests\TestCase;

Expand Down Expand Up @@ -154,4 +156,32 @@ public function testGetWithQueryParamsInNestedScope(): void
$response->assertBodySame('{"foo":"bar","baz":{"foo1":"bar1"}}');
$this->assertSame(['bar', 'foo', 'root'], Introspector::scopeNames($this->getContainer()));
}

public function testFinalizersAreCalledAfterRequest(): void
{
$finalizer = new FakeFinalizer();
$this->getContainer()->bindSingleton(FinalizerInterface::class, $finalizer);

$this->assertCount(0, $finalizer->calls);

$this->fakeHttp()->get('/get/query-params');

$this->assertCount(1, $finalizer->calls);
$this->assertFalse($finalizer->calls[0]['terminate']);
}

public function testFinalizersAreCalledAfterEachRequest(): void
{
$finalizer = new FakeFinalizer();
$this->getContainer()->bindSingleton(FinalizerInterface::class, $finalizer);

$this->fakeHttp()->get('/get/query-params');
$this->fakeHttp()->post('/post/json', ['foo' => 'bar']);
$this->fakeHttp()->get('/get/headers');

$this->assertCount(3, $finalizer->calls);
foreach ($finalizer->calls as $call) {
$this->assertFalse($call['terminate']);
}
}
}
23 changes: 23 additions & 0 deletions tests/src/Http/Stub/FakeFinalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\Http\Stub;

use Spiral\Boot\FinalizerInterface;

final class FakeFinalizer implements FinalizerInterface
{
/** @var array<array{terminate: bool}> */
public array $calls = [];

public function addFinalizer(callable $finalizer): static
{
return $this;
}

public function finalize(bool $terminate = false): void
{
$this->calls[] = ['terminate' => $terminate];
}
}
Loading