From 3b3efc7cbbe1775a651cc76c06db0ef56718dc8b Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Fri, 11 Aug 2023 15:04:30 +0100 Subject: [PATCH] chore: adapt test infra --- .../core/src/Foundation/AppInterface.php | 4 +- framework/core/src/Foundation/Application.php | 3 +- .../core/src/Foundation/InstalledApp.php | 23 ++++----- .../core/src/Foundation/InstalledSite.php | 3 +- framework/core/src/Http/Server.php | 18 ++++--- .../src/integration/BuildsHttpRequests.php | 47 ++++++++++--------- .../testing/src/integration/TestCase.php | 20 ++++---- 7 files changed, 61 insertions(+), 57 deletions(-) diff --git a/framework/core/src/Foundation/AppInterface.php b/framework/core/src/Foundation/AppInterface.php index 2bbf784ea..acf383809 100644 --- a/framework/core/src/Foundation/AppInterface.php +++ b/framework/core/src/Foundation/AppInterface.php @@ -9,12 +9,12 @@ namespace Flarum\Foundation; -use Illuminate\Contracts\Container\Container; +use Illuminate\Contracts\Foundation\Application as ApplicationContract; use Symfony\Component\Console\Command\Command; interface AppInterface { - public function getContainer(): Container; + public function getContainer(): ApplicationContract; public function getMiddlewareStack(): array; diff --git a/framework/core/src/Foundation/Application.php b/framework/core/src/Foundation/Application.php index d510d90df..160ed22f6 100644 --- a/framework/core/src/Foundation/Application.php +++ b/framework/core/src/Foundation/Application.php @@ -12,6 +12,7 @@ namespace Flarum\Foundation; use Flarum\Foundation\Concerns\InteractsWithLaravel; use Flarum\Http\RoutingServiceProvider; use Illuminate\Container\Container as IlluminateContainer; +use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Foundation\Application as LaravelApplication; use Illuminate\Events\EventServiceProvider; use Illuminate\Support\Arr; @@ -70,7 +71,7 @@ class Application extends IlluminateContainer implements LaravelApplication IlluminateContainer::setInstance($this); $this->instance('app', $this); - $this->instance('container', $this); + $this->instance(Container::class, $this); $this->instance('flarum', $this); $this->instance('flarum.paths', $this->paths); } diff --git a/framework/core/src/Foundation/InstalledApp.php b/framework/core/src/Foundation/InstalledApp.php index 4a7d66978..6a12b3102 100644 --- a/framework/core/src/Foundation/InstalledApp.php +++ b/framework/core/src/Foundation/InstalledApp.php @@ -9,34 +9,29 @@ namespace Flarum\Foundation; -use Flarum\Http\Middleware as HttpMiddleware; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Console\Command; -use Illuminate\Contracts\Container\Container; +use Illuminate\Contracts\Foundation\Application as ApplicationContract; use Laminas\Stratigility\Middleware\OriginalMessages; -use Laminas\Stratigility\MiddlewarePipe; use Middlewares\BasePath; -use Middlewares\BasePathRouter; -use Middlewares\RequestHandler; -use Psr\Http\Server\RequestHandlerInterface; class InstalledApp implements AppInterface { public function __construct( - protected Container $container, + protected ApplicationContract $app, protected Config $config ) { } - public function getContainer(): Container + public function getContainer(): ApplicationContract { - return $this->container; + return $this->app; } public function getMiddlewareStack(): array { // if ($this->config->inMaintenanceMode()) { -// return $this->container->make('flarum.maintenance.handler'); +// return $this->app->make('flarum.maintenance.handler'); // } return match ($this->needsUpdate()) { @@ -47,7 +42,7 @@ class InstalledApp implements AppInterface protected function needsUpdate(): bool { - $settings = $this->container->make(SettingsRepositoryInterface::class); + $settings = $this->app->make(SettingsRepositoryInterface::class); $version = $settings->get('version'); return $version !== Application::VERSION; @@ -76,13 +71,13 @@ class InstalledApp implements AppInterface public function getConsoleCommands(): array { return array_map(function ($command) { - $command = $this->container->make($command); + $command = $this->app->make($command); if ($command instanceof Command) { - $command->setLaravel($this->container); + $command->setLaravel($this->app); } return $command; - }, $this->container->make('flarum.console.commands')); + }, $this->app->make('flarum.console.commands')); } } diff --git a/framework/core/src/Foundation/InstalledSite.php b/framework/core/src/Foundation/InstalledSite.php index 2d52395f7..9e205e06b 100644 --- a/framework/core/src/Foundation/InstalledSite.php +++ b/framework/core/src/Foundation/InstalledSite.php @@ -40,6 +40,7 @@ use Illuminate\Config\Repository as ConfigRepository; use Illuminate\Contracts\Cache\Repository; use Illuminate\Contracts\Cache\Store; use Illuminate\Contracts\Container\Container; +use Illuminate\Contracts\Foundation\Application as ApplicationContract; use Illuminate\Filesystem\Filesystem; use Illuminate\Hashing\HashServiceProvider; use Illuminate\Validation\ValidationServiceProvider; @@ -87,7 +88,7 @@ class InstalledSite implements SiteInterface return $this; } - protected function bootLaravel(): Container + protected function bootLaravel(): ApplicationContract { $app = new Application($this->paths); diff --git a/framework/core/src/Http/Server.php b/framework/core/src/Http/Server.php index c37061fa4..a7091ec30 100644 --- a/framework/core/src/Http/Server.php +++ b/framework/core/src/Http/Server.php @@ -13,30 +13,36 @@ use Flarum\Foundation\AppInterface; use Flarum\Foundation\ErrorHandling\LogReporter; use Flarum\Foundation\SiteInterface; use Illuminate\Contracts\Container\Container; +use Illuminate\Contracts\Foundation\Application; use Illuminate\Http\Request; use Illuminate\Routing\Pipeline; use Psr\Log\LoggerInterface; +use Symfony\Component\HttpFoundation\Response; use Throwable; class Server { public function __construct( - private readonly SiteInterface $site + private readonly ?SiteInterface $site = null ) { } public function listen(): void { - $request = Request::capture(); $siteApp = $this->safelyBoot(); - $container = $siteApp->getContainer(); + $app = $siteApp->getContainer(); $globalMiddleware = $siteApp->getMiddlewareStack(); - (new Pipeline($container)) + $this->send(Request::capture(), $app, $globalMiddleware); + } + + public function send(Request $request, Application $app, array $globalMiddleware): Response + { + return (new Pipeline($app)) ->send($request) ->through($globalMiddleware) - ->then(function (Request $request) use ($container) { - return $container->make(Router::class)->dispatch($request); + ->then(function (Request $request) use ($app) { + return $app->make(Router::class)->dispatch($request); }); } diff --git a/php-packages/testing/src/integration/BuildsHttpRequests.php b/php-packages/testing/src/integration/BuildsHttpRequests.php index 43ed2cfe0..b76f33323 100644 --- a/php-packages/testing/src/integration/BuildsHttpRequests.php +++ b/php-packages/testing/src/integration/BuildsHttpRequests.php @@ -10,12 +10,11 @@ namespace Flarum\Testing\integration; use Carbon\Carbon; -use Dflydev\FigCookies\SetCookie; +use Illuminate\Http\Request; use Illuminate\Support\Str; -use Laminas\Diactoros\CallbackStream; -use Psr\Http\Message\ResponseInterface as Response; -use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Message\ServerRequestInterface as Request; +use Symfony\Component\HttpFoundation\Cookie; +use Symfony\Component\HttpFoundation\ParameterBag; +use Symfony\Component\HttpFoundation\Response; /** * A collection of helpers for building PSR-7 requests for integration tests. @@ -24,13 +23,10 @@ trait BuildsHttpRequests { protected function requestWithJsonBody(Request $req, array $json): Request { - return $req - ->withHeader('Content-Type', 'application/json') - ->withBody( - new CallbackStream(function () use ($json) { - return json_encode($json); - }) - ); + $req->headers->set('Content-Type', 'application/json'); + $req->setJson(new ParameterBag($json)); + + return $req; } protected function requestAsUser(Request $req, int $userId): Request @@ -49,36 +45,41 @@ trait BuildsHttpRequests 'type' => 'session' ]); - return $req - ->withAddedHeader('Authorization', "Token {$token}") - // We save the token as an attribute so that we can retrieve it for test purposes. - ->withAttribute('tests_token', $token); + $req->headers->set('Authorization', "Token {$token}"); + + // We save the token as an attribute so that we can retrieve it for test purposes. + $req->attributes->set('tests_token', $token); + + return $req; } protected function requestWithCookiesFrom(Request $req, Response $previous): Request { $cookies = array_reduce( - $previous->getHeader('Set-Cookie'), + $previous->headers->all('Set-Cookie'), function ($memo, $setCookieString) { - $setCookie = SetCookie::fromSetCookieString($setCookieString); - $memo[$setCookie->getName()] = $setCookie->getValue(); + $cookie = Cookie::fromString($setCookieString); + $memo[$cookie->getName()] = $cookie->getValue(); return $memo; }, [] ); - return $req->withCookieParams($cookies); + $req->cookies->add($cookies); + + return $req; } - protected function requestWithCsrfToken(ServerRequestInterface $request): ServerRequestInterface + protected function requestWithCsrfToken(Request $request): Request { $initial = $this->send( $this->request('GET', '/') ); - $token = $initial->getHeaderLine('X-CSRF-Token'); + $token = $initial->headers->get('X-CSRF-Token'); + $request->headers->set('X-CSRF-Token', $token); - return $this->requestWithCookiesFrom($request->withHeader('X-CSRF-Token', $token), $initial); + return $this->requestWithCookiesFrom($request, $initial); } } diff --git a/php-packages/testing/src/integration/TestCase.php b/php-packages/testing/src/integration/TestCase.php index 4c773e262..12459ba61 100644 --- a/php-packages/testing/src/integration/TestCase.php +++ b/php-packages/testing/src/integration/TestCase.php @@ -10,14 +10,14 @@ namespace Flarum\Testing\integration; use Flarum\Extend\ExtenderInterface; +use Flarum\Http\Server; use Flarum\Testing\integration\Setup\Bootstrapper; use Illuminate\Contracts\Cache\Store; use Illuminate\Database\ConnectionInterface; +use Illuminate\Http\Request; use Illuminate\Support\Arr; -use Laminas\Diactoros\ServerRequest; -use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; +use Symfony\Component\HttpFoundation\Response; abstract class TestCase extends \PHPUnit\Framework\TestCase { @@ -156,10 +156,10 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase */ protected $server; - protected function server(): RequestHandlerInterface + protected function server(): Server { if (is_null($this->server)) { - $this->server = $this->app()->getRequestHandler(); + $this->server = new Server(); } return $this->server; @@ -213,9 +213,9 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase /** * Send a full HTTP request through Flarum's middleware stack. */ - protected function send(ServerRequestInterface $request): ResponseInterface + protected function send(Request $request): Response { - return $this->server()->handle($request); + return $this->server()->send($request, $this->app->getContainer(), []); } /** @@ -240,11 +240,11 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase * interaction. All cookies returned from the server in that response * (via the "Set-Cookie" header) will be copied to the cookie params of * the new request. - * @return ServerRequestInterface + * @return Request */ - protected function request(string $method, string $path, array $options = []): ServerRequestInterface + protected function request(string $method, string $path, array $options = []): Request { - $request = new ServerRequest([], [], $path, $method); + $request = Request::create($path, $method); // Do we want a JSON request body? if (isset($options['json'])) {