API unit tests and updates

This commit is contained in:
Matthew Ross 2016-05-13 06:29:13 -04:00
parent 67e25d707e
commit 278adffcd6
3 changed files with 123 additions and 8 deletions

View File

@ -35,7 +35,7 @@ class Tasks extends BaseController {
// TODO: Get existing user to log user_id and name
$this->dbLogger->logChange($this->container, 0,
'$user->name added task ' . $task.name . '.',
'$user->name added task ' . $task->title . '.',
'', json_encode($task), 'task', $task->id);
$this->apiJson->setSuccess();

View File

@ -13,7 +13,7 @@ class Task extends BaseModel {
public $comments = []; // Comment model array
public function __construct($container, $id = 0) {
parent::__construct('column', $id, $container);
parent::__construct('task', $id, $container);
$this->loadFromBean($this->bean);
}
@ -36,12 +36,12 @@ class Task extends BaseModel {
foreach($this->attachments as $attachment) {
$attachment->updateBean();
$this->xownAttachmentList[] = $attachment->bean;
$bean->xownAttachmentList[] = $attachment->bean;
}
foreach($this->comments as $comment) {
$comment->updateBean();
$this->xownCommentList[] = $comment->bean;
$bean->xownCommentList[] = $comment->bean;
}
}
@ -106,15 +106,15 @@ class Task extends BaseModel {
private function loadPropertiesFrom($obj) {
try {
$this->id = $obj->id;
$this->id = (int)$obj->id;
$this->title = $obj->title;
$this->description = $obj->description;
$this->assignee_id = $obj->assignee_id;
$this->category_id = $obj->category_id;
$this->assignee_id = (int)$obj->assignee_id;
$this->category_id = (int)$obj->category_id;
$this->color = $obj->color;
$this->due_date = $obj->due_date;
$this->points = $obj->points;
$this->position = $obj->position;
$this->position = (int)$obj->position;
} catch (Exception $ex) {
$this->is_valid = false;
}

View File

@ -0,0 +1,115 @@
<?php
require_once __DIR__ . '/../Mocks.php';
class TasksTest extends PHPUnit_Framework_TestCase {
private $tasks;
public static function setupBeforeClass() {
try {
RedBeanPHP\R::setup('sqlite:tests.db');
// RedBeanPHP\R::fancyDebug(true);
} catch (Exception $ex) { }
}
public function setUp() {
RedBeanPHP\R::nuke();
$this->tasks = new Tasks(new ContainerMock());
}
public function testGetTask() {
$expected = new ApiJson();
$expected->addAlert('error', 'No task found for ID 1.');
$args = [];
$args['id'] = 1;
$actual = $this->tasks->getTask(null,
new ResponseMock(), $args);
$this->assertEquals($expected, $actual);
$this->createTask();
$actual = $this->tasks->getTask(null,
new ResponseMock(), $args);
$this->assertTrue($actual->status === 'success');
$this->assertTrue(count($actual->data) === 1);
}
public function testAddRemoveTask() {
$expected = new ApiJson();
$actual = $this->createTask();
$expected->setSuccess();
$expected->addAlert('success', 'Task test added.');
$this->assertEquals($expected, $actual);
$expected->addAlert('success', 'Task test removed.');
$args = [];
$args['id'] = 1;
$actual = $this->tasks->removeTask(null,
new ResponseMock(), $args);
$this->assertEquals($expected, $actual);
}
public function testAddBadTask() {
$request = new RequestMock();
$request->invalidPayload = true;
$response = $this->tasks->addTask($request,
new ResponseMock(), null);
$this->assertTrue($response->status === 'failure');
$this->assertTrue($response->alerts[0]['type'] === 'error');
}
public function testRemoveBadTask() {
$args = [];
$args['id'] = 5; // No such task
$response = $this->tasks->removeTask(null,
new ResponseMock(), $args);
$this->assertTrue($response->status === 'failure');
}
public function testUpdateTask() {
$this->createTask();
$task = DataMock::getTask();
$task->title = 'updated';
$args = [];
$args['id'] = $task->id;
$request = new RequestMock();
$request->payload = $task;
$response = $this->tasks->updateTask($request,
new ResponseMock(), $args);
$this->assertTrue($response->status === 'success');
$request->payload = new stdClass();
$response = $this->tasks->updateTask($request,
new ResponseMock(), $args);
$this->assertTrue($response->alerts[2]['type'] === 'error');
}
private function createTask() {
$request= new RequestMock();
$task = DataMock::getTask();
$task->id = 0;
$request->payload = $task;
$response = $this->tasks->addTask($request,
new ResponseMock(), null);
$this->assertTrue($response->status === 'success');
return $response;
}
}