added the create discussion test, also renamed some classes that seem to have been incorrectly renamed from the other testing branch

This commit is contained in:
Daniel Klabbers 2018-04-13 09:06:42 +02:00
parent 9438534232
commit 8ae6e0c766
5 changed files with 95 additions and 5 deletions

View File

@ -16,6 +16,7 @@ use Flarum\Foundation\Application;
use Flarum\Http\Controller\ControllerInterface;
use Flarum\User\User;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\ServerRequestFactory;
class Client
@ -46,10 +47,10 @@ class Client
* @param User|null $actor
* @param array $queryParams
* @param array $body
* @return \Psr\Http\Message\ResponseInterface
* @return ResponseInterface
* @throws Exception
*/
public function send($controller, $actor, array $queryParams = [], array $body = [])
public function send($controller, User $actor = null, array $queryParams = [], array $body = []): ResponseInterface
{
$request = ServerRequestFactory::fromGlobals(null, $queryParams, $body);

View File

@ -0,0 +1,85 @@
<?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\CreateDiscussionController;
use Flarum\Discussion\Discussion;
use Flarum\Post\Post;
use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers;
use Illuminate\Support\Arr;
class CreateDiscussionControllerTest extends ApiControllerTestCase
{
use RetrievesAuthorizedUsers;
protected $controller = CreateDiscussionController::class;
protected $data = [
'title' => 'test - too-obscure',
'content' => 'predetermined content for automated testing - too-obscure'
];
/**
* @test
*/
public function can_create_discussion()
{
$this->actor = $this->getAdminUser();
$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'], array_get($data, "data.attributes.title"));
}
/**
* @test
* @expectedException \Illuminate\Validation\ValidationException
* @expectedExceptionMessage The given data was invalid.
*/
public function cannot_create_discussion_without_content()
{
$this->actor = $this->getAdminUser();
$data = Arr::except($this->data, 'content');
$this->callWith($data);
}
/**
* @test
* @expectedException \Illuminate\Validation\ValidationException
* @expectedExceptionMessage The given data was invalid.
*/
public function cannot_create_discussion_without_title()
{
$this->actor = $this->getAdminUser();
$data = Arr::except($this->data, 'title');
$this->callWith($data);
}
public function tearDown()
{
Discussion::where('title', $this->data['title'])->delete();
// Prevent floodgate from kicking in.
Post::where('user_id', $this->getAdminUser()->id)->delete();
parent::tearDown();
}
}

View File

@ -16,7 +16,7 @@ use Flarum\Group\Group;
use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers;
use Illuminate\Support\Str;
class CreateGroupControllerTestTestCase extends ApiControllerTestCase
class CreateGroupControllerTest extends ApiControllerTestCase
{
use RetrievesAuthorizedUsers;
@ -52,11 +52,13 @@ class CreateGroupControllerTestTestCase extends ApiControllerTestCase
$this->assertEquals(201, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true);
$group = Group::where('icon', $this->data['icon'])->firstOrFail();
foreach ($this->data as $property => $value) {
$this->assertEquals($value, array_get($data, "data.attributes.$property"), "$property not matching to json response");
$property = Str::snake($property);
$this->assertEquals($value, $group->{$property});
$this->assertEquals($value, $group->{$property}, "$property not matching to database result");
}
}

View File

@ -17,7 +17,7 @@ use Flarum\Tests\Test\Concerns\RetrievesAuthorizedUsers;
use Flarum\User\User;
use Illuminate\Support\Arr;
class CreateUserControllerTestTestCase extends ApiControllerTestCase
class CreateUserControllerTest extends ApiControllerTestCase
{
use RetrievesAuthorizedUsers;

View File

@ -15,12 +15,14 @@ use Flarum\Api\ApiServiceProvider;
use Flarum\Api\Client;
use Flarum\User\Guest;
use Flarum\User\User;
use Flarum\User\UserServiceProvider;
use Psr\Http\Message\ResponseInterface;
trait MakesApiRequests
{
public function call(string $controller, User $actor = null, array $queryParams = [], array $body = []): ResponseInterface
{
$this->app->register(UserServiceProvider::class);
$this->app->register(ApiServiceProvider::class);
$this->app->make('flarum.api.middleware');
/** @var Client $api */