Unit test updates and implementation

This commit is contained in:
kiswa 2016-04-30 02:28:53 +00:00
parent 579d798176
commit ad6a5ec4a4
25 changed files with 839 additions and 153 deletions

View File

@ -1,14 +1,13 @@
<phpunit>
<filter>
<blacklist>
<directory>
src/api/vendor
</directory>
</blacklist>
<whitelist>
<directory>src/api/controllers</directory>
<directory>src/api/models</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="api-coverage/"
lowUpperBound="35" highLowerBound="75" />
lowUpperBound="40" highLowerBound="80" />
</logging>
</phpunit>

View File

@ -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

View File

@ -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);

View File

@ -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"

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -1,5 +1,6 @@
<?php
class Attachment extends BaseModel {
public $id = 0;
public $filename = '';
public $name = '';
public $type = '';
@ -13,12 +14,41 @@ class Attachment extends BaseModel {
}
public function updateBean() {
$bean = $this->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;
}
}

View File

@ -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;
}
}

View File

@ -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()
]);
}
}

View File

@ -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 = [];
}
}

View File

@ -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) {
}
}

View File

@ -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) {
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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) {
}
}

View File

@ -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) {
}
}

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

@ -0,0 +1,69 @@
<?php
require_once 'Mocks.php';
class ActivityTest 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;
}
$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);
}
}

View File

@ -0,0 +1,81 @@
<?php
require_once 'Mocks.php';
class AttachmentTest 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;
}
$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);
}
}

View File

@ -0,0 +1,85 @@
<?php
require_once 'Mocks.php';
class AutoActionTest 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;
}
$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 === '');
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

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

@ -0,0 +1,69 @@
<?php
require_once 'Mocks.php';
class CommentTest 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;
}
$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');
}
}

View File

@ -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 }';
}
}

86
test/api/TaskTest.php Normal file
View File

@ -0,0 +1,86 @@
<?php
require_once 'Mocks.php';
class TaskTest 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::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);
}
}

BIN
tests.db Normal file

Binary file not shown.