Skip to content

Commit

Permalink
Added find-by-task and remove function (#20)
Browse files Browse the repository at this point in the history
* added find-by-task function

* added remove method
  • Loading branch information
wachterjohannes committed Nov 23, 2016
1 parent cc16388 commit e9bda57
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Task/Storage/ArrayStorage/ArrayTaskExecutionRepository.php
Expand Up @@ -55,6 +55,16 @@ public function persist(TaskExecutionInterface $execution)
return $this;
}

/**
* {@inheritdoc}
*/
public function remove(TaskExecutionInterface $execution)
{
$this->taskExecutionCollection->removeElement($execution);

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -83,6 +93,18 @@ function (TaskExecutionInterface $execution) use ($task, $scheduleTime) {
return $filtered->first();
}

/**
* {@inheritdoc}
*/
public function findByTask(TaskInterface $task)
{
return array_values($this->taskExecutionCollection->filter(
function (TaskExecutionInterface $execution) use ($task) {
return $execution->getTask()->getUuid() === $task->getUuid();
}
)->toArray());
}

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Task/Storage/ArrayStorage/ArrayTaskRepository.php
Expand Up @@ -66,6 +66,16 @@ public function persist(TaskInterface $task)
return $this;
}

/**
* {@inheritdoc}
*/
public function remove(TaskInterface $task)
{
$this->taskCollection->removeElement($task);

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down
18 changes: 18 additions & 0 deletions src/Task/Storage/TaskExecutionRepositoryInterface.php
Expand Up @@ -38,6 +38,15 @@ public function create(TaskInterface $task, \DateTime $scheduleTime);
*/
public function persist(TaskExecutionInterface $execution);

/**
* Remove task-execution.
*
* @param TaskExecutionInterface $execution
*
* @return $this
*/
public function remove(TaskExecutionInterface $execution);

/**
* Flush storage.
*
Expand All @@ -55,6 +64,15 @@ public function flush();
*/
public function findByStartTime(TaskInterface $task, \DateTime $scheduleTime);

/**
* Find executions of given task.
*
* @param TaskInterface $task
*
* @return TaskExecutionInterface[]
*/
public function findByTask(TaskInterface $task);

/**
* Returns all task-executions.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Task/Storage/TaskRepositoryInterface.php
Expand Up @@ -44,6 +44,13 @@ public function create($handlerClass, $workload = null);
*/
public function persist(TaskInterface $task);

/**
* Persist task.
*
* @param TaskInterface $task
*/
public function remove(TaskInterface $task);

/**
* Flush storage.
*
Expand Down
Expand Up @@ -40,6 +40,21 @@ public function testPersist()
);
}

public function testRemove()
{
$taskExecutionCollection = $this->prophesize(Collection::class);
$taskExecutionRepository = new ArrayTaskExecutionRepository($taskExecutionCollection->reveal());

$execution = $this->prophesize(TaskExecutionInterface::class);

$taskExecutionCollection->removeElement($execution->reveal())->shouldBeCalled();

$this->assertEquals(
$taskExecutionRepository,
$taskExecutionRepository->remove($execution->reveal())
);
}

public function testFlush()
{
$taskExecutionCollection = $this->prophesize(Collection::class);
Expand Down Expand Up @@ -68,6 +83,22 @@ public function testFindByStartTime()
$this->assertEquals($executions[1], $repository->findByStartTime($task, $executions[1]->getScheduleTime()));
}

public function testFindByTask()
{
$task1 = new Task(\stdClass::class, 'Test 1', '123-123-123');
$task2 = new Task(\stdClass::class, 'Test 1');
$executions = [
new TaskExecution($task1, \stdClass::class, new \DateTime('+1 day'), 'Test 1'),
new TaskExecution($task2, \stdClass::class, new \DateTime('1 day ago'), 'Test 1'),
new TaskExecution($task1, \stdClass::class, new \DateTime('1 hour ago'), 'Test 1'),
];

$repository = new ArrayTaskExecutionRepository(new ArrayCollection($executions));

$this->assertEquals([$executions[0], $executions[2]], $repository->findByTask($task1));
$this->assertEquals([$executions[1]], $repository->findByTask($task2));
}

public function testFindByStartTimeNoResult()
{
$task = new Task(\stdClass::class, 'Test 1', '123-123-123');
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/Storage/ArrayStorage/ArrayTaskRepositoryTest.php
Expand Up @@ -65,6 +65,21 @@ public function testPersist()
);
}

public function testRemove()
{
$collection = $this->prophesize(Collection::class);
$repository = new ArrayTaskRepository($collection->reveal());

$task = $this->prophesize(TaskInterface::class);

$collection->removeElement($task->reveal())->shouldBeCalled();

$this->assertEquals(
$repository,
$repository->remove($task->reveal())
);
}

public function testFlush()
{
$collection = $this->prophesize(Collection::class);
Expand Down

0 comments on commit e9bda57

Please sign in to comment.