Rework API to correctly handle child objects

This commit is contained in:
kiswa 2016-09-01 21:40:40 +00:00
parent 8994099167
commit 039799a775
8 changed files with 163 additions and 171 deletions

View File

@ -166,13 +166,12 @@ class Users extends BaseController {
}
}
if ($user->default_board_id !== $update->default_board_id &&
$update->default_board_id !== 0) {
if ($user->default_board_id !== $update->default_board_id) {
$newId = $update->default_board_id;
if ($newId > 0 && !Auth::HasBoardAccess($this->container, $request,
$newId, $user->id)) {
$board = new Board($this->container, $update->default_board_id);
$board = new Board($this->container, $newId);
$board->users[] = $user;
$board->save();
}

View File

@ -19,6 +19,7 @@ abstract class BaseModel {
public abstract function loadFromBean($bean);
public abstract function loadFromJson($json);
// To make the bean accessible to tests
public function getBean() {
return $this->bean;
}
@ -41,7 +42,6 @@ abstract class BaseModel {
]);
return false; // @codeCoverageIgnore
// Due to false negative
}
return true;
@ -51,5 +51,54 @@ abstract class BaseModel {
$this->updateBean();
R::trash($this->bean);
}
protected function updateBeanList(&$objList, &$beanList) {
foreach ($objList as $obj) {
$obj->updateBean();
if ($obj->id > 0 && array_key_exists($obj->id, $beanList)) {
$beanList[$obj->id] = $obj->bean;
} else {
$beanList[] = $obj->bean;
}
}
foreach($beanList as $bean) {
$found = false;
foreach($objList as $obj) {
if ($obj->bean->id == $bean->id) {
$found = true;
}
}
if (!$found) {
unset($beanList[(int)$bean->id]);
}
}
}
protected function updateObjList(&$objList, &$beanList, $ctor) {
// Beans are indexed by id, the object list is zero-based
$count = 0;
foreach($beanList as $bean) {
if (array_key_exists($count, $objList)) {
$objList[$count]->bean = $bean;
$objList[$count]->loadFromBean($bean);
} else {
$objList[] = $ctor($bean->id);
}
$count++;
}
// Remove extra objects
$len = count($objList);
if ($len > $count) {
for (; $count < $len; $count++) {
unset($objList[$count]);
}
}
}
}

View File

