API tests at 100% and organized

This commit is contained in:
kiswa 2016-05-06 00:16:20 +00:00
parent 132f6374b2
commit 49b2ae5cf1
20 changed files with 185 additions and 89 deletions

View File

@ -40,9 +40,8 @@ class Boards extends BaseController {
public function addBoard($request, $response, $args) {
$board = Board::fromJson($this->container, $request->getBody());
$board->save();
if ($board->id === 0) {
if (!$board->save()) {
$this->logger->addError('Add Board: ', [$board]);
$this->apiJson->addAlert('error', 'Error adding board. ' .
'Please check your entries and try again.');
@ -63,7 +62,7 @@ class Boards extends BaseController {
public function updateBoard($request, $response, $args) {
$board = new Board($this->container, (int)$args['id']);
$update = Board::fromJson($request->getBody());
$update = Board::fromJson($this->container, $request->getBody());
if ($board->id !== $update->id) {
$this->logger->addError('Update Board: ', [$board, $update]);

View File

@ -16,14 +16,16 @@ class ActionType extends Enum {
class AutoAction extends BaseModel {
public $id = 0;
public $trigger = ActionTrigger::MoveToColumn;
public $trigger_id = 0; // ID of the column etc. which triggers the action
public $type = ActionType::SetColor;
public $trigger;
public $source_id = 0; // ID of the column etc. which triggers the action
public $type;
public $change_to = ''; // Whatever the target of the action changes to
public function __construct($container, $id = 0) {
parent::__construct('auto_action', $id, $container);
$this->trigger = new ActionTrigger(ActionTrigger::MoveToColumn);
$this->type = new ActionType(ActionType::SetColor);
$this->loadFromBean($this->bean);
}
@ -31,9 +33,9 @@ class AutoAction extends BaseModel {
$bean = $this->bean;
$bean->id = $this->id;
$bean->trigger = $this->trigger;
$bean->trigger_id = $this->trigger_id;
$bean->type = $this->type;
$bean->trigger = $this->trigger->getValue();
$bean->source_id = $this->source_id;
$bean->type = $this->type->getValue();
$bean->change_to = $this->change_to;
}
@ -58,7 +60,7 @@ class AutoAction extends BaseModel {
private function loadPropertiesFrom($obj) {
$this->id = (int) $obj->id;
$this->trigger = new ActionTrigger((int) $obj->trigger);
$this->trigger_id = (int) $obj->trigger_id;
$this->source_id = (int) $obj->source_id;
$this->type = new ActionType((int) $obj->type);
$this->change_to = $obj->change_to;
}

View File

@ -35,7 +35,11 @@ abstract class BaseModel {
$ex->getMessage(),
$ex->getTrace()
]);
return false;
}
return true;
}
public function delete() {

View File

@ -1,5 +1,4 @@
<?php
class DbLogger {
public static function logChange($container, $user_id, $log_text, $before,
$after, $item_type, $item_id) {

View File

@ -1,60 +0,0 @@
<?php
require_once 'Mocks.php';
class BoardsTest extends PHPUnit_Framework_TestCase {
private $boards;
public static function setupBeforeClass() {
try {
RedBeanPHP\R::setup('sqlite:tests.db');
} catch (Exception $ex) { }
}
public function setUp() {
RedBeanPHP\R::nuke();
$this->boards = new Boards(new ContainerMock());
}
public function testGetAllBoards() {
$expected = new ApiJson();
$expected->addAlert('info', 'No boards in database.');
$this->assertEquals($expected,
$this->boards->getAllBoards(null, new ResponseMock(), null));
}
public function testGetBoard() {
$expected = new ApiJson();
$expected->addAlert('error', 'No board found for ID 1.');
$args = [];
$args['id'] = '1';
$this->assertEquals($expected,
$this->boards->getBoard(null, new ResponseMock(), $args));
}
public function testAddRemoveBoard() {
$expected = new ApiJson();
$actual = $this->boards->addBoard(new RequestMock(),
new ResponseMock(), null);
$expected->setSuccess();
$expected->addAlert('success', 'Board test added.');
$this->assertEquals($expected, $actual);
$expected->addAlert('success', 'Board test removed.');
$args = [];
$args['id'] = '1';
$actual = $this->boards->removeBoard(null, new ResponseMock(), $args);
$this->assertEquals($expected, $actual);
}
}

View File

@ -46,7 +46,7 @@ class DataMock {
$auto_action = new stdClass();
$auto_action->id = 1;
$auto_action->trigger = ActionTrigger::SetToCategory;
$auto_action->trigger_id = 1;
$auto_action->source_id = 1;
$auto_action->type = ActionType::ClearDueDate;
$auto_action->change_to = 'null';
@ -159,13 +159,26 @@ class ContainerMock {
}
class RequestMock {
public $board;
public function getBody() {
if ($this->board) {
return json_encode($this->board);
}
return json_encode(DataMock::getBoard());
}
}
class BadRequestMock {
public function getBody() {
return '{}';
}
}
class ResponseMock {
public function withJson($apiJson) {

View File

@ -0,0 +1,116 @@
<?php
require_once __DIR__ . '/../Mocks.php';
class BoardsTest extends PHPUnit_Framework_TestCase {
private $boards;
public static function setupBeforeClass() {
try {
RedBeanPHP\R::setup('sqlite:tests.db');
} catch (Exception $ex) { }
}
public function setUp() {
RedBeanPHP\R::nuke();
$this->boards = new Boards(new ContainerMock());
}
public function testGetAllBoards() {
$expected = new ApiJson();
$expected->addAlert('info', 'No boards in database.');
$actual = $this->boards->getAllBoards(null, new ResponseMock(), null);
$this->assertEquals($expected, $actual);
$this->createBoard();
$boards = $this->boards->getAllBoards(null, new ResponseMock, null);
$this->assertTrue(count($boards->data) === 1);
$this->assertTrue($boards->status === 'success');
}
public function testGetBoard() {
$expected = new ApiJson();
$expected->addAlert('error', 'No board found for ID 1.');
$args = [];
$args['id'] = '1';
$actual = $this->boards->getBoard(null, new ResponseMock(), $args);
$this->assertEquals($expected, $actual);
$this->createBoard();
$actual = $this->boards->getBoard(null, new ResponseMock(), $args);
$this->assertTrue($actual->status === 'success');
$this->assertTrue(count($actual->data) === 1);
}
public function testAddRemoveBoard() {
$expected = new ApiJson();
$actual = $this->createBoard();
$expected->setSuccess();
$expected->addAlert('success', 'Board test added.');
$this->assertEquals($expected, $actual);
$expected->addAlert('success', 'Board test removed.');
$args = [];
$args['id'] = '1';
$actual = $this->boards->removeBoard(null, new ResponseMock(), $args);
$this->assertEquals($expected, $actual);
}
public function testAddBadBoard() {
$response = $this->boards->addBoard(new BadRequestMock(),
new ResponseMock(), null);
$this->assertTrue($response->status === 'failure');
$this->assertTrue($response->alerts[0]['type'] === 'error');
}
public function testRemoveBadBoard() {
$args = [];
$args['id'] = 5; // No such board
$response =
$this->boards->removeBoard(null, new ResponseMock(), $args);
$this->assertTrue($response->status === 'failure');
}
public function testUpdateBoard() {
$this->createBoard();
$board = DataMock::getBoard();
$board->is_active = false;
$args = [];
$args['id'] = $board->id;
$request = new RequestMock();
$request->board = $board;
$response = $this->boards->updateBoard($request,
new ResponseMock(), $args);
$this->assertTrue($response->status === 'success');
$request->board = new stdClass();
$response = $this->boards->updateBoard($request,
new ResponseMock(), $args);
$this->assertTrue($response->alerts[2]['type'] === 'error');
}
private function createBoard() {
$response = $this->boards->addBoard(new RequestMock(),
new ResponseMock(), DataMock::getBoard());
$this->assertTrue($response->status === 'success');
return $response;
}
}

View File

@ -0,0 +1,22 @@
<?php
require_once __DIR__ . '/../Mocks.php';
class InvalidTest extends PHPUnit_Framework_TestCase {
public function testCreateInvalid() {
$invalid = new Invalid(new ContainerMock());
$expected = new ApiJson();
$data = new stdClass();
$data->status = 'One of "success" or "failure".';
$data->data = 'An array of data (JSON objects and/or arrays).';
$data->alerts = 'An array of alerts, with "type" of "success", "error", "warn", or "info" and a "text" message.';
$expected->addAlert('error',
'No API functionality at this endpoint.');
$expected->addData($data);
$actual = $invalid->noApi(new RequestMock(), new ResponseMock, null);
$this->assertEquals($expected, $actual);
}
}

View File

@ -1,5 +1,5 @@
<?php
require_once 'Mocks.php';
require_once __DIR__ . '/../Mocks.php';
class ActivityTest extends PHPUnit_Framework_TestCase {
private $json = '';

View File

@ -1,5 +1,5 @@
<?php
require_once 'Mocks.php';
require_once __DIR__ . '/../Mocks.php';
class AttachmentTest extends PHPUnit_Framework_TestCase {
private $json = '';

View File

@ -1,5 +1,5 @@
<?php
require_once 'Mocks.php';
require_once __DIR__ . '/../Mocks.php';
class AutoActionTest extends PHPUnit_Framework_TestCase {
private $json = '';
@ -53,9 +53,9 @@ class AutoActionTest extends PHPUnit_Framework_TestCase {
$bean = $action->getBean();
$this->assertTrue($bean->id === $action->id);
$this->assertTrue($bean->trigger === $action->trigger);
$this->assertTrue($bean->trigger_id === $action->trigger_id);
$this->assertTrue($bean->type === $action->type);
$this->assertTrue($bean->trigger === $action->trigger->getValue());
$this->assertTrue($bean->source_id === $action->source_id);
$this->assertTrue($bean->type === $action->type->getValue());
$this->assertTrue($bean->change_to === $action->change_to);
}
@ -66,7 +66,7 @@ class AutoActionTest extends PHPUnit_Framework_TestCase {
$this->assertTrue($attachment->id === 1);
$this->assertTrue($attachment->trigger->getValue() ===
$trigger->getValue());
$this->assertTrue($attachment->trigger_id === 1);
$this->assertTrue($attachment->source_id === 1);
$this->assertTrue($attachment->type->getValue() ===
$type->getValue());
$this->assertTrue($attachment->change_to === 'null');
@ -74,10 +74,11 @@ class AutoActionTest extends PHPUnit_Framework_TestCase {
private function assertDefaultProperties($attachment) {
$this->assertTrue($attachment->id === 0);
$this->assertTrue($attachment->trigger ==
$this->assertTrue($attachment->trigger->getValue() ===
ActionTrigger::MoveToColumn);
$this->assertTrue($attachment->trigger_id === 0);
$this->assertTrue($attachment->type == ActionType::SetColor);
$this->assertTrue($attachment->source_id === 0);
$this->assertTrue($attachment->type->getValue() ===
ActionType::SetColor);
$this->assertTrue($attachment->change_to === '');
}

View File

@ -1,5 +1,5 @@
<?php
require_once 'Mocks.php';
require_once __DIR__ . '/../Mocks.php';
class BoardTest extends PHPUnit_Framework_TestCase {
private $json = '';

View File

@ -1,5 +1,5 @@
<?php
require_once 'Mocks.php';
require_once __DIR__ . '/../Mocks.php';
class CategoryTest extends PHPUnit_Framework_TestCase {
private $json = '';

View File

@ -1,5 +1,5 @@
<?php
require_once 'Mocks.php';
require_once __DIR__ . '/../Mocks.php';
class ColumnTest extends PHPUnit_Framework_TestCase {
private $json = '';

View File

@ -1,5 +1,5 @@
<?php
require_once 'Mocks.php';
require_once __DIR__ . '/../Mocks.php';
class CommentTest extends PHPUnit_Framework_TestCase {
private $json = '';

View File

@ -1,5 +1,5 @@
<?php
require_once 'Mocks.php';
require_once __DIR__ . '/../Mocks.php';
class TaskTest extends PHPUnit_Framework_TestCase {
private $json = '';

View File

@ -1,5 +1,5 @@
<?php
require_once 'Mocks.php';
require_once __DIR__ . '/../Mocks.php';
class UserOptionsTest extends PHPUnit_Framework_TestCase {
private $json = '';

View File

@ -1,5 +1,5 @@
<?php
require_once 'Mocks.php';
require_once __DIR__ . '/../Mocks.php';
class UserTest extends PHPUnit_Framework_TestCase {
private $json = '';

BIN
tests.db

Binary file not shown.