Convert more controller tests to request tests

This commit is contained in:
Franz Liedke 2020-03-20 18:54:20 +01:00
parent a70acdf9cb
commit b3dc2c7571
3 changed files with 153 additions and 107 deletions

View File

@ -1,89 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\integration\api\Controller;
use Flarum\Api\Controller\CreateDiscussionController;
use Flarum\Discussion\Discussion;
use Flarum\User\User;
use Illuminate\Support\Arr;
class CreateDiscussionControllerTest extends ApiControllerTestCase
{
protected $controller = CreateDiscussionController::class;
protected $data = [
'title' => '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());
}
}

View File

@ -0,0 +1,140 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\integration\api\discussions;
use Flarum\Discussion\Discussion;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Illuminate\Support\Arr;
class CreationTest extends TestCase
{
use RetrievesAuthorizedUsers;
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 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'));
}
}

View File

@ -7,15 +7,15 @@
* LICENSE file that was distributed with this source code. * 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 Carbon\Carbon;
use Flarum\Api\Controller\DeleteDiscussionController; use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\User\User; use Flarum\Tests\integration\TestCase;
class DeleteDiscussionControllerTest extends ApiControllerTestCase class DeletionTest extends TestCase
{ {
protected $controller = DeleteDiscussionController::class; use RetrievesAuthorizedUsers;
public function setUp() public function setUp()
{ {
@ -46,22 +46,17 @@ class DeleteDiscussionControllerTest extends ApiControllerTestCase
*/ */
public function admin_can_delete() public function admin_can_delete()
{ {
$this->actor = User::find(1); $response = $this->send(
$this->request('DELETE', '/api/discussions/1', [
$response = $this->callWith([], ['id' => 1]); 'authenticatedAs' => 1,
'json' => [],
])
);
$this->assertEquals(204, $response->getStatusCode()); $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'); $this->assertNull($this->database()->table('posts')->find(1), 'Post exists in the DB');
} }
} }