WIP - will break build

This commit is contained in:
kiswa 2016-05-02 21:17:55 +00:00
parent 34602c95cc
commit e58c48dad0
10 changed files with 475 additions and 61 deletions

View File

@ -1,39 +1,42 @@
<?php
class Category extends BaseModel {
public $id = 0;
public $title = '';
public $name = '';
public function __construct($container, $id = 0, $internal = false) {
public function __construct($container, $id = 0) {
parent::__construct('column', $id, $container);
if ($internal) {
return;
}
$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;
$bean->id = $this->id;
$bean->name = $this->name;
}
public function loadFromBean($bean) {
if (!isset($bean->id) || $bean->id === 0) {
return;
}
$this->loadPropertiesFrom($bean);
}
public function loadFromJson($json) {
$obj = json_decode($json);
if (!isset($obj->id) || $obj->id === 0) {
return;
}
$this->loadPropertiesFrom($obj);
}
private function loadPropertiesFrom($obj) {
$this->id = (int) $obj->id;
$this->name = $obj->name;
}
}

View File

@ -5,37 +5,61 @@ class Column extends BaseModel {
public $position = 0;
public $tasks = []; // Task model array
public function __construct($container, $id = 0, $internal = false) {
public function __construct($container, $id = 0) {
parent::__construct('column', $id, $container);
if ($internal) {
return;
}
$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;
$bean->id = $this->id;
$bean->name = $this->name;
$bean->position = $this->position;
$bean->xownTaskList = [];
foreach($this->tasks as $task) {
$bean->xownTaskList[] = $task->bean;
}
}
public function loadFromBean($bean) {
if (!isset($bean->id) || $bean->id === 0) {
return;
}
$this->loadPropertiesFrom($bean);
$this->tasks = [];
if (isset($bean->xownTaskList)) {
foreach($bean->xownTaskList as $item) {
$this->tasks[] = new Task($this->container, $item->id);
}
}
}
public function loadFromJson($json) {
$obj = json_decode($json);
if (!isset($obj->id) || $obj->id === 0) {
return;
}
$this->loadPropertiesFrom($obj);
$this->tasks = [];
if (isset($obj->tasks)) {
foreach($obj->tasks as $item) {
$this->tasks[] = new Task($this->container, $item->id);
}
}
}
private function loadPropertiesFrom($obj) {
$this->id = (int) $obj->id;
$this->name = $obj->name;
$this->position = (int) $obj->position;
}
}

View File

@ -17,37 +17,69 @@ class User extends BaseModel {
public $default_board_id = 0;
public $options = []; // UserOptions model
public function __construct($container, $id = 0, $internal = false) {
public function __construct($container, $id = 0) {
parent::__construct('user', $id, $container);
if ($internal) {
return;
}
$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;
$bean->id = $this->id;
$bean->security_level = $this->security_level;
$bean->username = $this->username;
$bean->salt = $this->salt;
$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;
}
}
public function loadFromBean($bean) {
if (!isset($bean->id) || $bean->id === 0) {
return;
}
$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($obj) {
public function loadFromJson($json) {
$obj = json_decode($json);
if (!isset($obj->id) || $obj->id === 0) {
return;
}
$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;
}
}

View File

@ -1,8 +1,10 @@
<?php
class UserOptions extends BaseModel {
public $task_order = 0;
public $id;
public $new_tasks_at_bottom = true;
public $show_animations = true;
public $show_assignee = true;
public $multiple_tasks_per_row = false;
public function __construct($container, $id = 0) {
parent::__construct('user', $id, $container);
@ -11,12 +13,39 @@ class UserOptions extends BaseModel {
}
public function updateBean() {
$bean = $this->bean;
$bean->id = $this->id;
$bean->new_tasks_at_bottom = $this->new_tasks_at_bottom;
$bean->show_animations = $this->show_animations;
$bean->show_assignee = $this->show_assignee;
$bean->multiple_tasks_per_row = $this->multiple_tasks_per_row;
}
public function loadFromBean($bean) {
if (!isset($bean->id) || $bean->id === 0) {
return;
}
$this->loadPropertiesFrom($bean);
}
public function loadFromJson($obj) {
$obj = json_decode($json);
if (!isset($obj->id) || $obj->id === 0) {
return;
}
$this->loadPropertiesFrom($obj);
}
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;
}
}

69
test/api/CategoryTest.php Normal file
View File

@ -0,0 +1,69 @@
<?php
require_once 'Mocks.php';
class CategoryTest extends PHPUnit_Framework_TestCase {
private $json = '';
private $bean;
public static function setupBeforeClass() {
try {
RedBeanPHP\R::setup('sqlite:tests.db');
} catch (Exception $ex) { }
}
protected function setUp() {
RedBeanPHP\R::nuke();
if ($this->json !== '') {
return;
}
$task = DataMock::getCategory();
$this->json = json_encode($task);
$this->bean = $task;
}
public function testCreateCategory() {
$category = new Category(new ContainerMock());
$this->assertDefaultProperties($category);
}
public function testLoadFromBean() {
$category = new Category(new ContainerMock());
$category->loadFromBean($this->bean);
$this->assertMockProperties($category);
}
public function testLoadFromJson() {
$category = new Category(new ContainerMock());
$category->loadFromJson('');
$this->assertDefaultProperties($category);
$category->loadFromJson($this->json);
$this->assertMockProperties($category);
}
public function testUpdateBean() {
$category = new Category(new ContainerMock());
$category->loadFromBean($this->bean);
$category->updateBean();
$bean = $category->getBean();
$this->assertTrue($bean->id === $category->id);
$this->assertTrue($bean->name === $category->name);
}
private function assertDefaultProperties($category) {
$this->assertTrue($category->id === 0);
$this->assertTrue($category->name === '');
}
private function assertMockProperties($category) {
$this->assertTrue($category->id === 1);
$this->assertTrue($category->name === 'cat1');
}
}

74
test/api/ColumnTest.php Normal file
View File

@ -0,0 +1,74 @@
<?php
require_once 'Mocks.php';
class ColumnTest extends PHPUnit_Framework_TestCase {
private $json = '';
private $bean;
public static function setupBeforeClass() {
try {
RedBeanPHP\R::setup('sqlite:tests.db');
} catch (Exception $ex) { }
}
protected function setUp() {
RedBeanPHP\R::nuke();
if ($this->json !== '') {
return;
}
$column = DataMock::getColumn();
$this->json = json_encode($column);
$this->bean = $column;
// Convert to bean format
$this->bean->xownTaskList = $column->tasks;
}
public function testCreateColumn() {
$column = new Column(new ContainerMock());
$this->assertDefaultProperties($column);
}
public function testLoadFromBean() {
$column = new Column(new ContainerMock());
$column->loadFromBean($this->bean);
$this->assertMockProperties($column);
}
public function testLoadFromJson() {
$column = new Column(new ContainerMock());
$column->loadFromJson('');
$this->assertDefaultProperties($column);
$column->loadFromJson($this->json);
$this->assertMockProperties($column);
}
public function testUpdateBean() {
$column = new Column(new ContainerMock());
$column->loadFromBean($this->bean);
$column->updateBean();
$bean = $column->getBean();
$this->assertTrue($bean->id === $column->id);
$this->assertTrue($bean->name === $column->name);
$this->assertTrue($bean->position === $column->position);
}
private function assertDefaultProperties($column) {
$this->assertTrue($column->id === 0);
$this->assertTrue($column->name === '');
$this->assertTrue($column->position === 0);
}
private function assertMockProperties($column) {
$this->assertTrue($column->id === 1);
$this->assertTrue($column->name === 'col1');
$this->assertTrue($column->position === 1);
}
}

View File

@ -28,6 +28,8 @@ class DataMock {
$column = new stdClass();
$column->id = 1;
$column->name = 'col1';
$column->position = 1;
$column->tasks[] = DataMock::getTask();
return $column;
}
@ -54,8 +56,13 @@ class DataMock {
public static function getUser() {
$user = new stdClass();
$user->id = 1;
$user->security_level = 1;
$user->security_level = SecurityLevel::BoardAdmin;
$user->username = 'tester';
$user->salt = 'salty1234';
$user->password_hash = 'hashpass1234';
$user->email = 'user@example.com';
$user->default_board_id = 1;
$user->options[] = DataMock::getUserOptions();
return $user;
}
@ -110,6 +117,17 @@ class DataMock {
return $task;
}
public static function getUserOptions() {
$options = new stdClass();
$options->id = 1;
$options->new_tasks_at_bottom = false;
$options->show_animations = false;
$options->show_assignee = false;
$options->multiple_tasks_per_row = true;
return $options;
}
}
class LoggerMock {

View File

@ -0,0 +1,78 @@
<?php
require_once 'Mocks.php';
class UserOptionsTest extends PHPUnit_Framework_TestCase {
private $json = '';
private $bean;
public static function setupBeforeClass() {
try {
RedBeanPHP\R::setup('sqlite:tests.db');
} catch (Exception $ex) { }
}
protected function setUp() {
RedBeanPHP\R::nuke();
if ($this->json !== '') {
return;
}
$options = DataMock::getUserOptions();
$this->json = json_encode($options);
$this->bean = $user;
}
public function testCreateUserOptions() {
$options = new UserOptions(new ContainerMock());
$this->assertDefaultProperties($options);
}
public function testLoadFromBean() {
$options = new UserOptions(new ContainerMock());
$options->loadFromBean($this->bean);
$this->assertMockProperties($options);
}
public function testLoadFromJson() {
$options = new UserOptions(new ContainerMock());
$options->loadFromJson('');
$this->assertDefaultProperties($options);
$options->loadFromJson($this->json);
$this->assertMockProperties($options);
}
public function testUpdateBean() {
$options = new UserOptions(new ContainerMock());
$options->loadFromBean($this->bean);
$options->updateBean();
$bean = $options->getBean();
$this->assertTrue($bean->id === $user->id);
$this->assertTrue($bean->new_tasks_at_bottom === $user->new_tasks_at_bottom);
$this->assertTrue($bean->show_animations === $user->show_animations);
$this->assertTrue($bean->show_assignee === $user->show_assignee);
$this->assertTrue($bean->multiple_tasks_per_row === $user->multiple_tasks_per_row);
}
private function assertDefaultProperties($user) {
$this->assertTrue($user->id === 0);
$this->assertTrue($user->security_level == SecurityLevel::User);
$this->assertTrue($user->username === '');
$this->assertTrue($user->salt === '');
$this->assertTrue($user->password_hash === '');
}
private function assertMockProperties($user) {
$this->assertTrue($user->id === 1);
$this->assertTrue($user->salt === 'salty1234');
$this->assertTrue($user->password_hash === 'hashpass1234');
$this->assertTrue($user->email === 'user@example.com');
$this->assertTrue($user->default_board_id === 1);
}
}

87
test/api/UserTest.php Normal file
View File

@ -0,0 +1,87 @@
<?php
require_once 'Mocks.php';
class UserTest extends PHPUnit_Framework_TestCase {
private $json = '';
private $bean;
public static function setupBeforeClass() {
try {
RedBeanPHP\R::setup('sqlite:tests.db');
} catch (Exception $ex) { }
}
protected function setUp() {
RedBeanPHP\R::nuke();
if ($this->json !== '') {
return;
}
$user = DataMock::getUser();
$this->json = json_encode($user);
$this->bean = $user;
// Convert to bean format
$this->bean->xownOptionList = $user->options;
}
public function testCreateUser() {
$user = new User(new ContainerMock());
$this->assertDefaultProperties($user);
}
public function testLoadFromBean() {
$user = new User(new ContainerMock());
$user->loadFromBean($this->bean);
$this->assertMockProperties($user);
}
public function testLoadFromJson() {
$user = new User(new ContainerMock());
$user->loadFromJson('');
$this->assertDefaultProperties($user);
$user->loadFromJson($this->json);
$this->assertMockProperties($user);
}
public function testUpdateBean() {
$user = new User(new ContainerMock());
$user->loadFromBean($this->bean);
$user->updateBean();
$bean = $user->getBean();
$this->assertTrue($bean->id === $user->id);
$this->assertTrue($bean->security_level === $user->security_level);
$this->assertTrue($bean->username === $user->username);
$this->assertTrue($bean->salt === $user->salt);
$this->assertTrue($bean->password_hash === $user->password_hash);
$this->assertTrue($bean->email === $user->email);
$this->assertTrue($bean->default_board_id === $user->default_board_id);
}
private function assertDefaultProperties($user) {
$this->assertTrue($user->id === 0);
$this->assertTrue($user->security_level == SecurityLevel::User);
$this->assertTrue($user->username === '');
$this->assertTrue($user->salt === '');
$this->assertTrue($user->password_hash === '');
$this->assertTrue($user->email === '');
$this->assertTrue($user->default_board_id === 0);
}
private function assertMockProperties($user) {
$this->assertTrue($user->id === 1);
$this->assertTrue($user->security_level->getValue() ===
SecurityLevel::BoardAdmin);
$this->assertTrue($user->username === 'tester');
$this->assertTrue($user->salt === 'salty1234');
$this->assertTrue($user->password_hash === 'hashpass1234');
$this->assertTrue($user->email === 'user@example.com');
$this->assertTrue($user->default_board_id === 1);
}
}

BIN
tests.db

Binary file not shown.