Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always run HttpFaker requests in http scope #74

Merged
merged 4 commits into from
Sep 13, 2024
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
39 changes: 20 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,34 @@
"nyholm/psr7": "^1.5",
"mockery/mockery": "^1.5",
"phpunit/phpunit": "^9.6 || ^10.0",
"spiral/auth": "^3.12",
"spiral/auth-http": "^3.12",
"spiral/boot": "^3.12",
"spiral/events": "^3.12",
"spiral/console": "^3.12",
"spiral/core": "^3.12",
"spiral/http": "^3.12",
"spiral/mailer": "^3.12",
"spiral/queue": "^3.12",
"spiral/session": "^3.12",
"spiral/security": "^3.12",
"spiral/tokenizer": "^3.12",
"spiral/storage": "^3.12",
"spiral/views": "^3.12",
"spiral/translator": "^3.12",
"spiral/scaffolder": "^3.12",
"spiral/auth": "^3.14.3",
"spiral/auth-http": "^3.14.3",
"spiral/boot": "^3.14.3",
"spiral/events": "^3.14.3",
"spiral/console": "^3.14.3",
"spiral/core": "^3.14.3",
"spiral/http": "^3.14.3",
"spiral/mailer": "^3.14.3",
"spiral/queue": "^3.14.3",
"spiral/session": "^3.14.3",
"spiral/security": "^3.14.3",
"spiral/tokenizer": "^3.14.3",
"spiral/storage": "^3.14.3",
"spiral/views": "^3.14.3",
"spiral/translator": "^3.14.3",
"spiral/scaffolder": "^3.14.3",
"symfony/mime": "^6.0 || ^7.0"
},
"suggest": {
"brianium/paratest": "Required to run tests in parallel (^6.0).",
"ext-gd": "Required to use generate fake image files"
},
"require-dev": {
"spiral/framework": "^3.12",
"spiral/roadrunner-bridge": "^2.2 || ^3.0",
"spiral/dumper": "^3.3",
"spiral/framework": "^3.14.3",
"spiral/roadrunner-bridge": "^2.2 || ^3.7 || ^4.0",
"spiral-packages/league-event": "^1.0.1",
"spiral/nyholm-bridge": "^1.2",
"spiral/nyholm-bridge": "^1.3",
"vimeo/psalm": "^5.9"
},
"autoload": {
Expand Down
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
7 changes: 7 additions & 0 deletions tests/app/Controller/GetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Spiral\Testing\Tests\App\Controller;

use Psr\Http\Message\ServerRequestInterface;
use Spiral\Core\Internal\Introspector;
use Spiral\Router\Annotation\Route;

class GetController
Expand All @@ -20,4 +21,10 @@ public function headers(ServerRequestInterface $request): array
{
return $request->getHeaders();
}

#[Route('/get/scopes', 'get.scopes')]
public function scopes(ServerRequestInterface $request): array
{
return Introspector::scopeNames();
}
}
3 changes: 2 additions & 1 deletion tests/src/Event/EventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Spiral\Testing\Tests\Event;

use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\ExpectationFailedException;
use Spiral\Testing\Events\FakeEventDispatcher;
use Spiral\Testing\Tests\App\Event\AnotherEvent;
Expand Down Expand Up @@ -78,7 +79,7 @@ public function testAssertNotDispatchedSomeEventShouldThrowAnException(): void
public function testAssertNothingDispatchedShouldThrowAnException(): void
{
$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage('3 unexpected events were dispatched.');
$this->expectExceptionMessageMatches('/\d+ unexpected events were dispatched./');

$this->http->get('/dispatch/some');

Expand Down
13 changes: 13 additions & 0 deletions tests/src/Http/FakeHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ public function testGetBodySame(): void
$response->assertBodySame('[]');
}

#[TestScope('http')]
public function testHttpScopeDoesNotConflict(): void
{
$response = $this->fakeHttp()->get('/get/query-params');
$response->assertBodySame('[]');
}

public function testAutoHttpScope(): void
{
$response = $this->fakeHttp()->get('/get/scopes');
$response->assertBodySame('["http-request","http","root"]');
}

public function testGetWithQueryParams(): void
{
$response = $this->fakeHttp()->get('/get/query-params', ['foo' => 'bar', 'baz' => ['foo1' => 'bar1']]);
Expand Down
Loading