Skip to content

Commit 967a677

Browse files
author
igor-chepurnoi
committed
add tests
1 parent be24d92 commit 967a677

File tree

9 files changed

+251
-3
lines changed

9 files changed

+251
-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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- 7.0
8+
- hhvm
9+
10+
# run build against hhvm but allow them to fail
11+
# http://docs.travis-ci.com/user/build-configuration/#Rows-That-are-Allowed-To-Fail
12+
matrix:
13+
fast_finish: true
14+
allow_failures:
15+
- php: hhvm
16+
17+
# faster builds on new travis setup not using sudo
18+
sudo: false
19+
20+
# cache vendor dirs
21+
cache:
22+
directories:
23+
- $HOME/.composer/cache
24+
25+
install:
26+
- travis_retry composer self-update && composer --version
27+
- travis_retry composer global require "fxp/composer-asset-plugin:~1.1.1"
28+
- export PATH="$HOME/.composer/vendor/bin:$PATH"
29+
- travis_retry composer install --prefer-dist --no-interaction
30+
31+
script:
32+
- phpunit --verbose $PHPUNIT_FLAGS

models/CronScheduleModel.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Yii;
66
use yii\db\ActiveRecord;
7-
use yii\db\Expression;
87
use yii2mod\cron\models\enumerables\CronScheduleStatus;
98

109
/**
@@ -68,7 +67,7 @@ public function startCronSchedule($jobCode, $status = CronScheduleStatus::RUN, $
6867
$this->jobCode = $jobCode;
6968
$this->status = $status;
7069
$this->messages = $messages;
71-
$this->dateCreated = new Expression('NOW()');
70+
$this->dateCreated = Yii::$app->formatter->asDatetime(time(), 'php: Y-m-d H:i:s');
7271

7372
return $this->save();
7473
}
@@ -84,7 +83,7 @@ public function startCronSchedule($jobCode, $status = CronScheduleStatus::RUN, $
8483
public function endCronSchedule($status, $messages = null)
8584
{
8685
if ($this->id) {
87-
$this->dateFinished = new Expression('NOW()');
86+
$this->dateFinished = Yii::$app->formatter->asDatetime(time(), 'php: Y-m-d H:i:s');
8887
$this->status = $status;
8988
$this->messages = $messages;
9089

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/CronLoggerBehaviorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace yii2mod\cron\tests;
4+
5+
use Yii;
6+
use yii\console\Controller;
7+
use yii2mod\cron\models\CronScheduleModel;
8+
9+
/**
10+
* Class CronLoggerBehaviorTest
11+
* @package yii2mod\cron\tests
12+
*/
13+
class CronLoggerBehaviorTest extends TestCase
14+
{
15+
public function testCronLog()
16+
{
17+
Yii::$app->runAction('hello');
18+
$cronScheduleModel = CronScheduleModel::find()->one();
19+
20+
$this->assertNotEmpty($cronScheduleModel, 'Cron log is empty!');
21+
$this->assertEquals(Controller::EXIT_CODE_NORMAL, $cronScheduleModel->status, 'Invalid cron status!');
22+
}
23+
}

tests/TestCase.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace yii2mod\cron\tests;
4+
5+
use yii\helpers\ArrayHelper;
6+
use Yii;
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+
$this->setupTestDbData();
20+
}
21+
22+
protected function tearDown()
23+
{
24+
$this->destroyApplication();
25+
}
26+
27+
/**
28+
* Populates Yii::$app with a new application
29+
* The application will be destroyed on tearDown() automatically.
30+
* @param array $config The application configuration, if needed
31+
* @param string $appClass name of the application class to create
32+
*/
33+
protected function mockApplication($config = [], $appClass = '\yii\console\Application')
34+
{
35+
new $appClass(ArrayHelper::merge([
36+
'id' => 'testapp',
37+
'basePath' => __DIR__,
38+
'vendorPath' => $this->getVendorPath(),
39+
'controllerMap' => [
40+
'hello' => 'yii2mod\cron\tests\data\HelloController'
41+
],
42+
'components' => [
43+
'db' => [
44+
'class' => 'yii\db\Connection',
45+
'dsn' => 'sqlite::memory:',
46+
],
47+
'errorHandler' => [
48+
'class' => 'yii2mod\cron\components\ErrorHandler',
49+
],
50+
'mutex' => [
51+
'class' => 'yii\mutex\FileMutex'
52+
],
53+
],
54+
], $config));
55+
}
56+
57+
/**
58+
* @return string vendor path
59+
*/
60+
protected function getVendorPath()
61+
{
62+
return dirname(__DIR__) . '/vendor';
63+
}
64+
65+
/**
66+
* Destroys application in Yii::$app by setting it to null.
67+
*/
68+
protected function destroyApplication()
69+
{
70+
Yii::$app = null;
71+
}
72+
73+
/**
74+
* Setup tables for test ActiveRecord
75+
*/
76+
protected function setupTestDbData()
77+
{
78+
$db = Yii::$app->getDb();
79+
80+
// Structure :
81+
82+
$db->createCommand()->createTable('{{%CronSchedule}}', [
83+
'id' => 'pk',
84+
'jobCode' => 'string not null',
85+
'status' => 'smallint not null',
86+
'messages' => 'text',
87+
'dateCreated' => 'timestamp null',
88+
'dateFinished' => 'timestamp null',
89+
])->execute();
90+
}
91+
}

tests/bootstrap.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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');

tests/data/HelloController.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace yii2mod\cron\tests\data;
4+
5+
use yii\console\Controller;
6+
7+
/**
8+
* Class HelloController
9+
* @package yii2mod\cron\tests\data
10+
*/
11+
class HelloController extends Controller
12+
{
13+
/**
14+
* @return array
15+
*/
16+
public function behaviors()
17+
{
18+
return [
19+
'cronLogger' => [
20+
'class' => 'yii2mod\cron\behaviors\CronLoggerBehavior',
21+
'actions' => ['index']
22+
]
23+
];
24+
}
25+
26+
/**
27+
* This command echoes what you have entered as the message.
28+
* @param string $message the message to be echoed.
29+
*/
30+
public function actionIndex($message = 'hello world')
31+
{
32+
echo $message . "\n";
33+
}
34+
}

0 commit comments

Comments
 (0)