Skip to content

Commit

Permalink
tests(core): improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Guikingone committed Apr 12, 2022
1 parent 855848c commit 3242550
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 14 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ php-cs-fixer-dry: ## Run PHP-CS-FIXER in --dry-run mode
php-cs-fixer-dry: .php-cs-fixer.dist.php
$(PHP) vendor/bin/php-cs-fixer fix --allow-risky=yes --dry-run

phpstan: ## Run PHPStan (the configuration must be defined in phpstan.neon.dist)
phpstan: phpstan.neon.dist
$(PHP) vendor/bin/phpstan analyse --memory-limit 2G --xdebug
phpstan_80: ## Run PHPStan (the configuration must be defined in phpstan.neon.dist)
phpstan_80: phpstan.neon.8.0.dist
$(PHP) vendor/bin/phpstan analyse --memory-limit 2G --xdebug --configuration phpstan.neon.8.0.dist

phpstan_81: ## Run PHPStan (the configuration must be defined in phpstan.neon.dist)
phpstan_81: phpstan.neon.8.1.dist
$(PHP) vendor/bin/phpstan analyse --memory-limit 2G --xdebug --configuration phpstan.neon.8.1.dist

rector: rector.php
$(PHP) vendor/bin/rector
Expand Down
8 changes: 6 additions & 2 deletions src/Task/CommandTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
*/
final class CommandTask extends AbstractTask
{
public function __construct(string $name, string $command, array $arguments = [], array $options = [])
{
public function __construct(
string $name,
string $command,
array $arguments = [],
array $options = []
) {
$this->validateCommand($command);

$this->defineOptions([
Expand Down
1 change: 1 addition & 0 deletions src/Test/Constraint/TaskFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\Constraint\Constraint;
use SchedulerBundle\Event\TaskEventList;
use function count;
use function is_countable;
use function sprintf;

/**
Expand Down
1 change: 0 additions & 1 deletion src/Transport/FilesystemTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function __construct(
public function list(bool $lazy = false): TaskListInterface
{
$tasks = new TaskList();

$finder = new Finder();

$this->filesystem->mkdir($this->options['path']);
Expand Down
32 changes: 32 additions & 0 deletions tests/Expression/ComputedExpressionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Generator;
use PHPUnit\Framework\TestCase;
use SchedulerBundle\Expression\ComputedExpressionBuilder;
use Throwable;
use function explode;

/**
Expand All @@ -22,6 +23,26 @@ public function testBuilderSupport(): void
self::assertTrue($computedExpressionBuilder->support('# * * * *'));
}

/**
* @throws Throwable {@see ExpressionBuilderInterface::build()}
*/
public function testBuilderCanHandleEmptyParts(): void
{
$builder = new ComputedExpressionBuilder();

$expression = $builder->build('* * * * *');
$explodedExpression = explode(' ', $expression->getExpression());

self::assertSame('*', $explodedExpression[0]);
self::assertSame('*', $explodedExpression[1]);
self::assertSame('*', $explodedExpression[2]);
self::assertSame('*', $explodedExpression[3]);
self::assertSame('*', $explodedExpression[4]);
}

/**
* @throws Throwable {@see ExpressionBuilderInterface::build()}
*/
public function testBuilderCanHandleMinutes(): void
{
$builder = new ComputedExpressionBuilder();
Expand All @@ -37,6 +58,9 @@ public function testBuilderCanHandleMinutes(): void
self::assertSame('*', $explodedExpression[4]);
}

/**
* @throws Throwable {@see ExpressionBuilderInterface::build()}
*/
public function testBuilderCanHandleHours(): void
{
$builder = new ComputedExpressionBuilder();
Expand All @@ -52,6 +76,9 @@ public function testBuilderCanHandleHours(): void
self::assertSame('*', $explodedExpression[4]);
}

/**
* @throws Throwable {@see ExpressionBuilderInterface::build()}
*/
public function testBuilderCanHandleDays(): void
{
$builder = new ComputedExpressionBuilder();
Expand All @@ -67,6 +94,9 @@ public function testBuilderCanHandleDays(): void
self::assertSame('*', $explodedExpression[4]);
}

/**
* @throws Throwable {@see ExpressionBuilderInterface::build()}
*/
public function testBuilderCanHandleMonths(): void
{
$builder = new ComputedExpressionBuilder();
Expand All @@ -84,6 +114,8 @@ public function testBuilderCanHandleMonths(): void

/**
* @dataProvider provideExpression
*
* @throws Throwable {@see ExpressionBuilderInterface::build()}
*/
public function testBuilderCanBuild(string $expression): void
{
Expand Down
4 changes: 4 additions & 0 deletions tests/Expression/CronExpressionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use SchedulerBundle\Expression\CronExpressionBuilder;
use Throwable;

/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
Expand All @@ -20,6 +21,9 @@ public function testBuilderSupport(): void
self::assertTrue($cronExpressionBuilder->support('* * * * *'));
}

/**
* @throws Throwable {@see ExpressionBuilderInterface::build()}
*/
public function testBuilderCanBuildExpression(): void
{
$cronExpressionBuilder = new CronExpressionBuilder();
Expand Down
12 changes: 11 additions & 1 deletion tests/Middleware/ProbeTaskMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SchedulerBundle\Task\ProbeTask;
use SchedulerBundle\Task\TaskInterface;
use SchedulerBundle\Worker\WorkerInterface;
use Throwable;

/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
Expand All @@ -17,6 +18,9 @@
*/
final class ProbeTaskMiddlewareTest extends TestCase
{
/**
* @throws Throwable {@see PreExecutionMiddlewareInterface::preExecute()}
*/
public function testMiddlewareCannotBeCalledOnInvalidTask(): void
{
$worker = $this->createMock(WorkerInterface::class);
Expand All @@ -28,6 +32,9 @@ public function testMiddlewareCannotBeCalledOnInvalidTask(): void
$middleware->preExecute($task);
}

/**
* @throws Throwable {@see PreExecutionMiddlewareInterface::preExecute()}
*/
public function testMiddlewareCannotBeCalledOnTaskWithoutDelay(): void
{
$worker = $this->createMock(WorkerInterface::class);
Expand All @@ -39,7 +46,10 @@ public function testMiddlewareCannotBeCalledOnTaskWithoutDelay(): void
$middleware->preExecute($task);
}

public function testMiddlewareCannotBeCalledOnTaskWithDelay(): void
/**
* @throws Throwable {@see PreExecutionMiddlewareInterface::preExecute()}
*/
public function testMiddlewareCanBeCalledOnTaskWithDelay(): void
{
$worker = $this->createMock(WorkerInterface::class);
$worker->expects(self::never())->method('isRunning');
Expand Down
7 changes: 7 additions & 0 deletions tests/Runner/CallBackTaskRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function testRunnerCanExecuteValidTaskWithExtraSpacedOutput(): void

$output = $callbackTaskRunner->run($callbackTask, $worker);

self::assertSame(Output::SUCCESS, $output->getType());
self::assertNull($callbackTask->getExecutionState());
self::assertSame('hello', $output->getOutput());
self::assertNull($output->getTask()->getExecutionState());
Expand All @@ -63,6 +64,7 @@ public function testRunnerCanExecuteValidTask(): void

$output = $callbackTaskRunner->run($callbackTask, $worker);

self::assertSame(Output::SUCCESS, $output->getType());
self::assertNull($callbackTask->getExecutionState());
self::assertSame('2', $output->getOutput());
self::assertNull($output->getTask()->getExecutionState());
Expand All @@ -77,6 +79,7 @@ public function testRunnerCanExecuteValidTaskWithCallable(): void

$output = $callbackTaskRunner->run($callbackTask, $worker);

self::assertSame(Output::SUCCESS, $output->getType());
self::assertNull($callbackTask->getExecutionState());
self::assertSame('Symfony', $callbackTaskRunner->run($callbackTask, $worker)->getOutput());
self::assertNull($output->getTask()->getExecutionState());
Expand All @@ -91,6 +94,7 @@ public function testRunnerCanExecuteValidTaskWithArguments(): void

$output = $callbackTaskRunner->run($callbackTask, $worker);

self::assertSame(Output::SUCCESS, $output->getType());
self::assertNull($callbackTask->getExecutionState());
self::assertSame('2', $callbackTaskRunner->run($callbackTask, $worker)->getOutput());
self::assertNull($output->getTask()->getExecutionState());
Expand All @@ -105,6 +109,7 @@ public function testRunnerCanExecuteInvalidTask(): void

$output = $callbackTaskRunner->run($callbackTask, $worker);

self::assertSame(Output::ERROR, $output->getType());
self::assertNull($callbackTask->getExecutionState());
self::assertNull($output->getOutput());
self::assertNull($output->getTask()->getExecutionState());
Expand All @@ -119,6 +124,7 @@ public function testRunnerCanExecuteTaskWithFalseReturn(): void

$output = $callbackTaskRunner->run($callbackTask, $worker);

self::assertSame(Output::ERROR, $output->getType());
self::assertNull($callbackTask->getExecutionState());
self::assertNull($output->getOutput());
self::assertNull($output->getTask()->getExecutionState());
Expand All @@ -133,6 +139,7 @@ public function testRunnerCanExecuteTaskWithTrueReturn(): void

$output = $callbackTaskRunner->run($callbackTask, $worker);

self::assertSame(Output::SUCCESS, $output->getType());
self::assertNull($callbackTask->getExecutionState());
self::assertSame('1', $output->getOutput());
self::assertNull($output->getTask()->getExecutionState());
Expand Down
32 changes: 28 additions & 4 deletions tests/SchedulePolicy/IdlePolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,28 @@ public function testTasksCanBeSortedWithSamePriority(): void
'priority' => 10,
]);

$thirdTask = new NullTask('third', [
'priority' => 19,
]);

$fourthTask = new NullTask('fourth', [
'priority' => -10,
]);

$idlePolicy = new IdlePolicy();
$tasks = $idlePolicy->sort(new TaskList([$secondTask, $task]));
$tasks = $idlePolicy->sort(new TaskList([
$secondTask,
$thirdTask,
$task,
$fourthTask,
]));

self::assertCount(2, $tasks);
self::assertCount(4, $tasks);
self::assertSame([
'third' => $thirdTask,
'app' => $secondTask,
'foo' => $task,
'fourth' => $fourthTask,
], $tasks->toArray());
}

Expand All @@ -72,13 +87,22 @@ public function testTasksCanBeSortedWithPolicyPriority(): void
'priority' => 19,
]);

$thirdTask = new NullTask('third', [
'priority' => 15,
]);

$idlePolicy = new IdlePolicy();
$tasks = $idlePolicy->sort(new TaskList([$secondTask, $task]));
$tasks = $idlePolicy->sort(new TaskList([
$secondTask,
$task,
$thirdTask,
]));

self::assertCount(2, $tasks);
self::assertCount(3, $tasks);
self::assertSame([
'app' => $secondTask,
'foo' => $task,
'third' => $thirdTask,
], $tasks->toArray());
}

Expand Down
56 changes: 56 additions & 0 deletions tests/SchedulePolicy/NicePolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,60 @@ public function testTasksCanBeSorted(): void
'foo' => $secondTask,
], $sortedTasks->toArray());
}

