API work and unit test updates
This commit is contained in:
parent
24e43fb0e8
commit
c3a3c11afc
@ -8,7 +8,7 @@
|
||||
</filter>
|
||||
<logging>
|
||||
<log type="coverage-html" target="api-coverage/"
|
||||
lowUpperBound="30" highLowerBound="70" />
|
||||
lowUpperBound="35" highLowerBound="75" />
|
||||
</logging>
|
||||
</phpunit>
|
||||
|
||||
|
@ -10,7 +10,7 @@ class Boards extends BaseController {
|
||||
$this->apiJson->setSuccess();
|
||||
|
||||
foreach($boardBeans as $bean) {
|
||||
$this->apiJson->addData(Board::fromBean($bean));
|
||||
$this->apiJson->addData(Board::fromBean($this->container, $bean));
|
||||
}
|
||||
} else {
|
||||
$this->logger->addInfo('No boards in database.');
|
||||
|
@ -36,6 +36,13 @@ class AutoAction extends BaseModel {
|
||||
$this->loadFromBean($this->bean);
|
||||
}
|
||||
|
||||
public static function fromBean($container, $bean) {
|
||||
$instance = new self($container, 0, true);
|
||||
$instance->loadFromBean($container, $bean);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function fromJson($container, $json) {
|
||||
$instance = new self($container, 0, true);
|
||||
$instance->loadFromJson($container, $json);
|
||||
|
@ -10,10 +10,11 @@ abstract class BaseModel {
|
||||
$this->bean = R::load($type, $id);
|
||||
}
|
||||
|
||||
public abstract function updateBean();
|
||||
public abstract function loadFromBean($container, $bean);
|
||||
public abstract function loadFromJson($container, $obj);
|
||||
|
||||
public abstract function updateBean();
|
||||
|
||||
public function save() {
|
||||
$this->updateBean();
|
||||
|
||||
|
@ -8,11 +8,8 @@ class Board extends BaseModel {
|
||||
public $auto_actions = [];
|
||||
public $users = [];
|
||||
|
||||
private $container;
|
||||
|
||||
public function __construct($container, $id = 0, $internal = false) {
|
||||
parent::__construct('board', $id, $container);
|
||||
$this->container = $container;
|
||||
|
||||
if ($internal) {
|
||||
return;
|
||||
@ -45,6 +42,30 @@ class Board extends BaseModel {
|
||||
|
||||
$this->loadPropertiesFrom($bean);
|
||||
|
||||
if (isset($bean->columns)) {
|
||||
foreach($bean->columns as $item) {
|
||||
$this->columns[] = Column::fromBean($container, $item);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($bean->categories)) {
|
||||
foreach($bean->categories as $item) {
|
||||
$this->categories[] = Category::fromBean($container, $item);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($bean->auto_actions)) {
|
||||
foreach($bean->auto_actions as $item) {
|
||||
$this->auto_actions[] = AutoAction::fromBean($container, $item);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($bean->users)) {
|
||||
foreach($bean->users as $item) {
|
||||
$this->users[] = User::fromBean($container, $item);
|
||||
}
|
||||
}
|
||||
|
||||
$this->updateBean();
|
||||
}
|
||||
|
||||
@ -59,29 +80,29 @@ class Board extends BaseModel {
|
||||
|
||||
if (isset($obj->columns)) {
|
||||
foreach($obj->columns as $item) {
|
||||
$this->columns[] = Column::fromJson($this->container,
|
||||
json_encode($item));
|
||||
$this->columns[] =
|
||||
Column::fromJson($container, json_encode($item));
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($obj->categories)) {
|
||||
foreach($obj->categories as $item) {
|
||||
$this->categories[] = Category::fromJson($this->container,
|
||||
json_encode($item));
|
||||
$this->categories[] =
|
||||
Category::fromJson($container, json_encode($item));
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($obj->auto_actions)) {
|
||||
foreach($obj->auto_actions as $item) {
|
||||
$this->auto_actions[] = AutoAction::fromJson($this->container,
|
||||
json_encode($item));
|
||||
$this->auto_actions[] =
|
||||
AutoAction::fromJson($container, json_encode($item));
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($obj->users)) {
|
||||
foreach($obj->users as $item) {
|
||||
$this->users[] = User::fromJson($this->container,
|
||||
json_encode($item));
|
||||
$this->users[] =
|
||||
User::fromJson($container, json_encode($item));
|
||||
}
|
||||
}
|
||||
|
||||
@ -89,9 +110,9 @@ class Board extends BaseModel {
|
||||
}
|
||||
|
||||
private function loadPropertiesFrom($obj) {
|
||||
$this->id = $obj->id;
|
||||
$this->id = (int) $obj->id;
|
||||
$this->name = $obj->name;
|
||||
$this->is_active = $obj->is_active;
|
||||
$this->is_active = (bool) $obj->is_active;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,13 @@ class Category extends BaseModel {
|
||||
$this->loadFromBean($this->bean);
|
||||
}
|
||||
|
||||
public static function fromBean($container, $bean) {
|
||||
$instance = new self($container, 0, true);
|
||||
$instance->loadFromBean($container, $bean);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function fromJson($container, $json) {
|
||||
$instance = new self($container, 0, true);
|
||||
$instance->loadFromJson($container, $json);
|
||||
|
@ -15,6 +15,13 @@ class Column extends BaseModel {
|
||||
$this->loadFromBean($this->bean);
|
||||
}
|
||||
|
||||
public static function fromBean($container, $bean) {
|
||||
$instance = new self($container, 0, true);
|
||||
$instance->loadFromBean($container, $bean);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function fromJson($container, $json) {
|
||||
$instance = new self($container, 0, true);
|
||||
$instance->loadFromJson($container, $json);
|
||||
|
@ -27,6 +27,13 @@ class User extends BaseModel {
|
||||
$this->loadFromBean($this->bean);
|
||||
}
|
||||
|
||||
public static function fromBean($container, $bean) {
|
||||
$instance = new self($container, 0, true);
|
||||
$instance->loadFromBean($container, $bean);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function fromJson($container, $json) {
|
||||
$instance = new self($container, 0, true);
|
||||
$instance->loadFromJson($container, $json);
|
||||
|
@ -3,6 +3,7 @@ require_once 'Mocks.php';
|
||||
|
||||
class BoardTest extends PHPUnit_Framework_TestCase {
|
||||
private $json = '';
|
||||
private $bean;
|
||||
|
||||
public static function setupBeforeClass() {
|
||||
try {
|
||||
@ -21,38 +22,9 @@ class BoardTest extends PHPUnit_Framework_TestCase {
|
||||
return;
|
||||
}
|
||||
|
||||
$board = new stdClass();
|
||||
$board->id = 1;
|
||||
$board->name = 'test';
|
||||
$board->is_active = true;
|
||||
$board->columns = [];
|
||||
|
||||
$column = new stdClass();
|
||||
$column->id = 1;
|
||||
$column->name = 'col1';
|
||||
|
||||
$category = new stdClass();
|
||||
$category->id = 1;
|
||||
$category->name = 'cat1';
|
||||
|
||||
$auto_action = new stdClass();
|
||||
$auto_action->id = 1;
|
||||
$auto_action->trigger = ActionTrigger::MoveToColumn;
|
||||
$auto_action->trigger_id = 1;
|
||||
$auto_action->type = ActionType::SetColor;
|
||||
$auto_action->color = '#ffffff';
|
||||
|
||||
$user = new stdClass();
|
||||
$user->id = 1;
|
||||
$user->security_level = 1;
|
||||
$user->username = 'tester';
|
||||
|
||||
$board->columns[] = $column;
|
||||
$board->categories[] = $category;
|
||||
$board->auto_actions[] = $auto_action;
|
||||
$board->users[] = $user;
|
||||
|
||||
$board = DataMock::getBoard();
|
||||
$this->json = json_encode($board);
|
||||
$this->bean = $board;
|
||||
}
|
||||
|
||||
// Just to get the complete code coverage
|
||||
@ -80,6 +52,8 @@ class BoardTest extends PHPUnit_Framework_TestCase {
|
||||
$this->assertTrue($board->name === '');
|
||||
$this->assertTrue($board->is_active === true);
|
||||
$this->assertArraySubset($board->columns, []);
|
||||
|
||||
$board = Board::fromBean(new ContainerMock(), $this->bean);
|
||||
}
|
||||
|
||||
public function testCreateFromJson() {
|
||||
@ -97,5 +71,18 @@ class BoardTest extends PHPUnit_Framework_TestCase {
|
||||
$this->assertTrue($board->is_active === true);
|
||||
}
|
||||
|
||||
public function testSaveAndDelete() {
|
||||
$board = Board::fromJson(new ContainerMock(),
|
||||
json_encode(DataMock::getBoard()));
|
||||
$board->save();
|
||||
|
||||
$board = new Board(new ContainerMock(), 1);
|
||||
$this->assertTrue($board->id === 1);
|
||||
|
||||
$board->delete();
|
||||
|
||||
$board = new Board(new ContainerMock(), $board->id);
|
||||
$this->assertTrue($board->id === 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,57 @@ class AppMock {
|
||||
|
||||
$app = new AppMock();
|
||||
|
||||
class DataMock {
|
||||
public static function getBoard() {
|
||||
$board = new stdClass();
|
||||
$board->id = 1;
|
||||
$board->name = 'test';
|
||||
$board->is_active = true;
|
||||
$board->columns[] = DataMock::getColumn();
|
||||
$board->categories[] = DataMock::getCategory();
|
||||
$board->auto_actions[] = DataMock::getAutoAction();
|
||||
$board->users[] = DataMock::getUser();
|
||||
|
||||
return $board;
|
||||
}
|
||||
|
||||
public static function getColumn() {
|
||||
$column = new stdClass();
|
||||
$column->id = 1;
|
||||
$column->name = 'col1';
|
||||
|
||||
return $column;
|
||||
}
|
||||
|
||||
public static function getCategory() {
|
||||
$category = new stdClass();
|
||||
$category->id = 1;
|
||||
$category->name = 'cat1';
|
||||
|
||||
return $category;
|
||||
}
|
||||
|
||||
public static function getAutoAction() {
|
||||
$auto_action = new stdClass();
|
||||
$auto_action->id = 1;
|
||||
$auto_action->trigger = ActionTrigger::MoveToColumn;
|
||||
$auto_action->trigger_id = 1;
|
||||
$auto_action->type = ActionType::SetColor;
|
||||
$auto_action->color = '#ffffff';
|
||||
|
||||
return $auto_action;
|
||||
}
|
||||
|
||||
public static function getUser() {
|
||||
$user = new stdClass();
|
||||
$user->id = 1;
|
||||
$user->security_level = 1;
|
||||
$user->username = 'tester';
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
|
||||
class LoggerMock {
|
||||
|
||||
public function addInfo() {
|
||||
|
Reference in New Issue
Block a user