From b0bd7ab8f357f6ff4968fec7d863534ba0175876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 22:03:03 +0100 Subject: [PATCH 01/20] Updated license --- README.md | 30 ------------------------------ license.txt | 2 +- 2 files changed, 1 insertion(+), 31 deletions(-) diff --git a/README.md b/README.md index f2a45be..1023c83 100644 --- a/README.md +++ b/README.md @@ -169,33 +169,3 @@ start from any reason. * @cronner-time 11:00, 23:30 - 05:00 */ ``` - -## Author - -My name is Martin Štekl. Feel free to contact me on [e-mail](mailto:martin.stekl@gmail.com) -or follow me on [Twitter](https://twitter.com/stekycz). - -## License - -Copyright (c) 2013 Martin Štekl - -Permission is hereby granted, free of charge, to any person -obtaining a copy of this software and associated documentation -files (the "Software"), to deal in the Software without -restriction, including without limitation the rights to use, -copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the -Software is furnished to do so, subject to the following -conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -OTHER DEALINGS IN THE SOFTWARE. diff --git a/license.txt b/license.txt index 96d474f..d403d93 100644 --- a/license.txt +++ b/license.txt @@ -1,4 +1,4 @@ -Copyright (c) 2013 Martin Štekl +Copyright (c) 2017 Martin Štekl Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation From 76c257fae7f406a46257c1aab67d3125a22f7657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 22:03:31 +0100 Subject: [PATCH 02/20] Updated dependencies to newer versions --- composer.json | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 51b1e26..4172731 100644 --- a/composer.json +++ b/composer.json @@ -18,17 +18,17 @@ }, "require": { "php": ">=5.3.3", - "tracy/tracy": "@dev", - "nette/di": "@dev", - "nette/bootstrap": "@dev", - "nette/utils": "@dev", - "nette/reflection": "@dev", - "nette/safe-stream": "@dev" + "tracy/tracy": "^2.4", + "nette/di": "^2.4", + "nette/bootstrap": "^2.4", + "nette/utils": "^2.4", + "nette/reflection": "^2.4", + "nette/safe-stream": "^2.3" }, "require-dev": { - "nette/tester": "~1.3", - "janmarek/mockista": "~1.0", - "jakub-onderka/php-parallel-lint": "~0.8" + "nette/tester": "^1.7", + "janmarek/mockista": "^1.0", + "jakub-onderka/php-parallel-lint": "^0.9" }, "suggest": { "kdyby/events": "Events for Nette Framework", From 406cd1ce64905241fe1d2201df15516cf2de6867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 22:03:45 +0100 Subject: [PATCH 03/20] Added PHP 7.1 into test matrix --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 0856681..0d097dd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ php: - 5.5 - 5.6 - 7.0 + - 7.1 - hhvm env: From 2e04048e1a593576bf30e54875d0465e2f1557fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 22:03:57 +0100 Subject: [PATCH 04/20] Do not prefer source in composer install --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0d097dd..e9ad64a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,7 +28,7 @@ before_install: install: - if [[ "$TRAVIS_PHP_VERSION" == "5.3.3" ]]; then composer config -g disable-tls true; composer config -g secure-http false; fi - composer self-update - - composer install --prefer-source --no-interaction --optimize-autoloader + - composer install --no-interaction --optimize-autoloader before_script: - ./vendor/bin/parallel-lint -e php,phpt --exclude vendor . From 490540cf80dc6c95e2fbedbcc7c2e6a446b747a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 22:28:53 +0100 Subject: [PATCH 05/20] Used Mockery instead of Mockista --- composer.json | 4 +-- tests/CronnerTests/Cronner.phpt | 55 ++++++++++++------------------ tests/CronnerTests/Tasks/Task.phpt | 37 ++++++-------------- tests/CronnerTests/bootstrap.php | 19 +---------- 4 files changed, 34 insertions(+), 81 deletions(-) diff --git a/composer.json b/composer.json index 4172731..8224dd8 100644 --- a/composer.json +++ b/composer.json @@ -27,8 +27,8 @@ }, "require-dev": { "nette/tester": "^1.7", - "janmarek/mockista": "^1.0", - "jakub-onderka/php-parallel-lint": "^0.9" + "jakub-onderka/php-parallel-lint": "^0.9", + "mockery/mockery": "^0.9" }, "suggest": { "kdyby/events": "Events for Nette Framework", diff --git a/tests/CronnerTests/Cronner.phpt b/tests/CronnerTests/Cronner.phpt index 438da80..fd0a47a 100644 --- a/tests/CronnerTests/Cronner.phpt +++ b/tests/CronnerTests/Cronner.phpt @@ -7,6 +7,7 @@ namespace stekycz\Cronner\tests; use Exception; +use Mockery; use Nette\Utils\DateTime; use stdClass; use stekycz\Cronner\Cronner; @@ -43,23 +44,16 @@ class CronnerTest extends \TestCase protected function setUp() { parent::setUp(); - $this->timestampStorage = $this->mockista->create( - '\stekycz\Cronner\ITimestampStorage', - array('setTaskName', 'saveRunTime', 'loadLastRunTime') - ); - $this->timestampStorage->expects('loadLastRunTime') - ->andReturn(new DateTime('2013-02-04 08:00:00')); + $timestampStorage = Mockery::mock('\stekycz\Cronner\ITimestampStorage'); + $timestampStorage->shouldReceive('setTaskName'); + $timestampStorage->shouldReceive('saveRunTime'); + $timestampStorage->shouldReceive('loadLastRunTime')->andReturn(new DateTime('2013-02-04 08:00:00')); + $this->timestampStorage = $timestampStorage; - $criticalSection = $this->mockista->create( - '\stekycz\Cronner\CriticalSection', - array("enter", "leave", "isEntered") - ); - $criticalSection->expects("enter") - ->andReturn(TRUE); - $criticalSection->expects("leave") - ->andReturn(TRUE); - $criticalSection->expects("isEntered") - ->andReturn(FALSE); + $criticalSection = Mockery::mock('\stekycz\Cronner\CriticalSection'); + $criticalSection->shouldReceive("enter")->andReturn(TRUE); + $criticalSection->shouldReceive("leave")->andReturn(TRUE); + $criticalSection->shouldReceive("isEntered")->andReturn(FALSE); $this->cronner = new Cronner($this->timestampStorage, $criticalSection); $this->cronner->onTaskBegin = array(); @@ -149,11 +143,8 @@ class CronnerTest extends \TestCase $now = new DateTime('2013-02-04 09:30:00'); $tasks = new TestObject(); - $this->timestampStorage->expects('setTaskName') - ->atLeast(8); - $this->timestampStorage->expects('saveRunTime') - ->with($now) - ->atLeastOnce(); + $this->timestampStorage->shouldReceive('setTaskName')->atLeast(8); + $this->timestampStorage->shouldReceive('saveRunTime')->with($now)->atLeast(1); $this->cronner->addTasks($tasks); Assert::equal(4, $this->cronner->countTasks()); @@ -196,19 +187,15 @@ class CronnerTest extends \TestCase { $now = new DateTime('2013-02-04 09:30:00'); - $logCallback = $this->mockista->create('\Nette\Object', array('logBegin', 'logFinished', 'logError')); - $logCallback->expects('logError') - ->once() - ->andCallback(function (Exception $e, Task $task) { - Assert::equal('Test 01', $e->getMessage()); - }); - $logCallback->expects('logBegin') - ->twice(); - $logCallback->expects('logFinished') - ->once(); - - $this->timestampStorage->expects('setTaskName'); - $this->timestampStorage->expects('saveRunTime'); + $logCallback = Mockery::mock('\Nette\Object'); + $logCallback->shouldReceive('logError')->once()->andReturnUsing(function (Exception $e, Task $task) { + Assert::equal('Test 01', $e->getMessage()); + }); + $logCallback->shouldReceive('logBegin')->twice(); + $logCallback->shouldReceive('logFinished')->once(); + + $this->timestampStorage->shouldReceive('setTaskName'); + $this->timestampStorage->shouldReceive('saveRunTime'); $this->cronner->onTaskBegin[] = function (Cronner $cronner, Task $task) use ($logCallback) { $logCallback->logBegin($task); diff --git a/tests/CronnerTests/Tasks/Task.phpt b/tests/CronnerTests/Tasks/Task.phpt index 9ca81c8..f1b809f 100644 --- a/tests/CronnerTests/Tasks/Task.phpt +++ b/tests/CronnerTests/Tasks/Task.phpt @@ -6,6 +6,7 @@ namespace stekycz\Cronner\tests\Tasks; +use Mockery; use Nette\Reflection\Method; use Nette; use stekycz\Cronner\Tasks\Task; @@ -40,15 +41,9 @@ class TaskTest extends \TestCase public function testInvokesTaskWithSavingLastRunTime() { $now = new Nette\Utils\DateTime(); - $timestampStorage = $this->mockista->create( - '\stekycz\Cronner\ITimestampStorage', - array("setTaskName", "saveRunTime", "loadLastRunTime") - ); - $timestampStorage->expects("saveRunTime") - ->with($now) - ->once(); - $timestampStorage->expects("setTaskName") - ->exactly(2); + $timestampStorage = Mockery::mock('\stekycz\Cronner\ITimestampStorage'); + $timestampStorage->shouldReceive("saveRunTime")->with($now)->once(); + $timestampStorage->shouldReceive("setTaskName")->times(2); $method = new Method($this->object, 'test01'); $task = new Task($this->object, $method, $timestampStorage); @@ -73,15 +68,9 @@ class TaskTest extends \TestCase $method = $this->object->getReflection()->getMethod($methodName); - $timestampStorage = $this->mockista->create( - '\stekycz\Cronner\ITimestampStorage', - array('setTaskName', 'saveRunTime', 'loadLastRunTime',) - ); - $timestampStorage->expects("loadLastRunTime") - ->exactly($loads) - ->andReturn($lastRunTime); - $timestampStorage->expects("setTaskName") - ->atLeastOnce(); + $timestampStorage = Mockery::mock('\stekycz\Cronner\ITimestampStorage'); + $timestampStorage->shouldReceive("loadLastRunTime")->times($loads)->andReturn($lastRunTime); + $timestampStorage->shouldReceive("setTaskName")->atLeast(1); $task = new Task($this->object, $method, $timestampStorage); Assert::same($expected, $task->shouldBeRun($now)); @@ -109,15 +98,9 @@ class TaskTest extends \TestCase public function testShouldBeRunOnShortLaterRun() { - $timestampStorage = $this->mockista->create( - '\stekycz\Cronner\ITimestampStorage', - array("setTaskName", "saveRunTime", "loadLastRunTime") - ); - $timestampStorage->expects("loadLastRunTime") - ->andReturn(new Nette\Utils\DateTime('2014-08-15 09:00:01')) - ->once(); - $timestampStorage->expects("setTaskName") - ->atLeastOnce(); + $timestampStorage = Mockery::mock('\stekycz\Cronner\ITimestampStorage'); + $timestampStorage->shouldReceive("loadLastRunTime")->once()->andReturn(new Nette\Utils\DateTime('2014-08-15 09:00:01')); + $timestampStorage->shouldReceive("setTaskName")->atLeast(1); $method = new Method($this->object, 'test03'); $task = new Task($this->object, $method, $timestampStorage); diff --git a/tests/CronnerTests/bootstrap.php b/tests/CronnerTests/bootstrap.php index 16c8bd7..a73b024 100644 --- a/tests/CronnerTests/bootstrap.php +++ b/tests/CronnerTests/bootstrap.php @@ -1,7 +1,5 @@ mockista = new Registry(); - usleep(1); // Hack for Mockista - } - - - protected function tearDown() { - $this->mockista->assertExpectations(); + Mockery::close(); } } From dbe66a75ea44456a6c7a21074a3ef3513f832718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 22:43:22 +0100 Subject: [PATCH 06/20] Updated code style --- Cronner/Bar/Tasks.php | 2 ++ Cronner/CriticalSection.php | 13 +-------- Cronner/Cronner.php | 24 ++--------------- Cronner/DI/CronnerExtension.php | 2 -- Cronner/ITimestampStorage.php | 6 ----- Cronner/Tasks/Parameters.php | 31 ++++++---------------- Cronner/Tasks/Parser.php | 32 +++-------------------- Cronner/Tasks/Task.php | 18 ------------- Cronner/TimestampStorage/DummyStorage.php | 6 ----- Cronner/TimestampStorage/FileStorage.php | 12 --------- Cronner/exceptions.php | 12 --------- 11 files changed, 16 insertions(+), 142 deletions(-) diff --git a/Cronner/Bar/Tasks.php b/Cronner/Bar/Tasks.php index 41319d0..b802761 100644 --- a/Cronner/Bar/Tasks.php +++ b/Cronner/Bar/Tasks.php @@ -31,6 +31,7 @@ public function getPanel() } ob_start(); require __DIR__ . '/templates/panel.phtml'; + return ob_get_clean(); } @@ -39,6 +40,7 @@ public function getTab() ob_start(); $count = $this->cronner->countTasks(); require __DIR__ . '/templates/tab.phtml'; + return ob_get_clean(); } diff --git a/Cronner/CriticalSection.php b/Cronner/CriticalSection.php index 01e3291..a1220e5 100644 --- a/Cronner/CriticalSection.php +++ b/Cronner/CriticalSection.php @@ -5,8 +5,6 @@ use Nette\Object; use Nette\Utils\FileSystem; - - /** * @author Martin Štekl */ @@ -23,8 +21,6 @@ class CriticalSection extends Object */ private $lockFilesDir; - - /** * @param string $lockFilesDir */ @@ -35,8 +31,6 @@ public function __construct($lockFilesDir) $this->lockFilesDir = $lockFilesDir; } - - /** * Enters critical section. * @@ -57,6 +51,7 @@ public function enter($label) $locked = flock($handle, LOCK_EX | LOCK_NB); if ($locked === FALSE) { fclose($handle); + return FALSE; } $this->locks[$label] = $handle; @@ -64,8 +59,6 @@ public function enter($label) return TRUE; } - - /** * Leaves critical section. * @@ -89,8 +82,6 @@ public function leave($label) return TRUE; } - - /** * Returns TRUE if critical section is entered. * @@ -102,8 +93,6 @@ public function isEntered($label) return array_key_exists($label, $this->locks) && $this->locks[$label] !== NULL; } - - /** * @param string $label * @return string diff --git a/Cronner/Cronner.php b/Cronner/Cronner.php index 55c5f01..8ed0d6a 100644 --- a/Cronner/Cronner.php +++ b/Cronner/Cronner.php @@ -3,17 +3,15 @@ namespace stekycz\Cronner; use Exception; -use Nette\Utils\DateTime; use Nette\Object; use Nette\Reflection\ClassType; +use Nette\Utils\DateTime; use Nette\Utils\Strings; use ReflectionMethod; use stekycz\Cronner\Tasks\Parameters; use stekycz\Cronner\Tasks\Task; use Tracy\Debugger; - - /** * @author Martin Štekl * @method onTaskBegin(\stekycz\Cronner\Cronner $cronner, \stekycz\Cronner\Tasks\Task $task) @@ -68,8 +66,6 @@ class Cronner extends Object */ private $skipFailedTask = TRUE; - - /** * @param \stekycz\Cronner\ITimestampStorage $timestampStorage * @param \stekycz\Cronner\CriticalSection $criticalSection @@ -111,8 +107,6 @@ public function setTimestampStorage(ITimestampStorage $timestampStorage) return $this; } - - /** * Sets max execution time for Cronner. It is used only when Cronner runs. * @@ -132,8 +126,6 @@ public function setMaxExecutionTime($maxExecutionTime = NULL) return $this; } - - /** * Sets flag that thrown exceptions will not be thrown but cached and logged. * @@ -147,8 +139,6 @@ public function setSkipFailedTask($skipFailedTask = TRUE) return $this; } - - /** * Returns max execution time for Cronner. It does not load INI value. * @@ -159,8 +149,6 @@ public function getMaxExecutionTime() return !is_null($this->maxExecutionTime) ? (int) $this->maxExecutionTime : NULL; } - - /** * Adds task case to be processed when cronner runs. If tasks * with name which is already added are given then throws @@ -181,7 +169,7 @@ public function addTasks($tasks) $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC); foreach ($methods as $method) { if (!Strings::startsWith($method->getName(), '__') && $method->hasAnnotation(Parameters::TASK)) { - $task = 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() . '".'); } @@ -193,8 +181,6 @@ public function addTasks($tasks) return $this; } - - /** * Runs all cron tasks. * @@ -235,8 +221,6 @@ public function run(\DateTime $now = NULL) } } - - /** * Returns count of added task objects. * @@ -247,8 +231,6 @@ public function countTaskObjects() return count($this->registeredTaskObjects); } - - /** * Returns count of added tasks. * @@ -259,8 +241,6 @@ public function countTasks() return count($this->tasks); } - - /** * Creates and returns identification string for given object. * diff --git a/Cronner/DI/CronnerExtension.php b/Cronner/DI/CronnerExtension.php index 7a84599..f0ae503 100644 --- a/Cronner/DI/CronnerExtension.php +++ b/Cronner/DI/CronnerExtension.php @@ -11,8 +11,6 @@ use Nette\Utils\Json; use Nette\Utils\Validators; - - /** * @author Martin Štekl */ diff --git a/Cronner/ITimestampStorage.php b/Cronner/ITimestampStorage.php index 2113a94..3ed56c3 100644 --- a/Cronner/ITimestampStorage.php +++ b/Cronner/ITimestampStorage.php @@ -4,8 +4,6 @@ use DateTime; - - /** * @author Martin Štekl */ @@ -19,8 +17,6 @@ interface ITimestampStorage */ public function setTaskName($taskName = NULL); - - /** * Saves current date and time as last invocation time. * @@ -28,8 +24,6 @@ public function setTaskName($taskName = NULL); */ public function saveRunTime(DateTime $now); - - /** * Returns date and time of last cron task invocation. * diff --git a/Cronner/Tasks/Parameters.php b/Cronner/Tasks/Parameters.php index 84bcd2e..c387a7e 100644 --- a/Cronner/Tasks/Parameters.php +++ b/Cronner/Tasks/Parameters.php @@ -8,8 +8,6 @@ use Nette\Reflection\Method; use Nette\Utils\Strings; - - /** * @author Martin Štekl */ @@ -26,8 +24,6 @@ final class Parameters extends Object */ private $values; - - /** * @param array $values */ @@ -39,8 +35,6 @@ public function __construct(array $values) $this->values = $values; } - - /** * Returns name of task. * @@ -51,8 +45,6 @@ public function getName() return $this->values[static::TASK]; } - - /** * Returns true if task is really a task. * @@ -63,8 +55,6 @@ public function isTask() return Strings::length($this->values[static::TASK]) > 0; } - - /** * Returns true if today is allowed day of week. * @@ -80,8 +70,6 @@ public function isInDay(DateTime $now) return TRUE; } - - /** * Returns true if current time is in allowed range. * @@ -107,8 +95,6 @@ public function isInTime(DateTime $now) return TRUE; } - - /** * Returns true if current time is next period of invocation. * @@ -121,14 +107,13 @@ public function isNextPeriod(DateTime $now, DateTime $lastRunTime = NULL) if (isset($this->values[static::PERIOD]) && $this->values[static::PERIOD]) { // Prevent run task on next cronner run because of a few seconds shift $now = Nette\Utils\DateTime::from($now)->modifyClone('+5 seconds'); + return $lastRunTime === NULL || $lastRunTime->modify('+ ' . $this->values[static::PERIOD]) <= $now; } return TRUE; } - - /** * Parse cronner values from annotations. * @@ -146,16 +131,16 @@ public static function parseParameters(Method $method) $parameters = array( static::TASK => Parser::parseName($method->getAnnotation(Parameters::TASK)) - ? : $taskName, + ?: $taskName, static::PERIOD => $method->hasAnnotation(Parameters::PERIOD) - ? Parser::parsePeriod($method->getAnnotation(Parameters::PERIOD)) - : NULL, + ? Parser::parsePeriod($method->getAnnotation(Parameters::PERIOD)) + : NULL, static::DAYS => $method->hasAnnotation(Parameters::DAYS) - ? Parser::parseDays($method->getAnnotation(Parameters::DAYS)) - : NULL, + ? Parser::parseDays($method->getAnnotation(Parameters::DAYS)) + : NULL, static::TIME => $method->hasAnnotation(Parameters::TIME) - ? Parser::parseTimes($method->getAnnotation(Parameters::TIME)) - : NULL, + ? Parser::parseTimes($method->getAnnotation(Parameters::TIME)) + : NULL, ); return $parameters; diff --git a/Cronner/Tasks/Parser.php b/Cronner/Tasks/Parser.php index 8c9ea51..111d310 100644 --- a/Cronner/Tasks/Parser.php +++ b/Cronner/Tasks/Parser.php @@ -6,8 +6,6 @@ use Nette\Utils\Strings; use stekycz\Cronner\InvalidParameterException; - - /** * @author Martin Štekl */ @@ -31,8 +29,6 @@ public static function parseName($annotation) return $name; } - - /** * Parses period of cron task. If annotation is invalid throws exception. * @@ -52,11 +48,9 @@ public static function parsePeriod($annotation) $period = $annotation; } - return $period ? : NULL; + return $period ?: NULL; } - - /** * Parses allowed days for cron task. If annotation is invalid * throws exception. @@ -85,11 +79,9 @@ public static function parseDays($annotation) $days = array_values(array_intersect($validValues, $days)); } - return $days ? : NULL; + return $days ?: NULL; } - - /** * Parses allowed time ranges for cron task. If annotation is invalid * throws exception. @@ -115,11 +107,9 @@ public static function parseTimes($annotation) } } - return $times ? : NULL; + return $times ?: NULL; } - - /** * Translates given annotation to day names. * @@ -149,8 +139,6 @@ private static function translateToDayNames($annotation) return array_unique($days); } - - /** * Expands given day names and day ranges to day names only. The day range must be * in "Mon-Fri" format. @@ -186,8 +174,6 @@ private static function expandDaysRange(array $days) return array_unique($expandedValues); } - - /** * Splits given annotation by comma into array. * @@ -199,8 +185,6 @@ private static function splitMultipleValues($annotation) return Strings::split($annotation, '/\s*,\s*/'); } - - /** * Returns True if time in valid format is given, False otherwise. * @@ -212,8 +196,6 @@ private static function isValidTime($time) return (bool) Strings::match($time, '/^\d{2}:\d{2}$/u'); } - - /** * Parses one time annotation. If it is invalid throws exception. * @@ -241,8 +223,6 @@ private static function parseOneTime($time) return $times; } - - /** * Translates given annotation to day names. * @@ -263,8 +243,6 @@ private static function translateToTimes($time) return array_key_exists($time, $translationMap) ? $translationMap[$time] : $time; } - - /** * Returns True if given times includes midnight, False otherwise. * @@ -277,8 +255,6 @@ private static function isTimeOverMidnight($from, $to) return $to !== NULL && $to < $from; } - - /** * Returns array structure with given times. * @@ -294,8 +270,6 @@ private static function timePartsToArray($from, $to) ); } - - /** * Checks if given annotation is valid. Throws exception if not. * diff --git a/Cronner/Tasks/Task.php b/Cronner/Tasks/Task.php index f5dac1e..4df50c2 100644 --- a/Cronner/Tasks/Task.php +++ b/Cronner/Tasks/Task.php @@ -8,8 +8,6 @@ use Nette\Reflection\Method; use stekycz\Cronner\ITimestampStorage; - - /** * @author Martin Štekl */ @@ -36,8 +34,6 @@ final class Task extends Object */ private $parameters = NULL; - - /** * Creates instance of one task. * @@ -52,8 +48,6 @@ public function __construct($object, Method $method, ITimestampStorage $timestam $this->timestampStorage = $timestampStorage; } - - /** * @return string */ @@ -62,8 +56,6 @@ public function getObjectName() return get_class($this->object); } - - /** * @return \Nette\Reflection\Method */ @@ -72,8 +64,6 @@ public function getMethodReflection() return $this->method; } - - /** * @return string */ @@ -84,8 +74,6 @@ public function getObjectPath() return $reflection->getFileName(); } - - /** * Returns True if given parameters should be run. * @@ -109,8 +97,6 @@ public function shouldBeRun(DateTime $now = NULL) && $parameters->isNextPeriod($now, $this->timestampStorage->loadLastRunTime()); } - - /** * Returns task name. * @@ -121,8 +107,6 @@ public function getName() return $this->getParameters()->getName(); } - - public function __invoke(\DateTime $now) { $this->method->invoke($this->object); @@ -131,8 +115,6 @@ public function __invoke(\DateTime $now) $this->timestampStorage->setTaskName(); } - - /** * Returns instance of parsed parameters. * diff --git a/Cronner/TimestampStorage/DummyStorage.php b/Cronner/TimestampStorage/DummyStorage.php index a18a45a..9788a29 100644 --- a/Cronner/TimestampStorage/DummyStorage.php +++ b/Cronner/TimestampStorage/DummyStorage.php @@ -6,8 +6,6 @@ use Nette\Object; use stekycz\Cronner\ITimestampStorage; - - /** * @author Martin Štekl */ @@ -24,8 +22,6 @@ public function setTaskName($taskName = NULL) // Dummy } - - /** * Saves current date and time as last invocation time. * @@ -36,8 +32,6 @@ public function saveRunTime(DateTime $now) // Dummy } - - /** * Returns date and time of last cron task invocation. * diff --git a/Cronner/TimestampStorage/FileStorage.php b/Cronner/TimestampStorage/FileStorage.php index 4325ba9..97071eb 100644 --- a/Cronner/TimestampStorage/FileStorage.php +++ b/Cronner/TimestampStorage/FileStorage.php @@ -11,8 +11,6 @@ use stekycz\Cronner\InvalidTaskNameException; use stekycz\Cronner\ITimestampStorage; - - /** * @author Martin Štekl */ @@ -31,8 +29,6 @@ class FileStorage extends Object implements ITimestampStorage */ private $taskName = NULL; - - /** * @param string $directory */ @@ -44,8 +40,6 @@ public function __construct($directory) $this->directory = $directory; } - - /** * Sets name of current task. * @@ -59,8 +53,6 @@ public function setTaskName($taskName = NULL) $this->taskName = $taskName; } - - /** * Saves current date and time as last invocation time. * @@ -72,8 +64,6 @@ public function saveRunTime(DateTime $now) file_put_contents($filepath, $now->format(self::DATETIME_FORMAT)); } - - /** * Returns date and time of last cron task invocation. * @@ -91,8 +81,6 @@ public function loadLastRunTime() return $date ? $date : NULL; } - - /** * Builds file path from directory and task name. * diff --git a/Cronner/exceptions.php b/Cronner/exceptions.php index d0070c1..027bff9 100644 --- a/Cronner/exceptions.php +++ b/Cronner/exceptions.php @@ -2,8 +2,6 @@ namespace stekycz\Cronner; - - /** * @author Martin Štekl */ @@ -12,8 +10,6 @@ class InvalidArgumentException extends \InvalidArgumentException } - - /** * @author Martin Štekl */ @@ -22,8 +18,6 @@ class InvalidParameterException extends InvalidArgumentException } - - /** * @author Martin Štekl */ @@ -32,8 +26,6 @@ class InvalidTaskNameException extends InvalidArgumentException } - - /** * @author Martin Štekl */ @@ -42,8 +34,6 @@ class EmptyTaskNameException extends InvalidArgumentException } - - /** * @author Martin Štekl */ @@ -52,8 +42,6 @@ class DuplicateTaskNameException extends InvalidArgumentException } - - /** * @author Martin Štekl */ From 111b5df0a403482216729f7b8fd500229e0dda18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 22:46:23 +0100 Subject: [PATCH 07/20] Removed not needed author annotations --- Cronner/CriticalSection.php | 3 --- Cronner/Cronner.php | 1 - Cronner/DI/CronnerExtension.php | 3 --- Cronner/ITimestampStorage.php | 3 --- Cronner/Tasks/Parameters.php | 3 --- Cronner/Tasks/Parser.php | 3 --- Cronner/Tasks/Task.php | 3 --- Cronner/TimestampStorage/DummyStorage.php | 3 --- Cronner/TimestampStorage/FileStorage.php | 3 --- Cronner/exceptions.php | 18 ------------------ 10 files changed, 43 deletions(-) diff --git a/Cronner/CriticalSection.php b/Cronner/CriticalSection.php index a1220e5..f8c0fe0 100644 --- a/Cronner/CriticalSection.php +++ b/Cronner/CriticalSection.php @@ -5,9 +5,6 @@ use Nette\Object; use Nette\Utils\FileSystem; -/** - * @author Martin Štekl - */ class CriticalSection extends Object { diff --git a/Cronner/Cronner.php b/Cronner/Cronner.php index 8ed0d6a..4945bf3 100644 --- a/Cronner/Cronner.php +++ b/Cronner/Cronner.php @@ -13,7 +13,6 @@ use Tracy\Debugger; /** - * @author Martin Štekl * @method onTaskBegin(\stekycz\Cronner\Cronner $cronner, \stekycz\Cronner\Tasks\Task $task) * @method onTaskFinished(\stekycz\Cronner\Cronner $cronner, \stekycz\Cronner\Tasks\Task $task) * @method onTaskError(\stekycz\Cronner\Cronner $cronner, \Exception $exception, \stekycz\Cronner\Tasks\Task $task) diff --git a/Cronner/DI/CronnerExtension.php b/Cronner/DI/CronnerExtension.php index f0ae503..0f80ed1 100644 --- a/Cronner/DI/CronnerExtension.php +++ b/Cronner/DI/CronnerExtension.php @@ -11,9 +11,6 @@ use Nette\Utils\Json; use Nette\Utils\Validators; -/** - * @author Martin Štekl - */ class CronnerExtension extends CompilerExtension { diff --git a/Cronner/ITimestampStorage.php b/Cronner/ITimestampStorage.php index 3ed56c3..1e30f0a 100644 --- a/Cronner/ITimestampStorage.php +++ b/Cronner/ITimestampStorage.php @@ -4,9 +4,6 @@ use DateTime; -/** - * @author Martin Štekl - */ interface ITimestampStorage { diff --git a/Cronner/Tasks/Parameters.php b/Cronner/Tasks/Parameters.php index c387a7e..19908ad 100644 --- a/Cronner/Tasks/Parameters.php +++ b/Cronner/Tasks/Parameters.php @@ -8,9 +8,6 @@ use Nette\Reflection\Method; use Nette\Utils\Strings; -/** - * @author Martin Štekl - */ final class Parameters extends Object { diff --git a/Cronner/Tasks/Parser.php b/Cronner/Tasks/Parser.php index 111d310..fa3678e 100644 --- a/Cronner/Tasks/Parser.php +++ b/Cronner/Tasks/Parser.php @@ -6,9 +6,6 @@ use Nette\Utils\Strings; use stekycz\Cronner\InvalidParameterException; -/** - * @author Martin Štekl - */ class Parser extends Object { diff --git a/Cronner/Tasks/Task.php b/Cronner/Tasks/Task.php index 4df50c2..3f7bf9e 100644 --- a/Cronner/Tasks/Task.php +++ b/Cronner/Tasks/Task.php @@ -8,9 +8,6 @@ use Nette\Reflection\Method; use stekycz\Cronner\ITimestampStorage; -/** - * @author Martin Štekl - */ final class Task extends Object { diff --git a/Cronner/TimestampStorage/DummyStorage.php b/Cronner/TimestampStorage/DummyStorage.php index 9788a29..3b1d87b 100644 --- a/Cronner/TimestampStorage/DummyStorage.php +++ b/Cronner/TimestampStorage/DummyStorage.php @@ -6,9 +6,6 @@ use Nette\Object; use stekycz\Cronner\ITimestampStorage; -/** - * @author Martin Štekl - */ class DummyStorage extends Object implements ITimestampStorage { diff --git a/Cronner/TimestampStorage/FileStorage.php b/Cronner/TimestampStorage/FileStorage.php index 97071eb..f85cfd9 100644 --- a/Cronner/TimestampStorage/FileStorage.php +++ b/Cronner/TimestampStorage/FileStorage.php @@ -11,9 +11,6 @@ use stekycz\Cronner\InvalidTaskNameException; use stekycz\Cronner\ITimestampStorage; -/** - * @author Martin Štekl - */ class FileStorage extends Object implements ITimestampStorage { diff --git a/Cronner/exceptions.php b/Cronner/exceptions.php index 027bff9..29f402a 100644 --- a/Cronner/exceptions.php +++ b/Cronner/exceptions.php @@ -2,49 +2,31 @@ namespace stekycz\Cronner; -/** - * @author Martin Štekl - */ class InvalidArgumentException extends \InvalidArgumentException { } -/** - * @author Martin Štekl - */ class InvalidParameterException extends InvalidArgumentException { } -/** - * @author Martin Štekl - */ class InvalidTaskNameException extends InvalidArgumentException { } -/** - * @author Martin Štekl - */ class EmptyTaskNameException extends InvalidArgumentException { } -/** - * @author Martin Štekl - */ class DuplicateTaskNameException extends InvalidArgumentException { } -/** - * @author Martin Štekl - */ class RuntimeException extends \RuntimeException { From 14ef5b6adfe574b08f7a3a6afaf092e8e074ce61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 22:59:38 +0100 Subject: [PATCH 08/20] Removed used FQN --- Cronner/Bar/Tasks.php | 8 +++++- Cronner/Cronner.php | 34 +++++++++++------------ Cronner/DI/CronnerExtension.php | 5 ++-- Cronner/ITimestampStorage.php | 4 +-- Cronner/Tasks/Parameters.php | 10 +++---- Cronner/Tasks/Parser.php | 10 +++---- Cronner/Tasks/Task.php | 21 +++++++------- Cronner/TimestampStorage/DummyStorage.php | 4 +-- Cronner/TimestampStorage/FileStorage.php | 7 +++-- 9 files changed, 56 insertions(+), 47 deletions(-) diff --git a/Cronner/Bar/Tasks.php b/Cronner/Bar/Tasks.php index b802761..04f85a5 100644 --- a/Cronner/Bar/Tasks.php +++ b/Cronner/Bar/Tasks.php @@ -10,7 +10,7 @@ class Tasks extends Object implements IBarPanel { /** - * @var \stekycz\Cronner\Cronner + * @var Cronner */ protected $cronner; @@ -19,6 +19,9 @@ public function __construct(Cronner $cronner) $this->cronner = $cronner; } + /** + * @return string + */ public function getPanel() { $tasks = array(); @@ -35,6 +38,9 @@ public function getPanel() return ob_get_clean(); } + /** + * @return string + */ public function getTab() { ob_start(); diff --git a/Cronner/Cronner.php b/Cronner/Cronner.php index 4945bf3..986b3a8 100644 --- a/Cronner/Cronner.php +++ b/Cronner/Cronner.php @@ -13,9 +13,9 @@ use Tracy\Debugger; /** - * @method onTaskBegin(\stekycz\Cronner\Cronner $cronner, \stekycz\Cronner\Tasks\Task $task) - * @method onTaskFinished(\stekycz\Cronner\Cronner $cronner, \stekycz\Cronner\Tasks\Task $task) - * @method onTaskError(\stekycz\Cronner\Cronner $cronner, \Exception $exception, \stekycz\Cronner\Tasks\Task $task) + * @method onTaskBegin(Cronner $cronner, Task $task) + * @method onTaskFinished(Cronner $cronner, Task $task) + * @method onTaskError(Cronner $cronner, Exception $exception, Task $task) */ class Cronner extends Object { @@ -36,7 +36,7 @@ class Cronner extends Object public $onTaskError = array(); /** - * @var \stekycz\Cronner\Tasks\Task[] + * @var Task[] */ private $tasks = array(); @@ -46,12 +46,12 @@ class Cronner extends Object private $registeredTaskObjects = array(); /** - * @var \stekycz\Cronner\ITimestampStorage + * @var ITimestampStorage */ private $timestampStorage; /** - * @var \stekycz\Cronner\CriticalSection + * @var CriticalSection */ private $criticalSection; @@ -66,8 +66,8 @@ class Cronner extends Object private $skipFailedTask = TRUE; /** - * @param \stekycz\Cronner\ITimestampStorage $timestampStorage - * @param \stekycz\Cronner\CriticalSection $criticalSection + * @param ITimestampStorage $timestampStorage + * @param CriticalSection $criticalSection * @param int|null $maxExecutionTime It is used only when Cronner runs * @param bool $skipFailedTask */ @@ -88,7 +88,7 @@ public function __construct( } /** - * @return \stekycz\Cronner\Tasks\Task[] + * @return Task[] */ public function getTasks() { @@ -96,8 +96,8 @@ public function getTasks() } /** - * @param \stekycz\Cronner\ITimestampStorage $timestampStorage - * @return \stekycz\Cronner\Cronner + * @param ITimestampStorage $timestampStorage + * @return Cronner */ public function setTimestampStorage(ITimestampStorage $timestampStorage) { @@ -110,8 +110,8 @@ public function setTimestampStorage(ITimestampStorage $timestampStorage) * Sets max execution time for Cronner. It is used only when Cronner runs. * * @param int|null $maxExecutionTime - * @return \stekycz\Cronner\Cronner - * @throws \stekycz\Cronner\InvalidArgumentException + * @return Cronner + * @throws InvalidArgumentException */ public function setMaxExecutionTime($maxExecutionTime = NULL) { @@ -129,7 +129,7 @@ public function setMaxExecutionTime($maxExecutionTime = NULL) * Sets flag that thrown exceptions will not be thrown but cached and logged. * * @param bool $skipFailedTask - * @return \stekycz\Cronner\Cronner + * @return Cronner */ public function setSkipFailedTask($skipFailedTask = TRUE) { @@ -154,8 +154,8 @@ public function getMaxExecutionTime() * an exception. * * @param object $tasks - * @return \stekycz\Cronner\Cronner - * @throws \stekycz\Cronner\InvalidArgumentException + * @return Cronner + * @throws InvalidArgumentException */ public function addTasks($tasks) { @@ -183,7 +183,7 @@ public function addTasks($tasks) /** * Runs all cron tasks. * - * @param \DateTime $now + * @param DateTime|null $now */ public function run(\DateTime $now = NULL) { diff --git a/Cronner/DI/CronnerExtension.php b/Cronner/DI/CronnerExtension.php index 0f80ed1..dcaba14 100644 --- a/Cronner/DI/CronnerExtension.php +++ b/Cronner/DI/CronnerExtension.php @@ -6,6 +6,7 @@ use Nette\DI\Compiler; use Nette\DI\CompilerExtension; use Nette\DI\ContainerBuilder; +use Nette\DI\ServiceDefinition; use Nette\DI\Statement; use Nette\PhpGenerator\ClassType; use Nette\Utils\Json; @@ -33,7 +34,7 @@ class CronnerExtension extends CompilerExtension /** * @param ContainerBuilder $containerBuilder * - * @return \Nette\DI\ServiceDefinition + * @return ServiceDefinition */ protected function createTimestampStorage(ContainerBuilder $containerBuilder) { @@ -129,7 +130,7 @@ public function afterCompile(ClassType $class) { } /** - * @param \Nette\Configurator $configurator + * @param Configurator $configurator */ public static function register(Configurator $configurator) { diff --git a/Cronner/ITimestampStorage.php b/Cronner/ITimestampStorage.php index 1e30f0a..bbe061c 100644 --- a/Cronner/ITimestampStorage.php +++ b/Cronner/ITimestampStorage.php @@ -17,14 +17,14 @@ public function setTaskName($taskName = NULL); /** * Saves current date and time as last invocation time. * - * @param \DateTime $now + * @param DateTime $now */ public function saveRunTime(DateTime $now); /** * Returns date and time of last cron task invocation. * - * @return \DateTime|null + * @return DateTime|null */ public function loadLastRunTime(); diff --git a/Cronner/Tasks/Parameters.php b/Cronner/Tasks/Parameters.php index 19908ad..1c7ba34 100644 --- a/Cronner/Tasks/Parameters.php +++ b/Cronner/Tasks/Parameters.php @@ -55,7 +55,7 @@ public function isTask() /** * Returns true if today is allowed day of week. * - * @param \DateTime $now + * @param DateTime $now * @return bool */ public function isInDay(DateTime $now) @@ -70,7 +70,7 @@ public function isInDay(DateTime $now) /** * Returns true if current time is in allowed range. * - * @param \DateTime $now + * @param DateTime $now * @return bool */ public function isInTime(DateTime $now) @@ -95,8 +95,8 @@ public function isInTime(DateTime $now) /** * Returns true if current time is next period of invocation. * - * @param \DateTime $now - * @param \DateTime|null $lastRunTime + * @param DateTime $now + * @param DateTime|null $lastRunTime * @return bool */ public function isNextPeriod(DateTime $now, DateTime $lastRunTime = NULL) @@ -114,7 +114,7 @@ public function isNextPeriod(DateTime $now, DateTime $lastRunTime = NULL) /** * Parse cronner values from annotations. * - * @param \Nette\Reflection\Method $method + * @param Method $method * @return array */ public static function parseParameters(Method $method) diff --git a/Cronner/Tasks/Parser.php b/Cronner/Tasks/Parser.php index fa3678e..eb26577 100644 --- a/Cronner/Tasks/Parser.php +++ b/Cronner/Tasks/Parser.php @@ -31,7 +31,7 @@ public static function parseName($annotation) * * @param string $annotation * @return string|null - * @throws \stekycz\Cronner\InvalidParameterException + * @throws InvalidParameterException */ public static function parsePeriod($annotation) { @@ -54,7 +54,7 @@ public static function parsePeriod($annotation) * * @param string $annotation * @return string[]|null - * @throws \stekycz\Cronner\InvalidParameterException + * @throws InvalidParameterException */ public static function parseDays($annotation) { @@ -85,7 +85,7 @@ public static function parseDays($annotation) * * @param string $annotation * @return string[][]|null - * @throws \stekycz\Cronner\InvalidParameterException + * @throws InvalidParameterException */ public static function parseTimes($annotation) { @@ -198,7 +198,7 @@ private static function isValidTime($time) * * @param string $time * @return string[][] - * @throws \stekycz\Cronner\InvalidParameterException + * @throws InvalidParameterException */ private static function parseOneTime($time) { @@ -271,7 +271,7 @@ private static function timePartsToArray($from, $to) * Checks if given annotation is valid. Throws exception if not. * * @param string $annotation - * @throws \stekycz\Cronner\InvalidParameterException + * @throws InvalidParameterException */ private static function checkAnnotation($annotation) { diff --git a/Cronner/Tasks/Task.php b/Cronner/Tasks/Task.php index 3f7bf9e..c9095aa 100644 --- a/Cronner/Tasks/Task.php +++ b/Cronner/Tasks/Task.php @@ -6,6 +6,7 @@ use Nette; use Nette\Object; use Nette\Reflection\Method; +use ReflectionClass; use stekycz\Cronner\ITimestampStorage; final class Task extends Object @@ -17,17 +18,17 @@ final class Task extends Object private $object; /** - * @var \Nette\Reflection\Method + * @var Method */ private $method; /** - * @var \stekycz\Cronner\ITimestampStorage + * @var ITimestampStorage */ private $timestampStorage; /** - * @var \stekycz\Cronner\Tasks\Parameters|null + * @var Parameters|null */ private $parameters = NULL; @@ -35,8 +36,8 @@ final class Task extends Object * Creates instance of one task. * * @param object $object - * @param \Nette\Reflection\Method $method - * @param \stekycz\Cronner\ITimestampStorage $timestampStorage + * @param Method $method + * @param ITimestampStorage $timestampStorage */ public function __construct($object, Method $method, ITimestampStorage $timestampStorage) { @@ -54,7 +55,7 @@ public function getObjectName() } /** - * @return \Nette\Reflection\Method + * @return Method */ public function getMethodReflection() { @@ -66,7 +67,7 @@ public function getMethodReflection() */ public function getObjectPath() { - $reflection = new \ReflectionClass($this->object); + $reflection = new ReflectionClass($this->object); return $reflection->getFileName(); } @@ -74,7 +75,7 @@ public function getObjectPath() /** * Returns True if given parameters should be run. * - * @param \DateTime $now + * @param DateTime $now * @return bool */ public function shouldBeRun(DateTime $now = NULL) @@ -104,7 +105,7 @@ public function getName() return $this->getParameters()->getName(); } - public function __invoke(\DateTime $now) + public function __invoke(DateTime $now) { $this->method->invoke($this->object); $this->timestampStorage->setTaskName($this->getName()); @@ -115,7 +116,7 @@ public function __invoke(\DateTime $now) /** * Returns instance of parsed parameters. * - * @return \stekycz\Cronner\Tasks\Parameters + * @return Parameters */ private function getParameters() { diff --git a/Cronner/TimestampStorage/DummyStorage.php b/Cronner/TimestampStorage/DummyStorage.php index 3b1d87b..4803e78 100644 --- a/Cronner/TimestampStorage/DummyStorage.php +++ b/Cronner/TimestampStorage/DummyStorage.php @@ -22,7 +22,7 @@ public function setTaskName($taskName = NULL) /** * Saves current date and time as last invocation time. * - * @param \DateTime $now + * @param DateTime $now */ public function saveRunTime(DateTime $now) { @@ -32,7 +32,7 @@ public function saveRunTime(DateTime $now) /** * Returns date and time of last cron task invocation. * - * @return \DateTime|null + * @return DateTime|null */ public function loadLastRunTime() { diff --git a/Cronner/TimestampStorage/FileStorage.php b/Cronner/TimestampStorage/FileStorage.php index f85cfd9..380e0f8 100644 --- a/Cronner/TimestampStorage/FileStorage.php +++ b/Cronner/TimestampStorage/FileStorage.php @@ -6,6 +6,7 @@ use Nette; use Nette\Object; use Nette\Utils\FileSystem; +use Nette\Utils\SafeStream; use Nette\Utils\Strings; use stekycz\Cronner\EmptyTaskNameException; use stekycz\Cronner\InvalidTaskNameException; @@ -31,7 +32,7 @@ class FileStorage extends Object implements ITimestampStorage */ public function __construct($directory) { - Nette\Utils\SafeStream::register(); + SafeStream::register(); $directory = rtrim($directory, DIRECTORY_SEPARATOR); FileSystem::createDir($directory); $this->directory = $directory; @@ -53,7 +54,7 @@ public function setTaskName($taskName = NULL) /** * Saves current date and time as last invocation time. * - * @param \DateTime $now + * @param DateTime $now */ public function saveRunTime(DateTime $now) { @@ -64,7 +65,7 @@ public function saveRunTime(DateTime $now) /** * Returns date and time of last cron task invocation. * - * @return \DateTime|null + * @return DateTime|null */ public function loadLastRunTime() { From 9a23681ce9a0e26d666533dc099378495a7d29e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 23:00:47 +0100 Subject: [PATCH 09/20] Used constant for specific safe stream protocol --- Cronner/TimestampStorage/FileStorage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cronner/TimestampStorage/FileStorage.php b/Cronner/TimestampStorage/FileStorage.php index 380e0f8..193c4b9 100644 --- a/Cronner/TimestampStorage/FileStorage.php +++ b/Cronner/TimestampStorage/FileStorage.php @@ -90,7 +90,7 @@ private function buildFilePath() throw new EmptyTaskNameException('Task name was not set.'); } - return 'safe://' . $this->directory . '/' . sha1($this->taskName); + return SafeStream::PROTOCOL . '://' . $this->directory . '/' . sha1($this->taskName); } } From f1d41ba955671f6231cd73a08213b50641382f61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 23:19:09 +0100 Subject: [PATCH 10/20] Introduced PSR-4 --- Cronner/exceptions.php | 33 ----------------- composer.json | 4 ++- {Cronner => src/Cronner}/Bar/Tasks.php | 0 .../Cronner}/Bar/templates/panel.phtml | 0 .../Cronner}/Bar/templates/tab.phtml | 0 {Cronner => src/Cronner}/CriticalSection.php | 0 {Cronner => src/Cronner}/Cronner.php | 3 ++ .../Cronner}/DI/CronnerExtension.php | 36 +++++++++---------- .../Exceptions/DuplicateTaskNameException.php | 8 +++++ .../Exceptions/EmptyTaskNameException.php | 8 +++++ .../Exceptions/InvalidArgumentException.php | 8 +++++ .../Exceptions/InvalidParameterException.php | 8 +++++ .../Exceptions/InvalidTaskNameException.php | 8 +++++ src/Cronner/Exceptions/RuntimeException.php | 8 +++++ .../Cronner}/ITimestampStorage.php | 0 {Cronner => src/Cronner}/Tasks/Parameters.php | 0 {Cronner => src/Cronner}/Tasks/Parser.php | 2 +- {Cronner => src/Cronner}/Tasks/Task.php | 0 .../TimestampStorage/DummyStorage.php | 0 .../Cronner}/TimestampStorage/FileStorage.php | 5 ++- tests/CronnerTests/Cronner.phpt | 10 +++--- tests/CronnerTests/Tasks/Parser.phpt | 8 ++--- .../TimestampStorage/FileStorage.phpt | 4 +-- 23 files changed, 83 insertions(+), 70 deletions(-) delete mode 100644 Cronner/exceptions.php rename {Cronner => src/Cronner}/Bar/Tasks.php (100%) rename {Cronner => src/Cronner}/Bar/templates/panel.phtml (100%) rename {Cronner => src/Cronner}/Bar/templates/tab.phtml (100%) rename {Cronner => src/Cronner}/CriticalSection.php (100%) rename {Cronner => src/Cronner}/Cronner.php (97%) rename {Cronner => src/Cronner}/DI/CronnerExtension.php (82%) create mode 100644 src/Cronner/Exceptions/DuplicateTaskNameException.php create mode 100644 src/Cronner/Exceptions/EmptyTaskNameException.php create mode 100644 src/Cronner/Exceptions/InvalidArgumentException.php create mode 100644 src/Cronner/Exceptions/InvalidParameterException.php create mode 100644 src/Cronner/Exceptions/InvalidTaskNameException.php create mode 100644 src/Cronner/Exceptions/RuntimeException.php rename {Cronner => src/Cronner}/ITimestampStorage.php (100%) rename {Cronner => src/Cronner}/Tasks/Parameters.php (100%) rename {Cronner => src/Cronner}/Tasks/Parser.php (99%) rename {Cronner => src/Cronner}/Tasks/Task.php (100%) rename {Cronner => src/Cronner}/TimestampStorage/DummyStorage.php (100%) rename {Cronner => src/Cronner}/TimestampStorage/FileStorage.php (94%) diff --git a/Cronner/exceptions.php b/Cronner/exceptions.php deleted file mode 100644 index 29f402a..0000000 --- a/Cronner/exceptions.php +++ /dev/null @@ -1,33 +0,0 @@ - NULL, 'criticalSectionTempDir' => "%tempDir%/critical-section", 'tasks' => array(), - 'bar' => '%debugMode%' + 'bar' => '%debugMode%', ); /** @@ -39,8 +39,8 @@ class CronnerExtension extends CompilerExtension protected function createTimestampStorage(ContainerBuilder $containerBuilder) { return $containerBuilder->addDefinition($this->prefix('timestampStorage')) - ->setAutowired(FALSE) - ->setInject(FALSE); + ->setAutowired(FALSE) + ->setInject(FALSE); } public function loadConfiguration() @@ -52,7 +52,6 @@ public function loadConfiguration() Validators::assert($config['maxExecutionTime'], 'integer|null', 'Script max execution time'); Validators::assert($config['criticalSectionTempDir'], 'string', 'Critical section files directory path'); - if ($config['timestampStorage'] === NULL) { $storageServiceName = $container->getByType('stekycz\Cronner\ITimestampStorage'); @@ -74,25 +73,25 @@ public function loadConfiguration() } $criticalSection = $container->addDefinition($this->prefix("criticalSection")) - ->setClass('stekycz\Cronner\CriticalSection', array( - $config['criticalSectionTempDir'] - )) - ->setAutowired(FALSE) - ->setInject(FALSE); + ->setClass('stekycz\Cronner\CriticalSection', array( + $config['criticalSectionTempDir'], + )) + ->setAutowired(FALSE) + ->setInject(FALSE); $runner = $container->addDefinition($this->prefix('runner')) - ->setClass('stekycz\Cronner\Cronner', array( - $storage, - $criticalSection, - $config['maxExecutionTime'], - array_key_exists('debugMode', $config) ? !$config['debugMode'] : TRUE, - )); + ->setClass('stekycz\Cronner\Cronner', array( + $storage, + $criticalSection, + $config['maxExecutionTime'], + array_key_exists('debugMode', $config) ? !$config['debugMode'] : TRUE, + )); Validators::assert($config['tasks'], 'array'); foreach ($config['tasks'] as $task) { $def = $container->addDefinition($this->prefix('task.' . md5(Json::encode($task)))); list($def->factory) = Compiler::filterArguments(array( - is_string($task) ? new Statement($task) : $task + is_string($task) ? new Statement($task) : $task, )); if (class_exists($def->factory->entity)) { @@ -106,7 +105,7 @@ public function loadConfiguration() if ($config['bar'] && class_exists('Tracy\Bar')) { $container->addDefinition($this->prefix('bar')) - ->setClass('stekycz\Cronner\Bar\Tasks', array($this->prefix('@runner'), $this->prefix('@timestampStorage'))); + ->setClass('stekycz\Cronner\Bar\Tasks', array($this->prefix('@runner'), $this->prefix('@timestampStorage'))); } } @@ -120,7 +119,8 @@ public function beforeCompile() } } - public function afterCompile(ClassType $class) { + public function afterCompile(ClassType $class) + { $builder = $this->getContainerBuilder(); $init = $class->getMethod('initialize'); diff --git a/src/Cronner/Exceptions/DuplicateTaskNameException.php b/src/Cronner/Exceptions/DuplicateTaskNameException.php new file mode 100644 index 0000000..0268069 --- /dev/null +++ b/src/Cronner/Exceptions/DuplicateTaskNameException.php @@ -0,0 +1,8 @@ +cronner; Assert::exception(function () use ($cronner) { $cronner->addTasks(new SameTaskNameObject()); - }, '\stekycz\Cronner\DuplicateTaskNameException'); + }, '\stekycz\Cronner\Exceptions\DuplicateTaskNameException'); } @@ -228,7 +226,7 @@ class CronnerTest extends \TestCase Assert::exception(function () use ($cronner) { $cronner->addTasks(new AnotherSimpleTestObject()); $cronner->addTasks(new NextSimpleTestObject()); - }, '\stekycz\Cronner\DuplicateTaskNameException'); + }, '\stekycz\Cronner\Exceptions\DuplicateTaskNameException'); } } diff --git a/tests/CronnerTests/Tasks/Parser.phpt b/tests/CronnerTests/Tasks/Parser.phpt index cfc0b7c..b99e5b8 100644 --- a/tests/CronnerTests/Tasks/Parser.phpt +++ b/tests/CronnerTests/Tasks/Parser.phpt @@ -10,8 +10,6 @@ use stdClass; use stekycz\Cronner\Tasks\Parser; use Tester\Assert; - - require_once(__DIR__ . "/../bootstrap.php"); /** @@ -77,7 +75,7 @@ class ParserTest extends \TestCase /** - * @throws \stekycz\Cronner\InvalidParameterException + * @throws \stekycz\Cronner\Exceptions\InvalidParameterException * @dataProvider dataProviderParsePeriodError * @param string $annotation */ @@ -154,7 +152,7 @@ class ParserTest extends \TestCase /** - * @throws \stekycz\Cronner\InvalidParameterException + * @throws \stekycz\Cronner\Exceptions\InvalidParameterException * @dataProvider dataProviderParseDaysError * @param string $annotation */ @@ -379,7 +377,7 @@ class ParserTest extends \TestCase /** - * @throws \stekycz\Cronner\InvalidParameterException + * @throws \stekycz\Cronner\Exceptions\InvalidParameterException * @dataProvider dataProviderParseTimesError * @param string $annotation */ diff --git a/tests/CronnerTests/TimestampStorage/FileStorage.phpt b/tests/CronnerTests/TimestampStorage/FileStorage.phpt index 24da7bc..65ee20b 100644 --- a/tests/CronnerTests/TimestampStorage/FileStorage.phpt +++ b/tests/CronnerTests/TimestampStorage/FileStorage.phpt @@ -13,8 +13,6 @@ use stdClass; use stekycz\Cronner\TimestampStorage\FileStorage; use Tester\Assert; - - require_once(__DIR__ . "/../bootstrap.php"); /** @@ -66,7 +64,7 @@ class FileStorageTest extends \TestCase /** * @dataProvider dataProviderSetTaskName - * @throws \stekycz\Cronner\InvalidTaskNameException + * @throws \stekycz\Cronner\Exceptions\InvalidTaskNameException */ public function testThrowsExceptionOnInvalidTaskName($taskName) { From 13d521d51dbe8924ba679e4760b90c8bbbecb09f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 23:28:05 +0100 Subject: [PATCH 11/20] Updated codestyle in tests --- tests/CronnerTests/CriticalSection.phpt | 23 +----- tests/CronnerTests/Cronner.phpt | 39 ++-------- tests/CronnerTests/DI/CronnerExtension.phpt | 31 +++----- tests/CronnerTests/Tasks/Parameters.phpt | 71 +++++++------------ .../CronnerTests/Tasks/ParametersParsing.phpt | 21 ++---- tests/CronnerTests/Tasks/Parser.phpt | 36 ++-------- tests/CronnerTests/Tasks/Task.phpt | 17 +---- .../TimestampStorage/FileStorage.phpt | 26 +------ tests/CronnerTests/bootstrap.php | 2 - .../objects/AnotherSimpleTestObject.php | 4 -- .../objects/NextSimpleTestObject.php | 4 -- .../objects/SameTaskNameObject.php | 7 -- .../objects/TestExceptionObject.php | 9 +-- tests/CronnerTests/objects/TestObject.php | 13 ---- 14 files changed, 54 insertions(+), 249 deletions(-) diff --git a/tests/CronnerTests/CriticalSection.phpt b/tests/CronnerTests/CriticalSection.phpt index c26e093..d575627 100644 --- a/tests/CronnerTests/CriticalSection.phpt +++ b/tests/CronnerTests/CriticalSection.phpt @@ -10,20 +10,15 @@ use Nette\Utils\FileSystem; use stekycz\Cronner\CriticalSection; use Tester\Assert; - - require_once(__DIR__ . "/bootstrap.php"); -/** - * @author Martin Štekl - */ class CriticalSectionTest extends \TestCase { const TEST_LABEL = "test"; /** - * @var \stekycz\Cronner\CriticalSection + * @var CriticalSection */ private $criticalSection; @@ -32,8 +27,6 @@ class CriticalSectionTest extends \TestCase */ private $filesDir; - - protected function setUp() { parent::setUp(); @@ -42,8 +35,6 @@ class CriticalSectionTest extends \TestCase $this->criticalSection = new CriticalSection($this->filesDir); } - - protected function tearDown() { parent::tearDown(); @@ -52,19 +43,15 @@ class CriticalSectionTest extends \TestCase } } - - public function testCanBeEnteredAndLeaved() { Assert::false($this->criticalSection->isEntered(self::TEST_LABEL)); - Assert::true($this-> criticalSection->enter(self::TEST_LABEL)); + Assert::true($this->criticalSection->enter(self::TEST_LABEL)); Assert::true($this->criticalSection->isEntered(self::TEST_LABEL)); Assert::true($this->criticalSection->leave(self::TEST_LABEL)); Assert::false($this->criticalSection->isEntered(self::TEST_LABEL)); } - - public function testCannotBeEnteredTwice() { Assert::true($this->criticalSection->enter(self::TEST_LABEL)); @@ -72,16 +59,12 @@ class CriticalSectionTest extends \TestCase Assert::false($this->criticalSection->enter(self::TEST_LABEL)); } - - public function testCannotBeLeavedWithoutEnter() { Assert::false($this->criticalSection->isEntered(self::TEST_LABEL)); Assert::false($this->criticalSection->leave(self::TEST_LABEL)); } - - public function testCannotBeLeavedTwice() { Assert::true($this->criticalSection->enter(self::TEST_LABEL)); @@ -90,8 +73,6 @@ class CriticalSectionTest extends \TestCase Assert::false($this->criticalSection->leave(self::TEST_LABEL)); } - - public function testMultipleCriticalSectionHandlers() { $criticalSection = $this->criticalSection; diff --git a/tests/CronnerTests/Cronner.phpt b/tests/CronnerTests/Cronner.phpt index ab1e76c..4921d3c 100644 --- a/tests/CronnerTests/Cronner.phpt +++ b/tests/CronnerTests/Cronner.phpt @@ -11,6 +11,8 @@ use Mockery; use Nette\Utils\DateTime; use stdClass; use stekycz\Cronner\Cronner; +use stekycz\Cronner\Exceptions\InvalidArgumentException; +use stekycz\Cronner\ITimestampStorage; use stekycz\Cronner\Tasks\Task; use stekycz\Cronner\tests\objects\AnotherSimpleTestObject; use stekycz\Cronner\tests\objects\NextSimpleTestObject; @@ -21,24 +23,19 @@ use Tester\Assert; require_once(__DIR__ . "/bootstrap.php"); -/** - * @author Martin Štekl - */ class CronnerTest extends \TestCase { /** - * @var \stekycz\Cronner\Cronner + * @var Cronner */ private $cronner; /** - * @var \stekycz\Cronner\ITimestampStorage + * @var ITimestampStorage */ private $timestampStorage; - - protected function setUp() { parent::setUp(); @@ -59,8 +56,6 @@ class CronnerTest extends \TestCase $this->cronner->onTaskError = array(); } - - /** * @dataProvider dataProviderSetMaxExecutionTime */ @@ -70,8 +65,6 @@ class CronnerTest extends \TestCase Assert::same($expected, $this->cronner->getMaxExecutionTime()); } - - public function dataProviderSetMaxExecutionTime() { return array( @@ -83,19 +76,15 @@ class CronnerTest extends \TestCase ); } - - /** * @dataProvider dataProviderSetMaxExecutionTimeError - * @throws \stekycz\Cronner\Exceptions\InvalidArgumentException + * @throws InvalidArgumentException */ public function testThrowsExceptionOnWrongTypeOfMaxExecutionTime($value) { $this->cronner->setMaxExecutionTime($value); } - - public function dataProviderSetMaxExecutionTimeError() { return array( @@ -114,16 +103,12 @@ class CronnerTest extends \TestCase ); } - - public function testAcceptsTasksObjectWithTaskMethods() { $this->cronner->addTasks(new \stdClass()); Assert::equal(1, $this->cronner->countTaskObjects()); } - - /** * @throws \stekycz\Cronner\Exceptions\InvalidArgumentException */ @@ -134,8 +119,6 @@ class CronnerTest extends \TestCase $this->cronner->addTasks($tasks); } - - public function testProcessesAllAddedTasks() { $now = new DateTime('2013-02-04 09:30:00'); @@ -149,8 +132,6 @@ class CronnerTest extends \TestCase $this->cronner->run($now); } - - public function testCanSetOnTaskBeginCallback() { $this->cronner->onTaskBegin[] = function (Cronner $cronner, Task $task) { @@ -159,8 +140,6 @@ class CronnerTest extends \TestCase Assert::equal(1, count($this->cronner->onTaskBegin)); } - - public function testCanSetOnTaskFinishedCallback() { $this->cronner->onTaskFinished[] = function (Cronner $cronner, Task $task) { @@ -169,8 +148,6 @@ class CronnerTest extends \TestCase Assert::equal(1, count($this->cronner->onTaskFinished)); } - - public function testCanSetOnTaskErrorCallback() { $this->cronner->onTaskError[] = function (Cronner $cronner, Exception $e, Task $task) { @@ -179,8 +156,6 @@ class CronnerTest extends \TestCase Assert::equal(1, count($this->cronner->onTaskError)); } - - public function testIsAbleToContinueWithNextTaskWhenOneTaskThrowException() { $now = new DateTime('2013-02-04 09:30:00'); @@ -208,8 +183,6 @@ class CronnerTest extends \TestCase $this->cronner->run($now); } - - public function testAddingTwoTestsWithTheSameNameInOneObject() { $cronner = $this->cronner; @@ -218,8 +191,6 @@ class CronnerTest extends \TestCase }, '\stekycz\Cronner\Exceptions\DuplicateTaskNameException'); } - - public function testAddingTwoTestsWithTheSameNameInMoreObjects() { $cronner = $this->cronner; diff --git a/tests/CronnerTests/DI/CronnerExtension.phpt b/tests/CronnerTests/DI/CronnerExtension.phpt index d537703..c83a10c 100644 --- a/tests/CronnerTests/DI/CronnerExtension.phpt +++ b/tests/CronnerTests/DI/CronnerExtension.phpt @@ -1,38 +1,31 @@ - */ class CronnerExtensionTest extends \TestCase { /** - * @var Nette\DI\Compiler + * @var Compiler */ private $compiler; - - protected function setUp() { parent::setUp(); - $builder = new Nette\DI\ContainerBuilder(); + $builder = new ContainerBuilder(); $builder->parameters = array( 'appDir' => __DIR__ . '/../..', 'wwwDir' => __DIR__ . '/../..', @@ -40,12 +33,10 @@ class CronnerExtensionTest extends \TestCase 'debugMode' => FALSE, 'productionMode' => TRUE, ); - $this->compiler = new Nette\DI\Compiler($builder); + $this->compiler = new Compiler($builder); $this->compiler->addExtension('cronner', new CronnerExtension()); } - - public function testDefaultConfiguration() { $compiler = $this->compiler; @@ -60,18 +51,16 @@ class CronnerExtensionTest extends \TestCase Assert::same('stekycz\Cronner\Cronner', $runner->getClass()); } - - public function testCompleteConfiguration() { $compiler = $this->compiler; $compiler->addConfig( array( 'cronner' => array( - 'timestampStorage' => new Nette\DI\Statement('stekycz\Cronner\TimestampStorage\DummyStorage'), + 'timestampStorage' => new Statement('stekycz\Cronner\TimestampStorage\DummyStorage'), 'maxExecutionTime' => 120, 'criticalSectionTempDir' => '%tempDir%/cronner', - ) + ), ) ); $compiler->compile(); @@ -87,6 +76,4 @@ class CronnerExtensionTest extends \TestCase } - - run(new CronnerExtensionTest()); diff --git a/tests/CronnerTests/Tasks/Parameters.phpt b/tests/CronnerTests/Tasks/Parameters.phpt index 126d175..2ebb38b 100644 --- a/tests/CronnerTests/Tasks/Parameters.phpt +++ b/tests/CronnerTests/Tasks/Parameters.phpt @@ -11,13 +11,8 @@ use Nette; use stekycz\Cronner\Tasks\Parameters; use Tester\Assert; - - require_once(__DIR__ . "/../bootstrap.php"); -/** - * @author Martin Štekl - */ class ParametersTest extends \TestCase { @@ -32,8 +27,6 @@ class ParametersTest extends \TestCase Assert::equal($expected, $params->getName()); } - - public function dataProviderGetName() { return array( @@ -49,8 +42,6 @@ class ParametersTest extends \TestCase ); } - - /** * @dataProvider dataProviderIsTask * @param bool $expected @@ -62,8 +53,6 @@ class ParametersTest extends \TestCase Assert::equal($expected, $params->isTask()); } - - public function dataProviderIsTask() { return array( @@ -79,13 +68,11 @@ class ParametersTest extends \TestCase ); } - - /** * @dataProvider dataProviderIsNextPeriod * @param bool $expected - * @param \DateTime $now - * @param \DateTime|null $lastRunTime + * @param DateTime $now + * @param DateTime|null $lastRunTime * @param array $parameters */ public function testDetectsIfNowIsInNextPeriod($expected, DateTime $now, DateTime $lastRunTime = NULL, array $parameters) @@ -94,8 +81,6 @@ class ParametersTest extends \TestCase Assert::same($expected, $params->isNextPeriod($now, $lastRunTime)); } - - public function dataProviderIsNextPeriod() { return array( @@ -103,72 +88,70 @@ class ParametersTest extends \TestCase TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:54:59'), - array(Parameters::PERIOD => '5 minutes',) + array(Parameters::PERIOD => '5 minutes',), ), array( TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:55:00'), - array(Parameters::PERIOD => '5 minutes',) + array(Parameters::PERIOD => '5 minutes',), ), array( TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:55:01'), - array(Parameters::PERIOD => '5 minutes',) + array(Parameters::PERIOD => '5 minutes',), ), array( TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:55:05'), - array(Parameters::PERIOD => '5 minutes',) + array(Parameters::PERIOD => '5 minutes',), ), array( FALSE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:55:06'), - array(Parameters::PERIOD => '5 minutes',) + array(Parameters::PERIOD => '5 minutes',), ), array( FALSE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:55:01'), - array(Parameters::PERIOD => '1 hour',) + array(Parameters::PERIOD => '1 hour',), ), array( TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:00:00'), - array(Parameters::PERIOD => '1 hour',) + array(Parameters::PERIOD => '1 hour',), ), array( TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:00:00'), - array() + array(), ), array( TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), NULL, - array(Parameters::PERIOD => '1 hour',) + array(Parameters::PERIOD => '1 hour',), ), array( TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), NULL, - array() + array(), ), ); } - - /** * @dataProvider dataProviderIsInDay * @param bool $expected * @param array $parameters - * @param \DateTime $now + * @param DateTime $now */ public function testDetectsAllowedDaysOfWeek($expected, array $parameters, DateTime $now) { @@ -176,8 +159,6 @@ class ParametersTest extends \TestCase Assert::equal($expected, $params->isInDay($now)); } - - public function dataProviderIsInDay() { return array( @@ -215,8 +196,6 @@ class ParametersTest extends \TestCase ); } - - /** * @dataProvider dataProviderIsInTime * @param bool $expected @@ -230,8 +209,6 @@ class ParametersTest extends \TestCase Assert::equal($expected, $params->isInTime($now)); } - - public function dataProviderIsInTime() { return array( @@ -245,7 +222,7 @@ class ParametersTest extends \TestCase ), ), ), - '2013-02-11 11:00:00' + '2013-02-11 11:00:00', ), array( TRUE, @@ -256,7 +233,7 @@ class ParametersTest extends \TestCase ), ), ), - '2013-02-11 11:00:59' + '2013-02-11 11:00:59', ), array( FALSE, @@ -267,7 +244,7 @@ class ParametersTest extends \TestCase ), ), ), - '2013-02-11 10:59:59' + '2013-02-11 10:59:59', ), array( FALSE, @@ -278,7 +255,7 @@ class ParametersTest extends \TestCase ), ), ), - '2013-02-11 11:01:00' + '2013-02-11 11:01:00', ), // Range array( @@ -290,7 +267,7 @@ class ParametersTest extends \TestCase ), ), ), - '2013-02-11 11:00:00' + '2013-02-11 11:00:00', ), array( TRUE, @@ -301,7 +278,7 @@ class ParametersTest extends \TestCase ), ), ), - '2013-02-11 11:30:00' + '2013-02-11 11:30:00', ), array( TRUE, @@ -309,7 +286,7 @@ class ParametersTest extends \TestCase array('from' => '11:00', 'to' => '12:00',), ), ), - '2013-02-11 12:00:59' + '2013-02-11 12:00:59', ), array( FALSE, @@ -317,7 +294,7 @@ class ParametersTest extends \TestCase array('from' => '11:00', 'to' => '12:00',), ), ), - '2013-02-11 10:59:59' + '2013-02-11 10:59:59', ), array( FALSE, @@ -325,18 +302,18 @@ class ParametersTest extends \TestCase array('from' => '11:00', 'to' => '12:00',), ), ), - '2013-02-11 12:01:00' + '2013-02-11 12:01:00', ), // Empty array( TRUE, array(Parameters::TIME => array(),), - '2013-02-11 12:00:00' + '2013-02-11 12:00:00', ), array( TRUE, array(Parameters::TIME => NULL,), - '2013-02-11 12:00:00' + '2013-02-11 12:00:00', ), ); } diff --git a/tests/CronnerTests/Tasks/ParametersParsing.phpt b/tests/CronnerTests/Tasks/ParametersParsing.phpt index 6c01bc9..2c80780 100644 --- a/tests/CronnerTests/Tasks/ParametersParsing.phpt +++ b/tests/CronnerTests/Tasks/ParametersParsing.phpt @@ -10,13 +10,8 @@ use stekycz\Cronner\Tasks\Parameters; use stekycz\Cronner\tests\objects\TestObject; use Tester\Assert; - - require_once(__DIR__ . "/../bootstrap.php"); -/** - * @author Martin Štekl - */ class ParametersParsingTest extends \TestCase { @@ -25,16 +20,12 @@ class ParametersParsingTest extends \TestCase */ private $object; - - protected function setUp() { parent::setUp(); $this->object = new TestObject(); } - - /** * @dataProvider dataProviderParse * @param array $expected @@ -51,8 +42,6 @@ class ParametersParsingTest extends \TestCase Assert::same($expected, Parameters::parseParameters($this->object->getReflection()->getMethod($methodName))); } - - public function dataProviderParse() { return array( @@ -61,7 +50,7 @@ class ParametersParsingTest extends \TestCase Parameters::PERIOD => '5 minutes', Parameters::DAYS => NULL, Parameters::TIME => NULL, - ), 'test01' + ), 'test01', ), array(array( Parameters::TASK => 'stekycz\Cronner\tests\objects\TestObject - test02', @@ -77,7 +66,7 @@ class ParametersParsingTest extends \TestCase 'to' => '16:00', ), ), - ), 'test02' + ), 'test02', ), array(array( Parameters::TASK => 'Test 3', @@ -89,14 +78,14 @@ class ParametersParsingTest extends \TestCase 'to' => '10:45', ), ), - ), 'test03' + ), 'test03', ), array(array( Parameters::TASK => 'Test 4', Parameters::PERIOD => '1 day', Parameters::DAYS => array('Sat', 'Sun',), - Parameters::TIME => NULL - ), 'test04' + Parameters::TIME => NULL, + ), 'test04', ), ); } diff --git a/tests/CronnerTests/Tasks/Parser.phpt b/tests/CronnerTests/Tasks/Parser.phpt index b99e5b8..a01a1ef 100644 --- a/tests/CronnerTests/Tasks/Parser.phpt +++ b/tests/CronnerTests/Tasks/Parser.phpt @@ -7,14 +7,12 @@ namespace stekycz\Cronner\tests\Tasks; use stdClass; +use stekycz\Cronner\Exceptions\InvalidParameterException; use stekycz\Cronner\Tasks\Parser; use Tester\Assert; require_once(__DIR__ . "/../bootstrap.php"); -/** - * @author Martin Štekl - */ class ParserTest extends \TestCase { @@ -28,8 +26,6 @@ class ParserTest extends \TestCase Assert::equal($expected, Parser::parseName($annotation)); } - - public function dataProviderParseName() { return array( @@ -47,8 +43,6 @@ class ParserTest extends \TestCase ); } - - /** * @dataProvider dataProviderParsePeriod * @param string $expected @@ -59,8 +53,6 @@ class ParserTest extends \TestCase Assert::equal($expected, Parser::parsePeriod($annotation)); } - - public function dataProviderParsePeriod() { return array( @@ -72,20 +64,16 @@ class ParserTest extends \TestCase ); } - - /** - * @throws \stekycz\Cronner\Exceptions\InvalidParameterException * @dataProvider dataProviderParsePeriodError * @param string $annotation + * @throws InvalidParameterException */ public function testThrowsExceptionOnWrongPeriodDefinition($annotation) { Parser::parsePeriod($annotation); } - - public function dataProviderParsePeriodError() { return array( @@ -102,8 +90,6 @@ class ParserTest extends \TestCase ); } - - /** * @dataProvider dataProviderParseDays * @param string $expected @@ -114,8 +100,6 @@ class ParserTest extends \TestCase Assert::equal($expected, Parser::parseDays($annotation)); } - - public function dataProviderParseDays() { return array( @@ -149,20 +133,16 @@ class ParserTest extends \TestCase ); } - - /** - * @throws \stekycz\Cronner\Exceptions\InvalidParameterException * @dataProvider dataProviderParseDaysError * @param string $annotation + * @throws InvalidParameterException */ public function testThrowsExceptionOnWrongDaysDefinition($annotation) { Parser::parseDays($annotation); } - - public function dataProviderParseDaysError() { return array( @@ -179,8 +159,6 @@ class ParserTest extends \TestCase ); } - - /** * @dataProvider dataProviderParseTimes * @param string $expected @@ -191,8 +169,6 @@ class ParserTest extends \TestCase Assert::equal($expected, Parser::parseTimes($annotation)); } - - public function dataProviderParseTimes() { return array( @@ -374,20 +350,16 @@ class ParserTest extends \TestCase ); } - - /** - * @throws \stekycz\Cronner\Exceptions\InvalidParameterException * @dataProvider dataProviderParseTimesError * @param string $annotation + * @throws InvalidParameterException */ public function testThrowsExceptionOnWrongTimesDefinition($annotation) { Parser::parseTimes($annotation); } - - public function dataProviderParseTimesError() { return array( diff --git a/tests/CronnerTests/Tasks/Task.phpt b/tests/CronnerTests/Tasks/Task.phpt index f1b809f..ae20393 100644 --- a/tests/CronnerTests/Tasks/Task.phpt +++ b/tests/CronnerTests/Tasks/Task.phpt @@ -7,19 +7,14 @@ namespace stekycz\Cronner\tests\Tasks; use Mockery; -use Nette\Reflection\Method; use Nette; +use Nette\Reflection\Method; use stekycz\Cronner\Tasks\Task; use stekycz\Cronner\tests\objects\TestObject; use Tester\Assert; - - require_once(__DIR__ . "/../bootstrap.php"); -/** - * @author Martin Štekl - */ class TaskTest extends \TestCase { @@ -28,16 +23,12 @@ class TaskTest extends \TestCase */ private $object; - - protected function setUp() { parent::setUp(); $this->object = new TestObject(); } - - public function testInvokesTaskWithSavingLastRunTime() { $now = new Nette\Utils\DateTime(); @@ -51,8 +42,6 @@ class TaskTest extends \TestCase Assert::$counter++; // Hack for nette tester } - - /** * @dataProvider dataProviderShouldBeRun * @param bool $expected @@ -76,8 +65,6 @@ class TaskTest extends \TestCase Assert::same($expected, $task->shouldBeRun($now)); } - - public function dataProviderShouldBeRun() { return array( @@ -94,8 +81,6 @@ class TaskTest extends \TestCase ); } - - public function testShouldBeRunOnShortLaterRun() { $timestampStorage = Mockery::mock('\stekycz\Cronner\ITimestampStorage'); diff --git a/tests/CronnerTests/TimestampStorage/FileStorage.phpt b/tests/CronnerTests/TimestampStorage/FileStorage.phpt index 65ee20b..711d242 100644 --- a/tests/CronnerTests/TimestampStorage/FileStorage.phpt +++ b/tests/CronnerTests/TimestampStorage/FileStorage.phpt @@ -10,14 +10,12 @@ use DateTime; use Nette; use Nette\Utils\FileSystem; use stdClass; +use stekycz\Cronner\Exceptions\InvalidTaskNameException; use stekycz\Cronner\TimestampStorage\FileStorage; use Tester\Assert; require_once(__DIR__ . "/../bootstrap.php"); -/** - * @author Martin Štekl - */ class FileStorageTest extends \TestCase { @@ -26,8 +24,6 @@ class FileStorageTest extends \TestCase */ private $storage; - - protected function setUp() { parent::setUp(); @@ -35,23 +31,17 @@ class FileStorageTest extends \TestCase $this->storage = new FileStorage(static::getTempDirPath()); } - - protected function tearDown() { parent::tearDown(); FileSystem::delete(static::getTempDirPath()); } - - private static function getTempDirPath() { return TEMP_DIR . '/cronner'; } - - public function testIsAbleToSetTaskName() { $this->storage->setTaskName('Test task 1'); @@ -60,19 +50,15 @@ class FileStorageTest extends \TestCase Assert::$counter++; // Hack for nette tester } - - /** * @dataProvider dataProviderSetTaskName - * @throws \stekycz\Cronner\Exceptions\InvalidTaskNameException + * @throws InvalidTaskNameException */ public function testThrowsExceptionOnInvalidTaskName($taskName) { $this->storage->setTaskName($taskName); } - - public function dataProviderSetTaskName() { return array( @@ -89,13 +75,11 @@ class FileStorageTest extends \TestCase ); } - - /** * Tests that saving do not throws any exception. * * @dataProvider dataProviderSaveRunTime - * @param \DateTime $date + * @param DateTime $date */ public function testLoadsAndSavesLastRunTimeWithoutErrors(DateTime $date) { @@ -112,8 +96,6 @@ class FileStorageTest extends \TestCase Assert::equal($date->format('Y-m-d H:i:s O'), $lastRunTime->format('Y-m-d H:i:s O')); } - - public function dataProviderSaveRunTime() { return array( @@ -123,8 +105,6 @@ class FileStorageTest extends \TestCase ); } - - public function testSavesLastRunTimeByTaskName() { $date = new DateTime('2013-01-30 17:30:00'); diff --git a/tests/CronnerTests/bootstrap.php b/tests/CronnerTests/bootstrap.php index a73b024..39cf84d 100644 --- a/tests/CronnerTests/bootstrap.php +++ b/tests/CronnerTests/bootstrap.php @@ -11,8 +11,6 @@ function run(Tester\TestCase $testCase) $testCase->run(isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : NULL); } - - class TestCase extends Tester\TestCase { diff --git a/tests/CronnerTests/objects/AnotherSimpleTestObject.php b/tests/CronnerTests/objects/AnotherSimpleTestObject.php index 890bbec..1dd4d13 100644 --- a/tests/CronnerTests/objects/AnotherSimpleTestObject.php +++ b/tests/CronnerTests/objects/AnotherSimpleTestObject.php @@ -4,10 +4,6 @@ use Nette\Object; - -/** - * @author Martin Štekl - */ class AnotherSimpleTestObject extends Object { diff --git a/tests/CronnerTests/objects/NextSimpleTestObject.php b/tests/CronnerTests/objects/NextSimpleTestObject.php index 302663b..9697140 100644 --- a/tests/CronnerTests/objects/NextSimpleTestObject.php +++ b/tests/CronnerTests/objects/NextSimpleTestObject.php @@ -4,10 +4,6 @@ use Nette\Object; - -/** - * @author Martin Štekl - */ class NextSimpleTestObject extends Object { diff --git a/tests/CronnerTests/objects/SameTaskNameObject.php b/tests/CronnerTests/objects/SameTaskNameObject.php index 8936792..1956137 100644 --- a/tests/CronnerTests/objects/SameTaskNameObject.php +++ b/tests/CronnerTests/objects/SameTaskNameObject.php @@ -4,11 +4,6 @@ use Nette\Object; - - -/** - * @author Martin Štekl - */ class SameTaskNameObject extends Object { @@ -19,8 +14,6 @@ public function test01() { } - - /** * @cronner-task Test */ diff --git a/tests/CronnerTests/objects/TestExceptionObject.php b/tests/CronnerTests/objects/TestExceptionObject.php index 5e3a609..3b1f9fa 100644 --- a/tests/CronnerTests/objects/TestExceptionObject.php +++ b/tests/CronnerTests/objects/TestExceptionObject.php @@ -2,14 +2,9 @@ namespace stekycz\Cronner\tests\objects; -use Nette\Object; use Exception; +use Nette\Object; - - -/** - * @author Martin Štekl - */ class TestExceptionObject extends Object { @@ -22,8 +17,6 @@ public function test01() throw new Exception('Test 01'); } - - /** * @cronner-task * @cronner-period 5 minutes diff --git a/tests/CronnerTests/objects/TestObject.php b/tests/CronnerTests/objects/TestObject.php index 1da86af..8730343 100644 --- a/tests/CronnerTests/objects/TestObject.php +++ b/tests/CronnerTests/objects/TestObject.php @@ -4,11 +4,6 @@ use Nette\Object; - - -/** - * @author Martin Štekl - */ class TestObject extends Object { @@ -20,8 +15,6 @@ public function __construct() { } - - /** * @cronner-task E-mail notifications * @cronner-period 5 minutes @@ -30,8 +23,6 @@ public function test01() { } - - /** * @cronner-task * @cronner-period 1 hour @@ -42,8 +33,6 @@ public function test02() { } - - /** * @cronner-task Test 3 * @cronner-period 17 minutes @@ -54,8 +43,6 @@ public function test03() { } - - /** * @cronner-task Test 4 * @cronner-period 1 day From c3d514d30b61fcd7aac59e1f42a079ac88ce5807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 23:31:09 +0100 Subject: [PATCH 12/20] Removed branch alias --- composer.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/composer.json b/composer.json index 07aef91..8c6407d 100644 --- a/composer.json +++ b/composer.json @@ -41,10 +41,5 @@ }, "autoload-dev": { "classmap": ["tests/CronnerTests/objects"] - }, - "extra": { - "branch-alias": { - "dev-master": "v1.1.x-dev" - } } } From e2cb671b399f65ee4e9fd0a152cd695ca39beff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 23:45:17 +0100 Subject: [PATCH 13/20] Fixed wrong type annotations --- src/Cronner/Cronner.php | 6 +++--- src/Cronner/Tasks/Parser.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Cronner/Cronner.php b/src/Cronner/Cronner.php index 04c41f6..3db01f2 100644 --- a/src/Cronner/Cronner.php +++ b/src/Cronner/Cronner.php @@ -24,17 +24,17 @@ class Cronner extends Object { /** - * @var callable + * @var callable[] */ public $onTaskBegin = array(); /** - * @var callable + * @var callable[] */ public $onTaskFinished = array(); /** - * @var callable + * @var callable[] */ public $onTaskError = array(); diff --git a/src/Cronner/Tasks/Parser.php b/src/Cronner/Tasks/Parser.php index 6ede44c..6aa6981 100644 --- a/src/Cronner/Tasks/Parser.php +++ b/src/Cronner/Tasks/Parser.php @@ -224,7 +224,7 @@ private static function parseOneTime($time) * Translates given annotation to day names. * * @param string $time - * @return string[] + * @return string */ private static function translateToTimes($time) { From d55088b9de8742e1bdb23d953a41cac49b277ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 23:45:29 +0100 Subject: [PATCH 14/20] Fixed not needed type cast --- src/Cronner/Cronner.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Cronner/Cronner.php b/src/Cronner/Cronner.php index 3db01f2..f3dad55 100644 --- a/src/Cronner/Cronner.php +++ b/src/Cronner/Cronner.php @@ -123,7 +123,7 @@ public function setMaxExecutionTime($maxExecutionTime = NULL) "Max execution time must be NULL or numeric value. Type '" . gettype($maxExecutionTime) . "' was given." ); } - $this->maxExecutionTime = $maxExecutionTime; + $this->maxExecutionTime = (int) $maxExecutionTime; return $this; } @@ -148,7 +148,7 @@ public function setSkipFailedTask($skipFailedTask = TRUE) */ public function getMaxExecutionTime() { - return !is_null($this->maxExecutionTime) ? (int) $this->maxExecutionTime : NULL; + return !is_null($this->maxExecutionTime) ? $this->maxExecutionTime : NULL; } /** @@ -194,7 +194,7 @@ public function run(\DateTime $now = NULL) $now = new DateTime(); } if ($this->maxExecutionTime !== NULL) { - set_time_limit((int) $this->maxExecutionTime); + set_time_limit($this->maxExecutionTime); } foreach ($this->tasks as $task) { From 251277ba6ac7d31da489e285cfddf5523d4479c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 23:45:43 +0100 Subject: [PATCH 15/20] Fixed usage of property instead of setter method --- src/Cronner/DI/CronnerExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cronner/DI/CronnerExtension.php b/src/Cronner/DI/CronnerExtension.php index 0db9c6b..3be5d6e 100644 --- a/src/Cronner/DI/CronnerExtension.php +++ b/src/Cronner/DI/CronnerExtension.php @@ -95,7 +95,7 @@ public function loadConfiguration() )); if (class_exists($def->factory->entity)) { - $def->class = $def->factory->entity; + $def->setClass($def->factory->entity); } $def->setAutowired(FALSE); From a024c4514aff9c7b1117e56a6295e41b2ee574ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 23:45:59 +0100 Subject: [PATCH 16/20] Fixed casting object to string --- src/Cronner/Tasks/Parameters.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Cronner/Tasks/Parameters.php b/src/Cronner/Tasks/Parameters.php index 1c7ba34..3e2d179 100644 --- a/src/Cronner/Tasks/Parameters.php +++ b/src/Cronner/Tasks/Parameters.php @@ -127,16 +127,16 @@ public static function parseParameters(Method $method) } $parameters = array( - static::TASK => Parser::parseName($method->getAnnotation(Parameters::TASK)) + static::TASK => Parser::parseName((string) $method->getAnnotation(Parameters::TASK)) ?: $taskName, static::PERIOD => $method->hasAnnotation(Parameters::PERIOD) - ? Parser::parsePeriod($method->getAnnotation(Parameters::PERIOD)) + ? Parser::parsePeriod((string) $method->getAnnotation(Parameters::PERIOD)) : NULL, static::DAYS => $method->hasAnnotation(Parameters::DAYS) - ? Parser::parseDays($method->getAnnotation(Parameters::DAYS)) + ? Parser::parseDays((string) $method->getAnnotation(Parameters::DAYS)) : NULL, static::TIME => $method->hasAnnotation(Parameters::TIME) - ? Parser::parseTimes($method->getAnnotation(Parameters::TIME)) + ? Parser::parseTimes((string) $method->getAnnotation(Parameters::TIME)) : NULL, ); From e06ed0341c16d611d3a15ff2851efa8afbbbed80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Tue, 31 Jan 2017 23:46:15 +0100 Subject: [PATCH 17/20] Introduced usage of PhpStan --- .travis.yml | 1 + composer.json | 3 ++- phpstan.neon | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 phpstan.neon diff --git a/.travis.yml b/.travis.yml index e9ad64a..530c5a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,6 +32,7 @@ install: before_script: - ./vendor/bin/parallel-lint -e php,phpt --exclude vendor . + - ./vendor/bin/phpstan analyze --no-progress -l 5 -c ./phpstan.neon ./ script: - ./vendor/bin/tester -s -p $PHP_BIN -j 32 -c ./tests/php.ini-unix ./tests/CronnerTests diff --git a/composer.json b/composer.json index 8c6407d..3d41aeb 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,8 @@ "require-dev": { "nette/tester": "^1.7", "jakub-onderka/php-parallel-lint": "^0.9", - "mockery/mockery": "^0.9" + "mockery/mockery": "^0.9", + "phpstan/phpstan": "^0.6" }, "suggest": { "kdyby/events": "Events for Nette Framework", diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..96e003e --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,11 @@ +parameters: + projectDir: %rootDir%/../../.. + autoload_files: + - %projectDir%/vendor/autoload.php + excludes_analyse: + - %projectDir%/tests/ + - %projectDir%/vendor/ + ignoreErrors: + - '#.*Cronner::onTaskBegin\(\).*#' + - '#.*Cronner::onTaskFinished\(\).*#' + - '#.*Cronner::onTaskError\(\).*#' From 0d2b9b168a900e8158d585367becafa432eb283c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Wed, 1 Feb 2017 14:40:31 +0100 Subject: [PATCH 18/20] Updated minimal version PHP to 7.0.0 and Nette to 2.4 --- .travis.yml | 5 - README.md | 2 +- composer.json | 2 +- src/Cronner/Bar/Tasks.php | 16 +- src/Cronner/CriticalSection.php | 28 +- src/Cronner/Cronner.php | 47 +- src/Cronner/DI/CronnerExtension.php | 51 +- .../Exceptions/DuplicateTaskNameException.php | 2 + .../Exceptions/EmptyTaskNameException.php | 2 + .../Exceptions/InvalidArgumentException.php | 2 + .../Exceptions/InvalidParameterException.php | 2 + .../Exceptions/InvalidTaskNameException.php | 2 + src/Cronner/Exceptions/RuntimeException.php | 2 + src/Cronner/ITimestampStorage.php | 4 +- src/Cronner/Tasks/Parameters.php | 48 +- src/Cronner/Tasks/Parser.php | 96 ++-- src/Cronner/Tasks/Task.php | 33 +- src/Cronner/TimestampStorage/DummyStorage.php | 4 +- src/Cronner/TimestampStorage/FileStorage.php | 12 +- tests/CronnerTests/CriticalSection.phpt | 2 + tests/CronnerTests/Cronner.phpt | 89 ++-- tests/CronnerTests/DI/CronnerExtension.phpt | 36 +- tests/CronnerTests/Tasks/Parameters.phpt | 361 +++++++------- .../CronnerTests/Tasks/ParametersParsing.phpt | 102 ++-- tests/CronnerTests/Tasks/Parser.phpt | 439 +++++++++--------- tests/CronnerTests/Tasks/Task.phpt | 35 +- .../TimestampStorage/FileStorage.phpt | 37 +- tests/CronnerTests/bootstrap.php | 2 + .../objects/AnotherSimpleTestObject.php | 2 + .../objects/NextSimpleTestObject.php | 2 + .../objects/SameTaskNameObject.php | 2 + .../objects/TestExceptionObject.php | 2 + tests/CronnerTests/objects/TestObject.php | 2 + 33 files changed, 725 insertions(+), 748 deletions(-) diff --git a/.travis.yml b/.travis.yml index 530c5a9..f4f2311 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,6 @@ language: php php: - - 5.3.3 - - 5.4 - - 5.5 - - 5.6 - 7.0 - 7.1 - hhvm @@ -26,7 +22,6 @@ before_install: - if [[ "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-add tests/php.ini-unix; fi install: - - if [[ "$TRAVIS_PHP_VERSION" == "5.3.3" ]]; then composer config -g disable-tls true; composer config -g secure-http false; fi - composer self-update - composer install --no-interaction --optimize-autoloader diff --git a/README.md b/README.md index 1023c83..a54be62 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Simple tool which helps with maintenance of cron tasks. -It requires **PHP >= 5.3.3** and **Nette Framework >= 2.0.0**. +It requires **PHP >= 7.0.0** and **Nette Framework >= 2.4.0**. ## Usage diff --git a/composer.json b/composer.json index 3d41aeb..dabd66f 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "source": "https://github.com/stekycz/Cronner" }, "require": { - "php": ">=5.3.3", + "php": ">=7.0.0", "tracy/tracy": "^2.4", "nette/di": "^2.4", "nette/bootstrap": "^2.4", diff --git a/src/Cronner/Bar/Tasks.php b/src/Cronner/Bar/Tasks.php index 04f85a5..8ffed89 100644 --- a/src/Cronner/Bar/Tasks.php +++ b/src/Cronner/Bar/Tasks.php @@ -1,5 +1,7 @@ cronner = $cronner; } - /** - * @return string - */ - public function getPanel() + public function getPanel() : string { - $tasks = array(); + $tasks = []; foreach ($this->cronner->getTasks() as $task) { if (!array_key_exists($task->getObjectName(), $tasks)) { - $tasks[$task->getObjectName()] = array(); + $tasks[$task->getObjectName()] = []; } $tasks[$task->getObjectName()][] = $task; @@ -38,10 +37,7 @@ public function getPanel() return ob_get_clean(); } - /** - * @return string - */ - public function getTab() + public function getTab() : string { ob_start(); $count = $this->cronner->countTasks(); diff --git a/src/Cronner/CriticalSection.php b/src/Cronner/CriticalSection.php index f8c0fe0..36933cb 100644 --- a/src/Cronner/CriticalSection.php +++ b/src/Cronner/CriticalSection.php @@ -1,5 +1,7 @@ isEntered($label)) { return FALSE; @@ -58,11 +54,8 @@ public function enter($label) /** * Leaves critical section. - * - * @param string $label - * @return bool */ - public function leave($label) + public function leave(string $label) : bool { if (!$this->isEntered($label)) { return FALSE; @@ -81,20 +74,13 @@ public function leave($label) /** * Returns TRUE if critical section is entered. - * - * @param string $label - * @return bool */ - public function isEntered($label) + public function isEntered(string $label) : bool { return array_key_exists($label, $this->locks) && $this->locks[$label] !== NULL; } - /** - * @param string $label - * @return string - */ - private function getFilePath($label) + private function getFilePath(string $label) : string { return $this->lockFilesDir . "/" . sha1($label); } diff --git a/src/Cronner/Cronner.php b/src/Cronner/Cronner.php index f3dad55..9a5d24b 100644 --- a/src/Cronner/Cronner.php +++ b/src/Cronner/Cronner.php @@ -1,5 +1,7 @@ setTimestampStorage($timestampStorage); @@ -93,16 +95,12 @@ public function __construct( /** * @return Task[] */ - public function getTasks() + public function getTasks() : array { return $this->tasks; } - /** - * @param ITimestampStorage $timestampStorage - * @return Cronner - */ - public function setTimestampStorage(ITimestampStorage $timestampStorage) + public function setTimestampStorage(ITimestampStorage $timestampStorage) : self { $this->timestampStorage = $timestampStorage; @@ -116,27 +114,22 @@ public function setTimestampStorage(ITimestampStorage $timestampStorage) * @return Cronner * @throws InvalidArgumentException */ - public function setMaxExecutionTime($maxExecutionTime = NULL) + public function setMaxExecutionTime(int $maxExecutionTime = NULL) : self { - if ($maxExecutionTime !== NULL && (!is_numeric($maxExecutionTime) || ((int) $maxExecutionTime) <= 0)) { - throw new InvalidArgumentException( - "Max execution time must be NULL or numeric value. Type '" . gettype($maxExecutionTime) . "' was given." - ); + if ($maxExecutionTime !== NULL && $maxExecutionTime <= 0) { + throw new InvalidArgumentException("Max execution time must be NULL or non negative number."); } - $this->maxExecutionTime = (int) $maxExecutionTime; + $this->maxExecutionTime = $maxExecutionTime; return $this; } /** * Sets flag that thrown exceptions will not be thrown but cached and logged. - * - * @param bool $skipFailedTask - * @return Cronner */ - public function setSkipFailedTask($skipFailedTask = TRUE) + public function setSkipFailedTask(bool $skipFailedTask = TRUE) : self { - $this->skipFailedTask = (bool) $skipFailedTask; + $this->skipFailedTask = $skipFailedTask; return $this; } @@ -160,7 +153,7 @@ public function getMaxExecutionTime() * @return Cronner * @throws InvalidArgumentException */ - public function addTasks($tasks) + public function addTasks($tasks) : self { $tasksId = $this->createIdFromObject($tasks); if (in_array($tasksId, $this->registeredTaskObjects)) { @@ -185,10 +178,8 @@ public function addTasks($tasks) /** * Runs all cron tasks. - * - * @param DateTime|null $now */ - public function run(\DateTime $now = NULL) + public function run(DateTime $now = NULL) { if ($now === NULL) { $now = new DateTime(); @@ -225,20 +216,16 @@ public function run(\DateTime $now = NULL) /** * Returns count of added task objects. - * - * @return int */ - public function countTaskObjects() + public function countTaskObjects() : int { return count($this->registeredTaskObjects); } /** * Returns count of added tasks. - * - * @return int */ - public function countTasks() + public function countTasks() : int { return count($this->tasks); } @@ -249,7 +236,7 @@ public function countTasks() * @param object $tasks * @return string */ - private function createIdFromObject($tasks) + private function createIdFromObject($tasks) : string { return sha1(get_class($tasks)); } diff --git a/src/Cronner/DI/CronnerExtension.php b/src/Cronner/DI/CronnerExtension.php index 3be5d6e..1636bf5 100644 --- a/src/Cronner/DI/CronnerExtension.php +++ b/src/Cronner/DI/CronnerExtension.php @@ -1,5 +1,7 @@ NULL, 'maxExecutionTime' => NULL, 'criticalSectionTempDir' => "%tempDir%/critical-section", - 'tasks' => array(), + 'tasks' => [], 'bar' => '%debugMode%', - ); + ]; /** * @param ContainerBuilder $containerBuilder - * * @return ServiceDefinition */ - protected function createTimestampStorage(ContainerBuilder $containerBuilder) + protected function createTimestampStorage(ContainerBuilder $containerBuilder) : ServiceDefinition { return $containerBuilder->addDefinition($this->prefix('timestampStorage')) ->setAutowired(FALSE) @@ -53,15 +59,15 @@ public function loadConfiguration() Validators::assert($config['criticalSectionTempDir'], 'string', 'Critical section files directory path'); if ($config['timestampStorage'] === NULL) { - $storageServiceName = $container->getByType('stekycz\Cronner\ITimestampStorage'); + $storageServiceName = $container->getByType(ITimestampStorage::class); $storage = $this->createTimestampStorage($container); if ($storageServiceName) { $storage->setFactory('@' . $storageServiceName); } else { - $storage->setClass(self::DEFAULT_STORAGE_CLASS, array( + $storage->setClass(self::DEFAULT_STORAGE_CLASS, [ $container->expand(self::DEFAULT_STORAGE_DIRECTORY), - )); + ]); } } else { $storage = $this->createTimestampStorage($container); @@ -73,26 +79,26 @@ public function loadConfiguration() } $criticalSection = $container->addDefinition($this->prefix("criticalSection")) - ->setClass('stekycz\Cronner\CriticalSection', array( + ->setClass(CriticalSection::class, [ $config['criticalSectionTempDir'], - )) + ]) ->setAutowired(FALSE) ->setInject(FALSE); $runner = $container->addDefinition($this->prefix('runner')) - ->setClass('stekycz\Cronner\Cronner', array( + ->setClass(Cronner::class, [ $storage, $criticalSection, $config['maxExecutionTime'], array_key_exists('debugMode', $config) ? !$config['debugMode'] : TRUE, - )); + ]); Validators::assert($config['tasks'], 'array'); foreach ($config['tasks'] as $task) { $def = $container->addDefinition($this->prefix('task.' . md5(Json::encode($task)))); - list($def->factory) = Compiler::filterArguments(array( + list($def->factory) = Compiler::filterArguments([ is_string($task) ? new Statement($task) : $task, - )); + ]); if (class_exists($def->factory->entity)) { $def->setClass($def->factory->entity); @@ -105,7 +111,10 @@ public function loadConfiguration() if ($config['bar'] && class_exists('Tracy\Bar')) { $container->addDefinition($this->prefix('bar')) - ->setClass('stekycz\Cronner\Bar\Tasks', array($this->prefix('@runner'), $this->prefix('@timestampStorage'))); + ->setClass(Tasks::class, [ + $this->prefix('@runner'), + $this->prefix('@timestampStorage'), + ]); } } @@ -115,7 +124,7 @@ public function beforeCompile() $runner = $builder->getDefinition($this->prefix('runner')); foreach (array_keys($builder->findByTag(self::TASKS_TAG)) as $serviceName) { - $runner->addSetup('addTasks', array('@' . $serviceName)); + $runner->addSetup('addTasks', ['@' . $serviceName]); } } @@ -125,13 +134,13 @@ public function afterCompile(ClassType $class) $init = $class->getMethod('initialize'); if ($builder->hasDefinition($this->prefix('bar'))) { - $init->addBody('$this->getByType(?)->addPanel($this->getService(?));', array('Tracy\Bar', $this->prefix('bar'))); + $init->addBody('$this->getByType(?)->addPanel($this->getService(?));', [ + 'Tracy\Bar', + $this->prefix('bar'), + ]); } } - /** - * @param Configurator $configurator - */ public static function register(Configurator $configurator) { $configurator->onCompile[] = function (Configurator $config, Compiler $compiler) { diff --git a/src/Cronner/Exceptions/DuplicateTaskNameException.php b/src/Cronner/Exceptions/DuplicateTaskNameException.php index 0268069..2731f8c 100644 --- a/src/Cronner/Exceptions/DuplicateTaskNameException.php +++ b/src/Cronner/Exceptions/DuplicateTaskNameException.php @@ -1,5 +1,7 @@ values = $values; } - /** - * Returns name of task. - * - * @return string - */ - public function getName() + public function getName() : string { return $this->values[static::TASK]; } - /** - * Returns true if task is really a task. - * - * @return bool - */ - public function isTask() + public function isTask() : bool { return Strings::length($this->values[static::TASK]) > 0; } /** * Returns true if today is allowed day of week. - * - * @param DateTime $now - * @return bool */ - public function isInDay(DateTime $now) + public function isInDay(DateTime $now) : bool { if (($days = $this->values[static::DAYS]) !== NULL) { return in_array($now->format('D'), $days); @@ -69,11 +58,8 @@ public function isInDay(DateTime $now) /** * Returns true if current time is in allowed range. - * - * @param DateTime $now - * @return bool */ - public function isInTime(DateTime $now) + public function isInTime(DateTime $now) : bool { if ($times = $this->values[static::TIME]) { foreach ($times as $time) { @@ -94,12 +80,8 @@ public function isInTime(DateTime $now) /** * Returns true if current time is next period of invocation. - * - * @param DateTime $now - * @param DateTime|null $lastRunTime - * @return bool */ - public function isNextPeriod(DateTime $now, DateTime $lastRunTime = NULL) + public function isNextPeriod(DateTime $now, DateTime $lastRunTime = NULL) : bool { if (isset($this->values[static::PERIOD]) && $this->values[static::PERIOD]) { // Prevent run task on next cronner run because of a few seconds shift @@ -113,11 +95,8 @@ public function isNextPeriod(DateTime $now, DateTime $lastRunTime = NULL) /** * Parse cronner values from annotations. - * - * @param Method $method - * @return array */ - public static function parseParameters(Method $method) + public static function parseParameters(Method $method) : array { $taskName = NULL; if ($method->hasAnnotation(Parameters::TASK)) { @@ -126,9 +105,12 @@ public static function parseParameters(Method $method) $taskName = $className . ' - ' . $methodName; } - $parameters = array( - static::TASK => Parser::parseName((string) $method->getAnnotation(Parameters::TASK)) - ?: $taskName, + $taskAnnotation = $method->getAnnotation(Parameters::TASK); + + $parameters = [ + static::TASK => is_string($taskAnnotation) + ? Parser::parseName($taskAnnotation) + : $taskName, static::PERIOD => $method->hasAnnotation(Parameters::PERIOD) ? Parser::parsePeriod((string) $method->getAnnotation(Parameters::PERIOD)) : NULL, @@ -138,7 +120,7 @@ public static function parseParameters(Method $method) static::TIME => $method->hasAnnotation(Parameters::TIME) ? Parser::parseTimes((string) $method->getAnnotation(Parameters::TIME)) : NULL, - ); + ]; return $parameters; } diff --git a/src/Cronner/Tasks/Parser.php b/src/Cronner/Tasks/Parser.php index 6aa6981..5d6ff79 100644 --- a/src/Cronner/Tasks/Parser.php +++ b/src/Cronner/Tasks/Parser.php @@ -1,5 +1,7 @@ 0 ? $name : NULL; return $name; } @@ -33,14 +32,15 @@ public static function parseName($annotation) * @return string|null * @throws InvalidParameterException */ - public static function parsePeriod($annotation) + public static function parsePeriod(string $annotation) { $period = NULL; - static::checkAnnotation($annotation); $annotation = Strings::trim($annotation); if (Strings::length($annotation)) { if (strtotime('+ ' . $annotation) === FALSE) { - throw new InvalidParameterException("Given period parameter '" . $annotation . "' must be valid for strtotime() with '+' sign as its prefix (added by Cronner automatically)."); + throw new InvalidParameterException( + "Given period parameter '" . $annotation . "' must be valid for strtotime() with '+' sign as its prefix (added by Cronner automatically)." + ); } $period = $annotation; } @@ -56,12 +56,11 @@ public static function parsePeriod($annotation) * @return string[]|null * @throws InvalidParameterException */ - public static function parseDays($annotation) + public static function parseDays(string $annotation) { - static $validValues = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun',); + static $validValues = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun',]; $days = NULL; - static::checkAnnotation($annotation); $annotation = Strings::trim($annotation); if (Strings::length($annotation)) { $days = static::translateToDayNames($annotation); @@ -87,14 +86,13 @@ public static function parseDays($annotation) * @return string[][]|null * @throws InvalidParameterException */ - public static function parseTimes($annotation) + public static function parseTimes(string $annotation) { $times = NULL; - static::checkAnnotation($annotation); $annotation = Strings::trim($annotation); if (Strings::length($annotation)) { if ($values = static::splitMultipleValues($annotation)) { - $times = array(); + $times = []; foreach ($values as $time) { $times = array_merge($times, static::parseOneTime($time)); } @@ -113,12 +111,12 @@ public static function parseTimes($annotation) * @param string $annotation * @return string[] */ - private static function translateToDayNames($annotation) + private static function translateToDayNames(string $annotation) : array { - static $workingDays = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri',); - static $weekend = array('Sat', 'Sun',); + static $workingDays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri',]; + static $weekend = ['Sat', 'Sun',]; - $days = array(); + $days = []; foreach (static::splitMultipleValues($annotation) as $value) { switch ($value) { case 'working days': @@ -143,10 +141,10 @@ private static function translateToDayNames($annotation) * @param string[] $days * @return string[] */ - private static function expandDaysRange(array $days) + private static function expandDaysRange(array $days) : array { - static $dayNames = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun',); - $expandedValues = array(); + static $dayNames = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun',]; + $expandedValues = []; foreach ($days as $day) { if (Strings::match($day, '~^\w{3}\s*-\s*\w{3}$~u')) { @@ -174,21 +172,18 @@ private static function expandDaysRange(array $days) /** * Splits given annotation by comma into array. * - * @param $annotation + * @param string $annotation * @return string[] */ - private static function splitMultipleValues($annotation) + private static function splitMultipleValues(string $annotation) : array { return Strings::split($annotation, '/\s*,\s*/'); } /** * Returns True if time in valid format is given, False otherwise. - * - * @param string $time - * @return bool */ - private static function isValidTime($time) + private static function isValidTime(string $time) : bool { return (bool) Strings::match($time, '/^\d{2}:\d{2}$/u'); } @@ -200,7 +195,7 @@ private static function isValidTime($time) * @return string[][] * @throws InvalidParameterException */ - private static function parseOneTime($time) + private static function parseOneTime(string $time) : array { $time = static::translateToTimes($time); $parts = Strings::split($time, '/\s*-\s*/'); @@ -209,7 +204,7 @@ private static function parseOneTime($time) "Times annotation is not in valid format. It must looks like 'hh:mm[ - hh:mm]' but '" . $time . "' was given." ); } - $times = array(); + $times = []; if (static::isTimeOverMidnight($parts[0], isset($parts[1]) ? $parts[1] : NULL)) { $times[] = static::timePartsToArray('00:00', $parts[1]); $times[] = static::timePartsToArray($parts[0], '23:59'); @@ -222,65 +217,38 @@ private static function parseOneTime($time) /** * Translates given annotation to day names. - * - * @param string $time - * @return string */ - private static function translateToTimes($time) + private static function translateToTimes(string $time) : string { - static $translationMap = array( + static $translationMap = [ 'morning' => '06:00 - 11:59', 'noon' => '12:00 - 12:29', 'afternoon' => '12:30 - 16:59', 'evening' => '17:00 - 21:59', 'night' => '22:00 - 05:59', 'midnight' => '00:00 - 00:29', - ); + ]; return array_key_exists($time, $translationMap) ? $translationMap[$time] : $time; } /** * Returns True if given times includes midnight, False otherwise. - * - * @param string $from - * @param string $to - * @return bool */ - private static function isTimeOverMidnight($from, $to) + private static function isTimeOverMidnight(string $from, string $to = NULL) : bool { return $to !== NULL && $to < $from; } /** * Returns array structure with given times. - * - * @param string $from - * @param string $to - * @return array */ - private static function timePartsToArray($from, $to) + private static function timePartsToArray(string $from, string $to = NULL) : array { - return array( + return [ 'from' => $from, 'to' => $to, - ); - } - - /** - * Checks if given annotation is valid. Throws exception if not. - * - * @param string $annotation - * @throws InvalidParameterException - */ - private static function checkAnnotation($annotation) - { - if (!is_string($annotation)) { - throw new InvalidParameterException( - "Cron task annotation must be string but '" . - !is_bool($annotation) && is_object($annotation) ? get_class($annotation) : gettype($annotation) . "' given." - ); - } + ]; } } diff --git a/src/Cronner/Tasks/Task.php b/src/Cronner/Tasks/Task.php index c9095aa..0dcad91 100644 --- a/src/Cronner/Tasks/Task.php +++ b/src/Cronner/Tasks/Task.php @@ -1,5 +1,7 @@ timestampStorage = $timestampStorage; } - /** - * @return string - */ - public function getObjectName() + public function getObjectName() : string { return get_class($this->object); } - /** - * @return Method - */ - public function getMethodReflection() + public function getMethodReflection() : Method { return $this->method; } - /** - * @return string - */ - public function getObjectPath() + public function getObjectPath() : string { $reflection = new ReflectionClass($this->object); @@ -74,11 +67,8 @@ public function getObjectPath() /** * Returns True if given parameters should be run. - * - * @param DateTime $now - * @return bool */ - public function shouldBeRun(DateTime $now = NULL) + public function shouldBeRun(DateTime $now = NULL) : bool { if ($now === NULL) { $now = new Nette\Utils\DateTime(); @@ -95,12 +85,7 @@ public function shouldBeRun(DateTime $now = NULL) && $parameters->isNextPeriod($now, $this->timestampStorage->loadLastRunTime()); } - /** - * Returns task name. - * - * @return string - */ - public function getName() + public function getName() : string { return $this->getParameters()->getName(); } @@ -115,10 +100,8 @@ public function __invoke(DateTime $now) /** * Returns instance of parsed parameters. - * - * @return Parameters */ - private function getParameters() + private function getParameters() : Parameters { if ($this->parameters === NULL) { $this->parameters = new Parameters(Parameters::parseParameters($this->method)); diff --git a/src/Cronner/TimestampStorage/DummyStorage.php b/src/Cronner/TimestampStorage/DummyStorage.php index 4803e78..40f72f9 100644 --- a/src/Cronner/TimestampStorage/DummyStorage.php +++ b/src/Cronner/TimestampStorage/DummyStorage.php @@ -1,5 +1,7 @@ taskName = $taskName; @@ -80,10 +82,8 @@ public function loadLastRunTime() /** * Builds file path from directory and task name. - * - * @return string */ - private function buildFilePath() + private function buildFilePath() : string { if ($this->taskName === NULL) { throw new EmptyTaskNameException('Task name was not set.'); diff --git a/tests/CronnerTests/CriticalSection.phpt b/tests/CronnerTests/CriticalSection.phpt index d575627..91e3081 100644 --- a/tests/CronnerTests/CriticalSection.phpt +++ b/tests/CronnerTests/CriticalSection.phpt @@ -1,5 +1,7 @@ shouldReceive('setTaskName'); $timestampStorage->shouldReceive('saveRunTime'); $timestampStorage->shouldReceive('loadLastRunTime')->andReturn(new DateTime('2013-02-04 08:00:00')); $this->timestampStorage = $timestampStorage; - $criticalSection = Mockery::mock('\stekycz\Cronner\CriticalSection'); + $criticalSection = Mockery::mock(CriticalSection::class); $criticalSection->shouldReceive("enter")->andReturn(TRUE); $criticalSection->shouldReceive("leave")->andReturn(TRUE); $criticalSection->shouldReceive("isEntered")->andReturn(FALSE); $this->cronner = new Cronner($this->timestampStorage, $criticalSection); - $this->cronner->onTaskBegin = array(); - $this->cronner->onTaskFinished = array(); - $this->cronner->onTaskError = array(); + $this->cronner->onTaskBegin = []; + $this->cronner->onTaskFinished = []; + $this->cronner->onTaskError = []; } /** * @dataProvider dataProviderSetMaxExecutionTime + * @param int|null $expected + * @param int|null $value */ - public function testCanSetMaxExecutionTime($expected, $value) + public function testCanSetMaxExecutionTime(int $expected = NULL, int $value = NULL) { $this->cronner->setMaxExecutionTime($value); Assert::same($expected, $this->cronner->getMaxExecutionTime()); } - public function dataProviderSetMaxExecutionTime() + public function dataProviderSetMaxExecutionTime() : array { - return array( - array(1234, 1234), - array(1234, '1234'), - array(1234, 1234.5), - array(1234, '1234.5'), - array(NULL, NULL), - ); + return [ + [1234, 1234], + [NULL, NULL], + ]; } /** * @dataProvider dataProviderSetMaxExecutionTimeError - * @throws InvalidArgumentException + * @throws \TypeError */ public function testThrowsExceptionOnWrongTypeOfMaxExecutionTime($value) { @@ -87,25 +91,40 @@ class CronnerTest extends \TestCase public function dataProviderSetMaxExecutionTimeError() { - return array( - array(-1234), - array('-1234'), - array(0), - array('0'), - array(-2.5), - array('-2.5'), - array(0.0), - array('0.0'), - array('nejaky blabol'), - array(TRUE), - array(FALSE), - array(new stdClass()), - ); + return [ + ['-1234'], + ['0'], + [-2.5], + ['-2.5'], + [0.0], + ['0.0'], + ['nejaky blabol'], + [TRUE], + [FALSE], + [new stdClass()], + ]; + } + + /** + * @dataProvider dataProviderSetMaxExecutionTimeWrongValue + * @throws \stekycz\Cronner\Exceptions\InvalidArgumentException + */ + public function testThrowsExceptionOnWrongValueOfMaxExecutionTime($value) + { + $this->cronner->setMaxExecutionTime($value); + } + + public function dataProviderSetMaxExecutionTimeWrongValue() + { + return [ + [-1234], + [0], + ]; } public function testAcceptsTasksObjectWithTaskMethods() { - $this->cronner->addTasks(new \stdClass()); + $this->cronner->addTasks(new stdClass()); Assert::equal(1, $this->cronner->countTaskObjects()); } @@ -114,7 +133,7 @@ class CronnerTest extends \TestCase */ public function testThrowsExceptionOnDuplicateTasksObjectAddition() { - $tasks = new \stdClass(); + $tasks = new stdClass(); $this->cronner->addTasks($tasks); $this->cronner->addTasks($tasks); } @@ -160,8 +179,8 @@ class CronnerTest extends \TestCase { $now = new DateTime('2013-02-04 09:30:00'); - $logCallback = Mockery::mock('\Nette\Object'); - $logCallback->shouldReceive('logError')->once()->andReturnUsing(function (Exception $e, Task $task) { + $logCallback = Mockery::mock(Object::class); + $logCallback->shouldReceive('logError')->once()->andReturnUsing( function (Exception $e, Task $task) { Assert::equal('Test 01', $e->getMessage()); }); $logCallback->shouldReceive('logBegin')->twice(); @@ -188,7 +207,7 @@ class CronnerTest extends \TestCase $cronner = $this->cronner; Assert::exception(function () use ($cronner) { $cronner->addTasks(new SameTaskNameObject()); - }, '\stekycz\Cronner\Exceptions\DuplicateTaskNameException'); + }, DuplicateTaskNameException::class); } public function testAddingTwoTestsWithTheSameNameInMoreObjects() @@ -197,7 +216,7 @@ class CronnerTest extends \TestCase Assert::exception(function () use ($cronner) { $cronner->addTasks(new AnotherSimpleTestObject()); $cronner->addTasks(new NextSimpleTestObject()); - }, '\stekycz\Cronner\Exceptions\DuplicateTaskNameException'); + }, DuplicateTaskNameException::class); } } diff --git a/tests/CronnerTests/DI/CronnerExtension.phpt b/tests/CronnerTests/DI/CronnerExtension.phpt index c83a10c..02b143c 100644 --- a/tests/CronnerTests/DI/CronnerExtension.phpt +++ b/tests/CronnerTests/DI/CronnerExtension.phpt @@ -9,7 +9,11 @@ namespace stekycz\Cronner\tests\DI; use Nette\DI\Compiler; use Nette\DI\ContainerBuilder; use Nette\DI\Statement; +use stekycz\Cronner\CriticalSection; +use stekycz\Cronner\Cronner; use stekycz\Cronner\DI\CronnerExtension; +use stekycz\Cronner\TimestampStorage\DummyStorage; +use stekycz\Cronner\TimestampStorage\FileStorage; use Tester\Assert; require_once(__DIR__ . "/../bootstrap.php"); @@ -26,13 +30,13 @@ class CronnerExtensionTest extends \TestCase { parent::setUp(); $builder = new ContainerBuilder(); - $builder->parameters = array( + $builder->parameters = [ 'appDir' => __DIR__ . '/../..', 'wwwDir' => __DIR__ . '/../..', 'tempDir' => TEMP_DIR, 'debugMode' => FALSE, 'productionMode' => TRUE, - ); + ]; $this->compiler = new Compiler($builder); $this->compiler->addExtension('cronner', new CronnerExtension()); } @@ -46,32 +50,30 @@ class CronnerExtensionTest extends \TestCase $criticalSection = $compiler->getContainerBuilder()->getDefinition('cronner.criticalSection'); $runner = $compiler->getContainerBuilder()->getDefinition('cronner.runner'); - Assert::same('stekycz\Cronner\TimestampStorage\FileStorage', $timestampStorage->getClass()); - Assert::same('stekycz\Cronner\CriticalSection', $criticalSection->getClass()); - Assert::same('stekycz\Cronner\Cronner', $runner->getClass()); + Assert::same(FileStorage::class, $timestampStorage->getClass()); + Assert::same(CriticalSection::class, $criticalSection->getClass()); + Assert::same(Cronner::class, $runner->getClass()); } public function testCompleteConfiguration() { $compiler = $this->compiler; - $compiler->addConfig( - array( - 'cronner' => array( - 'timestampStorage' => new Statement('stekycz\Cronner\TimestampStorage\DummyStorage'), - 'maxExecutionTime' => 120, - 'criticalSectionTempDir' => '%tempDir%/cronner', - ), - ) - ); + $compiler->addConfig([ + 'cronner' => [ + 'timestampStorage' => new Statement('stekycz\Cronner\TimestampStorage\DummyStorage'), + 'maxExecutionTime' => 120, + 'criticalSectionTempDir' => '%tempDir%/cronner', + ], + ]); $compiler->compile(); $timestampStorage = $compiler->getContainerBuilder()->getDefinition('cronner.timestampStorage'); $criticalSection = $compiler->getContainerBuilder()->getDefinition('cronner.criticalSection'); $runner = $compiler->getContainerBuilder()->getDefinition('cronner.runner'); - Assert::same('stekycz\Cronner\TimestampStorage\DummyStorage', $timestampStorage->getClass()); - Assert::same('stekycz\Cronner\CriticalSection', $criticalSection->getClass()); - Assert::same('stekycz\Cronner\Cronner', $runner->getClass()); + Assert::same(DummyStorage::class, $timestampStorage->getClass()); + Assert::same(CriticalSection::class, $criticalSection->getClass()); + Assert::same(Cronner::class, $runner->getClass()); } } diff --git a/tests/CronnerTests/Tasks/Parameters.phpt b/tests/CronnerTests/Tasks/Parameters.phpt index 2ebb38b..67fb2d8 100644 --- a/tests/CronnerTests/Tasks/Parameters.phpt +++ b/tests/CronnerTests/Tasks/Parameters.phpt @@ -1,5 +1,7 @@ getName()); } - public function dataProviderGetName() + public function dataProviderGetName() : array { - return array( - array('Name of task', array(Parameters::TASK => 'Name of task',)), - array('0', array(Parameters::TASK => '0',)), - array('', array(Parameters::TASK => ' ',)), - array('', array(Parameters::TASK => '',)), - array('', array(Parameters::TASK => TRUE,)), - array('', array(Parameters::TASK => FALSE,)), - array('', array(Parameters::TASK => NULL,)), - array('', array(Parameters::TASK => 0,)), - array('', array()), - ); + return [ + ['Name of task', [Parameters::TASK => 'Name of task',]], + ['0', [Parameters::TASK => '0',]], + ['', [Parameters::TASK => ' ',]], + ['', [Parameters::TASK => '',]], + ['', [Parameters::TASK => TRUE,]], + ['', [Parameters::TASK => FALSE,]], + ['', [Parameters::TASK => NULL,]], + ['', [Parameters::TASK => 0,]], + ['', []], + ]; } /** @@ -47,25 +49,25 @@ class ParametersTest extends \TestCase * @param bool $expected * @param array $parameters */ - public function testDetectsTask($expected, array $parameters) + public function testDetectsTask(bool $expected, array $parameters) { $params = new Parameters($parameters); Assert::equal($expected, $params->isTask()); } - public function dataProviderIsTask() + public function dataProviderIsTask() : array { - return array( - array(TRUE, array(Parameters::TASK => 'Name of task',)), - array(TRUE, array(Parameters::TASK => '0',)), - array(FALSE, array(Parameters::TASK => ' ',)), - array(FALSE, array(Parameters::TASK => '',)), - array(FALSE, array(Parameters::TASK => TRUE,)), - array(FALSE, array(Parameters::TASK => FALSE,)), - array(FALSE, array(Parameters::TASK => NULL,)), - array(FALSE, array(Parameters::TASK => 0,)), - array(FALSE, array()), - ); + return [ + [TRUE, [Parameters::TASK => 'Name of task',]], + [TRUE, [Parameters::TASK => '0',]], + [FALSE, [Parameters::TASK => ' ',]], + [FALSE, [Parameters::TASK => '',]], + [FALSE, [Parameters::TASK => TRUE,]], + [FALSE, [Parameters::TASK => FALSE,]], + [FALSE, [Parameters::TASK => NULL,]], + [FALSE, [Parameters::TASK => 0,]], + [FALSE, []], + ]; } /** @@ -75,76 +77,76 @@ class ParametersTest extends \TestCase * @param DateTime|null $lastRunTime * @param array $parameters */ - public function testDetectsIfNowIsInNextPeriod($expected, DateTime $now, DateTime $lastRunTime = NULL, array $parameters) + public function testDetectsIfNowIsInNextPeriod(bool $expected, DateTime $now, DateTime $lastRunTime = NULL, array $parameters) { $params = new Parameters($parameters); Assert::same($expected, $params->isNextPeriod($now, $lastRunTime)); } - public function dataProviderIsNextPeriod() + public function dataProviderIsNextPeriod() : array { - return array( - array( + return [ + [ TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:54:59'), - array(Parameters::PERIOD => '5 minutes',), - ), - array( + [Parameters::PERIOD => '5 minutes',], + ], + [ TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:55:00'), - array(Parameters::PERIOD => '5 minutes',), - ), - array( + [Parameters::PERIOD => '5 minutes',], + ], + [ TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:55:01'), - array(Parameters::PERIOD => '5 minutes',), - ), - array( + [Parameters::PERIOD => '5 minutes',], + ], + [ TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:55:05'), - array(Parameters::PERIOD => '5 minutes',), - ), - array( + [Parameters::PERIOD => '5 minutes',], + ], + [ FALSE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:55:06'), - array(Parameters::PERIOD => '5 minutes',), - ), - array( + [Parameters::PERIOD => '5 minutes',], + ], + [ FALSE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:55:01'), - array(Parameters::PERIOD => '1 hour',), - ), - array( + [Parameters::PERIOD => '1 hour',], + ], + [ TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:00:00'), - array(Parameters::PERIOD => '1 hour',), - ), - array( + [Parameters::PERIOD => '1 hour',], + ], + [ TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), new Nette\Utils\DateTime('2013-02-03 16:00:00'), - array(), - ), - array( + [], + ], + [ TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), NULL, - array(Parameters::PERIOD => '1 hour',), - ), - array( + [Parameters::PERIOD => '1 hour',], + ], + [ TRUE, new Nette\Utils\DateTime('2013-02-03 17:00:00'), NULL, - array(), - ), - ); + [], + ], + ]; } /** @@ -153,47 +155,47 @@ class ParametersTest extends \TestCase * @param array $parameters * @param DateTime $now */ - public function testDetectsAllowedDaysOfWeek($expected, array $parameters, DateTime $now) + public function testDetectsAllowedDaysOfWeek(bool $expected, array $parameters, DateTime $now) { $params = new Parameters($parameters); Assert::equal($expected, $params->isInDay($now)); } - public function dataProviderIsInDay() + public function dataProviderIsInDay() : array { - return array( + return [ // One day - array(TRUE, array(Parameters::DAYS => array('Mon',),), new Nette\Utils\DateTime('2013-02-11 12:34:56')), - array(FALSE, array(Parameters::DAYS => array('Mon',),), new Nette\Utils\DateTime('2013-02-12 12:34:56')), - array(TRUE, array(Parameters::DAYS => array('Tue',),), new Nette\Utils\DateTime('2013-02-12 12:34:56')), - array(FALSE, array(Parameters::DAYS => array('Tue',),), new Nette\Utils\DateTime('2013-02-13 12:34:56')), - array(TRUE, array(Parameters::DAYS => array('Wed',),), new Nette\Utils\DateTime('2013-02-13 12:34:56')), - array(FALSE, array(Parameters::DAYS => array('Wed',),), new Nette\Utils\DateTime('2013-02-14 12:34:56')), - array(TRUE, array(Parameters::DAYS => array('Thu',),), new Nette\Utils\DateTime('2013-02-14 12:34:56')), - array(FALSE, array(Parameters::DAYS => array('Thu',),), new Nette\Utils\DateTime('2013-02-15 12:34:56')), - array(TRUE, array(Parameters::DAYS => array('Fri',),), new Nette\Utils\DateTime('2013-02-15 12:34:56')), - array(FALSE, array(Parameters::DAYS => array('Fri',),), new Nette\Utils\DateTime('2013-02-16 12:34:56')), - array(TRUE, array(Parameters::DAYS => array('Sat',),), new Nette\Utils\DateTime('2013-02-16 12:34:56')), - array(FALSE, array(Parameters::DAYS => array('Sat',),), new Nette\Utils\DateTime('2013-02-17 12:34:56')), - array(TRUE, array(Parameters::DAYS => array('Sun',),), new Nette\Utils\DateTime('2013-02-17 12:34:56')), - array(FALSE, array(Parameters::DAYS => array('Sun',),), new Nette\Utils\DateTime('2013-02-18 12:34:56')), + [TRUE, [Parameters::DAYS => ['Mon',],], new Nette\Utils\DateTime('2013-02-11 12:34:56')], + [FALSE, [Parameters::DAYS => ['Mon',],], new Nette\Utils\DateTime('2013-02-12 12:34:56')], + [TRUE, [Parameters::DAYS => ['Tue',],], new Nette\Utils\DateTime('2013-02-12 12:34:56')], + [FALSE, [Parameters::DAYS => ['Tue',],], new Nette\Utils\DateTime('2013-02-13 12:34:56')], + [TRUE, [Parameters::DAYS => ['Wed',],], new Nette\Utils\DateTime('2013-02-13 12:34:56')], + [FALSE, [Parameters::DAYS => ['Wed',],], new Nette\Utils\DateTime('2013-02-14 12:34:56')], + [TRUE, [Parameters::DAYS => ['Thu',],], new Nette\Utils\DateTime('2013-02-14 12:34:56')], + [FALSE, [Parameters::DAYS => ['Thu',],], new Nette\Utils\DateTime('2013-02-15 12:34:56')], + [TRUE, [Parameters::DAYS => ['Fri',],], new Nette\Utils\DateTime('2013-02-15 12:34:56')], + [FALSE, [Parameters::DAYS => ['Fri',],], new Nette\Utils\DateTime('2013-02-16 12:34:56')], + [TRUE, [Parameters::DAYS => ['Sat',],], new Nette\Utils\DateTime('2013-02-16 12:34:56')], + [FALSE, [Parameters::DAYS => ['Sat',],], new Nette\Utils\DateTime('2013-02-17 12:34:56')], + [TRUE, [Parameters::DAYS => ['Sun',],], new Nette\Utils\DateTime('2013-02-17 12:34:56')], + [FALSE, [Parameters::DAYS => ['Sun',],], new Nette\Utils\DateTime('2013-02-18 12:34:56')], // Empty days - array(FALSE, array(Parameters::DAYS => array(),), new Nette\Utils\DateTime('2013-02-11 12:34:56')), - array(FALSE, array(Parameters::DAYS => array(),), new Nette\Utils\DateTime('2013-02-12 12:34:56')), - array(FALSE, array(Parameters::DAYS => array(),), new Nette\Utils\DateTime('2013-02-13 12:34:56')), - array(FALSE, array(Parameters::DAYS => array(),), new Nette\Utils\DateTime('2013-02-14 12:34:56')), - array(FALSE, array(Parameters::DAYS => array(),), new Nette\Utils\DateTime('2013-02-15 12:34:56')), - array(FALSE, array(Parameters::DAYS => array(),), new Nette\Utils\DateTime('2013-02-16 12:34:56')), - array(FALSE, array(Parameters::DAYS => array(),), new Nette\Utils\DateTime('2013-02-17 12:34:56')), + [FALSE, [Parameters::DAYS => [],], new Nette\Utils\DateTime('2013-02-11 12:34:56')], + [FALSE, [Parameters::DAYS => [],], new Nette\Utils\DateTime('2013-02-12 12:34:56')], + [FALSE, [Parameters::DAYS => [],], new Nette\Utils\DateTime('2013-02-13 12:34:56')], + [FALSE, [Parameters::DAYS => [],], new Nette\Utils\DateTime('2013-02-14 12:34:56')], + [FALSE, [Parameters::DAYS => [],], new Nette\Utils\DateTime('2013-02-15 12:34:56')], + [FALSE, [Parameters::DAYS => [],], new Nette\Utils\DateTime('2013-02-16 12:34:56')], + [FALSE, [Parameters::DAYS => [],], new Nette\Utils\DateTime('2013-02-17 12:34:56')], // Without days - array(TRUE, array(Parameters::DAYS => NULL,), new Nette\Utils\DateTime('2013-02-11 12:34:56')), - array(TRUE, array(Parameters::DAYS => NULL,), new Nette\Utils\DateTime('2013-02-12 12:34:56')), - array(TRUE, array(Parameters::DAYS => NULL,), new Nette\Utils\DateTime('2013-02-13 12:34:56')), - array(TRUE, array(Parameters::DAYS => NULL,), new Nette\Utils\DateTime('2013-02-14 12:34:56')), - array(TRUE, array(Parameters::DAYS => NULL,), new Nette\Utils\DateTime('2013-02-15 12:34:56')), - array(TRUE, array(Parameters::DAYS => NULL,), new Nette\Utils\DateTime('2013-02-16 12:34:56')), - array(TRUE, array(Parameters::DAYS => NULL,), new Nette\Utils\DateTime('2013-02-17 12:34:56')), - ); + [TRUE, [Parameters::DAYS => NULL,], new Nette\Utils\DateTime('2013-02-11 12:34:56')], + [TRUE, [Parameters::DAYS => NULL,], new Nette\Utils\DateTime('2013-02-12 12:34:56')], + [TRUE, [Parameters::DAYS => NULL,], new Nette\Utils\DateTime('2013-02-13 12:34:56')], + [TRUE, [Parameters::DAYS => NULL,], new Nette\Utils\DateTime('2013-02-14 12:34:56')], + [TRUE, [Parameters::DAYS => NULL,], new Nette\Utils\DateTime('2013-02-15 12:34:56')], + [TRUE, [Parameters::DAYS => NULL,], new Nette\Utils\DateTime('2013-02-16 12:34:56')], + [TRUE, [Parameters::DAYS => NULL,], new Nette\Utils\DateTime('2013-02-17 12:34:56')], + ]; } /** @@ -202,120 +204,129 @@ class ParametersTest extends \TestCase * @param array $parameters * @param string $now */ - public function testDetectsAllowedTimeRange($expected, array $parameters, $now) + public function testDetectsAllowedTimeRange(bool $expected, array $parameters, string $now) { $now = new Nette\Utils\DateTime($now); $params = new Parameters($parameters); Assert::equal($expected, $params->isInTime($now)); } - public function dataProviderIsInTime() + public function dataProviderIsInTime() : array { - return array( + return [ // One minute - array( + [ TRUE, - array(Parameters::TIME => array( - array( - 'from' => '11:00', - 'to' => NULL, - ), - ), - ), + [ + Parameters::TIME => [ + [ + 'from' => '11:00', + 'to' => NULL, + ], + ], + ], '2013-02-11 11:00:00', - ), - array( + ], + [ TRUE, - array(Parameters::TIME => array( - array( - 'from' => '11:00', - 'to' => NULL, - ), - ), - ), + [ + Parameters::TIME => [ + [ + 'from' => '11:00', + 'to' => NULL, + ], + ], + ], '2013-02-11 11:00:59', - ), - array( + ], + [ FALSE, - array(Parameters::TIME => array( - array( - 'from' => '11:00', - 'to' => NULL, - ), - ), - ), + [ + Parameters::TIME => [ + [ + 'from' => '11:00', + 'to' => NULL, + ], + ], + ], '2013-02-11 10:59:59', - ), - array( + ], + [ FALSE, - array(Parameters::TIME => array( - array( - 'from' => '11:00', - 'to' => NULL, - ), - ), - ), + [ + Parameters::TIME => [ + [ + 'from' => '11:00', + 'to' => NULL, + ], + ], + ], '2013-02-11 11:01:00', - ), + ], // Range - array( + [ TRUE, - array(Parameters::TIME => array( - array( - 'from' => '11:00', - 'to' => '12:00', - ), - ), - ), + [ + Parameters::TIME => [ + [ + 'from' => '11:00', + 'to' => '12:00', + ], + ], + ], '2013-02-11 11:00:00', - ), - array( + ], + [ TRUE, - array(Parameters::TIME => array( - array( - 'from' => '11:00', - 'to' => '12:00', - ), - ), - ), + [ + Parameters::TIME => [ + [ + 'from' => '11:00', + 'to' => '12:00', + ], + ], + ], '2013-02-11 11:30:00', - ), - array( + ], + [ TRUE, - array(Parameters::TIME => array( - array('from' => '11:00', 'to' => '12:00',), - ), - ), + [ + Parameters::TIME => [ + ['from' => '11:00', 'to' => '12:00',], + ], + ], '2013-02-11 12:00:59', - ), - array( + ], + [ FALSE, - array(Parameters::TIME => array( - array('from' => '11:00', 'to' => '12:00',), - ), - ), + [ + Parameters::TIME => [ + ['from' => '11:00', 'to' => '12:00',], + ], + ], '2013-02-11 10:59:59', - ), - array( + ], + [ FALSE, - array(Parameters::TIME => array( - array('from' => '11:00', 'to' => '12:00',), - ), - ), + [ + Parameters::TIME => [ + ['from' => '11:00', 'to' => '12:00',], + ], + ], '2013-02-11 12:01:00', - ), + ], // Empty - array( + [ TRUE, - array(Parameters::TIME => array(),), + [Parameters::TIME => [],], '2013-02-11 12:00:00', - ), - array( + ], + [ TRUE, - array(Parameters::TIME => NULL,), + [Parameters::TIME => NULL,], '2013-02-11 12:00:00', - ), - ); + ], + ]; } } diff --git a/tests/CronnerTests/Tasks/ParametersParsing.phpt b/tests/CronnerTests/Tasks/ParametersParsing.phpt index 2c80780..fcdbd91 100644 --- a/tests/CronnerTests/Tasks/ParametersParsing.phpt +++ b/tests/CronnerTests/Tasks/ParametersParsing.phpt @@ -1,5 +1,7 @@ object->getReflection()->hasMethod($methodName)) { Assert::fail('Tested class does not have method "' . $methodName . '".'); @@ -42,52 +44,60 @@ class ParametersParsingTest extends \TestCase Assert::same($expected, Parameters::parseParameters($this->object->getReflection()->getMethod($methodName))); } - public function dataProviderParse() + public function dataProviderParse() : array { - return array( - array(array( - Parameters::TASK => 'E-mail notifications', - Parameters::PERIOD => '5 minutes', - Parameters::DAYS => NULL, - Parameters::TIME => NULL, - ), 'test01', - ), - array(array( - Parameters::TASK => 'stekycz\Cronner\tests\objects\TestObject - test02', - Parameters::PERIOD => '1 hour', - Parameters::DAYS => array('Mon', 'Wed', 'Fri',), - Parameters::TIME => array( - array( - 'from' => '09:00', - 'to' => '10:00', - ), - array( - 'from' => '15:00', - 'to' => '16:00', - ), - ), - ), 'test02', - ), - array(array( - Parameters::TASK => 'Test 3', - Parameters::PERIOD => '17 minutes', - Parameters::DAYS => array('Mon', 'Tue', 'Wed', 'Thu', 'Fri',), - Parameters::TIME => array( - array( - 'from' => '09:00', - 'to' => '10:45', - ), - ), - ), 'test03', - ), - array(array( - Parameters::TASK => 'Test 4', - Parameters::PERIOD => '1 day', - Parameters::DAYS => array('Sat', 'Sun',), - Parameters::TIME => NULL, - ), 'test04', - ), - ); + return [ + [ + [ + Parameters::TASK => 'E-mail notifications', + Parameters::PERIOD => '5 minutes', + Parameters::DAYS => NULL, + Parameters::TIME => NULL, + ], + 'test01', + ], + [ + [ + Parameters::TASK => 'stekycz\Cronner\tests\objects\TestObject - test02', + Parameters::PERIOD => '1 hour', + Parameters::DAYS => ['Mon', 'Wed', 'Fri',], + Parameters::TIME => [ + [ + 'from' => '09:00', + 'to' => '10:00', + ], + [ + 'from' => '15:00', + 'to' => '16:00', + ], + ], + ], + 'test02', + ], + [ + [ + Parameters::TASK => 'Test 3', + Parameters::PERIOD => '17 minutes', + Parameters::DAYS => ['Mon', 'Tue', 'Wed', 'Thu', 'Fri',], + Parameters::TIME => [ + [ + 'from' => '09:00', + 'to' => '10:45', + ], + ], + ], + 'test03', + ], + [ + [ + Parameters::TASK => 'Test 4', + Parameters::PERIOD => '1 day', + Parameters::DAYS => ['Sat', 'Sun',], + Parameters::TIME => NULL, + ], + 'test04', + ], + ]; } } diff --git a/tests/CronnerTests/Tasks/Parser.phpt b/tests/CronnerTests/Tasks/Parser.phpt index a01a1ef..c914ed1 100644 --- a/tests/CronnerTests/Tasks/Parser.phpt +++ b/tests/CronnerTests/Tasks/Parser.phpt @@ -1,5 +1,7 @@ '11:00', 'to' => NULL, - ), - ), '11:00', - ), - array( - array( - array( + ], + ], + '11:00', + ], + [ + [ + [ 'from' => '11:00', 'to' => '12:00', - ), - ), '11:00 - 12:00', - ), + ], + ], + '11:00 - 12:00', + ], // Multiple - array( - array( - array( + [ + [ + [ 'from' => '11:00', 'to' => NULL, - ), - array( + ], + [ 'from' => '17:00', 'to' => NULL, - ), - ), '11:00, 17:00', - ), - array( - array( - array( + ], + ], + '11:00, 17:00', + ], + [ + [ + [ 'from' => '11:00', 'to' => '12:00', - ), - array( + ], + [ 'from' => '17:00', 'to' => '19:00', - ), - ), '11:00 - 12:00, 17:00-19:00', - ), + ], + ], + '11:00 - 12:00, 17:00-19:00', + ], // Many whitespaces - array( - array( - array( + [ + [ + [ 'from' => '11:00', 'to' => '12:00', - ), - ), ' 11:00 - 12:00 ', - ), - array( - array( - array( + ], + ], + ' 11:00 - 12:00 ', + ], + [ + [ + [ 'from' => '11:00', 'to' => '12:00', - ), - array( + ], + [ 'from' => '17:00', 'to' => '19:00', - ), - ), ' 11:00 - 12:00 , 17:00 - 19:00 ', - ), + ], + ], + ' 11:00 - 12:00 , 17:00 - 19:00 ', + ], // Over midnight - array( - array( - array( + [ + [ + [ 'from' => '00:00', 'to' => '05:00', - ), - array( + ], + [ 'from' => '21:30', 'to' => '23:59', - ), - ), '21:30 - 05:00', - ), + ], + ], + '21:30 - 05:00', + ], // Critical - array( - array( - array( + [ + [ + [ 'from' => '00:00', 'to' => '05:00', - ), - array( + ], + [ 'from' => '16:00', 'to' => '18:00', - ), - array( + ], + [ 'from' => '21:30', 'to' => '23:59', - ), - ), '16:00 - 18:00, 21:30 - 05:00', - ), + ], + ], + '16:00 - 18:00, 21:30 - 05:00', + ], // Shortcuts - array( - array( - array( + [ + [ + [ 'from' => '06:00', 'to' => '11:59', - ), - ), 'morning', - ), - array( - array( - array( + ], + ], + 'morning', + ], + [ + [ + [ 'from' => '12:00', 'to' => '12:29', - ), - ), 'noon', - ), - array( - array( - array( + ], + ], + 'noon', + ], + [ + [ + [ 'from' => '12:30', 'to' => '16:59', - ), - ), 'afternoon', - ), - array( - array( - array( + ], + ], + 'afternoon', + ], + [ + [ + [ 'from' => '17:00', 'to' => '21:59', - ), - ), 'evening', - ), - array( - array( - array( + ], + ], + 'evening', + ], + [ + [ + [ 'from' => '00:00', 'to' => '05:59', - ), - array( + ], + [ 'from' => '22:00', 'to' => '23:59', - ), - ), 'night', - ), - array( - array( - array( + ], + ], + 'night', + ], + [ + [ + [ 'from' => '00:00', 'to' => '00:29', - ), - ), 'midnight', - ), + ], + ], + 'midnight', + ], // Combined - array( - array( - array( + [ + [ + [ 'from' => '00:00', 'to' => '00:29', - ), - array( + ], + [ 'from' => '06:00', 'to' => '11:59', - ), - ), 'morning, midnight', - ), - array( - array( - array( + ], + ], + 'morning, midnight', + ], + [ + [ + [ 'from' => '00:00', 'to' => '00:29', - ), - array( + ], + [ 'from' => '03:00', 'to' => '04:00', - ), - array( + ], + [ 'from' => '06:00', 'to' => '11:59', - ), - ), 'morning, midnight, 03:00 - 04:00', - ), - ); + ], + ], + 'morning, midnight, 03:00 - 04:00', + ], + ]; } /** * @dataProvider dataProviderParseTimesError * @param string $annotation - * @throws InvalidParameterException + * @throws \stekycz\Cronner\Exceptions\InvalidParameterException */ - public function testThrowsExceptionOnWrongTimesDefinition($annotation) + public function testThrowsExceptionOnWrongTimesDefinition(string $annotation) { Parser::parseTimes($annotation); } - public function dataProviderParseTimesError() + public function dataProviderParseTimesError() : array { - return array( - array('nejaky blabol'), - array('true'), - array('false'), - array('0'), - array('1'), - array(TRUE), - array(FALSE), - array(0), - array(1), - array(new stdClass()), - ); + return [ + ['nejaky blabol'], + ['true'], + ['false'], + ['0'], + ['1'], + ]; } } diff --git a/tests/CronnerTests/Tasks/Task.phpt b/tests/CronnerTests/Tasks/Task.phpt index ae20393..086176d 100644 --- a/tests/CronnerTests/Tasks/Task.phpt +++ b/tests/CronnerTests/Tasks/Task.phpt @@ -1,5 +1,7 @@ shouldReceive("saveRunTime")->with($now)->once(); $timestampStorage->shouldReceive("setTaskName")->times(2); @@ -48,16 +51,16 @@ class TaskTest extends \TestCase * @param int $loads * @param string $methodName * @param string $now - * @param string $lastRunTime + * @param string|null $lastRunTime */ - public function testChecksIfCanBeRun($expected, $loads, $methodName, $now, $lastRunTime) + public function testChecksIfCanBeRun(bool $expected, int $loads, string $methodName, string $now, string $lastRunTime = NULL) { $now = new Nette\Utils\DateTime($now); $lastRunTime = $lastRunTime ? new Nette\Utils\DateTime($lastRunTime) : NULL; $method = $this->object->getReflection()->getMethod($methodName); - $timestampStorage = Mockery::mock('\stekycz\Cronner\ITimestampStorage'); + $timestampStorage = Mockery::mock(ITimestampStorage::class); $timestampStorage->shouldReceive("loadLastRunTime")->times($loads)->andReturn($lastRunTime); $timestampStorage->shouldReceive("setTaskName")->atLeast(1); @@ -65,25 +68,25 @@ class TaskTest extends \TestCase Assert::same($expected, $task->shouldBeRun($now)); } - public function dataProviderShouldBeRun() + public function dataProviderShouldBeRun() : array { - return array( + return [ // Test 01 - array(TRUE, 1, 'test01', '2013-02-01 12:00:00', NULL), - array(TRUE, 1, 'test01', '2013-02-01 12:10:00', '2013-02-01 12:00:00'), - array(FALSE, 1, 'test01', '2013-02-01 12:04:00', '2013-02-01 12:00:00'), + [TRUE, 1, 'test01', '2013-02-01 12:00:00', NULL], + [TRUE, 1, 'test01', '2013-02-01 12:10:00', '2013-02-01 12:00:00'], + [FALSE, 1, 'test01', '2013-02-01 12:04:00', '2013-02-01 12:00:00'], // Test 02 - array(FALSE, 0, 'test02', '2013-02-05 12:00:00', NULL), - array(FALSE, 0, 'test02', '2013-02-04 12:00:00', NULL), - array(FALSE, 1, 'test02', '2013-02-04 09:30:00', '2013-02-04 09:00:00'), - array(TRUE, 1, 'test02', '2013-02-04 09:30:00', NULL), - array(TRUE, 1, 'test02', '2013-02-04 09:30:00', '2013-02-03 15:30:00'), - ); + [FALSE, 0, 'test02', '2013-02-05 12:00:00', NULL], + [FALSE, 0, 'test02', '2013-02-04 12:00:00', NULL], + [FALSE, 1, 'test02', '2013-02-04 09:30:00', '2013-02-04 09:00:00'], + [TRUE, 1, 'test02', '2013-02-04 09:30:00', NULL], + [TRUE, 1, 'test02', '2013-02-04 09:30:00', '2013-02-03 15:30:00'], + ]; } public function testShouldBeRunOnShortLaterRun() { - $timestampStorage = Mockery::mock('\stekycz\Cronner\ITimestampStorage'); + $timestampStorage = Mockery::mock(ITimestampStorage::class); $timestampStorage->shouldReceive("loadLastRunTime")->once()->andReturn(new Nette\Utils\DateTime('2014-08-15 09:00:01')); $timestampStorage->shouldReceive("setTaskName")->atLeast(1); diff --git a/tests/CronnerTests/TimestampStorage/FileStorage.phpt b/tests/CronnerTests/TimestampStorage/FileStorage.phpt index 711d242..8fd3aed 100644 --- a/tests/CronnerTests/TimestampStorage/FileStorage.phpt +++ b/tests/CronnerTests/TimestampStorage/FileStorage.phpt @@ -1,5 +1,7 @@ storage->setTaskName($taskName); } - public function dataProviderSetTaskName() + public function dataProviderSetTaskName() : array { - return array( - array(''), - array(0), - array(1), - array(0.0), - array(1.0), - array(FALSE), - array(TRUE), - array(new stdClass()), - array(array()), - array(array('Test task 1')), - ); + return [ + [''], + ]; } /** @@ -96,13 +89,13 @@ class FileStorageTest extends \TestCase Assert::equal($date->format('Y-m-d H:i:s O'), $lastRunTime->format('Y-m-d H:i:s O')); } - public function dataProviderSaveRunTime() + public function dataProviderSaveRunTime() : array { - return array( - array(new Nette\Utils\DateTime('2013-01-30 17:30:00')), - array(new Nette\Utils\DateTime('2013-01-30 18:30:01')), - array(new Nette\Utils\DateTime('2013-01-30 18:31:01')), - ); + return [ + [new Nette\Utils\DateTime('2013-01-30 17:30:00')], + [new Nette\Utils\DateTime('2013-01-30 18:30:01')], + [new Nette\Utils\DateTime('2013-01-30 18:31:01')], + ]; } public function testSavesLastRunTimeByTaskName() diff --git a/tests/CronnerTests/bootstrap.php b/tests/CronnerTests/bootstrap.php index 39cf84d..01eeacd 100644 --- a/tests/CronnerTests/bootstrap.php +++ b/tests/CronnerTests/bootstrap.php @@ -1,5 +1,7 @@ Date: Wed, 1 Feb 2017 14:41:28 +0100 Subject: [PATCH 19/20] Ignore phpstan.neon file on git export --- .gitattributes | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index c3822b4..00ab51c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,5 @@ .gitattributes export-ignore .gitignore export-ignore .travis.yml export-ignore +phpstan.neon export-ignore tests export-ignore From 0dd795384ba43affc9cabbaa6fba5a1e6569e7ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=CC=8Ctekl?= Date: Wed, 1 Feb 2017 14:51:55 +0100 Subject: [PATCH 20/20] Removed building against HHVM --- .travis.yml | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index f4f2311..7623c1f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,23 +3,13 @@ language: php php: - 7.0 - 7.1 - - hhvm env: - PHP_BIN=php - PHP_BIN=php-cgi -matrix: - allow_failures: - - php: hhvm - - exclude: - - php: hhvm - env: PHP_BIN=php-cgi - before_install: - - if [[ "$TRAVIS_PHP_VERSION" == "hhvm" ]]; then cat tests/php.ini-unix >> /etc/hhvm/php.ini; fi - - if [[ "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-add tests/php.ini-unix; fi + - phpenv config-add tests/php.ini-unix install: - composer self-update