@ -21,36 +21,16 @@ class Board extends BaseModel {
$bean->name = $this->name;
$bean->is_active = $this->is_active;
$bean->xownColumnList = [];
$bean->xownCategoryList = [];
$bean->xownAutoActionList = [];
$bean->xownIssueTrackerList = [];
$bean->sharedUserList = [];
foreach($this->columns as $col) {
$col->updateBean();
$this->bean->xownColumnList[] = $col->bean;
}
foreach($this->categories as $cat) {
$cat->updateBean();
$this->bean->xownCategoryList[] = $cat->bean;
}
foreach($this->auto_actions as $act) {
$act->updateBean();
$this->bean->xownAutoActionList[] = $act->bean;
}
foreach($this->issue_trackers as $ist) {
$ist->updateBean();
$this->bean->xownIssueTrackerList[] = $ist->bean;
}
foreach($this->users as $user) {
$user->updateBean();
$this->bean->sharedUserList[] = $user->bean;
}
$this->updateBeanList($this->columns,
$bean->xownColumnList);
$this->updateBeanList($this->categories,
$bean->xownCategoryList);
$this->updateBeanList($this->auto_actions,
$bean->xownAutoActionList);
$this->updateBeanList($this->issue_trackers,
$bean->xownIssueTrackerList);
$this->updateBeanList($this->users,
$bean->sharedUserList);
}
public function loadFromBean($bean) {
@ -65,32 +45,28 @@ class Board extends BaseModel {
}
$this->is_valid = true;
$this->loadPropertiesFrom($bean);
$this->resetArrays();
foreach($bean->xownColumnList as $item) {
$this->columns[] = new Column($this->container, $item->id);
}
foreach($bean->xownCategoryList as $item) {
$this->categories[] =
new Category($this->container, $item->id);
}
foreach($bean->xownAutoActionList as $item) {
$this->auto_actions[] =
new AutoAction($this->container, $item->id);
}
foreach($bean->xownIssueTrackerList as $item) {
$this->issue_trackers[] =
new IssueTracker($this->container, $item->id);
}
foreach($bean->sharedUserList as $item) {
$this->users[] = new User($this->container, $item->id);
}
$this->updateObjList($this->columns, $bean->xownColumnList,
function($id) {
return new Column($this->container, $id);
});
$this->updateObjList($this->categories, $bean->xownCategoryList,
function($id) {
return new Category($this->container, $id);
});
$this->updateObjList($this->auto_actions, $bean->xownAutoActionList,
function($id) {
return new AutoAction($this->container, $id);
});
$this->updateObjList($this->issue_trackers, $bean->xownIssueTrackerList,
function($id) {
return new IssueTracker($this->container, $id);
});
$this->updateObjList($this->users, $bean->sharedUserList,
function($id) {
return new User($this->container, $id);
});
}
public function loadFromJson($json) {
@ -103,69 +79,43 @@ class Board extends BaseModel {
}
$this->is_valid = true;
$this->loadPropertiesFrom($obj);
$this->resetArrays();
if (isset($obj->columns)) {
foreach($obj->columns as $item) {
$column = new Column($this->container, $item->id);
$this->loadArray(isset($obj->columns) ? $obj->columns : null,
$this->columns, function($id) {
return new Column($this->container, $id);
});
$this->loadArray(isset($obj->categories) ? $obj->categories : null,
$this->categories, function($id) {
return new Category($this->container, $id);
});
$this->loadArray(isset($obj->auto_actions) ? $obj->auto_actions : null,
$this->auto_actions, function($id) {
return new AutoAction($this->container, $id);
});
$this->loadArray(isset($obj->issue_trackers) ? $obj->issue_trackers : null,
$this->issue_trackers, function($id) {
return new IssueTracker($this->container, $id);
});
$this->loadArray(isset($obj->users) ? $obj->users : null,
$this->users, function($id) {
return new User($this->container, $id);
});
}
if ($column->id === 0) {
$column->loadFromJson(json_encode($item));
}
$this->columns[] = $column;
}
private function loadArray($fromArray, &$toArray, $ctor) {
if (is_null($fromArray)) {
return;
}
if (isset($obj->categories)) {
foreach($obj->categories as $item) {
$category = new Category($this->container, $item->id);
foreach($fromArray as $item) {
$obj = $ctor($item->id);
if ($category->id === 0) {
$category->loadFromJson(json_encode($item));
}
$this->categories[] = $category;
if ($obj->id === 0) {
$obj->loadFromJson(json_encode($item));
}
}
if (isset($obj->auto_actions)) {
foreach($obj->auto_actions as $item) {
$auto_action = new AutoAction($this->container, $item->id);
if ($auto_action->id === 0) {
$auto_action->loadFromJson(json_encode($item));
}
$this->auto_actions[] = $auto_action;
}
}
if (isset($obj->issue_trackers)) {
foreach($obj->issue_trackers as $item) {
$issue_tracker = new IssueTracker($this->container, $item->id);
if ($issue_tracker->id === 0) {
$issue_tracker->loadFromJson(json_encode($item));
}
$this->issue_trackers[] = $issue_tracker;
}
}
if (isset($obj->users)) {
foreach($obj->users as $item) {
$user = new User($this->container, $item->id);
if ($user->id === 0) {
$user->loadFromJson(json_encode($item));
}
$this->users[] = $user;
}
$toArray[] = $obj;
}
}
@ -178,13 +128,5 @@ class Board extends BaseModel {
$this->is_valid = false;
}
}
private function resetArrays() {
$this->columns = [];
$this->categories = [];
$this->auto_actions = [];
$this->issue_trackers = [];
$this->users = [];
}
}

View File

@ -4,7 +4,7 @@ class Column extends BaseModel {
public $name = '';
public $position = 0;
public $board_id = 0;
public $tasks = []; // Task model array
public $tasks = [];
public function __construct($container, $id = 0) {
parent::__construct('column', $id, $container);
@ -19,11 +19,8 @@ class Column extends BaseModel {
$bean->name = $this->name;
$bean->position = $this->position;
$bean->board_id = $this->board_id;
$bean->xownTaskList = [];
foreach($this->tasks as $task) {
$bean->xownTaskList[] = $task->bean;
}
$this->updateBeanList($this->tasks, $bean->xownTaskList);
}
public function loadFromBean($bean) {
@ -39,11 +36,11 @@ class Column extends BaseModel {
$this->is_valid = true;
$this->loadPropertiesFrom($bean);
$this->tasks = [];
foreach($bean->xownTaskList as $item) {
$this->tasks[] = new Task($this->container, $item->id);
}
$this->updateObjList($this->tasks, $bean->xownTaskList,
function($id) {
return new Task($this->container, $id);
});
}
public function loadFromJson($json) {
@ -57,7 +54,6 @@ class Column extends BaseModel {
$this->is_valid = true;
$this->loadPropertiesFrom($obj);
$this->tasks = [];
if (isset($obj->tasks)) {
foreach($obj->tasks as $item) {

View File

@ -33,18 +33,10 @@ class Task extends BaseModel {
$bean->points = $this->points;
$bean->position = $this->position;
$bean->xownAttachmentList = [];
$bean->xownCommentList = [];
foreach($this->attachments as $attachment) {
$attachment->updateBean();
$bean->xownAttachmentList[] = $attachment->bean;
}
foreach($this->comments as $comment) {
$comment->updateBean();
$bean->xownCommentList[] = $comment->bean;
}
$this->updateBeanList($this->attachments,
$bean->xownAttachmentList);
$this->updateBeanList($this->comments,
$bean->xownCommentList);
}
public function loadFromBean($bean) {
@ -61,17 +53,14 @@ class Task extends BaseModel {
$this->is_valid = true;
$this->loadPropertiesFrom($bean);
$this->attachments = [];
$this->comments = [];
foreach($bean->xownAttachmentList as $item) {
$this->attachments[] =
new Attachment($this->container, $item->id);
}
foreach($bean->xownCommentList as $item) {
$this->comments[] = new Comment($this->container, $item->id);
}
$this->updateObjList($this->attachments, $bean->xownAttachmentList,
function($id) {
return new Attachment($this->container, $id);
});
$this->updateObjList($this->comments, $bean->xownCommentList,
function($id) {
return new Comment($this->container, $id);
});
}
public function loadFromJson($json) {
@ -86,12 +75,10 @@ class Task extends BaseModel {
$this->is_valid = true;
$this->loadPropertiesFrom($obj);
$this->attachments = [];
$this->comments = [];
if (isset($obj->attachments)) {
foreach($obj->attachments as $item) {
$this->comments[] = new Attachment($this->container, $item->id);
$this->attachments[] =
new Attachment($this->container, $item->id);
}
}

View File

@ -249,19 +249,19 @@ class LoggerMock {
public function addError() {
// Uncomment to log errors to file
// The tests cover errors, so there will be plenty to sift through
// $msg = func_get_arg(0);
// $err = 'API ERROR: ' . $msg . PHP_EOL;
$msg = func_get_arg(0);
$err = 'API ERROR: ' . $msg . PHP_EOL;
// $objs = func_get_args();
// array_splice($objs, 0, 1);
$objs = func_get_args();
array_splice($objs, 0, 1);
// ob_start();
// foreach($objs as $obj) {
// var_dump($obj);
// }
// $strings = ob_get_clean();
ob_start();
foreach($objs as $obj) {
var_dump($obj);
}
$strings = ob_get_clean();
// file_put_contents('tests.log', [$err, $strings], FILE_APPEND);
file_put_contents('tests.log', [$err, $strings], FILE_APPEND);
}
}

View File

@ -66,6 +66,28 @@ class ColumnTest extends PHPUnit_Framework_TestCase {
$this->assertTrue($bean->board_id === $column->board_id);
}
/**
* @group single
*/
public function testRemoveChild() {
$column = new Column(new ContainerMock());
$task = new Task(new ContainerMock());
$task->title = 'test';
$column->tasks[] = $task;
$task = new Task(new ContainerMock());
$task->title = 'test2';
$column->tasks[] = $task;
$column->save();
$this->assertEquals(2, count($column->getBean()->xownTaskList));
unset($column->tasks[1]);
$column->save();
$this->assertEquals(1, count($column->getBean()->xownTaskList));
}
private function assertDefaultProperties($column) {
$this->assertTrue($column->id === 0);
$this->assertTrue($column->name === '');

View File

@ -1,9 +1,6 @@
<?php
require_once __DIR__ . '/../Mocks.php';
/**
* @group single
*/
class UserTest extends PHPUnit_Framework_TestCase {
private $json = '';
private $bean;