1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\cart\tests; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\ArrayHelper; |
7
|
|
|
use yii2mod\cart\tests\data\Session; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This is the base class for all yii framework unit tests. |
11
|
|
|
*/ |
12
|
|
|
class TestCase extends \PHPUnit\Framework\TestCase |
13
|
|
|
{ |
14
|
|
|
protected function setUp() |
15
|
|
|
{ |
16
|
|
|
parent::setUp(); |
17
|
|
|
|
18
|
|
|
$this->mockApplication(); |
19
|
|
|
|
20
|
|
|
$this->setupTestDbData(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function tearDown() |
24
|
|
|
{ |
25
|
|
|
$this->destroyApplication(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Populates Yii::$app with a new application |
30
|
|
|
* The application will be destroyed on tearDown() automatically. |
31
|
|
|
* |
32
|
|
|
* @param array $config The application configuration, if needed |
33
|
|
|
* @param string $appClass name of the application class to create |
34
|
|
|
*/ |
35
|
|
|
protected function mockApplication($config = [], $appClass = '\yii\console\Application') |
36
|
|
|
{ |
37
|
|
|
new $appClass(ArrayHelper::merge([ |
38
|
|
|
'id' => 'testapp', |
39
|
|
|
'basePath' => __DIR__, |
40
|
|
|
'vendorPath' => $this->getVendorPath(), |
41
|
|
|
'components' => [ |
42
|
|
|
'db' => [ |
43
|
|
|
'class' => 'yii\db\Connection', |
44
|
|
|
'dsn' => 'sqlite::memory:', |
45
|
|
|
], |
46
|
|
|
'session' => [ |
47
|
|
|
'class' => Session::class, |
48
|
|
|
], |
49
|
|
|
'cart' => [ |
50
|
|
|
'class' => 'yii2mod\cart\Cart', |
51
|
|
|
], |
52
|
|
|
], |
53
|
|
|
], $config)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string vendor path |
58
|
|
|
*/ |
59
|
|
|
protected function getVendorPath() |
60
|
|
|
{ |
61
|
|
|
return dirname(__DIR__) . '/vendor'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Destroys application in Yii::$app by setting it to null. |
66
|
|
|
*/ |
67
|
|
|
protected function destroyApplication() |
68
|
|
|
{ |
69
|
|
|
Yii::$app = null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Setup tables for test ActiveRecord |
74
|
|
|
*/ |
75
|
|
|
protected function setupTestDbData() |
76
|
|
|
{ |
77
|
|
|
$db = Yii::$app->getDb(); |
78
|
|
|
|
79
|
|
|
// Structure : |
80
|
|
|
|
81
|
|
|
$db->createCommand()->createTable('cart', [ |
82
|
|
|
'sessionId' => 'string primary key', |
83
|
|
|
'cartData' => 'text', |
84
|
|
|
])->execute(); |
85
|
|
|
|
86
|
|
|
$db->createCommand()->createTable('product', [ |
87
|
|
|
'id' => 'pk', |
88
|
|
|
'name' => 'string', |
89
|
|
|
'price' => 'decimal', |
90
|
|
|
])->execute(); |
91
|
|
|
|
92
|
|
|
$db->createCommand()->createTable('session', [ |
93
|
|
|
'id' => 'char(40) not null primary key', |
94
|
|
|
'expire' => 'integer', |
95
|
|
|
'data' => 'longblob', |
96
|
|
|
])->execute(); |
97
|
|
|
|
98
|
|
|
// Data : |
99
|
|
|
|
100
|
|
|
$db->createCommand()->insert('product', [ |
101
|
|
|
'name' => 'Amazon Kindle', |
102
|
|
|
'price' => 100, |
103
|
|
|
])->execute(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|