summaryrefslogtreecommitdiff
path: root/tests/units/Model
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2016-01-16 19:19:05 -0500
committerFrederic Guillot <fred@kanboard.net>2016-01-16 19:19:05 -0500
commit2e4c2b6e0571690e8b8d0488dbb53e3373a86ff5 (patch)
treece725462022d39d8b8088cfdab4dd7e05b396d36 /tests/units/Model
parent73ff5ec89b711791b3993f9158dc9554a623602c (diff)
Improve class ProjectDailyColumnStats
Diffstat (limited to 'tests/units/Model')
-rw-r--r--tests/units/Model/ProjectDailyColumnStatsTest.php290
1 files changed, 216 insertions, 74 deletions
diff --git a/tests/units/Model/ProjectDailyColumnStatsTest.php b/tests/units/Model/ProjectDailyColumnStatsTest.php
index 4c801e02..f8a64054 100644
--- a/tests/units/Model/ProjectDailyColumnStatsTest.php
+++ b/tests/units/Model/ProjectDailyColumnStatsTest.php
@@ -4,6 +4,7 @@ require_once __DIR__.'/../Base.php';
use Kanboard\Model\Project;
use Kanboard\Model\ProjectDailyColumnStats;
+use Kanboard\Model\Config;
use Kanboard\Model\Task;
use Kanboard\Model\TaskCreation;
use Kanboard\Model\TaskStatus;
@@ -12,79 +13,220 @@ class ProjectDailyColumnStatsTest extends Base
{
public function testUpdateTotals()
{
- $p = new Project($this->container);
- $pds = new ProjectDailyColumnStats($this->container);
- $tc = new TaskCreation($this->container);
- $ts = new TaskStatus($this->container);
-
- $this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
- $this->assertEquals(0, $pds->countDays(1, date('Y-m-d', strtotime('-2days')), date('Y-m-d')));
-
- for ($i = 0; $i < 10; $i++) {
- $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 1)));
- }
-
- for ($i = 0; $i < 5; $i++) {
- $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 4)));
- }
-
- $pds->updateTotals(1, date('Y-m-d', strtotime('-2days')));
-
- for ($i = 0; $i < 15; $i++) {
- $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 3)));
- }
-
- for ($i = 0; $i < 25; $i++) {
- $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 2)));
- }
-
- $pds->updateTotals(1, date('Y-m-d', strtotime('-1 day')));
-
- $this->assertNotFalse($ts->close(1));
- $this->assertNotFalse($ts->close(2));
-
- for ($i = 0; $i < 3; $i++) {
- $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 3)));
- }
-
- for ($i = 0; $i < 5; $i++) {
- $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 2)));
- }
-
- for ($i = 0; $i < 4; $i++) {
- $this->assertNotFalse($tc->create(array('title' => 'Task #'.$i, 'project_id' => 1, 'column_id' => 4)));
- }
-
- $pds->updateTotals(1, date('Y-m-d'));
-
- $this->assertEquals(3, $pds->countDays(1, date('Y-m-d', strtotime('-2days')), date('Y-m-d')));
- $metrics = $pds->getAggregatedMetrics(1, date('Y-m-d', strtotime('-2days')), date('Y-m-d'));
-
- $this->assertNotEmpty($metrics);
- $this->assertEquals(4, count($metrics));
- $this->assertEquals(5, count($metrics[0]));
- $this->assertEquals('Date', $metrics[0][0]);
- $this->assertEquals('Backlog', $metrics[0][1]);
- $this->assertEquals('Ready', $metrics[0][2]);
- $this->assertEquals('Work in progress', $metrics[0][3]);
- $this->assertEquals('Done', $metrics[0][4]);
-
- $this->assertEquals(date('Y-m-d', strtotime('-2days')), $metrics[1][0]);
- $this->assertEquals(10, $metrics[1][1]);
- $this->assertEquals(0, $metrics[1][2]);
- $this->assertEquals(0, $metrics[1][3]);
- $this->assertEquals(5, $metrics[1][4]);
-
- $this->assertEquals(date('Y-m-d', strtotime('-1day')), $metrics[2][0]);
- $this->assertEquals(10, $metrics[2][1]);
- $this->assertEquals(25, $metrics[2][2]);
- $this->assertEquals(15, $metrics[2][3]);
- $this->assertEquals(5, $metrics[2][4]);
-
- $this->assertEquals(date('Y-m-d'), $metrics[3][0]);
- $this->assertEquals(10, $metrics[3][1]);
- $this->assertEquals(30, $metrics[3][2]);
- $this->assertEquals(18, $metrics[3][3]);
- $this->assertEquals(9, $metrics[3][4]);
+ $projectModel = new Project($this->container);
+ $projectDailyColumnStats = new ProjectDailyColumnStats($this->container);
+ $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest')));
+
+ $this->createTasks(1, 2, 1);
+ $this->createTasks(1, 3, 0);
+
+ $this->createTasks(2, 5, 1);
+ $this->createTasks(2, 8, 1);
+ $this->createTasks(2, 0, 0);
+ $this->createTasks(2, 0, 0);
+
+ $projectDailyColumnStats->updateTotals(1, '2016-01-16');
+
+ $this->createTasks(1, 9, 1);
+ $this->createTasks(1, 7, 0);
+
+ $projectDailyColumnStats->updateTotals(1, '2016-01-16');
+
+ $this->createTasks(3, 0, 1);
+
+ $projectDailyColumnStats->updateTotals(1, '2016-01-17');
+
+ $stats = $this->container['db']->table(ProjectDailyColumnStats::TABLE)
+ ->asc('day')
+ ->asc('column_id')
+ ->columns('day', 'project_id', 'column_id', 'total', 'score')
+ ->findAll();
+
+ $expected = array(
+ array(
+ 'day' => '2016-01-16',
+ 'project_id' => 1,
+ 'column_id' => 1,
+ 'total' => 4,
+ 'score' => 11,
+ ),
+ array(
+ 'day' => '2016-01-16',
+ 'project_id' => 1,
+ 'column_id' => 2,
+ 'total' => 4,
+ 'score' => 13,
+ ),
+ array(
+ 'day' => '2016-01-17',
+ 'project_id' => 1,
+ 'column_id' => 1,
+ 'total' => 4,
+ 'score' => 11,
+ ),
+ array(
+ 'day' => '2016-01-17',
+ 'project_id' => 1,
+ 'column_id' => 2,
+ 'total' => 4,
+ 'score' => 13,
+ ),
+ array(
+ 'day' => '2016-01-17',
+ 'project_id' => 1,
+ 'column_id' => 3,
+ 'total' => 1,
+ 'score' => 0,
+ ),
+ );
+
+ $this->assertEquals($expected, $stats);
+ }
+
+ public function testUpdateTotalsWithOnlyOpenTasks()
+ {
+ $configModel = new Config($this->container);
+ $projectModel = new Project($this->container);
+ $projectDailyColumnStats = new ProjectDailyColumnStats($this->container);
+
+ $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest')));
+ $this->assertTrue($configModel->save(array('cfd_include_closed_tasks' => 0)));
+ $this->container['memoryCache']->flush();
+
+ $this->createTasks(1, 2, 1);
+ $this->createTasks(1, 3, 0);
+
+ $this->createTasks(2, 5, 1);
+ $this->createTasks(2, 8, 1);
+ $this->createTasks(2, 0, 0);
+ $this->createTasks(2, 0, 0);
+
+ $projectDailyColumnStats->updateTotals(1, '2016-01-16');
+
+ $this->createTasks(1, 9, 1);
+ $this->createTasks(1, 7, 0);
+
+ $projectDailyColumnStats->updateTotals(1, '2016-01-16');
+
+ $this->createTasks(3, 0, 1);
+
+ $projectDailyColumnStats->updateTotals(1, '2016-01-17');
+
+ $stats = $this->container['db']->table(ProjectDailyColumnStats::TABLE)
+ ->asc('day')
+ ->asc('column_id')
+ ->columns('day', 'project_id', 'column_id', 'total', 'score')
+ ->findAll();
+
+ $expected = array(
+ array(
+ 'day' => '2016-01-16',
+ 'project_id' => 1,
+ 'column_id' => 1,
+ 'total' => 2,
+ 'score' => 11,
+ ),
+ array(
+ 'day' => '2016-01-16',
+ 'project_id' => 1,
+ 'column_id' => 2,
+ 'total' => 2,
+ 'score' => 13,
+ ),
+ array(
+ 'day' => '2016-01-17',
+ 'project_id' => 1,
+ 'column_id' => 1,
+ 'total' => 2,
+ 'score' => 11,
+ ),
+ array(
+ 'day' => '2016-01-17',
+ 'project_id' => 1,
+ 'column_id' => 2,
+ 'total' => 2,
+ 'score' => 13,
+ ),
+ array(
+ 'day' => '2016-01-17',
+ 'project_id' => 1,
+ 'column_id' => 3,
+ 'total' => 1,
+ 'score' => 0,
+ ),
+ );
+
+ $this->assertEquals($expected, $stats);
+ }
+
+ public function testCountDays()
+ {
+ $projectModel = new Project($this->container);
+ $projectDailyColumnStats = new ProjectDailyColumnStats($this->container);
+
+ $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest')));
+
+ $this->createTasks(1, 2, 1);
+ $projectDailyColumnStats->updateTotals(1, '2016-01-16');
+ $this->assertEquals(1, $projectDailyColumnStats->countDays(1, '2016-01-16', '2016-01-17'));
+
+ $projectDailyColumnStats->updateTotals(1, '2016-01-17');
+ $this->assertEquals(2, $projectDailyColumnStats->countDays(1, '2016-01-16', '2016-01-17'));
+ }
+
+ public function testGetAggregatedMetrics()
+ {
+ $projectModel = new Project($this->container);
+ $projectDailyColumnStats = new ProjectDailyColumnStats($this->container);
+ $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest')));
+
+ $this->createTasks(1, 2, 1);
+ $this->createTasks(1, 3, 0);
+
+ $this->createTasks(2, 5, 1);
+ $this->createTasks(2, 8, 1);
+ $this->createTasks(2, 0, 0);
+ $this->createTasks(2, 0, 0);
+
+ $projectDailyColumnStats->updateTotals(1, '2016-01-16');
+
+ $this->createTasks(1, 9, 1);
+ $this->createTasks(1, 7, 0);
+
+ $projectDailyColumnStats->updateTotals(1, '2016-01-16');
+
+ $this->createTasks(3, 0, 1);
+
+ $projectDailyColumnStats->updateTotals(1, '2016-01-17');
+
+ $this->createTasks(2, 1, 1);
+ $this->createTasks(3, 1, 1);
+ $this->createTasks(3, 0, 1);
+
+ $projectDailyColumnStats->updateTotals(1, '2016-01-18');
+
+ $expected = array(
+ array('Date', 'Backlog', 'Ready', 'Work in progress', 'Done'),
+ array('2016-01-16', 4, 4, 0, 0),
+ array('2016-01-17', 4, 4, 1, 0),
+ array('2016-01-18', 4, 5, 3, 0),
+ );
+
+ $this->assertEquals($expected, $projectDailyColumnStats->getAggregatedMetrics(1, '2016-01-16', '2016-01-18'));
+
+ $expected = array(
+ array('Date', 'Backlog', 'Ready', 'Work in progress', 'Done'),
+ array('2016-01-16', 11, 13, 0, 0),
+ array('2016-01-17', 11, 13, 0, 0),
+ array('2016-01-18', 11, 14, 1, 0),
+ );
+
+ $this->assertEquals($expected, $projectDailyColumnStats->getAggregatedMetrics(1, '2016-01-16', '2016-01-18', 'score'));
+ }
+
+ private function createTasks($column_id, $score, $is_active)
+ {
+ $taskCreationModel = new TaskCreation($this->container);
+ $this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'column_id' => $column_id, 'score' => $score, 'is_active' => $is_active)));
}
}