API updates and additions

Planned API endpoints and created placeholder classes for their
controllers. Updated unit tests to test existing API code.
This commit is contained in:
kiswa 2016-04-22 17:09:02 +00:00
parent 853edbb61d
commit 3b48bd03bc
16 changed files with 187 additions and 16 deletions

View File

@ -147,7 +147,7 @@ gulp.task('coverage', ['tsc', 'vendor'], () => {
gulp.task('test-api', () => {
return gulp.src('')
.pipe(phpunit('./src/api/vendor/phpunit/phpunit/phpunit test/api/*Test.php'));
.pipe(phpunit('./src/api/vendor/phpunit/phpunit/phpunit test/api/'));
});
gulp.task('watch', () => {

View File

@ -0,0 +1,6 @@
<?php
use RedBeanPHP\R;
class Attachments extends BaseController {
}

View File

@ -0,0 +1,6 @@
<?php
use RedBeanPHP\R;
class AutoActions extends BaseController {
}

View File

@ -2,10 +2,12 @@
abstract class BaseController {
protected $apiJson;
protected $logger;
protected $container;
public function __construct($container) {
$this->apiJson = new ApiJson();
$this->logger = $container->get('logger');
$this->container = $container;
}
public function jsonResponse($response) {

View File

@ -20,5 +20,32 @@ class Boards extends BaseController {
return $this->jsonResponse($response);
}
public function getBoard($request, $response, $args) {
$board = new Board($this->container, (int)$args['id']);
if ($board->id === 0) {
$this->logger->addError('Attempt to load board ' . $args['id'] .
' failed.');
$this->apiJson->addAlert('error', 'No board found for ID ' .
$args['id'] . '.');
return $this->jsonResponse($response);
}
$this->apiJson->setSuccess();
$this->apiJson->addData($board);
return $this->jsonResponse($response);
}
public function addBoard($request, $response, $args) {
}
public function updateBoard($request, $response, $args) {
}
public function removeBoard($request, $response, $args) {
}
}

View File

@ -0,0 +1,6 @@
<?php
use RedBeanPHP\R;
class Columns extends BaseController {
}

View File

@ -0,0 +1,6 @@
<?php
use RedBeanPHP\R;
class Comments extends BaseController {
}

View File

@ -0,0 +1,6 @@
<?php
use RedBeanPHP\R;
class Users extends BaseController {
}

View File

@ -10,7 +10,45 @@ require 'app-setup.php';
$app->get('/', 'Invalid:noApi');
$app->get('/boards', 'Boards:getAllBoards');
$app->get('/boards/{id}', 'Boards:getBoard');
$app->post('/boards', 'Boards:addBoard');
$app->post('/boards/{id}', 'Boards:updateBoard');
$app->delete('/boards/{id}', 'Boards:removeBoard');
/*
$app->get('/autoactions', 'AutoActions:getAllActions');
$app->post('/autoactions', 'AutoActions:addAction');
$app->delete('/autoactions/{id}', 'AutoActions:removeAction');
$app->get('/columns/{id}', 'Columns:getColumn');
$app->post('/columns', 'Columns:addColumn');
$app->post('/columns/{id}', 'Columns:updateColumn');
$app->delete('/columns/{id}', 'Columns:removeColumn');
$app->get('/items/{id}', 'Items:getItem');
$app->post('/items', 'Items:addItem');
$app->post('/items/{id}', 'Items:updateItem');
$app->delete('/items/{id}', 'Items:removeItem');
$app->get('/comments/{id}', 'Comments:getComment');
$app->post('/comments', 'Comments:addComment');
$app->post('/comments/{id}', 'Comments:updateComment');
$app->delete('/comments/{id}', 'Comments:removeComment');
$app->get('/attachments/{id}', 'Attachments:getAttachment');
$app->post('/attachments', 'Attachments:addAttachment');
$app->post('/attachments/{id}', 'Attachments:updateAttachment');
$app->delete('/attachments/{id}', 'Attachments:removeAttachment');
$app->get('/users', 'Users:getAllUsers');
$app->get('/users/{id}', 'Users:getUser');
$app->post('/users', 'Users:addUser');
$app->post('/users/{id}', 'Users:updateUser');
$app->delete('/users/{id}', 'Users:removeUser');
$app->post('/authenticate', 'Users:authenticate');
$app->post('/users/login', 'Users:login');
$app->post('/users/logout', 'Users:logout');
*/
$app->run();
R::close();

View File

@ -5,10 +5,7 @@ abstract class BaseModel {
protected $logger;
protected $bean;
public function __construct($type, $id) {
global $app;
$container = $app->getContainer();
public function __construct($type, $id, $container) {
$this->logger = $container->get('logger');
$this->bean = R::load($type, $id);
}

View File

@ -1,8 +1,12 @@
<?php
class Board extends BaseModel {
public $id = 0;
public $name = '';
public $is_active = true;
public $columns = [];
public function __construct($id = 0, $internal = false) {
parent::__construct('board', $id);
public function __construct($container, $id = 0, $internal = false) {
parent::__construct('board', $id, $container);
if ($internal) {
return;
@ -11,13 +15,17 @@ class Board extends BaseModel {
$this->loadFromBean($this->bean);
}
public static function fromBean($bean) {
public static function fromBean($container, $bean) {
$instance = new self($container, 0, true);
$instance->loadFromBean($bean);
return $instance;
}
public function updateBean() {
}
public function loadFromBean() {
public function loadFromBean($bean) {
}
}

View File

@ -2,7 +2,7 @@
<html lang="en">
<head>
<title>TaskBoard</title>
<base href="/web/TaskBoard/dist/">
<base href="">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
@ -26,7 +26,9 @@
</script>
<div class="container">
<app-component>Loading...</app-component>
<app-component>
<div class="loading">TaskBoard is Loading...</div>
</app-component>
</div>
</body>
</html>

View File

@ -18,3 +18,7 @@ h6 {
margin: 0;
}
.loading {
padding-top: 2em;
text-align: center;
}

37
test/api/BoardTest.php Normal file
View File

@ -0,0 +1,37 @@
<?php
require_once 'Mocks.php';
class BoardTest extends PHPUnit_Framework_TestCase {
public static function setupBeforeClass() {
try {
RedBeanPHP\R::setup('sqlite:tests.db');
} catch (Exception $ex) { }
}
public static function tearDownAfterClass() {
if (file_exists('tests.db')) {
unlink('tests.db');
}
}
public function testCreateNewBoard() {
$board = new Board(new ContainerMock());
$this->assertTrue($board->id === 0);
$this->assertTrue($board->name === '');
$this->assertTrue($board->is_active === true);
$this->assertArraySubset($board->columns, []);
}
public function testCreateFromBean() {
$board = Board::fromBean(new ContainerMock(), null);
$this->assertTrue($board->id === 0);
$this->assertTrue($board->name === '');
$this->assertTrue($board->is_active === true);
$this->assertArraySubset($board->columns, []);
}
}

View File

@ -1,18 +1,22 @@
<?php
require 'Mocks.php';
require_once 'Mocks.php';
class BoardsTest extends PHPUnit_Framework_TestCase {
public static function setupBeforeClass() {
try {
RedBeanPHP\R::setup('sqlite:tests.db');
} catch (Exception $ex) { }
}
public static function tearDownAfterClass() {
if (file_exists('tests.db')) {
unlink('tests.db');
}
}
public function testGetAllBoards() {
$boards = new Boards(new ControllerMock());
$boards = new Boards(new ContainerMock());
$expected = new ApiJson();
$expected->addAlert('info', 'No boards in database.');
@ -21,5 +25,18 @@ class BoardsTest extends PHPUnit_Framework_TestCase {
$boards->getAllBoards(null, new ResponseMock(), null));
}
public function testGetBoard() {
$boards = new Boards(new ContainerMock());
$expected = new ApiJson();
$expected->addAlert('error', 'No board found for ID 1.');
$args = [];
$args['id'] = '1';
$this->assertEquals($expected,
$boards->getBoard(null, new ResponseMock(), $args));
}
}

View File

@ -1,5 +1,14 @@
<?php
class AppMock {
public function getContainer() {
return new ContainerMock();
}
}
$app = new AppMock();
class LoggerMock {
public function addInfo() {
@ -10,7 +19,7 @@ class LoggerMock {
}
class ControllerMock {
class ContainerMock {
public function get() {
return new LoggerMock();