Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Yii2mod Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
62 changes: 62 additions & 0 deletions tests/ScheduleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace yii2mod\scheduling\tests;

use Yii;
use yii2mod\scheduling\Schedule;

/**
* Class ScheduleTest
*
* @package yii2mod\scheduling\tests
*/
class ScheduleTest extends TestCase
{
/**
* @var Schedule
*/
private $_schedule;

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();

$this->_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;
});
}
}
56 changes: 56 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace yii2mod\scheduling\tests;

use Yii;
use yii\helpers\ArrayHelper;

/**
* This is the base class for all yii framework unit tests.
*/
class TestCase extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
parent::setUp();

$this->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;
}
}
15 changes: 15 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

// ensure we get report on all possible php errors
error_reporting(-1);

define('YII_ENABLE_ERROR_HANDLER', false);
define('YII_DEBUG', true);

$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
$_SERVER['SCRIPT_FILENAME'] = __FILE__;

require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

Yii::setAlias('@yii2mod/tests/scheduling', __DIR__);
5 changes: 5 additions & 0 deletions tests/data/schedule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

/* @var $schedule \yii2mod\scheduling\Schedule */
$schedule->exec('ls')->description('Show list of files')->everyMinute();
$schedule->command('migrate')->description('Execute migrations')->daily();