Skip to content

Commit

Permalink
test callable task
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed May 20, 2024
1 parent 3f6b3ed commit 611f9af
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/CallableTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Amp\Cancellation;
use Amp\Parallel\Worker\Task;
use Amp\Sync\Channel;
use InvalidArgumentException;

/**
* @template-implements Task<mixed, never, never>
Expand All @@ -30,9 +31,12 @@ final class CallableTask implements Task
private array $arguments;

public function __construct(
string $callable,
callable $callable,
mixed ...$arguments
) {
if (! is_string($callable)) {
throw new InvalidArgumentException('Argument $callable must be a callable string');
}
$this->callable = $callable;
$this->arguments = $arguments;
}
Expand Down
45 changes: 45 additions & 0 deletions tests/CallableTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Chevere\Tests;

use Chevere\Workflow\CallableTask;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

final class CallableTaskTest extends TestCase
{
public function testConstruct(): void
{
$callable = 'is_string';
$task = new CallableTask($callable, 'string');
$channel = $this->createMock('Amp\Sync\Channel');
$cancellation = $this->createMock('Amp\Cancellation');
$return = $task->run($channel, $cancellation);
$this->assertTrue($return);
$task = new CallableTask($callable, 1);
$return = $task->run($channel, $cancellation);
$this->assertFalse($return);
}

public function testInvalidCallable(): void
{
$callable = function () {
return 'string';
};
$arguments = [];
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Argument $callable must be a callable string');
new CallableTask($callable, ...$arguments);
}
}

0 comments on commit 611f9af

Please sign in to comment.