Permalink
Browse files

Task Creation scenario implemented

  • Loading branch information...
1 parent 0e233d6 commit 0f80973be9579424ebb76157d681b39ae988293d @AlexKupreev committed May 30, 2014
View
@@ -16,8 +16,8 @@ public function __construct($data)
$this->id = $data['id'];
$this->userId = $data['userId'];
$this->title = $data['title'];
- $this->description = $data['description'];
- $this->notes = $data['notes'];
+ $this->description = ! empty($data['description']) ? $data['description'] : '';
+ $this->notes = ! empty($data['notes']) ? $data['notes'] : '';
if ( ! empty($data['children']) and is_array($data['children'])) {
foreach ($data['children'] as $child) {
@@ -1,22 +1,46 @@
<?php
-namespace app\interactor;
+namespace app\interactor\Task;
use app\entity as Entity;
use app\repository as Repo;
+use app\service as Service;
/**
* Task Creation Interactor
*/
class Creation
{
- public function __construct(Repo\Task $taskRepo)
+ /**
+ *
+ * @var Repo\TaskInterface
+ */
+ protected $taskRepo;
+
+ /**
+ *
+ * @var Repo\UserInterface
+ */
+ protected $userRepo;
+
+ /**
+ *
+ * @var Service\SessionInterface
+ */
+ protected $session;
+
+ public function __construct(Repo\TaskInterface $taskRepo, Repo\UserInterface $userRepo, Service\SessionInterface $session)
{
-
+ $this->taskRepo = $taskRepo;
+ $this->userRepo = $userRepo;
+ $this->session = $session;
}
- public function execute($params = [])
+ public function execute($data = [])
{
-
+ $data['userId'] = $this->session->getLoggedInUserId();
+ $this->taskRepo->create($data);
+
+ return true;
}
}
@@ -1,6 +1,6 @@
<?php
-namespace app\interactor;
+namespace app\interactor\User;
use app\entity as Entity;
use app\repository as Repo;
@@ -2,9 +2,50 @@
namespace app\mock\repository;
+use app\entity as Entity;
use app\repository as Repo;
class Task implements Repo\TaskInterface
{
-
+ /**
+ * Array of root Task Entities
+ * @var array
+ */
+ protected $root = [];
+
+ /**
+ * Array of occupied task IDs
+ * @var array
+ */
+ protected $taskIds = [];
+
+ /**
+ * Creates Task entity from a data array
+ * @param array $data
+ * @return Entity\Task
+ */
+ public function create(array $data)
+ {
+ if (empty($data['id']) or ! $this->isFreeId($data['id'])) {
+ $data['id'] = $this->getUniqId();
+ }
+ $task = new Entity\Task($data);
+ $this->taskIds[] = $task->id;
+
+ return $task;
+ }
+
+ protected function isFreeId($id)
+ {
+ return array_key_exists($id, $this->taskIds);
+ }
+
+ protected function getUniqId()
+ {
+ $keys = array_keys($this->taskIds);
+ if (empty($keys)) {
+ return 1;
+ }
+ return max($keys) + 1;
+ }
}
@@ -4,5 +4,10 @@
interface TaskInterface
{
-
+ /**
+ * Creates a task from a data array
+ * @param array $data
+ */
+ public function create(array $data);
+
}
@@ -6,6 +6,7 @@
Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
+use PHPUnit_Framework_Assert as Test;
//
// Require 3rd-party libraries here:
@@ -20,15 +21,20 @@
use app\mock\service as MockService;
use app\repository as Repo;
+use app\interactor as Interactor;
use app\service as Service;
/**
* Features context.
*/
class FeatureContext extends BehatContext
{
+ protected $boolResult;
+
protected $userRepo;
+ protected $taskRepo;
protected $passwordService;
+ protected $sessionService;
/**
* Initializes context.
@@ -39,6 +45,7 @@ class FeatureContext extends BehatContext
public function __construct(array $parameters)
{
$this->userRepo = new MockRepo\User;
+ $this->taskRepo = new MockRepo\Task;
$this->passwordService = new Service\Password;
$this->sessionService = new MockService\Session;
}
@@ -80,15 +87,22 @@ public function iAmARegisteredUser($login)
*/
public function iCreateATaskWithTheFollowingInformation(TableNode $table)
{
- throw new PendingException();
+ $data = [];
+ foreach ($table->getNumeratedRows() as $row)
+ {
+ $data[$row[0]] = $row[1];
+ }
+
+ $taskCreator = new Interactor\Task\Creation($this->taskRepo, $this->userRepo, $this->sessionService);
+ $this->boolResult = $taskCreator->execute($data);
}
/**
* @Then /^task should be created$/
*/
public function taskShouldBeCreated()
{
- throw new PendingException();
+ Test::assertTrue($this->boolResult);
}
/**

0 comments on commit 0f80973

Please sign in to comment.