Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
=====

* (feature) Add `task-manager:run-worker` command as wrapper for Symfony messengers `consume` command.
* (improvement) Default to limit of 5 messages in run worker command, if no other limit is given.
* (improvement) Default to `limit` of 5 messages in run worker command, if no other limit is given.
* (improvement) Validate unique task ids to a specific format.


2.0.3
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"21torr/janus": "^1.4.0",
"bamarni/composer-bin-plugin": "^1.8",
"roave/security-advisories": "dev-latest",
"symfony/phpunit-bridge": "^7.2"
"symfony/phpunit-bridge": "^7.2",
"symfony/translation-contracts": "^3.6"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 12 additions & 0 deletions src/Exception/Task/InvalidTaskDefinitionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace Torr\TaskManager\Exception\Task;

use Torr\TaskManager\Exception\TaskManagerException;

/**
* @final
*/
class InvalidTaskDefinitionException extends \InvalidArgumentException implements TaskManagerException
{
}
18 changes: 17 additions & 1 deletion src/Task/TaskMetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Torr\TaskManager\Task;

use Symfony\Component\String\Slugger\AsciiSlugger;
use Torr\TaskManager\Exception\Task\InvalidTaskDefinitionException;

use function Symfony\Component\String\u;

Expand All @@ -17,13 +18,28 @@ public function __construct (
public string $label,
public ?string $group = null,
public ?string $uniqueTaskId = null,
) {}
)
{
if (null !== $this->uniqueTaskId && !preg_match('~^[a-z0-9]+([.\\-_][a-z0-9]+)*$~', $this->uniqueTaskId))
{
throw new InvalidTaskDefinitionException(\sprintf(
"Invalid unique task id: '%s'",
$this->uniqueTaskId,
));
}
}

/**
* Returns a unique key for this task
*/
public function getKey () : string
{
// these are validated to be safe, so we can keep using these
if (null !== $this->uniqueTaskId)
{
return $this->uniqueTaskId;
}

$slugger = new AsciiSlugger("en");
$key = u($this->label)->lower()->toString();

Expand Down
60 changes: 60 additions & 0 deletions tests/Task/TaskMetaDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types=1);

namespace Tests\Torr\TaskManager\Task;

use PHPUnit\Framework\TestCase;
use Torr\TaskManager\Exception\Task\InvalidTaskDefinitionException;
use Torr\TaskManager\Task\TaskMetaData;

/**
* @internal
*/
final class TaskMetaDataTest extends TestCase
{
public static function provideValidUniqueTaskIds () : iterable
{
yield "plain" => ["test"];
yield "with dash" => ["a-b"];
yield "with underscore" => ["a-b_c"];
yield "all characters" => ["a-b_c.d"];
yield "numbers" => ["5"];
yield "numbers longer" => ["1-2-3-4"];
}

/**
* @dataProvider provideValidUniqueTaskIds
*/
public function testValidUniqueTaskIds (string $uniqueTaskId) : void
{
$metadata = new TaskMetaData("Test", uniqueTaskId: $uniqueTaskId);
self::assertSame($uniqueTaskId, $metadata->getKey());
}

public static function provideInvalidUniqueTaskIds () : iterable
{
yield "empty" => [""];
yield "dash at the end" => ["test-"];
yield "dot at the end" => ["test."];
yield "underscore at the end" => ["test_"];
yield "dash at the beginning" => ["-test"];
yield "dot at the beginning" => ["_test"];
yield "underscore at the beginning" => [".test"];
yield "double dash" => ["a--b"];
yield "special characters" => ["a@b"];
yield "upper case characters" => ["aBc"];
}

/**
* @dataProvider provideInvalidUniqueTaskIds
*/
public function testInvalidUniqueTaskIds (string $uniqueTaskId) : void
{
$this->expectException(InvalidTaskDefinitionException::class);
$this->expectExceptionMessage(\sprintf(
"Invalid unique task id: '%s'",
$uniqueTaskId,
));

new TaskMetaData("Test", uniqueTaskId: $uniqueTaskId);
}
}