2018-04-13 13:52:39 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Flarum.
|
|
|
|
*
|
2020-02-05 05:11:08 +08:00
|
|
|
* For detailed copyright and license information, please view the
|
|
|
|
* LICENSE file that was distributed with this source code.
|
2018-04-13 13:52:39 +08:00
|
|
|
*/
|
|
|
|
|
2019-01-02 05:17:09 +08:00
|
|
|
namespace Flarum\Tests\integration\api\Controller;
|
2018-04-13 13:52:39 +08:00
|
|
|
|
2019-01-31 04:15:27 +08:00
|
|
|
use Flarum\Api\Client;
|
2019-01-02 05:17:09 +08:00
|
|
|
use Flarum\Tests\integration\RetrievesAuthorizedUsers;
|
|
|
|
use Flarum\Tests\integration\TestCase;
|
2019-01-31 04:15:27 +08:00
|
|
|
use Flarum\User\Guest;
|
2018-04-13 13:52:39 +08:00
|
|
|
use Flarum\User\User;
|
2018-05-14 19:32:19 +08:00
|
|
|
use Illuminate\Support\Arr;
|
2018-04-13 13:52:39 +08:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2018-05-30 06:12:06 +08:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2018-04-13 13:52:39 +08:00
|
|
|
|
|
|
|
abstract class ApiControllerTestCase extends TestCase
|
|
|
|
{
|
2018-11-09 18:20:48 +08:00
|
|
|
use RetrievesAuthorizedUsers;
|
|
|
|
|
2018-04-13 13:52:39 +08:00
|
|
|
/**
|
2018-05-30 06:12:06 +08:00
|
|
|
* @var RequestHandlerInterface
|
2018-04-13 13:52:39 +08:00
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var null|User
|
|
|
|
*/
|
|
|
|
protected $actor = null;
|
|
|
|
|
2018-05-14 19:32:19 +08:00
|
|
|
protected function callWith(array $body = [], array $queryParams = []): ResponseInterface
|
2018-04-13 13:52:39 +08:00
|
|
|
{
|
2018-05-14 19:32:19 +08:00
|
|
|
if (! Arr::get($body, 'data') && Arr::isAssoc($body)) {
|
|
|
|
$body = ['data' => ['attributes' => $body]];
|
|
|
|
}
|
|
|
|
|
2018-04-13 13:52:39 +08:00
|
|
|
return $this->call(
|
|
|
|
$this->controller,
|
|
|
|
$this->actor,
|
2018-05-14 19:32:19 +08:00
|
|
|
$queryParams,
|
|
|
|
$body
|
2018-04-13 13:52:39 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-31 04:15:27 +08:00
|
|
|
public function call(string $controller, User $actor = null, array $queryParams = [], array $body = []): ResponseInterface
|
2018-04-13 13:52:39 +08:00
|
|
|
{
|
2019-01-31 04:15:27 +08:00
|
|
|
/** @var Client $api */
|
|
|
|
$api = $this->app()->getContainer()->make(Client::class);
|
|
|
|
|
|
|
|
return $api->send($controller, $actor ?? new Guest, $queryParams, $body);
|
2018-04-13 13:52:39 +08:00
|
|
|
}
|
|
|
|
}
|