From ad6a5ec4a40c8e56672b129b953d346ad3896e54 Mon Sep 17 00:00:00 2001 From: kiswa Date: Sat, 30 Apr 2016 02:28:53 +0000 Subject: [PATCH] Unit test updates and implementation --- PhpUnit.xml | 11 ++-- README.md | 8 +++ gulpfile.js | 10 +++- package.json | 2 +- src/api/controllers/Boards.php | 2 +- src/api/models/Activity.php | 46 +++++++++++++-- src/api/models/Attachment.php | 34 ++++++++++- src/api/models/AutoAction.php | 58 ++++++++++--------- src/api/models/BaseModel.php | 24 ++++++-- src/api/models/Board.php | 88 ++++++++++++++++++++--------- src/api/models/Category.php | 8 +-- src/api/models/Column.php | 8 +-- src/api/models/Comment.php | 25 ++++++++- src/api/models/Task.php | 100 +++++++++++++++++++++++++++------ src/api/models/User.php | 8 +-- src/api/models/UserOptions.php | 4 +- test/api/ActivityTest.php | 69 +++++++++++++++++++++++ test/api/AttachmentTest.php | 81 ++++++++++++++++++++++++++ test/api/AutoActionTest.php | 85 ++++++++++++++++++++++++++++ test/api/BoardTest.php | 65 +++++++++++++-------- test/api/BoardsTest.php | 32 +++++------ test/api/CommentTest.php | 69 +++++++++++++++++++++++ test/api/Mocks.php | 69 +++++++++++++++++++++-- test/api/TaskTest.php | 86 ++++++++++++++++++++++++++++ tests.db | Bin 0 -> 12288 bytes 25 files changed, 839 insertions(+), 153 deletions(-) create mode 100644 test/api/ActivityTest.php create mode 100644 test/api/AttachmentTest.php create mode 100644 test/api/AutoActionTest.php create mode 100644 test/api/CommentTest.php create mode 100644 test/api/TaskTest.php create mode 100644 tests.db diff --git a/PhpUnit.xml b/PhpUnit.xml index 63c7346..799f227 100644 --- a/PhpUnit.xml +++ b/PhpUnit.xml @@ -1,14 +1,13 @@ - - - src/api/vendor - - + + src/api/controllers + src/api/models + + lowUpperBound="40" highLowerBound="80" /> diff --git a/README.md b/README.md index c1e38e0..d7a584e 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,14 @@ Developing on TaskBoard is pretty simple too. Both the API and App are unit tested. To run all tests, use the command `gulp test`. For only one set, run `gulp test-api` or `gulp test-app`. +If you want to run a single API test, add the following comment block before the test function and use the command `gulp test-api-single`. + +``` php +/** + * @group single + */ +``` + These tests are run by [Travis CI](https://travis-ci.org/) on PRs and commits. A PR with failing or missing tests will not be merged. ## How It's Made diff --git a/gulpfile.js b/gulpfile.js index cd29dde..19f82df 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -151,6 +151,13 @@ gulp.task('test-api', () => { '--configuration PhpUnit.xml test/api/')); }); +gulp.task('test-api-single', () => { + return gulp.src('') + .pipe(phpunit('./src/api/vendor/phpunit/phpunit/phpunit ' + + '--configuration PhpUnit.xml ' + + '--group single --no-coverage test/api/')) +}); + gulp.task('watch', () => { let watchTs = gulp.watch(paths.ts, ['tsc']), watchScss = gulp.watch(paths.scss, ['lintScss', 'styles']), @@ -159,7 +166,8 @@ gulp.task('watch', () => { watchApi = gulp.watch(paths.api, ['api']), onChanged = (event) => { - console.log('File ' + event.path + ' was ' + event.type + '. Running tasks...'); + console.log('File ' + event.path + ' was ' + event.type + + '. Running tasks...'); }; watchTs.on('change', onChanged); diff --git a/package.json b/package.json index 3a65786..5062e95 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "gulp-util": "^3.0.7", "merge-stream": "^1.0.0", "reflect-metadata": "0.1.2", - "rxjs": "5.0.0-beta.2", + "rxjs": "5.0.0-beta.6", "scss-base": "^1.1.0", "systemjs": "^0.19.26", "zone.js": "^0.6.12" diff --git a/src/api/controllers/Boards.php b/src/api/controllers/Boards.php index 27bcbd3..63b9f9c 100644 --- a/src/api/controllers/Boards.php +++ b/src/api/controllers/Boards.php @@ -108,7 +108,7 @@ class Boards extends BaseController { $this->apiJson->setSuccess(); $this->apiJson->addAlert('success', - 'Board ' . $before->name . ' updated.'); + 'Board ' . $before->name . ' removed.'); return $this->jsonResponse($response); } diff --git a/src/api/models/Activity.php b/src/api/models/Activity.php index 0b6622a..e51a63a 100644 --- a/src/api/models/Activity.php +++ b/src/api/models/Activity.php @@ -9,24 +9,60 @@ class Activity extends BaseModel { public $item_type = ''; public $item_id = 0; - public function __construct($container, $id = 0) { + public function __construct($container, $id = 0, $internal = false) { parent::__construct('activity', $id, $container); + + if ($internal) { + return; + } + + $this->loadFromBean($this->bean); } public static function fromBean($container, $bean) { - $instance = new self($container, 0); - $instance->loadFromBean($container, $bean); + $instance = new self($container, 0, true); + $instance->loadFromBean($bean); return $instance; } public function updateBean() { + $bean = $this->bean; + + $bean->user_id = $this->user_id; + $bean->log_text = $this->log_text; + $bean->before = $this->before; + $bean->after = $this->after; + $bean->item_type = $this->item_type; + $bean->item_id = $this->item_id; } - public function loadFromBean($container, $bean) { + public function loadFromBean($bean) { + if (!isset($bean->id) || $bean->id === 0) { + return; + } + + $this->loadPropertiesFrom($bean); } - public function loadFromJson($container, $obj) { + 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->user_id = (int) $obj->user_id; + $this->log_text = $obj->log_text; + $this->before = $obj->before; + $this->after = $obj->after; + $this->item_type = $obj->item_type; + $this->item_id = (int) $obj->item_id; } } diff --git a/src/api/models/Attachment.php b/src/api/models/Attachment.php index 51fe775..dcb2225 100644 --- a/src/api/models/Attachment.php +++ b/src/api/models/Attachment.php @@ -1,5 +1,6 @@ bean; + + $bean->id = $this->id; + $bean->filename = $this->filename; + $bean->name = $this->name; + $bean->type = $this->type; + $bean->user_id = $this->user_id; + $bean->timestamp = $this->timestamp; } - public function loadFromBean($container, $bean) { + public function loadFromBean($bean) { + if (!isset($bean->id) || $bean->id === 0) { + return; + } + + $this->loadPropertiesFrom($bean); } - public function loadFromJson($container, $obj) { + 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->filename = $obj->filename; + $this->name = $obj->name; + $this->type = $obj->type; + $this->user_id = (int) $obj->user_id; + $this->timestamp = (int) $obj->timestamp; } } diff --git a/src/api/models/AutoAction.php b/src/api/models/AutoAction.php index 94e7eda..c082cee 100644 --- a/src/api/models/AutoAction.php +++ b/src/api/models/AutoAction.php @@ -19,44 +19,48 @@ class AutoAction extends BaseModel { public $trigger = ActionTrigger::MoveToColumn; public $trigger_id = 0; // ID of the column etc. which triggers the action public $type = ActionType::SetColor; - // These are the target change to make when the action - // is performed. Only one will be set in an action. - // TODO: Consider other ways to do this. - public $color = ''; - public $category_id = 0; - public $assignee_id = 0; + public $change_to = ''; // Whatever the target of the action changes to - public function __construct($container, $id = 0, $internal = false) { + public function __construct($container, $id = 0) { parent::__construct('auto_action', $id, $container); - if ($internal) { - return; - } - $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); - - return $instance; - } - public function updateBean() { + $bean = $this->bean; + + $bean->id = $this->id; + $bean->trigger = $this->trigger; + $bean->trigger_id = $this->trigger_id; + $bean->type = $this->type; + $bean->change_to = $this->change_to; } - public function loadFromBean($container, $bean) { + public function loadFromBean($bean) { + if (!isset($bean->id) || $bean->id === 0) { + return; + } + + $this->loadPropertiesFrom($bean); } - public function loadFromJson($container, $obj) { + 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->trigger = new ActionTrigger((int) $obj->trigger); + $this->trigger_id = (int) $obj->trigger_id; + $this->type = new ActionType((int) $obj->type); + $this->change_to = $obj->change_to; } } diff --git a/src/api/models/BaseModel.php b/src/api/models/BaseModel.php index 00180d1..a3fd312 100644 --- a/src/api/models/BaseModel.php +++ b/src/api/models/BaseModel.php @@ -4,25 +4,37 @@ use RedBeanPHP\R; abstract class BaseModel { protected $logger; protected $bean; + protected $container; public function __construct($type, $id, $container) { - $this->logger = $container->get('logger'); + $this->container = $container; + + $this->logger = $this->container->get('logger'); $this->bean = R::load($type, $id); } - public abstract function loadFromBean($container, $bean); - public abstract function loadFromJson($container, $obj); - public abstract function updateBean(); + public abstract function loadFromBean($bean); + public abstract function loadFromJson($json); + + public function getBean() { + return $this->bean; + } + public function save() { $this->updateBean(); try { - R::store($this->bean); + $id = R::store($this->bean); + assert($id === $this->id); $this->loadFromBean($this->bean); } catch (Exception $ex) { - $this->logger->addError('Save Error: ', [$this->bean]); + $this->logger->addError('Save Error: ', [ + $this->bean, + $ex->getMessage(), + $ex->getTrace() + ]); } } diff --git a/src/api/models/Board.php b/src/api/models/Board.php index a035ee0..4ae0691 100644 --- a/src/api/models/Board.php +++ b/src/api/models/Board.php @@ -15,61 +15,91 @@ class Board extends BaseModel { return; } - $this->loadFromBean($container, $this->bean); + $this->loadFromBean($this->bean); } public static function fromBean($container, $bean) { $instance = new self($container, 0, true); - $instance->loadFromBean($container, $bean); + $instance->loadFromBean($bean); return $instance; } public static function fromJson($container, $json) { $instance = new self($container, 0, true); - $instance->loadFromJson($container, $json); + $instance->loadFromJson($json); return $instance; } public function updateBean() { + $bean = $this->bean; + + $bean->name = $this->name; + $bean->is_active = $this->is_active; + + $bean->xownColumnList = []; + $bean->xownCategoryList = []; + $bean->xownAutoActionList = []; + $bean->ownUserList = []; + + 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->users as $user) { + $user->updateBean(); + $this->bean->ownUserList[] = $user->bean; + } } - public function loadFromBean($container, $bean) { + public function loadFromBean($bean) { if (!isset($bean->id) || $bean->id === 0) { return; } $this->loadPropertiesFrom($bean); + $this->resetArrays(); - if (isset($bean->columns)) { - foreach($bean->columns as $item) { - $this->columns[] = Column::fromBean($container, $item); + if (isset($bean->xownColumnList)) { + foreach($bean->xownColumnList as $item) { + $this->columns[] = new Column($this->container, $item->id); } } - if (isset($bean->categories)) { - foreach($bean->categories as $item) { - $this->categories[] = Category::fromBean($container, $item); + if (isset($bean->xownCategoryList)) { + foreach($bean->xownCategoryList as $item) { + $this->categories[] = + new Category($this->container, $item->id); } } - if (isset($bean->auto_actions)) { - foreach($bean->auto_actions as $item) { - $this->auto_actions[] = AutoAction::fromBean($container, $item); + if (isset($bean->xownAutoActionList)) { + foreach($bean->xownAutoActionList as $item) { + $this->auto_actions[] = + new AutoAction($this->container, $item->id); } } - if (isset($bean->users)) { - foreach($bean->users as $item) { - $this->users[] = User::fromBean($container, $item); + if (isset($bean->ownUserList)) { + foreach($bean->ownUserList as $item) { + $this->users[] = new User($this->container, $item->id); } } - - $this->updateBean(); } - public function loadFromJson($container, $json) { + public function loadFromJson($json) { $obj = json_decode($json); if (!isset($obj->id) || $obj->id === 0) { @@ -77,36 +107,33 @@ class Board extends BaseModel { } $this->loadPropertiesFrom($obj); + $this->resetArrays(); if (isset($obj->columns)) { foreach($obj->columns as $item) { - $this->columns[] = - Column::fromJson($container, json_encode($item)); + $this->columns[] = new Column($this->container, $item->id); } } if (isset($obj->categories)) { foreach($obj->categories as $item) { $this->categories[] = - Category::fromJson($container, json_encode($item)); + new Category($this->container, $item->id); } } if (isset($obj->auto_actions)) { foreach($obj->auto_actions as $item) { $this->auto_actions[] = - AutoAction::fromJson($container, json_encode($item)); + new AutoAction($this->container, $item->id); } } if (isset($obj->users)) { foreach($obj->users as $item) { - $this->users[] = - User::fromJson($container, json_encode($item)); + $this->users[] = new User($this->container, $item->id); } } - - $this->updateBean(); } private function loadPropertiesFrom($obj) { @@ -114,5 +141,12 @@ class Board extends BaseModel { $this->name = $obj->name; $this->is_active = (bool) $obj->is_active; } + + private function resetArrays() { + $this->columns = []; + $this->categories = []; + $this->auto_actions = []; + $this->users = []; + } } diff --git a/src/api/models/Category.php b/src/api/models/Category.php index 6bf8e61..026d594 100644 --- a/src/api/models/Category.php +++ b/src/api/models/Category.php @@ -15,14 +15,14 @@ class Category extends BaseModel { public static function fromBean($container, $bean) { $instance = new self($container, 0, true); - $instance->loadFromBean($container, $bean); + $instance->loadFromBean($bean); return $instance; } public static function fromJson($container, $json) { $instance = new self($container, 0, true); - $instance->loadFromJson($container, $json); + $instance->loadFromJson($json); return $instance; } @@ -30,10 +30,10 @@ class Category extends BaseModel { public function updateBean() { } - public function loadFromBean($container, $bean) { + public function loadFromBean($bean) { } - public function loadFromJson($container, $json) { + public function loadFromJson($json) { } } diff --git a/src/api/models/Column.php b/src/api/models/Column.php index 92fcdce..6af9246 100644 --- a/src/api/models/Column.php +++ b/src/api/models/Column.php @@ -17,14 +17,14 @@ class Column extends BaseModel { public static function fromBean($container, $bean) { $instance = new self($container, 0, true); - $instance->loadFromBean($container, $bean); + $instance->loadFromBean($bean); return $instance; } public static function fromJson($container, $json) { $instance = new self($container, 0, true); - $instance->loadFromJson($container, $json); + $instance->loadFromJson($json); return $instance; } @@ -32,10 +32,10 @@ class Column extends BaseModel { public function updateBean() { } - public function loadFromBean($container, $bean) { + public function loadFromBean($bean) { } - public function loadFromJson($container, $json) { + public function loadFromJson($json) { } } diff --git a/src/api/models/Comment.php b/src/api/models/Comment.php index 885bb9c..2e99824 100644 --- a/src/api/models/Comment.php +++ b/src/api/models/Comment.php @@ -10,12 +10,33 @@ class Comment extends BaseModel { } public function updateBean() { + $bean = $this->bean; + + $bean->id = $this->id; + $bean->text = $this->text; } - public function loadFromBean($container, $bean) { + public function loadFromBean($bean) { + if (!isset($bean->id) || $bean->id === 0) { + return; + } + + $this->loadPropertiesFrom($bean); } - public function loadFromJson($container, $obj) { + 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->text = $obj->text; } } diff --git a/src/api/models/Task.php b/src/api/models/Task.php index 4fc5dd6..982002e 100644 --- a/src/api/models/Task.php +++ b/src/api/models/Task.php @@ -3,8 +3,8 @@ class Task extends BaseModel { public $id = 0; public $title = ''; public $description = ''; - public $assignee = null; // User model - public $category = null; // Category model + public $assignee_id = 0; + public $category_id = 0; // Category model public $color = ''; public $due_date = null; // Date or null if not set public $points = null; // Integer or null if not set @@ -12,30 +12,98 @@ class Task extends BaseModel { public $attachments = []; // Attachment model array public $comments = []; // Comment 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 fromJson($container, $json) { - $instance = new self($container, 0, true); - $instance->loadFromJson($container, $json); - - return $instance; - } - public function updateBean() { + $bean = $this->bean; + + $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->color = $this->color; + $bean->due_date = $this->due_date; + $bean->points = $this->points; + $bean->position = $this->position; + + $bean-> xownAttachmentList = []; + $bean->xownCommentList = []; + + foreach($this->attachments as $attachment) { + $attachment->updateBean(); + $this->xownAttachmentList[] = $attachment->bean; + } + + foreach($this->comments as $comment) { + $comment->updateBean(); + $this->xownCommentList[] = $comment->bean; + } } - public function loadFromBean($container, $bean) { + public function loadFromBean($bean) { + if (!isset($bean->id) || $bean->id === 0) { + return; + } + + $this->loadPropertiesFrom($bean); + + $this->attachments = []; + $this->comments = []; + + if (isset($bean->xownAttachmentList)) { + foreach($bean->xownAttachmentList as $item) { + $this->attachments[] = + new Attachment($this->container, $item->id); + } + } + + if (isset($bean->xownCommentList)) { + foreach($bean->xownCommentList as $item) { + $this->comments[] = new Comment($this->container, $item->id); + } + } } - public function loadFromJson($container, $json) { + public function loadFromJson($json) { + $obj = json_decode($json); + + if (!isset($obj->id) || $obj->id === 0) { + return; + } + + $this->loadPropertiesFrom($obj); + + $this->attachments = []; + $this->comments = []; + + if (isset($obj->attachments)) { + foreach($obj->attachments as $item) { + $this->comments[] = new Attachment($this->container, $item->id); + } + } + + if (isset($obj->comments)) { + foreach($obj->comments as $item) { + $this->comments[] = new Comment($this->container, $item->id); + } + } + } + + 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; } } diff --git a/src/api/models/User.php b/src/api/models/User.php index 2adb3f5..ef0026b 100644 --- a/src/api/models/User.php +++ b/src/api/models/User.php @@ -29,14 +29,14 @@ class User extends BaseModel { public static function fromBean($container, $bean) { $instance = new self($container, 0, true); - $instance->loadFromBean($container, $bean); + $instance->loadFromBean($bean); return $instance; } public static function fromJson($container, $json) { $instance = new self($container, 0, true); - $instance->loadFromJson($container, $json); + $instance->loadFromJson($json); return $instance; } @@ -44,10 +44,10 @@ class User extends BaseModel { public function updateBean() { } - public function loadFromBean($container, $bean) { + public function loadFromBean($bean) { } - public function loadFromJson($container, $obj) { + public function loadFromJson($obj) { } } diff --git a/src/api/models/UserOptions.php b/src/api/models/UserOptions.php index 3493683..490981c 100644 --- a/src/api/models/UserOptions.php +++ b/src/api/models/UserOptions.php @@ -13,10 +13,10 @@ class UserOptions extends BaseModel { public function updateBean() { } - public function loadFromBean($container, $bean) { + public function loadFromBean($bean) { } - public function loadFromJson($container, $obj) { + public function loadFromJson($obj) { } } diff --git a/test/api/ActivityTest.php b/test/api/ActivityTest.php new file mode 100644 index 0000000..6fda1f0 --- /dev/null +++ b/test/api/ActivityTest.php @@ -0,0 +1,69 @@ +json !== '') { + return; + } + + $activity = DataMock::getActivity(); + $this->json = json_encode($activity); + $this->bean = $activity; + } + + public function testCreateActivity() { + $activity = new Activity(new ContainerMock()); + $this->assertDefaultProperties($activity); + } + + public function testCreateFromBean() { + $activity = Activity::fromBean(new ContainerMock(), null); + $this->assertDefaultProperties($activity); + + $activity = Activity::fromBean(new ContainerMock(), $this->bean); + $this->assertMockProperties($activity); + } + + public function testLoadFromJson() { + $activity = new Activity(new ContainerMock()); + + $activity->loadFromJson(''); + $this->assertDefaultProperties($activity); + + $activity->loadFromJson($this->json); + $this->assertMockProperties($activity); + } + + private function assertMockProperties($activity) { + $this->assertTrue($activity->id === 1); + $this->assertTrue($activity->user_id === 1); + $this->assertTrue($activity->log_text === 'Log test.'); + $this->assertTrue($activity->before === null); + $this->assertTrue($activity->after === null); + $this->assertTrue($activity->item_type === 'test'); + $this->assertTrue($activity->item_id === 1); + } + + private function assertDefaultProperties($activity) { + $this->assertTrue($activity->id === 0); + $this->assertTrue($activity->user_id === 0); + $this->assertTrue($activity->log_text === ''); + $this->assertTrue($activity->before === null); + $this->assertTrue($activity->after === null); + $this->assertTrue($activity->item_type === ''); + $this->assertTrue($activity->item_id === 0); + } +} + diff --git a/test/api/AttachmentTest.php b/test/api/AttachmentTest.php new file mode 100644 index 0000000..66d1cd8 --- /dev/null +++ b/test/api/AttachmentTest.php @@ -0,0 +1,81 @@ +json !== '') { + return; + } + + $attachment = DataMock::getAttachment(); + $this->json = json_encode($attachment); + $this->bean = $attachment; + } + + public function testCreateAttachment() { + $attachment = new Attachment(new ContainerMock()); + $this->assertDefaultProperties($attachment); + } + + public function testLoadFromJson() { + $attachment = new Attachment(new ContainerMock()); + + $attachment->loadFromJson(''); + $this->assertDefaultProperties($attachment); + + $attachment->loadFromJson($this->json); + $this->assertMockProperties($attachment); + } + + public function testLoadFromBean() { + $attachment = new Attachment(new ContainerMock()); + + $attachment->loadFromBean($this->bean); + $this->assertMockProperties($attachment); + } + + public function testUpdateBean() { + $attachment = new Attachment(new ContainerMock()); + $attachment->loadFromBean($this->bean); + + $attachment->updateBean(); + $bean = $attachment->getBean(); + + $this->assertTrue($bean->id === $attachment->id); + $this->assertTrue($bean->filename === $attachment->filename); + $this->assertTrue($bean->name === $attachment->name); + $this->assertTrue($bean->type === $attachment->type); + $this->assertTrue($bean->user_id === $attachment->user_id); + $this->assertTrue($bean->timestamp === $attachment->timestamp); + } + + private function assertMockProperties($attachment) { + $this->assertTrue($attachment->id === 1); + $this->assertTrue($attachment->filename === 'file'); + $this->assertTrue($attachment->name === 'file.png'); + $this->assertTrue($attachment->type === 'image'); + $this->assertTrue($attachment->user_id === 1); + $this->assertTrue($attachment->timestamp === 1234567890); + } + + private function assertDefaultProperties($attachment) { + $this->assertTrue($attachment->id === 0); + $this->assertTrue($attachment->filename === ''); + $this->assertTrue($attachment->name === ''); + $this->assertTrue($attachment->type === ''); + $this->assertTrue($attachment->user_id === 0); + $this->assertTrue($attachment->timestamp === null); + } +} + diff --git a/test/api/AutoActionTest.php b/test/api/AutoActionTest.php new file mode 100644 index 0000000..e365975 --- /dev/null +++ b/test/api/AutoActionTest.php @@ -0,0 +1,85 @@ +json !== '') { + return; + } + + $action = DataMock::getAutoAction(); + $this->json = json_encode($action); + $this->bean = $action; + } + + public function testCreateAttachment() { + $action = new AutoAction(new ContainerMock()); + $this->assertDefaultProperties($action); + } + + public function testLoadFromJson() { + $action = new AutoAction(new ContainerMock()); + + $action->loadFromJson(''); + $this->assertDefaultProperties($action); + + $action->loadFromJson($this->json); + $this->assertMockProperties($action); + } + + public function testLoadFromBean() { + $action = new AutoAction(new ContainerMock()); + + $action->loadFromBean($this->bean); + $this->assertMockProperties($action); + } + + public function testUpdateBean() { + $action = new Attachment(new ContainerMock()); + $action->loadFromBean($this->bean); + + $action->updateBean(); + $bean = $action->getBean(); + + $this->assertTrue($bean->id === $action->id); + $this->assertTrue($bean->trigger === $action->trigger); + $this->assertTrue($bean->trigger_id === $action->trigger_id); + $this->assertTrue($bean->type === $action->type); + $this->assertTrue($bean->change_to === $action->change_to); + } + + private function assertMockProperties($attachment) { + $trigger = new ActionTrigger(ActionTrigger::SetToCategory); + $type = new ActionType(ActionType::ClearDueDate); + + $this->assertTrue($attachment->id === 1); + $this->assertTrue($attachment->trigger->getValue() === + $trigger->getValue()); + $this->assertTrue($attachment->trigger_id === 1); + $this->assertTrue($attachment->type->getValue() === + $type->getValue()); + $this->assertTrue($attachment->change_to === 'null'); + } + + private function assertDefaultProperties($attachment) { + $this->assertTrue($attachment->id === 0); + $this->assertTrue($attachment->trigger == + ActionTrigger::MoveToColumn); + $this->assertTrue($attachment->trigger_id === 0); + $this->assertTrue($attachment->type == ActionType::SetColor); + $this->assertTrue($attachment->change_to === ''); + + } +} + diff --git a/test/api/BoardTest.php b/test/api/BoardTest.php index 4926108..9c39e8f 100644 --- a/test/api/BoardTest.php +++ b/test/api/BoardTest.php @@ -11,13 +11,9 @@ class BoardTest extends PHPUnit_Framework_TestCase { } catch (Exception $ex) { } } - public static function tearDownAfterClass() { - if (file_exists('tests.db')) { - unlink('tests.db'); - } - } - protected function setUp() { + RedBeanPHP\R::nuke(); + if ($this->json !== '') { return; } @@ -25,6 +21,21 @@ class BoardTest extends PHPUnit_Framework_TestCase { $board = DataMock::getBoard(); $this->json = json_encode($board); $this->bean = $board; + // Convert to bean format + $this->bean->xownColumnList = $board->columns; + $this->bean->xownCategoryList = $board->categories; + $this->bean->xownAutoActionList = $board->auto_actions; + $this->bean->ownUserList = $board->users; + } + + private function assertDefaultProperties($board) { + $this->assertTrue($board->id === 0); + $this->assertTrue($board->name === ''); + $this->assertTrue($board->is_active === true); + $this->assertArraySubset($board->columns, []); + $this->assertArraySubset($board->categories, []); + $this->assertArraySubset($board->auto_actions, []); + $this->assertArraySubset($board->users, []); } // Just to get the complete code coverage @@ -39,30 +50,25 @@ class BoardTest extends PHPUnit_Framework_TestCase { public function testCreateNewBoard() { $board = new Board(new ContainerMock()); - $this->assertTrue($board->id === 0); - $this->assertTrue($board->name === ''); - $this->assertTrue($board->is_active === true); - $this->assertArraySubset($board->columns, []); + $this->assertDefaultProperties($board); } public function testCreateFromBean() { $board = Board::fromBean(new ContainerMock(), null); - $this->assertTrue($board->id === 0); - $this->assertTrue($board->name === ''); - $this->assertTrue($board->is_active === true); - $this->assertArraySubset($board->columns, []); + $this->assertDefaultProperties($board); $board = Board::fromBean(new ContainerMock(), $this->bean); + + $this->assertTrue($board->id === 1); + $this->assertTrue($board->name === 'test'); + $this->assertTrue($board->is_active === true); } public function testCreateFromJson() { $board = Board::fromJson(new ContainerMock(), null); - $this->assertTrue($board->id === 0); - $this->assertTrue($board->name === ''); - $this->assertTrue($board->is_active === true); - $this->assertArraySubset($board->columns, []); + $this->assertDefaultProperties($board); $board = Board::fromJson(new ContainerMock(), $this->json); @@ -71,18 +77,33 @@ class BoardTest extends PHPUnit_Framework_TestCase { $this->assertTrue($board->is_active === true); } - public function testSaveAndDelete() { - $board = Board::fromJson(new ContainerMock(), - json_encode(DataMock::getBoard())); + public function testSaveLoadDelete() { + $board = Board::fromJson(new ContainerMock(), $this->json); + $board->save(); + $this->assertTrue($board->id === 1); $board = new Board(new ContainerMock(), 1); $this->assertTrue($board->id === 1); $board->delete(); - $board = new Board(new ContainerMock(), $board->id); + $board = new Board(new ContainerMock(), 1); $this->assertTrue($board->id === 0); } + + public function testGetBean() { + $board = new Board(new ContainerMock()); + $bean = $board->getBean(); + + // Make sure bean properties exist + $this->assertTrue($bean->id === 0); + $this->assertArraySubset($bean->xownColumnList, []); + + $board->save(); + $bean = $board->getBean(); + + $this->assertTrue((int)$bean->id === 1); + } } diff --git a/test/api/BoardsTest.php b/test/api/BoardsTest.php index 513031b..c0af81c 100644 --- a/test/api/BoardsTest.php +++ b/test/api/BoardsTest.php @@ -10,13 +10,9 @@ class BoardsTest extends PHPUnit_Framework_TestCase { } catch (Exception $ex) { } } - public static function tearDownAfterClass() { - if (file_exists('tests.db')) { - unlink('tests.db'); - } - } + public function setUp() { + RedBeanPHP\R::nuke(); - public function setup() { $this->boards = new Boards(new ContainerMock()); } @@ -39,26 +35,24 @@ class BoardsTest extends PHPUnit_Framework_TestCase { $this->boards->getBoard(null, new ResponseMock(), $args)); } - public function testAddBoard() { + public function testAddRemoveBoard() { $expected = new ApiJson(); - $expected->addAlert('error', 'Error adding board. ' . - 'Please check your entries and try again.'); + $expected->setSuccess(); + $expected->addAlert('success', 'Board added.'); - $this->assertEquals($expected, - $this->boards->addBoard(new RequestMock(), - new ResponseMock(), null)); - } + $actual = $this->boards->addBoard(new RequestMock(), + new ResponseMock(), null); - public function testRemoveBoard() { - $expected = new ApiJson(); - $expected->addAlert('error', 'Error removing board. ' . - 'No board found for ID 1.'); + $this->assertEquals($expected, $actual); + + $expected->addAlert('success', 'Board removed.'); $args = []; $args['id'] = '1'; - $this->assertEquals($expected, - $this->boards->removeBoard(null, new ResponseMock(), $args)); + $actual = $this->boards->removeBoard(null, new ResponseMock(), $args); + + $this->assertEquals($expected, $actual); } } diff --git a/test/api/CommentTest.php b/test/api/CommentTest.php new file mode 100644 index 0000000..e1a1d99 --- /dev/null +++ b/test/api/CommentTest.php @@ -0,0 +1,69 @@ +json !== '') { + return; + } + + $comment = DataMock::getComment(); + $this->json = json_encode($comment); + $this->bean = $comment; + } + + public function testCreateComment() { + $comment = new Comment(new ContainerMock()); + $this->assertDefaultProperties($comment); + } + + public function testLoadFromBean() { + $comment = new Comment(new ContainerMock()); + + $comment->loadFromBean($this->bean); + $this->assertMockProperties($comment); + } + + public function testLoadFromJson() { + $comment = new Comment(new ContainerMock()); + + $comment->loadFromJson(''); + $this->assertDefaultProperties($comment); + + $comment->loadFromJson($this->json); + $this->assertMockProperties($comment); + } + + public function testUpdateBean() { + $comment = new Comment(new ContainerMock()); + $comment->loadFromBean($this->bean); + + $comment->updateBean(); + $bean = $comment->getBean(); + + $this->assertTrue($bean->id === $comment->id); + $this->assertTrue($bean->text === $comment->text); + } + + private function assertDefaultProperties($comment) { + $this->assertTrue($comment->id === 0); + $this->assertTrue($comment->text === ''); + } + + private function assertMockProperties($comment) { + $this->assertTrue($comment->id === 1); + $this->assertTrue($comment->text === 'test comment'); + } +} + diff --git a/test/api/Mocks.php b/test/api/Mocks.php index 870e0a9..2a576ed 100644 --- a/test/api/Mocks.php +++ b/test/api/Mocks.php @@ -43,10 +43,10 @@ class DataMock { public static function getAutoAction() { $auto_action = new stdClass(); $auto_action->id = 1; - $auto_action->trigger = ActionTrigger::MoveToColumn; + $auto_action->trigger = ActionTrigger::SetToCategory; $auto_action->trigger_id = 1; - $auto_action->type = ActionType::SetColor; - $auto_action->color = '#ffffff'; + $auto_action->type = ActionType::ClearDueDate; + $auto_action->change_to = 'null'; return $auto_action; } @@ -59,6 +59,57 @@ class DataMock { return $user; } + + public static function getActivity() { + $activity = new stdClass(); + $activity->id = 1; + $activity->user_id = 1; + $activity->log_text = 'Log test.'; + $activity->before = null; + $activity->after = null; + $activity->item_type = 'test'; + $activity->item_id = 1; + + return $activity; + } + + public static function getAttachment() { + $attachment = new stdClass(); + $attachment->id = 1; + $attachment->filename = 'file'; + $attachment->name = 'file.png'; + $attachment->type = 'image'; + $attachment->user_id = 1; + $attachment->timestamp = 1234567890; + + return $attachment; + } + + public static function getComment() { + $comment = new stdClass(); + + $comment->id = 1; + $comment->text = 'test comment'; + + return $comment; + } + + public static function getTask() { + $task = new stdClass(); + $task->id = 1; + $task->title = 'test'; + $task->description = 'description'; + $task->assignee_id = 1; + $task->category_id = 1; + $task->color = '#ffffff'; + $task->due_date = 1234567890; + $task->points = 3; + $task->position = 1; + $task->attachments = DataMock::getAttachment(); + $task->comments = DataMock::getComment(); + + return $task; + } } class LoggerMock { @@ -67,6 +118,16 @@ class LoggerMock { } public function addError() { + // Uncomment to see error logs in test output + // $msg = func_get_arg(0); + // print 'API ERROR: ' . $msg; + + // $objs = func_get_args(); + // array_splice($objs, 0, 1); + + // foreach($objs as $obj) { + // var_dump($obj); + // } } } @@ -82,7 +143,7 @@ class ContainerMock { class RequestMock { public function getBody() { - return '{}'; + return '{ "id": 1 }'; } } diff --git a/test/api/TaskTest.php b/test/api/TaskTest.php new file mode 100644 index 0000000..4e2483d --- /dev/null +++ b/test/api/TaskTest.php @@ -0,0 +1,86 @@ +json !== '') { + return; + } + + $task = DataMock::getTask(); + $this->json = json_encode($task); + $this->bean = $task; + // Convert to bean format + $this->bean->xownAttachmentList = $task->attachments; + $this->bean->xownCommentList = $task->comments; + } + + public function testCreateTask() { + $task = new Task(new ContainerMock()); + $this->assertDefaultProperties($task); + } + + public function testLoadFromBean() { + $task = new Task(new ContainerMock()); + + $task->loadFromBean($this->bean); + $this->assertMockProperties($task); + } + + public function testLoadFromJson() { + $task = new Task(new ContainerMock()); + + $task->loadFromJson(''); + $this->assertDefaultProperties($task); + + $task->loadFromJson($this->json); + $this->assertMockProperties($task); + } + + public function testUpdateBean() { + $task = new Task(new ContainerMock()); + $task->loadFromBean($this->bean); + + $task->updateBean(); + $bean = $task->getBean(); + + $this->assertTrue($bean->id === $task->id); + $this->assertTrue($bean->text === $task->text); + } + + private function assertDefaultProperties($task) { + $this->assertTrue($task->id === 0); + $this->assertTrue($task->title === ''); + $this->assertTrue($task->description === ''); + $this->assertTrue($task->assignee_id === 0); + $this->assertTrue($task->category_id === 0); + $this->assertTrue($task->color === ''); + $this->assertTrue($task->due_date === null); + $this->assertTrue($task->points === null); + $this->assertTrue($task->position === 0); + } + + private function assertMockProperties($task) { + $this->assertTrue($task->id === 1); + $this->assertTrue($task->title === 'test'); + $this->assertTrue($task->description === 'description'); + $this->assertTrue($task->assignee_id === 1); + $this->assertTrue($task->category_id === 1); + $this->assertTrue($task->color === '#ffffff'); + $this->assertTrue($task->due_date === 1234567890); + $this->assertTrue($task->points === 3); + $this->assertTrue($task->position === 1); + } +} + diff --git a/tests.db b/tests.db new file mode 100644 index 0000000000000000000000000000000000000000..b5beae0251b283cddd6ec808135b4a89f8c0bec3 GIT binary patch literal 12288 zcmWFz^vNtqRY=P(%1ta$FlJz3U}R))P*7lC5U65cVBle3U|?ZjVBlh4;E{u}8K8g> zO5>pfsu*;iG4V34WME_rXJ8CxZe^YVf_StH52*kqHgRoj#>kSyq@2{^!ko;K)cE4m z!qU{dWOOdGbC9cJh^s<~qmz%T0-9Qlyu{p8o#NC&O$G+0@WB!`{e#6J5O-XLP(zxm z9@4HKb^K@ujE2Ba3;|w{qgfajn7=V!1VIpY6px0$Xb2455YUG?T@A*7(qd2=L~(%# zIOb$vU|?flU|?lnVBlt8U;yz!DmWMz7}%lx04BKVQO0NpjE2By2#kinXb6mkz-S1J zhQMeDjE2By2#kinXb6mkz-S1JhQMeDjE2By2#kinXb6mkz-S1JhQMeDjE2By2#kin WXb6mkz-S1JhQMeDjE2CV2mt`W#V;oS literal 0 HcmV?d00001