mirror of
https://github.com/flarum/framework.git
synced 2025-01-19 18:12:59 +08:00
Begin testing API (see #3)
This commit is contained in:
parent
1fa5c7ae35
commit
237591690c
|
@ -151,7 +151,9 @@ abstract class Base extends Controller
|
|||
protected function respondWithDocument($statusCode = 200, $headers = [])
|
||||
{
|
||||
// @todo remove this
|
||||
$this->document->addMeta('pageload', microtime(true) - LARAVEL_START);
|
||||
if (defined('LARAVEL_START')) {
|
||||
$this->document->addMeta('pageload', microtime(true) - LARAVEL_START);
|
||||
}
|
||||
|
||||
Event::fire('flarum.api.willRespondWithDocument', [$this->document]);
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
function action_handler($class)
|
||||
$action = function($class)
|
||||
{
|
||||
return function () use ($class) {
|
||||
$action = \App::make($class);
|
||||
|
@ -8,9 +8,9 @@ function action_handler($class)
|
|||
$parameters = Route::current()->parameters();
|
||||
return $action->handle($request, $parameters);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
Route::group(['prefix' => 'api'], function () {
|
||||
Route::group(['prefix' => 'api'], function () use ($action) {
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
@ -21,31 +21,31 @@ Route::group(['prefix' => 'api'], function () {
|
|||
// List users
|
||||
Route::get('users', [
|
||||
'as' => 'flarum.api.users.index',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Users\Index')
|
||||
'uses' => $action('Flarum\Api\Actions\Users\Index')
|
||||
]);
|
||||
|
||||
// Register a user
|
||||
Route::post('users', [
|
||||
'as' => 'flarum.api.users.create',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Users\Create')
|
||||
'uses' => $action('Flarum\Api\Actions\Users\Create')
|
||||
]);
|
||||
|
||||
// Get a single user
|
||||
Route::get('users/{id}', [
|
||||
'as' => 'flarum.api.users.show',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Users\Show')
|
||||
'uses' => $action('Flarum\Api\Actions\Users\Show')
|
||||
]);
|
||||
|
||||
// Edit a user
|
||||
Route::put('users/{id}', [
|
||||
'as' => 'flarum.api.users.update',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Users\Update')
|
||||
'uses' => $action('Flarum\Api\Actions\Users\Update')
|
||||
]);
|
||||
|
||||
// Delete a user
|
||||
Route::delete('users/{id}', [
|
||||
'as' => 'flarum.api.users.delete',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Users\Delete')
|
||||
'uses' => $action('Flarum\Api\Actions\Users\Delete')
|
||||
]);
|
||||
|
||||
/*
|
||||
|
@ -57,13 +57,13 @@ Route::group(['prefix' => 'api'], function () {
|
|||
// List activity
|
||||
Route::get('activity', [
|
||||
'as' => 'flarum.api.activity.index',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Activity\Index')
|
||||
'uses' => $action('Flarum\Api\Actions\Activity\Index')
|
||||
]);
|
||||
|
||||
// List notifications for the current user
|
||||
Route::get('notifications', [
|
||||
'as' => 'flarum.api.notifications.index',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Notifications\Index')
|
||||
'uses' => $action('Flarum\Api\Actions\Notifications\Index')
|
||||
]);
|
||||
|
||||
/*
|
||||
|
@ -75,31 +75,31 @@ Route::group(['prefix' => 'api'], function () {
|
|||
// List discussions
|
||||
Route::get('discussions', [
|
||||
'as' => 'flarum.api.discussions.index',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Discussions\Index')
|
||||
'uses' => $action('Flarum\Api\Actions\Discussions\Index')
|
||||
]);
|
||||
|
||||
// Create a discussion
|
||||
Route::post('discussions', [
|
||||
'as' => 'flarum.api.discussions.create',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Discussions\Create')
|
||||
'uses' => $action('Flarum\Api\Actions\Discussions\Create')
|
||||
]);
|
||||
|
||||
// Show a single discussion
|
||||
Route::get('discussions/{id}', [
|
||||
'as' => 'flarum.api.discussions.show',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Discussions\Show')
|
||||
'uses' => $action('Flarum\Api\Actions\Discussions\Show')
|
||||
]);
|
||||
|
||||
// Edit a discussion
|
||||
Route::put('discussions/{id}', [
|
||||
'as' => 'flarum.api.discussions.update',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Discussions\Update')
|
||||
'uses' => $action('Flarum\Api\Actions\Discussions\Update')
|
||||
]);
|
||||
|
||||
// Delete a discussion
|
||||
Route::delete('discussions/{id}', [
|
||||
'as' => 'flarum.api.discussions.delete',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Discussions\Delete')
|
||||
'uses' => $action('Flarum\Api\Actions\Discussions\Delete')
|
||||
]);
|
||||
|
||||
/*
|
||||
|
@ -111,32 +111,32 @@ Route::group(['prefix' => 'api'], function () {
|
|||
// List posts, usually for a discussion
|
||||
Route::get('posts', [
|
||||
'as' => 'flarum.api.posts.index',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Posts\Index')
|
||||
'uses' => $action('Flarum\Api\Actions\Posts\Index')
|
||||
]);
|
||||
|
||||
// Create a post
|
||||
// @todo consider 'discussions/{id}/links/posts'?
|
||||
Route::post('posts', [
|
||||
'as' => 'flarum.api.posts.create',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Posts\Create')
|
||||
'uses' => $action('Flarum\Api\Actions\Posts\Create')
|
||||
]);
|
||||
|
||||
// Show a single or multiple posts by ID
|
||||
Route::get('posts/{id}', [
|
||||
'as' => 'flarum.api.posts.show',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Posts\Show')
|
||||
'uses' => $action('Flarum\Api\Actions\Posts\Show')
|
||||
]);
|
||||
|
||||
// Edit a post
|
||||
Route::put('posts/{id}', [
|
||||
'as' => 'flarum.api.posts.update',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Posts\Update')
|
||||
'uses' => $action('Flarum\Api\Actions\Posts\Update')
|
||||
]);
|
||||
|
||||
// Delete a post
|
||||
Route::delete('posts/{id}', [
|
||||
'as' => 'flarum.api.posts.delete',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Posts\Delete')
|
||||
'uses' => $action('Flarum\Api\Actions\Posts\Delete')
|
||||
]);
|
||||
|
||||
/*
|
||||
|
@ -148,31 +148,31 @@ Route::group(['prefix' => 'api'], function () {
|
|||
// List groups
|
||||
Route::get('groups', [
|
||||
'as' => 'flarum.api.groups.index',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Groups\Index')
|
||||
'uses' => $action('Flarum\Api\Actions\Groups\Index')
|
||||
]);
|
||||
|
||||
// Create a group
|
||||
Route::post('groups', [
|
||||
'as' => 'flarum.api.groups.create',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Groups\Create')
|
||||
'uses' => $action('Flarum\Api\Actions\Groups\Create')
|
||||
]);
|
||||
|
||||
// Show a single group
|
||||
Route::get('groups/{id}', [
|
||||
'as' => 'flarum.api.groups.show',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Groups\Show')
|
||||
'uses' => $action('Flarum\Api\Actions\Groups\Show')
|
||||
]);
|
||||
|
||||
// Edit a group
|
||||
Route::put('groups/{id}', [
|
||||
'as' => 'flarum.api.groups.update',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Groups\Update')
|
||||
'uses' => $action('Flarum\Api\Actions\Groups\Update')
|
||||
]);
|
||||
|
||||
// Delete a group
|
||||
Route::delete('groups/{id}', [
|
||||
'as' => 'flarum.api.groups.delete',
|
||||
'uses' => action_handler('Flarum\Api\Actions\Groups\Delete')
|
||||
'uses' => $action('Flarum\Api\Actions\Groups\Delete')
|
||||
]);
|
||||
|
||||
});
|
||||
|
|
10
framework/core/tests/_support/ApiHelper.php
Normal file
10
framework/core/tests/_support/ApiHelper.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
namespace Codeception\Module;
|
||||
|
||||
// here you can define custom actions
|
||||
// all public methods declared in helper class will be available in $I
|
||||
|
||||
class ApiHelper extends \Codeception\Module
|
||||
{
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
<?php //[STAMP] 62377ab49c890e65388dcf2e4aedfd9c
|
||||
<?php //[STAMP] 35004e907fa829d784ccbf5cd2166fcf
|
||||
|
||||
// This class was automatically generated by build task
|
||||
// You should not change it manually as it will be overwritten on next build
|
||||
|
|
3
framework/core/tests/api.suite.yml
Normal file
3
framework/core/tests/api.suite.yml
Normal file
|
@ -0,0 +1,3 @@
|
|||
class_name: ApiTester
|
||||
modules:
|
||||
enabled: [Laravel4, REST, Asserts, ApiHelper]
|
3032
framework/core/tests/api/ApiTester.php
Normal file
3032
framework/core/tests/api/ApiTester.php
Normal file
File diff suppressed because it is too large
Load Diff
41
framework/core/tests/api/DiscussionsResourceCest.php
Normal file
41
framework/core/tests/api/DiscussionsResourceCest.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
use \ApiTester;
|
||||
|
||||
class DiscussionsResourceCest {
|
||||
|
||||
protected $endpoint = '/api/discussions';
|
||||
|
||||
public function getDiscussions(ApiTester $I)
|
||||
{
|
||||
$I->wantTo('get discussions via API');
|
||||
|
||||
$id = $I->haveRecord('discussions', ['title' => 'Game of Thrones', 'last_time' => date('c')]);
|
||||
$id2 = $I->haveRecord('discussions', ['title' => 'Lord of the Rings', 'last_time' => date('c')]);
|
||||
|
||||
$I->sendGET($this->endpoint);
|
||||
$I->seeResponseCodeIs(200);
|
||||
$I->seeResponseIsJson();
|
||||
|
||||
$I->expect('both items are in response');
|
||||
$I->seeResponseContainsJson(['id' => (string) $id, 'title' => 'Game of Thrones']);
|
||||
$I->seeResponseContainsJson(['id' => (string) $id2, 'title' => 'Lord of the Rings']);
|
||||
|
||||
$I->expect('both items are in root discussions array');
|
||||
$I->seeResponseContainsJson(['discussions' => [['id' => (string) $id], ['id' => (string) $id2]]]);
|
||||
}
|
||||
|
||||
public function createDiscussion(ApiTester $I)
|
||||
{
|
||||
$I->wantTo('create a discussion via API');
|
||||
$I->haveHttpHeader('Authorization', 'Token 123456');
|
||||
|
||||
$I->sendPOST($this->endpoint, ['discussions' => ['title' => 'foo', 'content' => 'bar']]);
|
||||
$I->seeResponseCodeIs(200);
|
||||
$I->seeResponseIsJson();
|
||||
$I->seeResponseContainsJson(['title' => 'foo']);
|
||||
$I->seeResponseContainsJson(['type' => 'comment', 'contentHtml' => '<p>bar</p>']);
|
||||
|
||||
$id = $I->grabDataFromJsonResponse('discussions.id');
|
||||
$I->seeRecord('discussions', ['id' => $id, 'title' => 'foo']);
|
||||
}
|
||||
}
|
2
framework/core/tests/api/_bootstrap.php
Normal file
2
framework/core/tests/api/_bootstrap.php
Normal file
|
@ -0,0 +1,2 @@
|
|||
<?php
|
||||
// Here you can initialize variables that will be available to your tests
|
|
@ -1,4 +1,4 @@
|
|||
<?php //[STAMP] 92e3a5639db903a93dbc0ada7934f9ca
|
||||
<?php //[STAMP] 831f2f7900c1df5d29956ba25022e638
|
||||
|
||||
// This class was automatically generated by build task
|
||||
// You should not change it manually as it will be overwritten on next build
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
<?php //[STAMP] 4f61f396dfd2c13f154c1be4d8245c48
|
||||
<?php //[STAMP] 5f044d831fb05d6a3185c10128c6729e
|
||||
|
||||
// This class was automatically generated by build task
|
||||
// You should not change it manually as it will be overwritten on next build
|
||||
|
|
Loading…
Reference in New Issue
Block a user