Add unit test for the "task close" action
This commit is contained in:
@@ -2,30 +2,101 @@
|
|||||||
|
|
||||||
namespace Action;
|
namespace Action;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for automatic actions
|
||||||
|
*
|
||||||
|
* @package action
|
||||||
|
* @author Frederic Guillot
|
||||||
|
*/
|
||||||
abstract class Base implements \Core\Listener
|
abstract class Base implements \Core\Listener
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Project id
|
||||||
|
*
|
||||||
|
* @access private
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
private $project_id = 0;
|
private $project_id = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User parameters
|
||||||
|
*
|
||||||
|
* @access private
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
private $params = array();
|
private $params = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the action
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access public
|
||||||
|
* @param array $data Event data dictionary
|
||||||
|
* @return bool True if the action was executed or false when not executed
|
||||||
|
*/
|
||||||
abstract public function doAction(array $data);
|
abstract public function doAction(array $data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the required parameter for the action (defined by the user)
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access public
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
abstract public function getActionRequiredParameters();
|
abstract public function getActionRequiredParameters();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the required parameter for the event (check if for the event data)
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access public
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
abstract public function getEventRequiredParameters();
|
abstract public function getEventRequiredParameters();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param integer $project_id Project id
|
||||||
|
*/
|
||||||
public function __construct($project_id)
|
public function __construct($project_id)
|
||||||
{
|
{
|
||||||
$this->project_id = $project_id;
|
$this->project_id = $project_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set an user defined parameter
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $name Parameter name
|
||||||
|
* @param mixed $value Value
|
||||||
|
*/
|
||||||
public function setParam($name, $value)
|
public function setParam($name, $value)
|
||||||
{
|
{
|
||||||
$this->params[$name] = $value;
|
$this->params[$name] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an user defined parameter
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $name Parameter name
|
||||||
|
* @param mixed $default_value Default value
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
public function getParam($name, $default_value = null)
|
public function getParam($name, $default_value = null)
|
||||||
{
|
{
|
||||||
return isset($this->params[$name]) ? $this->params[$name] : $default_value;
|
return isset($this->params[$name]) ? $this->params[$name] : $default_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if an action is executable (right project and required parameters)
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param array $data Event data dictionary
|
||||||
|
* @return bool True if the action is executable
|
||||||
|
*/
|
||||||
public function isExecutable(array $data)
|
public function isExecutable(array $data)
|
||||||
{
|
{
|
||||||
if (isset($data['project_id']) && $data['project_id'] == $this->project_id && $this->hasRequiredParameters($data)) {
|
if (isset($data['project_id']) && $data['project_id'] == $this->project_id && $this->hasRequiredParameters($data)) {
|
||||||
@@ -35,6 +106,13 @@ abstract class Base implements \Core\Listener
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the event data has required parameters to execute the action
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param array $data Event data dictionary
|
||||||
|
* @return bool True if all keys are there
|
||||||
|
*/
|
||||||
public function hasRequiredParameters(array $data)
|
public function hasRequiredParameters(array $data)
|
||||||
{
|
{
|
||||||
foreach ($this->getEventRequiredParameters() as $parameter) {
|
foreach ($this->getEventRequiredParameters() as $parameter) {
|
||||||
@@ -44,6 +122,13 @@ abstract class Base implements \Core\Listener
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the action
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param array $data Event data dictionary
|
||||||
|
* @return bool True if the action was executed or false when not executed
|
||||||
|
*/
|
||||||
public function execute(array $data)
|
public function execute(array $data)
|
||||||
{
|
{
|
||||||
if ($this->isExecutable($data)) {
|
if ($this->isExecutable($data)) {
|
||||||
|
|||||||
@@ -4,14 +4,33 @@ namespace Action;
|
|||||||
|
|
||||||
require_once __DIR__.'/base.php';
|
require_once __DIR__.'/base.php';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close automatically a task
|
||||||
|
*
|
||||||
|
* @package action
|
||||||
|
* @author Frederic Guillot
|
||||||
|
*/
|
||||||
class TaskClose extends Base
|
class TaskClose extends Base
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param integer $project_id Project id
|
||||||
|
* @param Task $task Task model instance
|
||||||
|
*/
|
||||||
public function __construct($project_id, \Model\Task $task)
|
public function __construct($project_id, \Model\Task $task)
|
||||||
{
|
{
|
||||||
parent::__construct($project_id);
|
parent::__construct($project_id);
|
||||||
$this->task = $task;
|
$this->task = $task;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the required parameter for the action (defined by the user)
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function getActionRequiredParameters()
|
public function getActionRequiredParameters()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
@@ -19,6 +38,13 @@ class TaskClose extends Base
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the required parameter for the event (check if for the event data)
|
||||||
|
*
|
||||||
|
* @abstract
|
||||||
|
* @access public
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function getEventRequiredParameters()
|
public function getEventRequiredParameters()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
@@ -27,6 +53,13 @@ class TaskClose extends Base
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the action
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param array $data Event data dictionary
|
||||||
|
* @return bool True if the action was executed or false when not executed
|
||||||
|
*/
|
||||||
public function doAction(array $data)
|
public function doAction(array $data)
|
||||||
{
|
{
|
||||||
if ($data['column_id'] == $this->getParam('column_id')) {
|
if ($data['column_id'] == $this->getParam('column_id')) {
|
||||||
|
|||||||
62
tests/ActionTaskCloseTest.php
Normal file
62
tests/ActionTaskCloseTest.php
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__.'/base.php';
|
||||||
|
|
||||||
|
use Model\Task;
|
||||||
|
|
||||||
|
class ActionTaskCloseTest extends Base
|
||||||
|
{
|
||||||
|
public function testBadProject()
|
||||||
|
{
|
||||||
|
$action = new Action\TaskClose(3, new Task($this->db, $this->event));
|
||||||
|
$action->setParam('column_id', 5);
|
||||||
|
|
||||||
|
$event = array(
|
||||||
|
'project_id' => 2,
|
||||||
|
'task_id' => 3,
|
||||||
|
'column_id' => 5,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertFalse($action->doAction($event));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBadColumn()
|
||||||
|
{
|
||||||
|
$action = new Action\TaskClose(3, new Task($this->db, $this->event));
|
||||||
|
$action->setParam('column_id', 5);
|
||||||
|
|
||||||
|
$event = array(
|
||||||
|
'project_id' => 3,
|
||||||
|
'task_id' => 3,
|
||||||
|
'column_id' => 3,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertFalse($action->doAction($event));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExecute()
|
||||||
|
{
|
||||||
|
$action = new Action\TaskClose(1, new Task($this->db, $this->event));
|
||||||
|
$action->setParam('column_id', 2);
|
||||||
|
|
||||||
|
// We create a task in the first column
|
||||||
|
$task = new Task($this->db, $this->event);
|
||||||
|
$this->assertEquals(1, $p->create(array('name' => 'test')));
|
||||||
|
$this->assertEquals(1, $t->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 1)));
|
||||||
|
|
||||||
|
// We create an event to move the task to the 2nd column
|
||||||
|
$event = array(
|
||||||
|
'project_id' => 1,
|
||||||
|
'task_id' => 1,
|
||||||
|
'column_id' => 2,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Our event should be executed
|
||||||
|
$this->assertTrue($action->doAction($event));
|
||||||
|
|
||||||
|
// Our task should be closed
|
||||||
|
$t = $task->getById(1);
|
||||||
|
$this->assertNotEmpty($t);
|
||||||
|
$this->assertEquals(0, $t['is_active']);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ require_once __DIR__.'/../models/project.php';
|
|||||||
require_once __DIR__.'/../models/user.php';
|
require_once __DIR__.'/../models/user.php';
|
||||||
require_once __DIR__.'/../models/board.php';
|
require_once __DIR__.'/../models/board.php';
|
||||||
require_once __DIR__.'/../models/action.php';
|
require_once __DIR__.'/../models/action.php';
|
||||||
|
require_once __DIR__.'/../actions/task_close.php';
|
||||||
|
|
||||||
abstract class Base extends PHPUnit_Framework_TestCase
|
abstract class Base extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user