diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..59ea3d7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +# Ignore all test and documentation for archive +/.gitattributes export-ignore +/.gitignore export-ignore +/.scrutinizer.yml export-ignore +/.travis.yml export-ignore +/phpunit.xml.dist export-ignore +/tests export-ignore +/docs export-ignore \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8e375d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +# phpstorm project files +.idea + +# netbeans project files +nbproject + +# zend studio for eclipse project files +.buildpath +.project +.settings + +# windows thumbnail cache +Thumbs.db + +# composer vendor dir +/vendor + +/composer.lock + +# composer itself is not needed +composer.phar + +# Mac DS_Store Files +.DS_Store + +# phpunit itself is not needed +phpunit.phar +# local phpunit config +/phpunit.xml + +# local tests configuration +/tests/data/config.local.php + +# runtime cache +/tests/runtime \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..2b97385 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,24 @@ +language: php + +php: + - 5.6 + - 7.1 + +# faster builds on new travis setup not using sudo +sudo: false + +# cache vendor dirs +cache: + directories: + - $HOME/.composer/cache + - vendor + +install: + - travis_retry composer self-update && composer --version + - travis_retry composer global require "fxp/composer-asset-plugin:^1.2.0" + - export PATH="$HOME/.composer/vendor/bin:$PATH" + - travis_retry composer install --prefer-dist --no-interaction + +script: + - vendor/friendsofphp/php-cs-fixer/php-cs-fixer fix --dry-run --diff + - phpunit --verbose $PHPUNIT_FLAGS diff --git a/Event.php b/Event.php index 931cdd1..6ddd364 100644 --- a/Event.php +++ b/Event.php @@ -17,7 +17,14 @@ */ class Event extends Component { + /** + * Event is triggered before running the command. + */ const EVENT_BEFORE_RUN = 'beforeRun'; + + /** + * Event is triggered after running the command. + */ const EVENT_AFTER_RUN = 'afterRun'; /** @@ -105,11 +112,13 @@ public function __construct($command, $config = []) public function run(Application $app) { $this->trigger(self::EVENT_BEFORE_RUN); + if (count($this->_afterCallbacks) > 0) { $this->runCommandInForeground($app); } else { $this->runCommandInBackground($app); } + $this->trigger(self::EVENT_AFTER_RUN); } diff --git a/ScheduleController.php b/ScheduleController.php index a6a3ad5..1e42004 100644 --- a/ScheduleController.php +++ b/ScheduleController.php @@ -43,9 +43,9 @@ public function init() parent::init(); if (Yii::$app->has($this->schedule)) { - $this->schedule = Instance::ensure($this->schedule, Schedule::className()); + $this->schedule = Instance::ensure($this->schedule, Schedule::class); } else { - $this->schedule = Yii::createObject(Schedule::className()); + $this->schedule = Yii::createObject(Schedule::class); } } diff --git a/composer.json b/composer.json index 73bcbd8..86542ac 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "require": { - "php": ">=5.4.0", + "php": ">=5.6", "yiisoft/yii2": "2.0.*", "symfony/process": "2.6.*", "mtdowling/cron-expression": "~1.0", diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..81482a0 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,13 @@ + + + + + ./tests + + + diff --git a/tests/ScheduleTest.php b/tests/ScheduleTest.php new file mode 100644 index 0000000..5f03449 --- /dev/null +++ b/tests/ScheduleTest.php @@ -0,0 +1,62 @@ +_schedule = Yii::createObject(Schedule::class); + $this->importScheduleFile(); + } + + // Tests : + public function testGetEvents() + { + $events = $this->_schedule->getEvents(); + $event = $events[0]; + + $this->assertNotEmpty($events); + $this->assertCount(2, $events); + $this->assertEquals('Show list of files', $event->description); + $this->assertEquals('ls', $event->command); + $this->assertEquals('* * * * * *', $event->expression); + + $event = $events[1]; + + $this->assertEquals('Execute migrations', $event->description); + $this->assertContains('yii migrate', $event->command); + $this->assertEquals('0 0 * * * *', $event->expression); + } + + /** + * Import schedule file + */ + protected function importScheduleFile() + { + $scheduleFile = Yii::getAlias('@yii2mod/tests/scheduling/data/schedule.php'); + + $schedule = $this->_schedule; + call_user_func(function () use ($schedule, $scheduleFile) { + include $scheduleFile; + }); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..937c1aa --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,56 @@ +mockApplication(); + } + + protected function tearDown() + { + $this->destroyApplication(); + } + + /** + * Populates Yii::$app with a new application + * The application will be destroyed on tearDown() automatically. + * + * @param array $config The application configuration, if needed + * @param string $appClass name of the application class to create + */ + protected function mockApplication($config = [], $appClass = '\yii\console\Application') + { + new $appClass(ArrayHelper::merge([ + 'id' => 'testapp', + 'basePath' => __DIR__, + 'vendorPath' => $this->getVendorPath(), + ], $config)); + } + + /** + * @return string vendor path + */ + protected function getVendorPath() + { + return dirname(__DIR__) . '/vendor'; + } + + /** + * Destroys application in Yii::$app by setting it to null. + */ + protected function destroyApplication() + { + Yii::$app = null; + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..56bb614 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,15 @@ +exec('ls')->description('Show list of files')->everyMinute(); +$schedule->command('migrate')->description('Execute migrations')->daily();