Skip to content

Commit 3a3833e

Browse files
author
Igor Chepurnoy
authored
Merge pull request #1 from yii2mod/added_tests
Added tests
2 parents a628c23 + 5ad7e7f commit 3a3833e

File tree

11 files changed

+230
-3
lines changed

11 files changed

+230
-3
lines changed

.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Ignore all test and documentation for archive
2+
/.gitattributes export-ignore
3+
/.gitignore export-ignore
4+
/.scrutinizer.yml export-ignore
5+
/.travis.yml export-ignore
6+
/phpunit.xml.dist export-ignore
7+
/tests export-ignore
8+
/docs export-ignore

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# phpstorm project files
2+
.idea
3+
4+
# netbeans project files
5+
nbproject
6+
7+
# zend studio for eclipse project files
8+
.buildpath
9+
.project
10+
.settings
11+
12+
# windows thumbnail cache
13+
Thumbs.db
14+
15+
# composer vendor dir
16+
/vendor
17+
18+
/composer.lock
19+
20+
# composer itself is not needed
21+
composer.phar
22+
23+
# Mac DS_Store Files
24+
.DS_Store
25+
26+
# phpunit itself is not needed
27+
phpunit.phar
28+
# local phpunit config
29+
/phpunit.xml
30+
31+
# local tests configuration
32+
/tests/data/config.local.php
33+
34+
# runtime cache
35+
/tests/runtime

.travis.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
language: php
2+
3+
php:
4+
- 5.6
5+
- 7.1
6+
7+
# faster builds on new travis setup not using sudo
8+
sudo: false
9+
10+
# cache vendor dirs
11+
cache:
12+
directories:
13+
- $HOME/.composer/cache
14+
- vendor
15+
16+
install:
17+
- travis_retry composer self-update && composer --version
18+
- travis_retry composer global require "fxp/composer-asset-plugin:^1.2.0"
19+
- export PATH="$HOME/.composer/vendor/bin:$PATH"
20+
- travis_retry composer install --prefer-dist --no-interaction
21+
22+
script:
23+
- vendor/friendsofphp/php-cs-fixer/php-cs-fixer fix --dry-run --diff
24+
- phpunit --verbose $PHPUNIT_FLAGS