public function testTasksCanBeSortedWithZeroPriority(): void
{
$task = new NullTask('app', [
'nice' => 0,
]);

$secondTask = new NullTask('foo', [
'nice' => 5,
]);

$thirdTask = new NullTask('third', [
'nice' => 5,
]);

$nicePolicy = new NicePolicy();
$sortedTasks = $nicePolicy->sort(new TaskList([
$secondTask,
$task,
$thirdTask,
]));

self::assertSame([
'app' => $task,
'foo' => $secondTask,
'third' => $thirdTask,
], $sortedTasks->toArray());
}

public function testTasksCanBeSortedWithNegativePriority(): void
{
$task = new NullTask('app', [
'nice' => -10,
]);

$secondTask = new NullTask('foo', [
'nice' => 5,
]);

$thirdTask = new NullTask('third', [
'nice' => 5,
]);

$nicePolicy = new NicePolicy();
$sortedTasks = $nicePolicy->sort(new TaskList([
$secondTask,
$task,
$thirdTask,
]));

self::assertSame([
'app' => $task,
'foo' => $secondTask,
'third' => $thirdTask,
], $sortedTasks->toArray());
}
}
11 changes: 9 additions & 2 deletions tests/Serializer/NotificationTaskBagNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public function testNormalizerCanNormalize(): void
{
$objectNormalizer = new ObjectNormalizer();

$serializer = new Serializer([new NotificationTaskBagNormalizer($objectNormalizer), $objectNormalizer], [new JsonEncoder()]);
$serializer = new Serializer([
new NotificationTaskBagNormalizer($objectNormalizer),
$objectNormalizer,
], [new JsonEncoder()]);
$objectNormalizer->setSerializer($serializer);

$data = $serializer->normalize(new NotificationTaskBag(new Notification('foo', ['email']), new Recipient('test@test.fr', '')));
Expand Down Expand Up @@ -65,12 +68,16 @@ public function testNormalizerCanSerialize(): void
{
$objectNormalizer = new ObjectNormalizer();

$serializer = new Serializer([new NotificationTaskBagNormalizer($objectNormalizer), $objectNormalizer], [new JsonEncoder()]);
$serializer = new Serializer([
new NotificationTaskBagNormalizer($objectNormalizer),
$objectNormalizer,
], [new JsonEncoder()]);
$objectNormalizer->setSerializer($serializer);

$data = $serializer->serialize(new NotificationTaskBag(new Notification('foo', ['email']), new Recipient('test@test.fr', '')), 'json');
$bag = $serializer->deserialize($data, NotificationTaskBag::class, 'json');

self::assertNotNull($bag->getNotification());
self::assertCount(1, $bag->getRecipients());
}
}
Loading

0 comments on commit 3242550

Please sign in to comment.