1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\cart\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
|
|
|
$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
|
|
|
* |
31
|
|
|
* @param array $config The application configuration, if needed |
32
|
|
|
* @param string $appClass name of the application class to create |
33
|
|
|
*/ |
34
|
|
|
protected function mockApplication($config = [], $appClass = '\yii\console\Application') |
35
|
|
|
{ |
36
|
|
|
new $appClass(ArrayHelper::merge([ |
37
|
|
|
'id' => 'testapp', |
38
|
|
|
'basePath' => __DIR__, |
39
|
|
|
'vendorPath' => $this->getVendorPath(), |
40
|
|
|
'components' => [ |
41
|
|
|
'db' => [ |
42
|
|
|
'class' => 'yii\db\Connection', |
43
|
|
|
'dsn' => 'sqlite::memory:', |
44
|
|
|
], |
45
|
|
|
'session' => [ |
46
|
|
|
'class' => 'yii\web\DbSession', |
47
|
|
|
], |
48
|
|
|
'cart' => [ |
49
|
|
|
'class' => 'yii2mod\cart\Cart', |
50
|
|
|
], |
51
|
|
|
], |
52
|
|
|
], $config)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string vendor path |
57
|
|
|
*/ |
58
|
|
|
protected function getVendorPath() |
59
|
|
|
{ |
60
|
|
|
return dirname(__DIR__) . '/vendor'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Destroys application in Yii::$app by setting it to null. |
65
|
|
|
*/ |
66
|
|
|
protected function destroyApplication() |
67
|
|
|
{ |
68
|
|
|
Yii::$app = null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Setup tables for test ActiveRecord |
73
|
|
|
*/ |
74
|
|
|
protected function setupTestDbData() |
75
|
|
|
{ |
76
|
|
|
$db = Yii::$app->getDb(); |
77
|
|
|
|
78
|
|
|
// Structure : |
79
|
|
|
|
80
|
|
|
$db->createCommand()->createTable('cart', [ |
81
|
|
|
'sessionId' => 'string primary key', |
82
|
|
|
'cartData' => 'text', |
83
|
|
|
])->execute(); |
84
|
|
|
|
85
|
|
|
$db->createCommand()->createTable('product', [ |
86
|
|
|
'id' => 'pk', |
87
|
|
|
'name' => 'string', |
88
|
|
|
'price' => 'decimal', |
89
|
|
|
])->execute(); |
90
|
|
|
|
91
|
|
|
$db->createCommand()->createTable('session', [ |
92
|
|
|
'id' => 'char(40) not null primary key', |
93
|
|
|
'expire' => 'integer', |
94
|
|
|
'data' => 'longblob', |
95
|
|
|
])->execute(); |
96
|
|
|
|
97
|
|
|
// Data : |
98
|
|
|
|
99
|
|
|
$db->createCommand()->insert('product', [ |
100
|
|
|
'name' => 'Amazon Kindle', |
101
|
|
|
'price' => 100, |
102
|
|
|
])->execute(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|