Skip to content

Commit 9a497ec

Browse files
authored
Merge pull request #30 from 21TORR/unique-task-id-format
2 parents 4a605ec + ca8794f commit 9a497ec

5 files changed

Lines changed: 93 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
=====
33

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

78

89
2.0.3

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"21torr/janus": "^1.4.0",
3333
"bamarni/composer-bin-plugin": "^1.8",
3434
"roave/security-advisories": "dev-latest",
35-
"symfony/phpunit-bridge": "^7.2"
35+
"symfony/phpunit-bridge": "^7.2",
36+
"symfony/translation-contracts": "^3.6"
3637
},
3738
"autoload": {
3839
"psr-4": {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Torr\TaskManager\Exception\Task;
4+
5+
use Torr\TaskManager\Exception\TaskManagerException;
6+
7+
/**
8+
* @final
9+
*/
10+
class InvalidTaskDefinitionException extends \InvalidArgumentException implements TaskManagerException
11+
{
12+
}

src/Task/TaskMetaData.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Torr\TaskManager\Task;
44

55
use Symfony\Component\String\Slugger\AsciiSlugger;
6+
use Torr\TaskManager\Exception\Task\InvalidTaskDefinitionException;
67

78
use function Symfony\Component\String\u;
89

@@ -17,13 +18,28 @@ public function __construct (
1718
public string $label,
1819
public ?string $group = null,
1920
public ?string $uniqueTaskId = null,
20-
) {}
21+
)
22+
{
23+
if (null !== $this->uniqueTaskId && !preg_match('~^[a-z0-9]+([.\\-_][a-z0-9]+)*$~', $this->uniqueTaskId))
24+
{
25+
throw new InvalidTaskDefinitionException(\sprintf(
26+
"Invalid unique task id: '%s'",
27+
$this->uniqueTaskId,
28+
));
29+
}
30+
}
2131

2232
/**
2333
* Returns a unique key for this task
2434
*/
2535
public function getKey () : string
2636
{
37+
// these are validated to be safe, so we can keep using these
38+
if (null !== $this->uniqueTaskId)
39+
{
40+
return $this->uniqueTaskId;
41+
}
42+
2743
$slugger = new AsciiSlugger("en");
2844
$key = u($this->label)->lower()->toString();
2945

tests/Task/TaskMetaDataTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Tests\Torr\TaskManager\Task;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Torr\TaskManager\Exception\Task\InvalidTaskDefinitionException;
7+
use Torr\TaskManager\Task\TaskMetaData;
8+
9+
/**
10+
* @internal
11+
*/
12+
final class TaskMetaDataTest extends TestCase
13+
{
14+
public static function provideValidUniqueTaskIds () : iterable
15+
{
16+
yield "plain" => ["test"];
17+
yield "with dash" => ["a-b"];
18+
yield "with underscore" => ["a-b_c"];
19+
yield "all characters" => ["a-b_c.d"];
20+
yield "numbers" => ["5"];
21+
yield "numbers longer" => ["1-2-3-4"];
22+
}
23+
24+
/**
25+
* @dataProvider provideValidUniqueTaskIds
26+
*/
27+
public function testValidUniqueTaskIds (string $uniqueTaskId) : void
28+
{
29+
$metadata = new TaskMetaData("Test", uniqueTaskId: $uniqueTaskId);
30+
self::assertSame($uniqueTaskId, $metadata->getKey());
31+
}
32+
33+
public static function provideInvalidUniqueTaskIds () : iterable
34+
{
35+
yield "empty" => [""];
36+
yield "dash at the end" => ["test-"];
37+
yield "dot at the end" => ["test."];
38+
yield "underscore at the end" => ["test_"];
39+
yield "dash at the beginning" => ["-test"];
40+
yield "dot at the beginning" => ["_test"];
41+
yield "underscore at the beginning" => [".test"];
42+
yield "double dash" => ["a--b"];
43+
yield "special characters" => ["a@b"];
44+
yield "upper case characters" => ["aBc"];
45+
}
46+
47+
/**
48+
* @dataProvider provideInvalidUniqueTaskIds
49+
*/
50+
public function testInvalidUniqueTaskIds (string $uniqueTaskId) : void
51+
{
52+
$this->expectException(InvalidTaskDefinitionException::class);
53+
$this->expectExceptionMessage(\sprintf(
54+
"Invalid unique task id: '%s'",
55+
$uniqueTaskId,
56+
));
57+
58+
new TaskMetaData("Test", uniqueTaskId: $uniqueTaskId);
59+
}
60+
}

0 commit comments

Comments
 (0)