Convert last two controller tests to request tests

This commit is contained in:
Franz Liedke 2020-03-27 13:39:38 +01:00
parent 8c6fac62d6
commit 6b3d634917
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
3 changed files with 77 additions and 108 deletions

View File

@ -1,56 +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\Client;
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
use Flarum\Tests\integration\TestCase;
use Flarum\User\Guest;
use Flarum\User\User;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
abstract class ApiControllerTestCase extends TestCase
{
use RetrievesAuthorizedUsers;
/**
* @var RequestHandlerInterface
*/
protected $controller;
/**
* @var null|User
*/
protected $actor = null;
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,
$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);
}
}

View File

@ -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' => '<t><p>lightsail in text</p></t>'],
]);
$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' => '<t><p>lightsail in text</p></t>'],
]);
$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']);
}

View File

@ -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());
}