Extract components and add more tests
This commit is contained in:
58
src/Tests/Task/IntervalTest.php
Normal file
58
src/Tests/Task/IntervalTest.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Toalett\Multiprocessing\Tests\Task;
|
||||
|
||||
use Generator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Toalett\Multiprocessing\Exception\InvalidArgumentException;
|
||||
use Toalett\Multiprocessing\Task\Interval;
|
||||
|
||||
class IntervalTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @param $method
|
||||
* @param $val
|
||||
* @param $calculatedVal
|
||||
* @dataProvider zeroAndDownProvider
|
||||
*/
|
||||
public function testItDoesNotAllowLessThanZeroOrZero($method, $val, $calculatedVal): void
|
||||
{
|
||||
$this->setName(sprintf('It does not allow %d for %s', $val, $method));
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage(sprintf('Expected positive float, got \'%s\'', $calculatedVal));
|
||||
Interval::{$method}($val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $method
|
||||
* @param $val
|
||||
* @param $expected
|
||||
* @dataProvider oneAndUpProvider
|
||||
*/
|
||||
public function testItCalculatesTheCorrectInterval($method, $val, $expected): void
|
||||
{
|
||||
$this->setName('It calculates the correct interval in ' . $method);
|
||||
$interval = Interval::{$method}($val);
|
||||
|
||||
self::assertEquals($expected, $interval->asFloat());
|
||||
}
|
||||
|
||||
public function zeroAndDownProvider(): Generator
|
||||
{
|
||||
return $this->createProvider(0, -5, -9000);
|
||||
}
|
||||
|
||||
public function oneAndUpProvider(): Generator
|
||||
{
|
||||
return $this->createProvider(1, 5, 7500);
|
||||
}
|
||||
|
||||
public function createProvider(...$args): Generator
|
||||
{
|
||||
foreach ($args as $arg) {
|
||||
yield "$arg seconds" => ['seconds', $arg, $arg];
|
||||
yield "$arg minutes" => ['minutes', $arg, $arg * 60.0];
|
||||
yield "$arg hours" => ['hours', $arg, $arg * 3600.0];
|
||||
}
|
||||
}
|
||||
}
|
||||
37
src/Tests/Task/RepeatedTaskTest.php
Normal file
37
src/Tests/Task/RepeatedTaskTest.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Toalett\Multiprocessing\Tests\Task;
|
||||
|
||||
use Generator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use React\EventLoop\Timer\Timer;
|
||||
use Toalett\Multiprocessing\Task\Interval;
|
||||
use Toalett\Multiprocessing\Task\RepeatedTask;
|
||||
|
||||
class RepeatedTaskTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @param $interval
|
||||
* @dataProvider dataProvider
|
||||
*/
|
||||
public function testItRegistersWithTheProvidedInterval(Interval $interval): void
|
||||
{
|
||||
$loop = $this->createMock(LoopInterface::class);
|
||||
$loop->expects(self::once())
|
||||
->method('addPeriodicTimer')
|
||||
->with($interval->asFloat(), static fn() => null)
|
||||
->willReturn(new Timer($interval->asFloat(), static fn() => null, true));
|
||||
|
||||
$task = new RepeatedTask($interval, static fn() => null);
|
||||
$task->enable($loop);
|
||||
}
|
||||
|
||||
public function dataProvider(): Generator
|
||||
{
|
||||
yield "3 seconds" => [Interval::seconds(3)];
|
||||
yield "5 minutes" => [Interval::minutes(5)];
|
||||
yield "half an hour" => [Interval::hours(0.5)];
|
||||
yield "a day" => [Interval::hours(24)];
|
||||
}
|
||||
}
|
||||
91
src/Tests/Task/TasksTest.php
Normal file
91
src/Tests/Task/TasksTest.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace Toalett\Multiprocessing\Tests\Task;
|
||||
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use React\EventLoop\LoopInterface;
|
||||
use Toalett\Multiprocessing\Task\Task;
|
||||
use Toalett\Multiprocessing\Task\Tasks;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Toalett\Multiprocessing\Tests\Tools\PropertyInspector;
|
||||
|
||||
class TasksTest extends TestCase
|
||||
{
|
||||
use PropertyInspector;
|
||||
|
||||
public function testItAcceptsZeroTasks(): void
|
||||
{
|
||||
$this->expectNotToPerformAssertions();
|
||||
new Tasks();
|
||||
}
|
||||
|
||||
public function testItAcceptsMultipleTasks(): void
|
||||
{
|
||||
$this->expectNotToPerformAssertions();
|
||||
new Tasks(
|
||||
$this->createMock(Task::class),
|
||||
$this->createMock(Task::class)
|
||||
);
|
||||
}
|
||||
|
||||
public function testItDoesNotReEnableWhenEnabled(): void
|
||||
{
|
||||
$loop = $this->createMock(LoopInterface::class);
|
||||
$task = $this->createMock(Task::class);
|
||||
$tasks = new Tasks($task);
|
||||
|
||||
$task->expects(self::once())
|
||||
->method('enable')
|
||||
->with($loop);
|
||||
|
||||
$tasks->enable($loop);
|
||||
$tasks->enable($loop);
|
||||
}
|
||||
|
||||
public function testItEnablesAllTasksWhenEnableCalled(): void
|
||||
{
|
||||
$loop = $this->createMock(LoopInterface::class);
|
||||
$task1 = $this->createMock(Task::class);
|
||||
$task2 = $this->createMock(Task::class);
|
||||
$task3 = $this->createMock(Task::class);
|
||||
|
||||
foreach([$task1, $task2, $task3] as $task) {
|
||||
/** @var MockObject|Task $task */
|
||||
$task->expects(self::once())->method('enable')->with($loop);
|
||||
}
|
||||
|
||||
(new Tasks($task1, $task2, $task3))->enable($loop);
|
||||
}
|
||||
|
||||
public function testItCancelsAllTasksWhenCancelCalled(): void
|
||||
{
|
||||
$loop = $this->createMock(LoopInterface::class);
|
||||
$task1 = $this->createMock(Task::class);
|
||||
$task2 = $this->createMock(Task::class);
|
||||
$task3 = $this->createMock(Task::class);
|
||||
|
||||
foreach([$task1, $task2, $task3] as $task) {
|
||||
/** @var MockObject|Task $task */
|
||||
$task->expects(self::once())->method('cancel')->with($loop);
|
||||
}
|
||||
|
||||
$tasks = new Tasks($task1, $task2, $task3);
|
||||
$this->setProperty($tasks, 'loop', $loop);
|
||||
$tasks->cancel();
|
||||
}
|
||||
|
||||
public function testItDoesNotCancelTasksWhenTheyAreNotEnabled(): void
|
||||
{
|
||||
$task1 = $this->createMock(Task::class);
|
||||
$task2 = $this->createMock(Task::class);
|
||||
$task3 = $this->createMock(Task::class);
|
||||
|
||||
foreach([$task1, $task2, $task3] as $task) {
|
||||
/** @var MockObject|Task $task */
|
||||
$task->expects(self::never())->method('cancel');
|
||||
}
|
||||
|
||||
$tasks = new Tasks($task1, $task2, $task3);
|
||||
$tasks->cancel();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user