From 010f1939e01d068e7269a9a6f3be00e184e9565d Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 7 Feb 2020 23:22:22 +0100 Subject: [PATCH 1/4] Integration tests: Create app lazily when needed This will allow registering extenders in test scenarios. Previously, this would not have had any effect as the app would have booted already. --- framework/core/tests/integration/TestCase.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/framework/core/tests/integration/TestCase.php b/framework/core/tests/integration/TestCase.php index c6928812e..31d14199a 100644 --- a/framework/core/tests/integration/TestCase.php +++ b/framework/core/tests/integration/TestCase.php @@ -19,14 +19,6 @@ use Psr\Http\Message\ServerRequestInterface; abstract class TestCase extends \PHPUnit\Framework\TestCase { - public function setUp() - { - parent::setUp(); - - // Boot the Flarum app - $this->app(); - } - /** * @var \Flarum\Foundation\InstalledApp */ From d0aa0b79204d3ec234d47cad627ab2ef6851ee42 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 7 Feb 2020 23:28:37 +0100 Subject: [PATCH 2/4] Integration tests: Add lazy server helper This allows sending requests directly in an integration test, without having *explicitly* booted the app. --- framework/core/tests/integration/TestCase.php | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/framework/core/tests/integration/TestCase.php b/framework/core/tests/integration/TestCase.php index 31d14199a..cdb3fdc9a 100644 --- a/framework/core/tests/integration/TestCase.php +++ b/framework/core/tests/integration/TestCase.php @@ -16,6 +16,7 @@ use Laminas\Diactoros\CallbackStream; use Laminas\Diactoros\ServerRequest; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\RequestHandlerInterface; abstract class TestCase extends \PHPUnit\Framework\TestCase { @@ -24,11 +25,6 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase */ protected $app; - /** - * @var \Psr\Http\Server\RequestHandlerInterface - */ - protected $server; - /** * @return \Flarum\Foundation\InstalledApp */ @@ -52,6 +48,20 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase return $this->app; } + /** + * @var RequestHandlerInterface + */ + protected $server; + + protected function server(): RequestHandlerInterface + { + if (is_null($this->server)) { + $this->server = $this->app()->getRequestHandler(); + } + + return $this->server; + } + protected $database; protected function database(): ConnectionInterface @@ -103,7 +113,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase */ protected function send(ServerRequestInterface $request): ResponseInterface { - return $this->server->handle($request); + return $this->server()->handle($request); } /** From b2c3392a832c95444b328dde06dab3c1546023f1 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 7 Feb 2020 23:29:14 +0100 Subject: [PATCH 3/4] Integration tests: Allow registering extenders --- framework/core/tests/integration/TestCase.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/framework/core/tests/integration/TestCase.php b/framework/core/tests/integration/TestCase.php index cdb3fdc9a..687023dbe 100644 --- a/framework/core/tests/integration/TestCase.php +++ b/framework/core/tests/integration/TestCase.php @@ -10,6 +10,7 @@ namespace Flarum\Tests\integration; use Dflydev\FigCookies\SetCookie; +use Flarum\Extend\ExtenderInterface; use Flarum\Foundation\InstalledSite; use Illuminate\Database\ConnectionInterface; use Laminas\Diactoros\CallbackStream; @@ -41,13 +42,24 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase include __DIR__.'/tmp/config.php' ); + $site->extendWith($this->extenders); + $this->app = $site->bootApp(); - $this->server = $this->app->getRequestHandler(); } return $this->app; } + /** + * @var ExtenderInterface[] + */ + protected $extenders = []; + + protected function extend(ExtenderInterface $extender) + { + $this->extenders[] = $extender; + } + /** * @var RequestHandlerInterface */ From 12ff8dbcc37c81e1dcdb3860f0928ae96714dfbb Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 7 Feb 2020 23:30:07 +0100 Subject: [PATCH 4/4] Start testing Route extender --- .../integration/extenders/RoutesTest.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 framework/core/tests/integration/extenders/RoutesTest.php diff --git a/framework/core/tests/integration/extenders/RoutesTest.php b/framework/core/tests/integration/extenders/RoutesTest.php new file mode 100644 index 000000000..f72569e8c --- /dev/null +++ b/framework/core/tests/integration/extenders/RoutesTest.php @@ -0,0 +1,58 @@ +send( + $this->request('GET', '/custom') + ); + + $this->assertEquals(404, $response->getStatusCode()); + } + + /** + * @test + */ + public function custom_route_can_be_added_by_extender() + { + $this->extend( + (new Extend\Routes('forum')) + ->get('/custom', 'custom', CustomRoute::class) + ); + + $response = $this->send( + $this->request('GET', '/custom') + ); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertEquals('Hello Flarumites!', $response->getBody()); + } +} + +class CustomRoute implements RequestHandlerInterface +{ + public function handle(ServerRequestInterface $request): ResponseInterface + { + return new TextResponse('Hello Flarumites!'); + } +}