Completed
Push — master ( 6494b1...504660 )
by Igor
02:00
created

TestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 94
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A tearDown() 0 4 1
A mockApplication() 0 20 1
A getVendorPath() 0 4 1
A destroyApplication() 0 4 1
B setupTestDbData() 0 30 1
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