Event.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@
1717
*/
1818
class Event extends Component
1919
{
20+
/**
21+
* Event is triggered before running the command.
22+
*/
2023
const EVENT_BEFORE_RUN = 'beforeRun';
24+
25+
/**
26+
* Event is triggered after running the command.
27+
*/
2128
const EVENT_AFTER_RUN = 'afterRun';
2229

2330
/**
@@ -105,11 +112,13 @@ public function __construct($command, $config = [])
105112
public function run(Application $app)
106113
{
107114
$this->trigger(self::EVENT_BEFORE_RUN);
115+
108116
if (count($this->_afterCallbacks) > 0) {
109117
$this->runCommandInForeground($app);
110118
} else {
111119
$this->runCommandInBackground($app);
112120
}
121+
113122
$this->trigger(self::EVENT_AFTER_RUN);
114123
}
115124

ScheduleController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public function init()
4343
parent::init();
4444

4545
if (Yii::$app->has($this->schedule)) {
46-
$this->schedule = Instance::ensure($this->schedule, Schedule::className());
46+
$this->schedule = Instance::ensure($this->schedule, Schedule::class);
4747
} else {
48-
$this->schedule = Yii::createObject(Schedule::className());
48+
$this->schedule = Yii::createObject(Schedule::class);
4949
}
5050
}
5151

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
],
1616
"require": {
17-
"php": ">=5.4.0",
17+
"php": ">=5.6",
1818
"yiisoft/yii2": "2.0.*",
1919
"symfony/process": "~3.2",
2020
"mtdowling/cron-expression": "~1.0",

phpunit.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit bootstrap="./tests/bootstrap.php"
3+
colors="true"
4+
convertErrorsToExceptions="true"
5+
convertNoticesToExceptions="true"
6+
convertWarningsToExceptions="true"
7+
stopOnFailure="false">
8+
<testsuites>
9+
<testsuite name="Yii2mod Test Suite">
10+
<directory>./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
</phpunit>

tests/ScheduleTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace yii2mod\scheduling\tests;
4+
5+
use Yii;
6+
use yii2mod\scheduling\Schedule;
7+
8+
/**
9+
* Class ScheduleTest
10+
*
11+
* @package yii2mod\scheduling\tests
12+
*/
13+
class ScheduleTest extends TestCase
14+
{
15+
/**
16+
* @var Schedule
17+
*/
18+
private $_schedule;
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
protected function setUp()
24+
{
25+
parent::setUp();
26+
27+
$this->_schedule = Yii::createObject(Schedule::class);
28+
$this->importScheduleFile();
29+
}
30+
31+
// Tests :
32+
public function testGetEvents()
33+
{
34+
$events = $this->_schedule->getEvents();
35+
$event = $events[0];
36+
37+
$this->assertNotEmpty($events);
38+
$this->assertCount(2, $events);
39+
$this->assertEquals('Show list of files', $event->description);
40+
$this->assertEquals('ls', $event->command);
41+
$this->assertEquals('* * * * * *', $event->expression);
42+
43+
$event = $events[1];
44+
45+
$this->assertEquals('Execute migrations', $event->description);
46+
$this->assertContains('yii migrate', $event->command);
47+
$this->assertEquals('0 0 * * * *', $event->expression);
48+
}
49+
50+
/**
51+
* Import schedule file
52+
*/
53+
protected function importScheduleFile()
54+
{
55+
$scheduleFile = Yii::getAlias('@yii2mod/tests/scheduling/data/schedule.php');
56+
57+
$schedule = $this->_schedule;
58+
call_user_func(function () use ($schedule, $scheduleFile) {
59+
include $scheduleFile;
60+
});
61+
}
62+
}

tests/TestCase.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace yii2mod\scheduling\tests;
4+
5+
use Yii;
6+
use yii\helpers\ArrayHelper;
7+
8+
/**
9+
* This is the base class for all yii framework unit tests.
10+
*/
11+
class TestCase extends \PHPUnit_Framework_TestCase
12+
{
13+
protected function setUp()
14+
{
15+
parent::setUp();
16+
17+
$this->mockApplication();
18+
}
19+
20+
protected function tearDown()
21+
{
22+
$this->destroyApplication();
23+
}
24+
25+
/**
26+
* Populates Yii::$app with a new application
27+
* The application will be destroyed on tearDown() automatically.
28+
*
29+
* @param array $config The application configuration, if needed
30+
* @param string $appClass name of the application class to create
31+
*/
32+
protected function mockApplication($config = [], $appClass = '\yii\console\Application')
33+
{
34+
new $appClass(ArrayHelper::merge([
35+
'id' => 'testapp',
36+
'basePath' => __DIR__,
37+
'vendorPath' => $this->getVendorPath(),
38+
], $config));
39+
}
40+
41+
/**
42+
* @return string vendor path
43+
*/
44+
protected function getVendorPath()
45+
{
46+
return dirname(__DIR__) . '/vendor';
47+
}
48+
49+
/**
50+
* Destroys application in Yii::$app by setting it to null.
51+
*/
52+
protected function destroyApplication()
53+
{
54+
Yii::$app = null;
55+
}
56+
}

tests/bootstrap.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
// ensure we get report on all possible php errors
4+
error_reporting(-1);
5+
6+
define('YII_ENABLE_ERROR_HANDLER', false);
7+
define('YII_DEBUG', true);
8+
9+
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
10+
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
11+
12+
require_once(__DIR__ . '/../vendor/autoload.php');
13+
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
14+
15+
Yii::setAlias('@yii2mod/tests/scheduling', __DIR__);

0 commit comments

Comments
 (0)