diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bae1d6..1990d95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/composer.json b/composer.json index 188cc48..8c8d9b2 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Exception/Task/InvalidTaskDefinitionException.php b/src/Exception/Task/InvalidTaskDefinitionException.php new file mode 100644 index 0000000..5aeef02 --- /dev/null +++ b/src/Exception/Task/InvalidTaskDefinitionException.php @@ -0,0 +1,12 @@ +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(); diff --git a/tests/Task/TaskMetaDataTest.php b/tests/Task/TaskMetaDataTest.php new file mode 100644 index 0000000..b0d52cf --- /dev/null +++ b/tests/Task/TaskMetaDataTest.php @@ -0,0 +1,60 @@ + ["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); + } +}