From b10a17529d84dada27333fb07347d228bc3156d9 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 20 Mar 2020 18:54:20 +0100 Subject: [PATCH] Convert more controller tests to request tests --- .../CreateDiscussionControllerTest.php | 89 ----------- .../api/discussions/CreationTest.php | 140 ++++++++++++++++++ .../DeletionTest.php} | 31 ++-- 3 files changed, 153 insertions(+), 107 deletions(-) delete mode 100644 tests/integration/api/Controller/CreateDiscussionControllerTest.php create mode 100644 tests/integration/api/discussions/CreationTest.php rename tests/integration/api/{Controller/DeleteDiscussionControllerTest.php => discussions/DeletionTest.php} (67%) diff --git a/tests/integration/api/Controller/CreateDiscussionControllerTest.php b/tests/integration/api/Controller/CreateDiscussionControllerTest.php deleted file mode 100644 index 1309b6ade..000000000 --- a/tests/integration/api/Controller/CreateDiscussionControllerTest.php +++ /dev/null @@ -1,89 +0,0 @@ - 'test - too-obscure', - 'content' => 'predetermined content for automated testing - too-obscure' - ]; - - public function setUp() - { - parent::setUp(); - - $this->prepareDatabase([ - 'discussions' => [], - 'posts' => [], - 'users' => [ - $this->adminUser(), - ], - 'groups' => [ - $this->adminGroup(), - ], - 'group_user' => [ - ['user_id' => 1, 'group_id' => 1], - ], - ]); - } - - /** - * @test - */ - public function can_create_discussion() - { - $this->actor = User::find(1); - - $response = $this->callWith($this->data); - - $this->assertEquals(201, $response->getStatusCode()); - - /** @var Discussion $discussion */ - $discussion = Discussion::where('title', $this->data['title'])->firstOrFail(); - $data = json_decode($response->getBody()->getContents(), true); - - $this->assertEquals($this->data['title'], $discussion->title); - $this->assertEquals($this->data['title'], Arr::get($data, 'data.attributes.title')); - } - - /** - * @test - */ - public function cannot_create_discussion_without_content() - { - $this->actor = User::find(1); - - $data = Arr::except($this->data, 'content'); - $response = $this->callWith($data); - - $this->assertEquals(422, $response->getStatusCode()); - } - - /** - * @test - */ - public function cannot_create_discussion_without_title() - { - $this->actor = User::find(1); - - $data = Arr::except($this->data, 'title'); - $response = $this->callWith($data); - - $this->assertEquals(422, $response->getStatusCode()); - } -} diff --git a/tests/integration/api/discussions/CreationTest.php b/tests/integration/api/discussions/CreationTest.php new file mode 100644 index 000000000..6ebc26424 --- /dev/null +++ b/tests/integration/api/discussions/CreationTest.php @@ -0,0 +1,140 @@ +prepareDatabase([ + 'discussions' => [], + 'posts' => [], + 'users' => [ + $this->adminUser(), + ], + 'groups' => [ + $this->adminGroup(), + ], + 'group_user' => [ + ['user_id' => 1, 'group_id' => 1], + ], + ]); + } + + /** + * @test + */ + public function cannot_create_discussion_without_content() + { + $response = $this->send( + $this->request('POST', '/api/discussions', [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'title' => 'Test post', + 'content' => '', + ], + ], + ], + ]) + ); + + $this->assertEquals(422, $response->getStatusCode()); + + // The response body should contain details about the failed validation + $body = (string) $response->getBody(); + $this->assertJson($body); + $this->assertEquals([ + 'errors' => [ + [ + 'status' => '422', + 'code' => 'validation_error', + 'detail' => 'validation.required', + 'source' => ['pointer' => '/data/attributes/content'], + ], + ], + ], json_decode($body, true)); + } + + /** + * @test + */ + public function cannot_create_discussion_without_title() + { + $response = $this->send( + $this->request('POST', '/api/discussions', [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'title' => '', + 'content' => 'Test post', + ], + ], + ], + ]) + ); + + $this->assertEquals(422, $response->getStatusCode()); + + // The response body should contain details about the failed validation + $body = (string) $response->getBody(); + $this->assertJson($body); + $this->assertEquals([ + 'errors' => [ + [ + 'status' => '422', + 'code' => 'validation_error', + 'detail' => 'validation.required', + 'source' => ['pointer' => '/data/attributes/title'], + ], + ], + ], json_decode($body, true)); + } + + /** + * @test + */ + public function can_create_discussion() + { + $response = $this->send( + $this->request('POST', '/api/discussions', [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'title' => 'test - too-obscure', + 'content' => 'predetermined content for automated testing - too-obscure', + ], + ] + ], + ]) + ); + + $this->assertEquals(201, $response->getStatusCode()); + + /** @var Discussion $discussion */ + $discussion = Discussion::firstOrFail(); + $data = json_decode($response->getBody()->getContents(), true); + + $this->assertEquals('test - too-obscure', $discussion->title); + $this->assertEquals('test - too-obscure', Arr::get($data, 'data.attributes.title')); + } +} diff --git a/tests/integration/api/Controller/DeleteDiscussionControllerTest.php b/tests/integration/api/discussions/DeletionTest.php similarity index 67% rename from tests/integration/api/Controller/DeleteDiscussionControllerTest.php rename to tests/integration/api/discussions/DeletionTest.php index d2024f50f..31f736240 100644 --- a/tests/integration/api/Controller/DeleteDiscussionControllerTest.php +++ b/tests/integration/api/discussions/DeletionTest.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\DeleteDiscussionController; -use Flarum\User\User; +use Flarum\Tests\integration\RetrievesAuthorizedUsers; +use Flarum\Tests\integration\TestCase; -class DeleteDiscussionControllerTest extends ApiControllerTestCase +class DeletionTest extends TestCase { - protected $controller = DeleteDiscussionController::class; + use RetrievesAuthorizedUsers; public function setUp() { @@ -46,22 +46,17 @@ class DeleteDiscussionControllerTest extends ApiControllerTestCase */ public function admin_can_delete() { - $this->actor = User::find(1); - - $response = $this->callWith([], ['id' => 1]); + $response = $this->send( + $this->request('DELETE', '/api/discussions/1', [ + 'authenticatedAs' => 1, + 'json' => [], + ]) + ); $this->assertEquals(204, $response->getStatusCode()); - } - - /** - * @test - */ - public function deleting_discussions_deletes_their_posts() - { - $this->actor = User::find(1); - - $this->callWith([], ['id' => 1]); + // Ensure both the database and the corresponding post are deleted + $this->assertNull($this->database()->table('discussions')->find(1), 'Discussion exists in the DB'); $this->assertNull($this->database()->table('posts')->find(1), 'Post exists in the DB'); } }