Skip to content

Commit

Permalink
fix(bridge): exception thrown during task creation (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
babeuloula authored Jun 23, 2022
1 parent 4b7905e commit 25d9dad
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Bridge/Doctrine/Transport/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function get(string $taskName): TaskInterface
)->fetchOne();

if (0 === (int) $statement) {
throw new TransportException(sprintf('The task "%s" cannot be found', $taskName));
throw new TransportException(message: sprintf('The task "%s" cannot be found', $taskName));
}

try {
Expand Down Expand Up @@ -139,7 +139,7 @@ public function create(TaskInterface $task): void
)->fetchOne();

if (0 !== (int) $existingTask) {
return;
throw new TransportException(sprintf('The task "%s" has already been scheduled!', $task->getName()));
}

try {
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Redis/Transport/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function get(string $taskName): TaskInterface
public function create(TaskInterface $task): void
{
if ($this->connection->hExists($this->list, $task->getName())) {
throw new TransportException(sprintf('The task "%s" has already been scheduled!', $task->getName()));
throw new TransportException(message: sprintf('The task "%s" has already been scheduled!', $task->getName()));
}

$data = $this->serializer->serialize($task, 'json');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ public function testTaskCannotBeCreatedTwice(): void
$this->connection->create(new NullTask('foo'));
self::assertInstanceOf(NullTask::class, $this->connection->get('foo'));

self::expectException(TransportException::class);
self::expectExceptionMessage('The task "foo" has already been scheduled!');
self::expectExceptionCode(0);
$this->connection->create(new ShellTask('foo', []));
self::assertInstanceOf(NullTask::class, $this->connection->get('foo'));
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Bridge/Doctrine/Transport/ConnectionIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ public function testTaskCannotBeCreatedTwice(): void
$this->connection->create(new NullTask('foo'));
self::assertInstanceOf(NullTask::class, $this->connection->get('foo'));

self::expectException(TransportException::class);
self::expectExceptionMessage('The task "foo" has already been scheduled!');
self::expectExceptionCode(0);
$this->connection->create(new ShellTask('foo', []));
self::assertInstanceOf(NullTask::class, $this->connection->get('foo'));
}
Expand Down
12 changes: 7 additions & 5 deletions tests/Bridge/Doctrine/Transport/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Result as NextResult;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Result as NextResult;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Comparator;
Expand Down Expand Up @@ -259,7 +259,7 @@ public function testConnectionCannotInsertASingleTaskWithExistingIdentifier(): v
$serializer = $this->createMock(SerializerInterface::class);

$task = $this->createMock(TaskInterface::class);
$task->expects(self::once())->method('getName')->willReturn('foo');
$task->expects(self::exactly(2))->method('getName')->willReturn('foo');

$expressionBuilder = $this->createMock(ExpressionBuilder::class);
$expressionBuilder->expects(self::once())->method('eq')
Expand Down Expand Up @@ -295,7 +295,6 @@ public function testConnectionCannotInsertASingleTaskWithExistingIdentifier(): v
$driverConnection = $this->getDBALConnectionMock();
$driverConnection->expects(self::once())->method('executeQuery')->willReturn($statement);
$driverConnection->expects(self::once())->method('createQueryBuilder')->willReturn($queryBuilder);
$driverConnection->expects(self::never())->method('transactional');

$connection = new DoctrineConnection(new InMemoryConfiguration([
'auto_setup' => true,
Expand All @@ -306,15 +305,18 @@ public function testConnectionCannotInsertASingleTaskWithExistingIdentifier(): v
]), $driverConnection, $serializer, new SchedulePolicyOrchestrator([
new FirstInFirstOutPolicy(),
]));

self::expectException(TransportException::class);
self::expectExceptionMessage('The task "foo" has already been scheduled!');
self::expectExceptionCode(0);
$connection->create($task);
}

public function testConnectionCannotInsertASingleTaskWithDuplicatedIdentifier(): void
{
$serializer = $this->createMock(SerializerInterface::class);

$task = $this->createMock(TaskInterface::class);
$task->expects(self::once())->method('getName')->willReturn('foo');
$task = new NullTask('foo');

$expressionBuilder = $this->createMock(ExpressionBuilder::class);
$expressionBuilder->expects(self::once())->method('eq')
Expand Down
8 changes: 6 additions & 2 deletions tests/Bridge/Doctrine/Transport/DoctrineTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use SchedulerBundle\Bridge\Doctrine\Transport\DoctrineTransport;
use SchedulerBundle\Exception\TransportException;
use SchedulerBundle\SchedulePolicy\FirstInFirstOutPolicy;
use SchedulerBundle\SchedulePolicy\SchedulePolicyOrchestrator;
use SchedulerBundle\Task\LazyTask;
use SchedulerBundle\Task\LazyTaskList;
use SchedulerBundle\Task\NullTask;
use SchedulerBundle\Task\TaskInterface;
use SchedulerBundle\Task\TaskList;
use SchedulerBundle\Transport\Configuration\InMemoryConfiguration;
Expand Down Expand Up @@ -367,8 +369,7 @@ public function testTransportCannotCreateAnExistingTask(): void
{
$serializer = $this->createMock(SerializerInterface::class);

$task = $this->createMock(TaskInterface::class);
$task->expects(self::once())->method('getName')->willReturn('foo');
$task = new NullTask('foo');

$expression = $this->createMock(ExpressionBuilder::class);
$expression->expects(self::once())->method('eq')
Expand Down Expand Up @@ -427,6 +428,9 @@ public function testTransportCannotCreateAnExistingTask(): void
new FirstInFirstOutPolicy(),
]));

self::expectException(TransportException::class);
self::expectExceptionMessage('The task "foo" has already been scheduled!');
self::expectExceptionCode(0);
$transport->create($task);
}

Expand Down

0 comments on commit 25d9dad

Please sign in to comment.