From ff2ef03e38885fb0753ecf0a3a04bc1e72233fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Sun, 7 Feb 2016 12:10:02 +0100 Subject: [PATCH] Implemented detection of duplicate task name --- Cronner/Cronner.php | 6 +++- Cronner/exceptions.php | 10 ++++++ .../objects/AnotherSimpleTestObject.php | 21 +++++++++++++ .../objects/NextSimpleTestObject.php | 21 +++++++++++++ .../objects/SameTaskNameObject.php | 31 +++++++++++++++++++ 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tests/CronnerTests/objects/AnotherSimpleTestObject.php create mode 100644 tests/CronnerTests/objects/NextSimpleTestObject.php create mode 100644 tests/CronnerTests/objects/SameTaskNameObject.php diff --git a/Cronner/Cronner.php b/Cronner/Cronner.php index ce54c64..55c5f01 100644 --- a/Cronner/Cronner.php +++ b/Cronner/Cronner.php @@ -181,7 +181,11 @@ public function addTasks($tasks) $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC); foreach ($methods as $method) { if (!Strings::startsWith($method->getName(), '__') && $method->hasAnnotation(Parameters::TASK)) { - $this->tasks[] = new Task($tasks, $method, $this->timestampStorage); + $task = new Task($tasks, $method, $this->timestampStorage); + if (array_key_exists($task->getName(), $this->tasks)) { + throw new DuplicateTaskNameException('Cannot use more tasks with the same name "' . $task->getName() . '".'); + } + $this->tasks[$task->getName()] = $task; } } $this->registeredTaskObjects[] = $tasksId; diff --git a/Cronner/exceptions.php b/Cronner/exceptions.php index fafd34b..d0070c1 100644 --- a/Cronner/exceptions.php +++ b/Cronner/exceptions.php @@ -44,6 +44,16 @@ class EmptyTaskNameException extends InvalidArgumentException +/** + * @author Martin Štekl + */ +class DuplicateTaskNameException extends InvalidArgumentException +{ + +} + + + /** * @author Martin Štekl */ diff --git a/tests/CronnerTests/objects/AnotherSimpleTestObject.php b/tests/CronnerTests/objects/AnotherSimpleTestObject.php new file mode 100644 index 0000000..890bbec --- /dev/null +++ b/tests/CronnerTests/objects/AnotherSimpleTestObject.php @@ -0,0 +1,21 @@ + + */ +class AnotherSimpleTestObject extends Object +{ + + /** + * @cronner-task Test + */ + public function test01() + { + } + +} diff --git a/tests/CronnerTests/objects/NextSimpleTestObject.php b/tests/CronnerTests/objects/NextSimpleTestObject.php new file mode 100644 index 0000000..302663b --- /dev/null +++ b/tests/CronnerTests/objects/NextSimpleTestObject.php @@ -0,0 +1,21 @@ + + */ +class NextSimpleTestObject extends Object +{ + + /** + * @cronner-task Test + */ + public function test01() + { + } + +} diff --git a/tests/CronnerTests/objects/SameTaskNameObject.php b/tests/CronnerTests/objects/SameTaskNameObject.php new file mode 100644 index 0000000..8936792 --- /dev/null +++ b/tests/CronnerTests/objects/SameTaskNameObject.php @@ -0,0 +1,31 @@ + + */ +class SameTaskNameObject extends Object +{ + + /** + * @cronner-task Test + */ + public function test01() + { + } + + + + /** + * @cronner-task Test + */ + public function test02() + { + } + +}