From 278adffcd6a00c39071d7cb60eadebccb72b6da7 Mon Sep 17 00:00:00 2001 From: Matthew Ross Date: Fri, 13 May 2016 06:29:13 -0400 Subject: [PATCH] API unit tests and updates --- src/api/controllers/Tasks.php | 2 +- src/api/models/Task.php | 14 ++-- test/api/controllers/TasksTest.php | 115 +++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 test/api/controllers/TasksTest.php diff --git a/src/api/controllers/Tasks.php b/src/api/controllers/Tasks.php index c1b6b17..f5495a8 100644 --- a/src/api/controllers/Tasks.php +++ b/src/api/controllers/Tasks.php @@ -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(); diff --git a/src/api/models/Task.php b/src/api/models/Task.php index ed5d436..56af6a3 100644 --- a/src/api/models/Task.php +++ b/src/api/models/Task.php @@ -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; } diff --git a/test/api/controllers/TasksTest.php b/test/api/controllers/TasksTest.php new file mode 100644 index 0000000..0bf3005 --- /dev/null +++ b/test/api/controllers/TasksTest.php @@ -0,0 +1,115 @@ +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; + } +} +