Remove static methods from models and update tests

This commit is contained in:
kiswa 2016-05-10 20:39:08 +00:00
parent 18cb96c433
commit bedaebff6c
16 changed files with 216 additions and 96 deletions

View File

@ -27,7 +27,7 @@ class Attachments extends BaseController {
if (!$attachment->save()) {
$this->logger->addError('Add Attachment: ', [$attachment]);
$this->apiJson->addAlert('error', 'Error adding attachment.' .
$this->apiJson->addAlert('error', 'Error adding attachment. ' .
'Please try again.');
return $this->jsonResponse($response);
@ -52,7 +52,7 @@ class Attachments extends BaseController {
if ($attachment->id !== $update->id) {
$this->logger->addError('Update Attachment: ',
[$attachment, $update]);
$this->apiJson->addAlert('error', 'Error update attachment. ' .
$this->apiJson->addAlert('error', 'Error updating attachment. ' .
'Please try again.');
return $this->jsonResponse($response);
@ -62,13 +62,13 @@ class Attachments extends BaseController {
// TODO: Get existing user to log user_id and name
$this->dbLogger->logChange($this->container, 0,
'$user->name update attachment ' . $attachment->name,
'$user->name updated attachment ' . $update->name,
json_encode($attachment), json_encode($update),
'attachment', $attachment->id);
'attachment', $update->id);
$this->apiJson->setSuccess();
$this->apiJson->addAlert('success', 'Attachment ' .
$attachment->name . ' updated.');
$update->name . ' updated.');
return $this->jsonResponse($response);
}

View File

@ -10,8 +10,10 @@ class Boards extends BaseController {
$this->apiJson->setSuccess();
foreach($boardBeans as $bean) {
$this->apiJson->addData(
Board::fromBean($this->container, $bean));
$board = new Board($this->container);
$board->loadFromBean($bean);
$this->apiJson->addData($board);
}
} else {
$this->logger->addInfo('No boards in database.');
@ -40,7 +42,8 @@ class Boards extends BaseController {
}
public function addBoard($request, $response, $args) {
$board = Board::fromJson($this->container, $request->getBody());
$board = new Board($this->container);
$board->loadFromJson($request->getBody());
if (!$board->save()) {
$this->logger->addError('Add Board: ', [$board]);
@ -64,7 +67,9 @@ class Boards extends BaseController {
public function updateBoard($request, $response, $args) {
$board = new Board($this->container, (int)$args['id']);
$update = Board::fromJson($this->container, $request->getBody());
$update = new Board($this->container);
$update->loadFromJson($request->getBody());
if ($board->id !== $update->id) {
$this->logger->addError('Update Board: ', [$board, $update]);

View File

@ -2,5 +2,74 @@
use RedBeanPHP\R;
class Columns extends BaseController {
public function getColumn($request, $response, $args) {
$column = new Column($this->container, (int)$args['id']);
if ($column->id === 0) {
$this->logger->addError('Attempt to load column ' .
$args['id'] . ' failed.');
$this->apiJson->addAlert('error', 'No column found for ID ' .
$args['id'] . '.');
return $this->jsonResponse($response);
}
$this->apiJson->setSuccess();
$this->apiJson->addData($column);
return $this->jsonResponse($response);
}
public function addColumn($request, $response, $args) {
$column = new Column($this->container);
$column->loadFromJson($request->getBody());
if (!$column->save()) {
$this->logger->addError('Add Column: ', [$column]);
$this->apiJson->addAlert('error', 'Error adding column. ' .
'Please try again.');
}
// TODO: Get existing user to log user_id and name
$this->dbLogger->logChange($this->container, 0,
'$user->name added column ' . $column->name . '.',
'', json_encode($column), 'column', $column->id);
$this->apiJson->setSuccess();
$this->apiJson->addAlert('success', 'Column ' .
$column->name . 'added.');
return $this->jsonResponse($response);
}
public function updateColumn($request, $response, $args) {
$column = new Column($this->container, (int)$args['id']);
$update = new Column($this->container);
$update->loadFromJson($request->getBody());
if ($column->id !== $update->id) {
$this->logger->addError('Update Column: ',
[$column, $update]);
$this->apiJson->addAlert('error', 'Error updating column ' .
$update->name . '. Please try again.');
return $this->jsonResponse($response);
}
$update->save();
// TODO: Get existing user to log user_id and name
$this->dbLogger->logChange($this->container, 0,
'$user->name updated column ' . $update->name,
json_encode($column), json_encode($update),
'column', $update->id);
$this->apiJson->setSuccess();
$this->apiJson->addAlert('success', 'Column ' .
$update->name . ' updated.');
return $this->jsonResponse($response);
}
}

View File

@ -18,12 +18,12 @@ $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');

View File

@ -4,7 +4,7 @@ class Attachment extends BaseModel {
public $filename = '';
public $name = '';
public $type = '';
public $user = 0;
public $user_id = 0;
public $timestamp = null;
public function __construct($container, $id = 0) {
@ -20,7 +20,7 @@ class Attachment extends BaseModel {
$bean->filename = $this->filename;
$bean->name = $this->name;
$bean->type = $this->type;
$bean->user = $this->user;
$bean->user_id = $this->user_id;
$bean->timestamp = $this->timestamp;
}
@ -58,7 +58,7 @@ class Attachment extends BaseModel {
$this->filename = $obj->filename;
$this->name = $obj->name;
$this->type = $obj->type;
$this->user = (int) $obj->user;
$this->user_id = (int) $obj->user_id;
$this->timestamp = (int) $obj->timestamp;
} catch (Exception $ex) {
$this->is_valid = false;

View File

@ -18,20 +18,6 @@ class Board extends BaseModel {
$this->loadFromBean($this->bean);
}
public static function fromBean($container, $bean) {
$instance = new self($container, 0, true);
$instance->loadFromBean($bean);
return $instance;
}
public static function fromJson($container, $json) {
$instance = new self($container, 0, true);
$instance->loadFromJson($json);
return $instance;
}
public function updateBean() {
$bean = $this->bean;
@ -149,9 +135,13 @@ class Board extends BaseModel {
}
private function loadPropertiesFrom($obj) {
$this->id = (int) $obj->id;
$this->name = $obj->name;
$this->is_active = (bool) $obj->is_active;
try {
$this->id = (int) $obj->id;
$this->name = $obj->name;
$this->is_active = (bool) $obj->is_active;
} catch (Exception $ex) {
$this->is_valid = false;
}
}
private function resetArrays() {

View File

@ -45,8 +45,12 @@ class Category extends BaseModel {
}
private function loadPropertiesFrom($obj) {
$this->id = (int) $obj->id;
$this->name = $obj->name;
try {
$this->id = (int) $obj->id;
$this->name = $obj->name;
} catch (Exception $ex) {
$this->is_valid = false;
}
}
}

View File

@ -67,9 +67,13 @@ class Column extends BaseModel {
}
private function loadPropertiesFrom($obj) {
$this->id = (int) $obj->id;
$this->name = $obj->name;
$this->position = (int) $obj->position;
try {
$this->id = (int) $obj->id;
$this->name = $obj->name;
$this->position = (int) $obj->position;
} catch (Exception $ex) {
$this->is_valid = false;
}
}
}

View File

@ -45,8 +45,12 @@ class Comment extends BaseModel {
}
private function loadPropertiesFrom($obj) {
$this->id = (int) $obj->id;
$this->text = $obj->text;
try {
$this->id = (int) $obj->id;
$this->text = $obj->text;
} catch (Exception $ex) {
$this->is_valid = false;
}
}
}

View File

@ -4,7 +4,7 @@ class Task extends BaseModel {
public $title = '';
public $description = '';
public $assignee_id = 0;
public $category_id = 0; // Category model
public $category_id = 0;
public $color = '';
public $due_date = null; // Date or null if not set
public $points = null; // Integer or null if not set
@ -24,14 +24,14 @@ class Task extends BaseModel {
$bean->id = $this->id;
$bean->title = $this->title;
$bean->description = $this->description;
$bean-> assignee_id = $this->assignee_id;
$bean-> category_id = $this->category_id;
$bean->assignee_id = $this->assignee_id;
$bean->category_id = $this->category_id;
$bean->color = $this->color;
$bean->due_date = $this->due_date;
$bean->points = $this->points;
$bean->position = $this->position;
$bean-> xownAttachmentList = [];
$bean->xownAttachmentList = [];
$bean->xownCommentList = [];
foreach($this->attachments as $attachment) {
@ -105,15 +105,19 @@ class Task extends BaseModel {
}
private function loadPropertiesFrom($obj) {
$this->id = $obj->id;
$this->title = $obj->title;
$this->description = $obj->description;
$this->assignee_id = $obj->assignee_id;
$this->category_id = $obj->category_id;
$this->color = $obj->color;
$this->due_date = $obj->due_date;
$this->points = $obj->points;
$this->position = $obj->position;
try {
$this->id = $obj->id;
$this->title = $obj->title;
$this->description = $obj->description;
$this->assignee_id = $obj->assignee_id;
$this->category_id = $obj->category_id;
$this->color = $obj->color;
$this->due_date = $obj->due_date;
$this->points = $obj->points;
$this->position = $obj->position;
} catch (Exception $ex) {
$this->is_valid = false;
}
}
}

View File

@ -15,7 +15,7 @@ class User extends BaseModel {
public $password_hash = '';
public $email = '';
public $default_board_id = 0;
public $options = []; // UserOptions model
public $user_option_id = 0;
public function __construct($container, $id = 0) {
parent::__construct('user', $id, $container);
@ -33,11 +33,7 @@ class User extends BaseModel {
$bean->password_hash = $this->password_hash;
$bean->email = $this->email;
$bean->default_board_id = $this->default_board_id;
$bean->xownOptionList = [];
foreach($this->options as $option) {
$bean->xownOptionList[] = $option->bean;
}
$bean->user_option_id = $this->user_option_id;
}
public function loadFromBean($bean) {
@ -53,13 +49,6 @@ class User extends BaseModel {
$this->is_valid = true;
$this->loadPropertiesFrom($bean);
$this->options = [];
if (isset($bean->xownOptionList)) {
foreach($bean->xownOptionList as $item) {
$this->options[] = new UserOptions($this->container, $item->id);
}
}
}
public function loadFromJson($json) {
@ -73,23 +62,21 @@ class User extends BaseModel {
$this->is_valid = true;
$this->loadPropertiesFrom($obj);
$this->options = [];
if (isset($obj->options)) {
foreach($obj->options as $item) {
$this->options[] = new UserOptions($this->container, $item->id);
}
}
}
public function loadPropertiesFrom($obj) {
$this->id = (int) $obj->id;
$this->security_level = new SecurityLevel((int) $obj->security_level);
$this->username = $obj->username;
$this->salt = $obj->salt;
$this->password_hash = $obj->password_hash;
$this->email = $obj->email;
$this->default_board_id = (int) $obj->default_board_id;
try {
$this->id = (int) $obj->id;
$this->security_level = new SecurityLevel((int) $obj->security_level);
$this->username = $obj->username;
$this->salt = $obj->salt;
$this->password_hash = $obj->password_hash;
$this->email = $obj->email;
$this->default_board_id = (int) $obj->default_board_id;
$this->user_option_id = (int) $obj->user_option_id;
} catch (Exception $ex) {
$this->is_valid = false;
}
}
}

View File

@ -7,7 +7,7 @@ class UserOptions extends BaseModel {
public $multiple_tasks_per_row = false;
public function __construct($container, $id = 0) {
parent::__construct('user', $id, $container);
parent::__construct('user_option', $id, $container);
$this->loadFromBean($this->bean);
}
@ -51,11 +51,15 @@ class UserOptions extends BaseModel {
}
private function loadPropertiesFrom($obj) {
$this->id = (int) $obj->id;
$this->new_tasks_at_bottom = (bool) $obj->new_tasks_at_bottom;
$this->show_animations = (bool) $obj->show_animations;
$this->show_assignee = (bool) $obj->show_assignee;
$this->multiple_tasks_per_row = (bool) $obj->multiple_tasks_per_row;
try {
$this->id = (int) $obj->id;
$this->new_tasks_at_bottom = (bool) $obj->new_tasks_at_bottom;
$this->show_animations = (bool) $obj->show_animations;
$this->show_assignee = (bool) $obj->show_assignee;
$this->multiple_tasks_per_row = (bool) $obj->multiple_tasks_per_row;
} catch (Exception $ex) {
$this->is_valid = false;
}
}
}

View File

@ -86,7 +86,7 @@ class DataMock {
$attachment->filename = 'file';
$attachment->name = 'file.png';
$attachment->type = 'image';
$attachment->user = 1;
$attachment->user_id = 1;
$attachment->timestamp = 1234567890;
return $attachment;

View File

@ -27,11 +27,14 @@ class AttachmentsTest extends PHPUnit_Framework_TestCase {
$actual = $this->attachments->getAttachment(null,
new ResponseMock(), $args);
$this->assertEquals($expected, $actual);
$this->createAttachment();
$actual = $this->attachments->getAttachment(null,
new ResponseMock(), $args);
$this->assertTrue($actual->status === 'success');
$this->assertTrue(count($actual->data) === 1);
}
/**
* @group single
*/
public function testAddRemoveAttachment() {
$expected = new ApiJson();
@ -53,6 +56,48 @@ class AttachmentsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($expected, $actual);
}
public function testAddBadAttachment() {
$request = new RequestMock();
$request->invalidPayload = true;
$response = $this->attachments->addAttachment($request,
new ResponseMock(), null);
$this->assertTrue($response->status === 'failure');
$this->assertTrue($response->alerts[0]['type'] === 'error');
}
public function testRemoveBadAttachment() {
$args = [];
$args['id'] = 5; // No such attachment
$response = $this->attachments->removeAttachment(null,
new ResponseMock(), $args);
$this->assertTrue($response->status === 'failure');
}
public function testUpdateAttachment() {
$this->createAttachment();
$attachment = DataMock::getAttachment();
$attachment->type = 'text';
$args = [];
$args['id'] = $attachment->id;
$request = new RequestMock();
$request->payload = $attachment;
$response = $this->attachments->updateAttachment($request,
new ResponseMock(), $args);
$this->assertTrue($response->status === 'success');
$request->payload = new stdClass();
$esponse = $this->attachments->updateAttachment($request,
new ResponseMock(), $args);
$this->assertTrue($response->alerts[2]['type'] === 'error');
}
private function createAttachment() {
$request = new RequestMock();
$attachment = DataMock::getAttachment();

View File

@ -35,7 +35,7 @@ class BoardsTest extends PHPUnit_Framework_TestCase {
$expected->addAlert('error', 'No board found for ID 1.');
$args = [];
$args['id'] = '1';
$args['id'] = 1;
$actual = $this->boards->getBoard(null, new ResponseMock(), $args);
$this->assertEquals($expected, $actual);
@ -59,7 +59,7 @@ class BoardsTest extends PHPUnit_Framework_TestCase {
$expected->addAlert('success', 'Board test removed.');
$args = [];
$args['id'] = '1';
$args['id'] = 1;
$actual = $this->boards->removeBoard(null, new ResponseMock(), $args);

View File

@ -53,11 +53,12 @@ class BoardTest extends PHPUnit_Framework_TestCase {
}
public function testCreateFromBean() {
$board = Board::fromBean(new ContainerMock(), null);
$board = new Board(new ContainerMock());
$board->loadFromBean(null);
$this->assertDefaultProperties($board);
$board = Board::fromBean(new ContainerMock(), $this->bean);
$board->loadFromBean($this->bean);
$this->assertTrue($board->id === 1);
$this->assertTrue($board->name === 'test');
@ -65,13 +66,15 @@ class BoardTest extends PHPUnit_Framework_TestCase {
}
public function testCreateFromJson() {
$board = Board::fromJson(new ContainerMock(), null);
$board = new Board(new ContainerMock());
$board->loadFromJson(null);
$this->assertDefaultProperties($board);
$board = Board::fromJson(new ContainerMock(), '{"id":0}');
$board->loadFromJson('{"id":0}');
$this->assertDefaultProperties($board);
$board = Board::fromJson(new ContainerMock(), $this->json);
$board->loadFromJson($this->json);
$this->assertTrue($board->id === 1);
$this->assertTrue($board->name === 'test');
@ -79,7 +82,8 @@ class BoardTest extends PHPUnit_Framework_TestCase {
}
public function testSaveLoadDelete() {
$board = Board::fromJson(new ContainerMock(), $this->json);
$board = new Board(new ContainerMock());
$board->loadFromJson($this->json);
$board->save();
$this->assertTrue($board->id === 1);