From 67e3f649e6d6847759a5851eaced1fd7a3e078c8 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Thu, 9 Jun 2022 10:08:51 +0300 Subject: [PATCH] Fix tests, add Nyholm bridge --- composer.json | 5 +++-- src/Bootloader/SapiBootloader.php | 5 ----- tests/src/Dispatcher/SapiDispatcherTest.php | 12 +++++------ tests/src/TestCase.php | 23 ++++++++++++--------- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/composer.json b/composer.json index 36e7fba..c4f1764 100644 --- a/composer.json +++ b/composer.json @@ -14,14 +14,15 @@ "spiral/boot": "^3.0", "spiral/core": "^3.0", "spiral/exceptions": "^3.0", - "spiral/http": "^3.0" + "spiral/http": "^3.0", + "psr/http-factory-implementation": "^1.0" }, "require-dev": { "phpunit/phpunit": "^9.5.20", "spiral/testing": "^2.0", "vimeo/psalm": "^4.23", "spiral/framework": "^3.0", - "nyholm/psr7": "^1.5.0" + "spiral/nyholm-bridge": "^1.2" }, "autoload": { "psr-4": { diff --git a/src/Bootloader/SapiBootloader.php b/src/Bootloader/SapiBootloader.php index 59a459a..39a6b55 100644 --- a/src/Bootloader/SapiBootloader.php +++ b/src/Bootloader/SapiBootloader.php @@ -8,7 +8,6 @@ use Nyholm\Psr7Server\ServerRequestCreatorInterface; use Spiral\Boot\AbstractKernel; use Spiral\Boot\Bootloader\Bootloader; -use Spiral\Http\Bootloader\DiactorosBootloader; use Spiral\Core\Container\SingletonInterface; use Spiral\Core\FactoryInterface; use Spiral\Http\Config\HttpConfig; @@ -17,10 +16,6 @@ final class SapiBootloader extends Bootloader implements SingletonInterface { - protected const DEPENDENCIES = [ - DiactorosBootloader::class, - ]; - protected const SINGLETONS = [ SapiEmitter::class => [self::class, 'createEmitter'], ServerRequestCreatorInterface::class => ServerRequestCreator::class, diff --git a/tests/src/Dispatcher/SapiDispatcherTest.php b/tests/src/Dispatcher/SapiDispatcherTest.php index 70efbf1..04d0bd5 100644 --- a/tests/src/Dispatcher/SapiDispatcherTest.php +++ b/tests/src/Dispatcher/SapiDispatcherTest.php @@ -59,23 +59,23 @@ public function testDispatchNativeError(): void { $emitter = new BufferEmitter(); - $app = $this->initApp([ + $this->initApp([ 'DEBUG' => true ]); - $files = $app->getContainer()->get(FilesInterface::class)->getFiles( - $app->getContainer()->get(DirectoriesInterface::class)->get('runtime') . '/snapshots/' + $files = $this->getApp()->getContainer()->get(FilesInterface::class)->getFiles( + $this->getApp()->getContainer()->get(DirectoriesInterface::class)->get('runtime') . '/snapshots/' ); $this->assertCount(0, $files); $_SERVER['REQUEST_URI'] = '/error'; - $app->getContainer()->get(SapiDispatcher::class)->serve( + $this->getApp()->getContainer()->get(SapiDispatcher::class)->serve( \Closure::bind(function (ResponseInterface $response) {$this->emit($response);}, $emitter) ); - $files = $app->getContainer()->get(FilesInterface::class)->getFiles( - $app->getContainer()->get(DirectoriesInterface::class)->get('runtime') . '/snapshots/' + $files = $this->getApp()->getContainer()->get(FilesInterface::class)->getFiles( + $this->getApp()->getContainer()->get(DirectoriesInterface::class)->get('runtime') . '/snapshots/' ); $this->assertCount(1, $files); diff --git a/tests/src/TestCase.php b/tests/src/TestCase.php index 8dfaa1e..8129973 100644 --- a/tests/src/TestCase.php +++ b/tests/src/TestCase.php @@ -6,16 +6,18 @@ use App\Bootloader\AppBootloader; use App\TestApp; -use Nyholm\Psr7\Response; +use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; use Spiral\Boot\AbstractKernel; use Spiral\Bootloader\ExceptionHandlerBootloader; -use Spiral\Http\Bootloader\DiactorosBootloader; use Spiral\Bootloader\Http\ErrorHandlerBootloader; use Spiral\Bootloader\Http\HttpBootloader; use Spiral\Bootloader\Http\RouterBootloader; use Spiral\Bootloader\SnapshotsBootloader; +use Spiral\Core\Container; +use Spiral\Http\Config\HttpConfig; +use Spiral\Nyholm\Bootloader\NyholmBootloader; use Spiral\Sapi\Bootloader\SapiBootloader; use Spiral\Sapi\Emitter\SapiEmitter; use Spiral\Testing\TestableKernelInterface; @@ -29,9 +31,11 @@ protected function createResponse( $body = null, string $version = '1.1' ): ResponseInterface { - $response = (new Response()) - ->withStatus($status) - ->withProtocolVersion($version); + $this->getContainer()->bind(HttpConfig::class, new HttpConfig(['headers' => []])); + + $factory = $this->getContainer()->get(ResponseFactoryInterface::class); + $response = $factory->createResponse($status)->withProtocolVersion($version); + foreach ($headers as $header => $value) { $response = $response->withHeader($header, $value); } @@ -62,7 +66,7 @@ public function defineBootloaders(): array return [ SnapshotsBootloader::class, HttpBootloader::class, - DiactorosBootloader::class, + NyholmBootloader::class, ErrorHandlerBootloader::class, RouterBootloader::class, SapiBootloader::class, @@ -81,12 +85,11 @@ protected function tearDown(): void /** * @return AbstractKernel|TestableKernelInterface */ - public function createAppInstance(): TestableKernelInterface + public function createAppInstance(Container $container = new Container()): TestableKernelInterface { - return TestApp::createWithBootloaders( - $this->defineBootloaders(), + return TestApp::create( $this->defineDirectories($this->rootDirectory()), false - ); + )->withBootloaders($this->defineBootloaders()); } }