adds a few additional api controller tests (#1429)

* added CreatePostControllerTest

* added DeleteDiscussionControllerTest

* added ListDiscussionControllerTest

* Apply fixes from StyleCI

[ci skip] [skip ci]
This commit is contained in:
Daniël Klabbers 2018-05-14 13:32:19 +02:00 committed by GitHub
parent 19645785a3
commit 3f429fb08c
5 changed files with 140 additions and 4 deletions

View File

@ -14,6 +14,7 @@ namespace Flarum\Tests\Api\Controller;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\Tests\Test\TestCase;
use Flarum\User\User;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
abstract class ApiControllerTestCase extends TestCase
@ -28,13 +29,17 @@ abstract class ApiControllerTestCase extends TestCase
*/
protected $actor = null;
protected function callWith(array $body = []): ResponseInterface
protected function callWith(array $body = [], array $queryParams = []): ResponseInterface
{
if (! Arr::get($body, 'data') && Arr::isAssoc($body)) {
$body = ['data' => ['attributes' => $body]];
}
return $this->call(
$this->controller,
$this->actor,
[],
$body ? ['data' => ['attributes' => $body]] : []
$queryParams,
$body
);
}

View File

@ -0,0 +1,55 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\Api\Controller;
use Flarum\Api\Controller\CreatePostController;
use Flarum\Discussion\Discussion;
use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers;
use Illuminate\Support\Arr;
class CreatePostControllerTest extends ApiControllerTestCase
{
use RetrievesAuthorizedUsers;
protected $controller = CreatePostController::class;
protected $data = [
'content' => 'reply with predetermined content for automated testing - too-obscure'
];
/**
* @var Discussion
*/
protected $discussion;
protected function init()
{
$this->actor = $this->getNormalUser();
$this->discussion = Discussion::start(__CLASS__, $this->actor);
$this->discussion->save();
}
/**
* @test
*/
public function can_create_reply()
{
$body = [];
Arr::set($body, 'data.attributes', $this->data);
Arr::set($body, 'data.relationships.discussion.data.id', $this->discussion->id);
$response = $this->callWith($body);
$this->assertEquals(201, $response->getStatusCode());
}
}

View File

@ -0,0 +1,43 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\Api\Controller;
use Flarum\Api\Controller\DeleteDiscussionController;
use Flarum\Discussion\Discussion;
use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers;
class DeleteDiscussionControllerTest extends ApiControllerTestCase
{
use RetrievesAuthorizedUsers;
protected $controller = DeleteDiscussionController::class;
protected $discussion;
protected function init()
{
$this->discussion = Discussion::start(__CLASS__, $this->getNormalUser());
$this->discussion->save();
}
/**
* @test
*/
public function admin_can_delete()
{
$this->actor = $this->getAdminUser();
$response = $this->callWith([], ['id' => $this->discussion->id]);
$this->assertEquals(204, $response->getStatusCode());
}
}

View File

@ -0,0 +1,33 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Tests\Api\Controller;
use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Discussion\Discussion;
class ListDiscussionControllerTest extends ApiControllerTestCase
{
protected $controller = ListDiscussionsController::class;
/**
* @test
*/
public function shows_index_for_guest()
{
$response = $this->callWith();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$this->assertEquals(Discussion::count(), count($data['data']));
}
}

View File

@ -28,7 +28,7 @@ trait RetrievesAuthorizedUsers
public function getNormalUser()
{
User::unguarded(function () {
return User::unguarded(function () {
return User::firstOrCreate([
'username' => $this->userAttributes['username']
], $this->userAttributes);