diff --git a/tests/integration/TestCase.php b/tests/integration/TestCase.php index c6928812e..687023dbe 100644 --- a/tests/integration/TestCase.php +++ b/tests/integration/TestCase.php @@ -10,33 +10,22 @@ namespace Flarum\Tests\integration; use Dflydev\FigCookies\SetCookie; +use Flarum\Extend\ExtenderInterface; use Flarum\Foundation\InstalledSite; use Illuminate\Database\ConnectionInterface; 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 { - public function setUp() - { - parent::setUp(); - - // Boot the Flarum app - $this->app(); - } - /** * @var \Flarum\Foundation\InstalledApp */ protected $app; - /** - * @var \Psr\Http\Server\RequestHandlerInterface - */ - protected $server; - /** * @return \Flarum\Foundation\InstalledApp */ @@ -53,13 +42,38 @@ 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 + */ + 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 @@ -111,7 +125,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase */ protected function send(ServerRequestInterface $request): ResponseInterface { - return $this->server->handle($request); + return $this->server()->handle($request); } /** diff --git a/tests/integration/extenders/RoutesTest.php b/tests/integration/extenders/RoutesTest.php new file mode 100644 index 000000000..f72569e8c --- /dev/null +++ b/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!'); + } +}