From 20bc1e50fca85fdf8f64a0a25d46fa97a2229cd9 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 27 Mar 2020 13:39:38 +0100 Subject: [PATCH] Convert last two controller tests to request tests --- .../api/Controller/ApiControllerTestCase.php | 56 -------------- .../ListTest.php} | 77 +++++++++++-------- .../ShowTest.php} | 52 +++++++------ 3 files changed, 77 insertions(+), 108 deletions(-) delete mode 100644 framework/core/tests/integration/api/Controller/ApiControllerTestCase.php rename framework/core/tests/integration/api/{Controller/ListDiscussionsControllerTest.php => discussions/ListTest.php} (72%) rename framework/core/tests/integration/api/{Controller/ShowDiscussionControllerTest.php => discussions/ShowTest.php} (79%) diff --git a/framework/core/tests/integration/api/Controller/ApiControllerTestCase.php b/framework/core/tests/integration/api/Controller/ApiControllerTestCase.php deleted file mode 100644 index 958ae21b3..000000000 --- a/framework/core/tests/integration/api/Controller/ApiControllerTestCase.php +++ /dev/null @@ -1,56 +0,0 @@ - ['attributes' => $body]]; - } - - return $this->call( - $this->controller, - $this->actor, - $queryParams, - $body - ); - } - - public function call(string $controller, User $actor = null, array $queryParams = [], array $body = []): ResponseInterface - { - /** @var Client $api */ - $api = $this->app()->getContainer()->make(Client::class); - - return $api->send($controller, $actor ?? new Guest, $queryParams, $body); - } -} diff --git a/framework/core/tests/integration/api/Controller/ListDiscussionsControllerTest.php b/framework/core/tests/integration/api/discussions/ListTest.php similarity index 72% rename from framework/core/tests/integration/api/Controller/ListDiscussionsControllerTest.php rename to framework/core/tests/integration/api/discussions/ListTest.php index 0a04947f6..2cb2c0be4 100644 --- a/framework/core/tests/integration/api/Controller/ListDiscussionsControllerTest.php +++ b/framework/core/tests/integration/api/discussions/ListTest.php @@ -7,15 +7,15 @@ * LICENSE file that was distributed with this source code. */ -namespace Flarum\Tests\integration\api\Controller; +namespace Flarum\Tests\integration\api\discussions; use Carbon\Carbon; -use Flarum\Api\Controller\ListDiscussionsController; -use Flarum\User\User; +use Flarum\Tests\integration\RetrievesAuthorizedUsers; +use Flarum\Tests\integration\TestCase; -class ListDiscussionsControllerTest extends ApiControllerTestCase +class ListTest extends TestCase { - protected $controller = ListDiscussionsController::class; + use RetrievesAuthorizedUsers; public function setUp() { @@ -46,7 +46,9 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase */ public function shows_index_for_guest() { - $response = $this->callWith(); + $response = $this->send( + $this->request('GET', '/api/discussions') + ); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($response->getBody()->getContents(), true); @@ -59,14 +61,13 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase */ public function can_search_for_author() { - $user = User::find(2); - - $response = $this->callWith([], [ - 'filter' => [ - 'q' => 'author:'.$user->username.' foo' - ], - 'include' => 'mostRelevantPost' - ]); + $response = $this->send( + $this->request('GET', '/api/discussions') + ->withQueryParams([ + 'filter' => ['q' => 'author:normal foo'], + 'include' => 'mostRelevantPost', + ]) + ); $this->assertEquals(200, $response->getStatusCode()); } @@ -86,10 +87,14 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase ['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

lightsail in text

'], ]); - $response = $this->callWith([], [ - 'filter' => ['q' => 'lightsail'], - 'include' => 'mostRelevantPost' - ]); + $response = $this->send( + $this->request('GET', '/api/discussions') + ->withQueryParams([ + 'filter' => ['q' => 'lightsail'], + 'include' => 'mostRelevantPost', + ]) + ); + $data = json_decode($response->getBody()->getContents(), true); $ids = array_map(function ($row) { return $row['id']; @@ -114,10 +119,14 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase ['id' => 3, 'discussion_id' => 3, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '

lightsail in text

'], ]); - $response = $this->callWith([], [ - 'filter' => ['q' => 'lightsail+'], - 'include' => 'mostRelevantPost' - ]); + $response = $this->send( + $this->request('GET', '/api/discussions') + ->withQueryParams([ + 'filter' => ['q' => 'lightsail+'], + 'include' => 'mostRelevantPost', + ]) + ); + $data = json_decode($response->getBody()->getContents(), true); $ids = array_map(function ($row) { return $row['id']; @@ -132,17 +141,25 @@ class ListDiscussionsControllerTest extends ApiControllerTestCase */ public function search_for_special_characters_gives_empty_result() { - $response = $this->callWith([], [ - 'filter' => ['q' => '*'], - 'include' => 'mostRelevantPost' - ]); + $response = $this->send( + $this->request('GET', '/api/discussions') + ->withQueryParams([ + 'filter' => ['q' => '*'], + 'include' => 'mostRelevantPost', + ]) + ); + $data = json_decode($response->getBody()->getContents(), true); $this->assertEquals([], $data['data']); - $response = $this->callWith([], [ - 'filter' => ['q' => '@'], - 'include' => 'mostRelevantPost' - ]); + $response = $this->send( + $this->request('GET', '/api/discussions') + ->withQueryParams([ + 'filter' => ['q' => '@'], + 'include' => 'mostRelevantPost', + ]) + ); + $data = json_decode($response->getBody()->getContents(), true); $this->assertEquals([], $data['data']); } diff --git a/framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php b/framework/core/tests/integration/api/discussions/ShowTest.php similarity index 79% rename from framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php rename to framework/core/tests/integration/api/discussions/ShowTest.php index 92a7b859a..cb683dce0 100644 --- a/framework/core/tests/integration/api/Controller/ShowDiscussionControllerTest.php +++ b/framework/core/tests/integration/api/discussions/ShowTest.php @@ -7,24 +7,18 @@ * LICENSE file that was distributed with this source code. */ -namespace Flarum\Tests\integration\api\Controller; +namespace Flarum\Tests\integration\api\discussions; use Carbon\Carbon; -use Flarum\Api\Controller\ShowDiscussionController; -use Flarum\Discussion\Discussion; use Flarum\Event\ScopeModelVisibility; -use Flarum\User\User; +use Flarum\Tests\integration\RetrievesAuthorizedUsers; +use Flarum\Tests\integration\TestCase; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Arr; -class ShowDiscussionControllerTest extends ApiControllerTestCase +class ShowTest extends TestCase { - protected $controller = ShowDiscussionController::class; - - /** - * @var Discussion - */ - protected $discussion; + use RetrievesAuthorizedUsers; public function setUp() { @@ -63,9 +57,11 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase */ public function author_can_see_discussion() { - $this->actor = User::find(2); - - $response = $this->callWith([], ['id' => 1]); + $response = $this->send( + $this->request('GET', '/api/discussions/1', [ + 'authenticatedAs' => 2, + ]) + ); $this->assertEquals(200, $response->getStatusCode()); } @@ -75,7 +71,9 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase */ public function guest_cannot_see_empty_discussion() { - $response = $this->callWith([], ['id' => 1]); + $response = $this->send( + $this->request('GET', '/api/discussions/1') + ); $this->assertEquals(404, $response->getStatusCode()); } @@ -85,7 +83,9 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase */ public function guest_cannot_see_hidden_posts() { - $response = $this->callWith([], ['id' => 4]); + $response = $this->send( + $this->request('GET', '/api/discussions/4') + ); $json = json_decode($response->getBody()->getContents(), true); @@ -97,9 +97,11 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase */ public function author_can_see_hidden_posts() { - $this->actor = User::find(2); - - $response = $this->callWith([], ['id' => 4]); + $response = $this->send( + $this->request('GET', '/api/discussions/4', [ + 'authenticatedAs' => 2, + ]) + ); $json = json_decode($response->getBody()->getContents(), true); @@ -120,7 +122,9 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase } }); - $response = $this->callWith([], ['id' => 4]); + $response = $this->send( + $this->request('GET', '/api/discussions/4') + ); $json = json_decode($response->getBody()->getContents(), true); @@ -132,7 +136,9 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase */ public function guest_can_see_discussion() { - $response = $this->callWith([], ['id' => 2]); + $response = $this->send( + $this->request('GET', '/api/discussions/2') + ); $this->assertEquals(200, $response->getStatusCode()); } @@ -142,7 +148,9 @@ class ShowDiscussionControllerTest extends ApiControllerTestCase */ public function guests_cannot_see_private_discussion() { - $response = $this->callWith([], ['id' => 3]); + $response = $this->send( + $this->request('GET', '/api/discussions/3') + ); $this->assertEquals(404, $response->getStatusCode()); }