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+ }
0 commit comments