-
Notifications
You must be signed in to change notification settings - Fork 8
Fixed query to find pending executions #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Task\TaskBundle\Tests\Functional; | ||
|
||
use Doctrine\Common\DataFixtures\Executor\ORMExecutor; | ||
use Doctrine\Common\DataFixtures\ProxyReferenceRepository; | ||
use Doctrine\Common\DataFixtures\Purger\ORMPurger; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
/** | ||
* Extends kernel-test-case with additional functions/properties. | ||
*/ | ||
abstract class BaseDatabaseTestCase extends KernelTestCase | ||
{ | ||
const ENTITY_MANAGER_ID = 'doctrine.orm.entity_manager'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUp() | ||
{ | ||
self::bootKernel(); | ||
|
||
$this->purgeDatabase(); | ||
} | ||
|
||
/** | ||
* Purges database is necessary. | ||
*/ | ||
protected function purgeDatabase() | ||
{ | ||
if (!self::$kernel->getContainer()->has(self::ENTITY_MANAGER_ID)) { | ||
return; | ||
} | ||
|
||
/** @var EntityManagerInterface $manager */ | ||
$manager = self::$kernel->getContainer()->get(self::ENTITY_MANAGER_ID); | ||
$connection = $manager->getConnection(); | ||
|
||
if ($connection->getDriver() instanceof \Doctrine\DBAL\Driver\PDOMySql\Driver) { | ||
$connection->executeUpdate('SET foreign_key_checks = 0;'); | ||
} | ||
|
||
$purger = new ORMPurger(); | ||
$executor = new ORMExecutor($manager, $purger); | ||
$referenceRepository = new ProxyReferenceRepository($manager); | ||
$executor->setReferenceRepository($referenceRepository); | ||
$executor->purge(); | ||
|
||
if ($connection->getDriver() instanceof \Doctrine\DBAL\Driver\PDOMySql\Driver) { | ||
$connection->executeUpdate('SET foreign_key_checks = 1;'); | ||
} | ||
} | ||
} |
229 changes: 229 additions & 0 deletions
229
tests/Functional/Entity/TaskExecutionRepositoryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
<?php | ||
|
||
namespace Task\TaskBundle\Tests\Functional\Command; | ||
|
||
use Task\Execution\TaskExecutionInterface; | ||
use Task\Storage\TaskExecutionRepositoryInterface; | ||
use Task\Storage\TaskRepositoryInterface; | ||
use Task\TaskBundle\Entity\Task; | ||
use Task\TaskBundle\Entity\TaskExecution; | ||
use Task\TaskBundle\Tests\Functional\BaseDatabaseTestCase; | ||
use Task\TaskBundle\Tests\Functional\TestHandler; | ||
use Task\TaskInterface; | ||
use Task\TaskStatus; | ||
|
||
/** | ||
* Tests for TaskExecutionRepository. | ||
*/ | ||
class TaskExecutionRepositoryTest extends BaseDatabaseTestCase | ||
{ | ||
/** | ||
* @var TaskExecutionRepositoryInterface | ||
*/ | ||
protected $taskExecutionRepository; | ||
|
||
/** | ||
* @var TaskRepositoryInterface | ||
*/ | ||
protected $taskRepository; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->taskExecutionRepository = self::$kernel->getContainer()->get('task.storage.task_execution'); | ||
$this->taskRepository = self::$kernel->getContainer()->get('task.storage.task'); | ||
} | ||
|
||
public function testSave() | ||
{ | ||
$execution = $this->save(); | ||
|
||
$result = $this->taskExecutionRepository->findByUuid($execution->getUuid()); | ||
|
||
$this->assertEquals($execution->getUuid(), $result->getUuid()); | ||
$this->assertEquals($execution->getScheduleTime(), $result->getScheduleTime()); | ||
$this->assertEquals($execution->getStatus(), $result->getStatus()); | ||
$this->assertEquals($execution->getTask()->getUuid(), $result->getTask()->getUuid()); | ||
} | ||
|
||
public function testRemove() | ||
{ | ||
$execution = $this->save(); | ||
|
||
$this->taskExecutionRepository->remove($execution); | ||
|
||
$this->assertNull($this->taskExecutionRepository->findByUuid($execution->getUuid())); | ||
} | ||
|
||
public function testFindAll() | ||
{ | ||
$task = $this->createTask(); | ||
$this->taskRepository->save($task); | ||
|
||
$executions = []; | ||
for ($i = 0; $i < 3; ++$i) { | ||
$execution = $this->save($task); | ||
$executions[$execution->getUuid()] = $execution; | ||
} | ||
|
||
$result = $this->taskExecutionRepository->findAll(); | ||
|
||
$this->assertCount(3, $result); | ||
foreach ($result as $item) { | ||
$this->assertArrayHasKey($item->getUuid(), $executions); | ||
unset($executions[$item->getUuid()]); | ||
} | ||
} | ||
|
||
public function testFindAllPaginated() | ||
{ | ||
$task = $this->createTask(); | ||
$this->taskRepository->save($task); | ||
|
||
$executions = []; | ||
for ($i = 0; $i < 3; ++$i) { | ||
$execution = $this->save($task); | ||
$executions[$execution->getUuid()] = $execution; | ||
} | ||
|
||
$result = $this->taskExecutionRepository->findAll(1, 2); | ||
|
||
$this->assertCount(2, $result); | ||
foreach ($result as $item) { | ||
$this->assertArrayHasKey($item->getUuid(), $executions); | ||
unset($executions[$item->getUuid()]); | ||
} | ||
|
||
$result = $this->taskExecutionRepository->findAll(2, 2); | ||
|
||
$this->assertCount(1, $result); | ||
foreach ($result as $item) { | ||
$this->assertArrayHasKey($item->getUuid(), $executions); | ||
unset($executions[$item->getUuid()]); | ||
} | ||
|
||
$this->assertEmpty($executions); | ||
} | ||
|
||
public function testFindPending() | ||
{ | ||
$execution = $this->save(); | ||
$this->assertNotNull($this->taskExecutionRepository->findPending($execution->getTask())); | ||
|
||
$execution = $this->save(null, null, TaskStatus::RUNNING); | ||
$this->assertNotNull($this->taskExecutionRepository->findPending($execution->getTask())); | ||
|
||
$execution = $this->save(null, null, TaskStatus::COMPLETED); | ||
$this->assertNull($this->taskExecutionRepository->findPending($execution->getTask())); | ||
|
||
$execution = $this->save(null, null, TaskStatus::FAILED); | ||
$this->assertNull($this->taskExecutionRepository->findPending($execution->getTask())); | ||
} | ||
|
||
public function testFindByUuid() | ||
{ | ||
$execution = $this->save(); | ||
|
||
$result = $this->taskExecutionRepository->findByUuid($execution->getUuid()); | ||
$this->assertEquals($execution->getUuid(), $result->getUuid()); | ||
} | ||
|
||
public function testFindByTask() | ||
{ | ||
$execution = $this->save(); | ||
|
||
$result = $this->taskExecutionRepository->findByTask($execution->getTask()); | ||
|
||
$this->assertCount(1, $result); | ||
$this->assertEquals($execution->getTask()->getUuid(), $result[0]->getTask()->getUuid()); | ||
} | ||
|
||
public function testFindByTaskUuid() | ||
{ | ||
$execution = $this->save(); | ||
|
||
$result = $this->taskExecutionRepository->findByTaskUuid($execution->getTask()->getUuid()); | ||
|
||
$this->assertCount(1, $result); | ||
$this->assertEquals($execution->getTask()->getUuid(), $result[0]->getTask()->getUuid()); | ||
} | ||
|
||
public function testFindScheduledPast() | ||
{ | ||
$task = $this->createTask(); | ||
$this->taskRepository->save($task); | ||
|
||
$execution = $this->save($task, new \DateTime('-1 hour')); | ||
|
||
$result = $this->taskExecutionRepository->findScheduled(); | ||
|
||
$this->assertCount(1, $result); | ||
$this->assertEquals($execution->getUuid(), $result[0]->getUuid()); | ||
} | ||
|
||
public function testFindScheduledFuture() | ||
{ | ||
$task = $this->createTask(); | ||
$this->taskRepository->save($task); | ||
|
||
$this->save($task, new \DateTime('+1 hour')); | ||
|
||
$this->assertEmpty($this->taskExecutionRepository->findScheduled()); | ||
} | ||
|
||
/** | ||
* Save a new execution to database. | ||
* | ||
* @param TaskInterface $task | ||
* @param \DateTime $scheduleTime | ||
* @param string $status | ||
* | ||
* @return TaskExecutionInterface | ||
*/ | ||
private function save(TaskInterface $task = null, \DateTime $scheduleTime = null, $status = TaskStatus::PLANNED) | ||
{ | ||
if (!$scheduleTime) { | ||
$scheduleTime = new \DateTime(); | ||
} | ||
|
||
if (!$task) { | ||
$task = $this->createTask(); | ||
$this->taskRepository->save($task); | ||
} | ||
|
||
$execution = $this->createTaskExecution($task, $scheduleTime, $status); | ||
$this->taskExecutionRepository->save($execution); | ||
|
||
return $execution; | ||
} | ||
|
||
/** | ||
* Create a new task. | ||
* | ||
* @param string $handlerClass | ||
* | ||
* @return TaskInterface | ||
*/ | ||
private function createTask($handlerClass = TestHandler::class) | ||
{ | ||
return new Task($handlerClass); | ||
} | ||
|
||
/** | ||
* Create a new task-execution. | ||
* | ||
* @param TaskInterface $task | ||
* @param \DateTime $scheduleTime | ||
* @param string $status | ||
* | ||
* @return TaskExecutionInterface | ||
*/ | ||
private function createTaskExecution(TaskInterface $task, \DateTime $scheduleTime, $status = TaskStatus::PLANNED) | ||
{ | ||
$execution = new TaskExecution($task, $task->getHandlerClass(), $scheduleTime); | ||
$execution->setStatus($status); | ||
|
||
return $execution; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the interface has changed a few time ago. so i have removed it also from this class.