Move analytic logic to separate classes

This commit is contained in:
Frederic Guillot
2016-01-15 21:03:19 -05:00
parent 1bbd4faf56
commit c58478c0ab
13 changed files with 424 additions and 134 deletions

View File

@@ -0,0 +1,99 @@
<?php
require_once __DIR__.'/../Base.php';
use Kanboard\Model\TaskCreation;
use Kanboard\Model\Project;
use Kanboard\Analytic\EstimatedTimeComparisonAnalytic;
class EstimatedTimeComparisonAnalyticTest extends Base
{
public function testBuild()
{
$taskCreationModel = new TaskCreation($this->container);
$projectModel = new Project($this->container);
$estimatedTimeComparisonAnalytic = new EstimatedTimeComparisonAnalytic($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
$this->assertEquals(2, $projectModel->create(array('name' => 'test1')));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_estimated' => 5.5)));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_estimated' => 1.75)));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_estimated' => 1.25, 'is_active' => 0)));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_spent' => 8.25)));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_spent' => 0.25)));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_spent' => 0.5, 'is_active' => 0)));
$expected = array(
'open' => array(
'time_spent' => 8.5,
'time_estimated' => 7.25,
),
'closed' => array(
'time_spent' => 0.5,
'time_estimated' => 1.25,
)
);
$this->assertEquals($expected, $estimatedTimeComparisonAnalytic->build(1));
}
public function testBuildWithNoClosedTask()
{
$taskCreationModel = new TaskCreation($this->container);
$projectModel = new Project($this->container);
$estimatedTimeComparisonAnalytic = new EstimatedTimeComparisonAnalytic($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
$this->assertEquals(2, $projectModel->create(array('name' => 'test1')));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_estimated' => 5.5)));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_estimated' => 1.75)));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_spent' => 8.25)));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_spent' => 0.25)));
$expected = array(
'open' => array(
'time_spent' => 8.5,
'time_estimated' => 7.25,
),
'closed' => array(
'time_spent' => 0,
'time_estimated' => 0,
)
);
$this->assertEquals($expected, $estimatedTimeComparisonAnalytic->build(1));
}
public function testBuildWithOnlyClosedTask()
{
$taskCreationModel = new TaskCreation($this->container);
$projectModel = new Project($this->container);
$estimatedTimeComparisonAnalytic = new EstimatedTimeComparisonAnalytic($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
$this->assertEquals(2, $projectModel->create(array('name' => 'test1')));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_estimated' => 5.5, 'is_active' => 0)));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_estimated' => 1.75, 'is_active' => 0)));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_spent' => 8.25, 'is_active' => 0)));
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'time_spent' => 0.25, 'is_active' => 0)));
$expected = array(
'closed' => array(
'time_spent' => 8.5,
'time_estimated' => 7.25,
),
'open' => array(
'time_spent' => 0,
'time_estimated' => 0,
)
);
$this->assertEquals($expected, $estimatedTimeComparisonAnalytic->build(1));
}
}

View File

@@ -0,0 +1,62 @@
<?php
require_once __DIR__.'/../Base.php';
use Kanboard\Model\TaskCreation;
use Kanboard\Model\Project;
use Kanboard\Analytic\TaskDistributionAnalytic;
class TaskDistributionAnalyticTest extends Base
{
public function testBuild()
{
$projectModel = new Project($this->container);
$taskDistributionModel = new TaskDistributionAnalytic($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
$this->assertEquals(2, $projectModel->create(array('name' => 'test1')));
$this->createTasks(1, 20, 1);
$this->createTasks(2, 30, 1);
$this->createTasks(3, 40, 1);
$this->createTasks(4, 10, 1);
$expected = array(
array(
'column_title' => 'Backlog',
'nb_tasks' => 20,
'percentage' => 20.0,
),
array(
'column_title' => 'Ready',
'nb_tasks' => 30,
'percentage' => 30.0,
),
array(
'column_title' => 'Work in progress',
'nb_tasks' => 40,
'percentage' => 40.0,
),
array(
'column_title' => 'Done',
'nb_tasks' => 10,
'percentage' => 10.0,
)
);
$this->assertEquals($expected, $taskDistributionModel->build(1));
}
private function createTasks($column_id, $nb_active, $nb_inactive)
{
$taskCreationModel = new TaskCreation($this->container);
for ($i = 0; $i < $nb_active; $i++) {
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'column_id' => $column_id, 'is_active' => 1)));
}
for ($i = 0; $i < $nb_inactive; $i++) {
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'column_id' => $column_id, 'is_active' => 0)));
}
}
}

View File

@@ -0,0 +1,83 @@
<?php
require_once __DIR__.'/../Base.php';
use Kanboard\Model\TaskCreation;
use Kanboard\Model\Project;
use Kanboard\Model\ProjectUserRole;
use Kanboard\Model\User;
use Kanboard\Analytic\UserDistributionAnalytic;
use Kanboard\Core\Security\Role;
class UserDistributionAnalyticTest extends Base
{
public function testBuild()
{
$projectModel = new Project($this->container);
$projectUserRoleModel = new ProjectUserRole($this->container);
$userModel = new User($this->container);
$userDistributionModel = new UserDistributionAnalytic($this->container);
$this->assertEquals(1, $projectModel->create(array('name' => 'test1')));
$this->assertEquals(2, $projectModel->create(array('name' => 'test1')));
$this->assertEquals(2, $userModel->create(array('username' => 'user1')));
$this->assertEquals(3, $userModel->create(array('username' => 'user2')));
$this->assertEquals(4, $userModel->create(array('username' => 'user3')));
$this->assertEquals(5, $userModel->create(array('username' => 'user4')));
$this->assertTrue($projectUserRoleModel->addUser(1, 2, Role::PROJECT_MEMBER));
$this->assertTrue($projectUserRoleModel->addUser(1, 3, Role::PROJECT_MEMBER));
$this->assertTrue($projectUserRoleModel->addUser(1, 4, Role::PROJECT_MEMBER));
$this->assertTrue($projectUserRoleModel->addUser(1, 5, Role::PROJECT_MEMBER));
$this->createTasks(0, 10, 1);
$this->createTasks(2, 30, 1);
$this->createTasks(3, 40, 1);
$this->createTasks(4, 10, 1);
$this->createTasks(5, 10, 1);
$expected = array(
array(
'user' => 'Unassigned',
'nb_tasks' => 10,
'percentage' => 10.0,
),
array(
'user' => 'user1',
'nb_tasks' => 30,
'percentage' => 30.0,
),
array(
'user' => 'user2',
'nb_tasks' => 40,
'percentage' => 40.0,
),
array(
'user' => 'user3',
'nb_tasks' => 10,
'percentage' => 10.0,
),
array(
'user' => 'user4',
'nb_tasks' => 10,
'percentage' => 10.0,
)
);
$this->assertEquals($expected, $userDistributionModel->build(1));
}
private function createTasks($user_id, $nb_active, $nb_inactive)
{
$taskCreationModel = new TaskCreation($this->container);
for ($i = 0; $i < $nb_active; $i++) {
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'owner_id' => $user_id, 'is_active' => 1)));
}
for ($i = 0; $i < $nb_inactive; $i++) {
$this->assertNotFalse($taskCreationModel->create(array('project_id' => 1, 'title' => 'test', 'owner_id' => $user_id, 'is_active' => 0)));
}
}
}