Completed
Push — refactor-db-tests ( 8ba032...86f17c )
by Carsten
10:02
created

InitDbFixture   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 2
dl 0
loc 71
rs 10
c 0
b 0
f 0
ccs 21
cts 23
cp 0.913

6 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeLoad() 0 4 1
A afterLoad() 0 4 1
A load() 0 7 2
A beforeUnload() 0 4 1
A afterUnload() 0 4 1
A checkIntegrity() 0 6 2
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\test;
9
10
use Yii;
11
12
/**
13
 * InitDbFixture represents the initial state needed for DB-related tests.
14
 *
15
 * Its main task is to toggle integrity check of the database during data loading.
16
 * This is needed by other DB-related fixtures (e.g. [[ActiveFixture]]) so that they can populate
17
 * data into the database without triggering integrity check errors.
18
 *
19
 * Besides, DbFixture also attempts to load an [[initScript|initialization script]] if it exists.
20
 *
21
 * You should normally use InitDbFixture to prepare a skeleton test database.
22
 * Other DB fixtures will then add specific tables and data to this database.
23
 *
24
 * @author Qiang Xue <[email protected]>
25
 * @since 2.0
26
 */
27
class InitDbFixture extends DbFixture
28
{
29
    /**
30
     * @var string the init script file that should be executed when loading this fixture.
31
     * This should be either a file path or path alias. Note that if the file does not exist,
32
     * no error will be raised.
33
     */
34
    public $initScript = '@app/tests/fixtures/initdb.php';
35
    /**
36
     * @var array list of database schemas that the test tables may reside in. Defaults to
37
     * `['']`, meaning using the default schema (an empty string refers to the
38
     * default schema). This property is mainly used when turning on and off integrity checks
39
     * so that fixture data can be populated into the database without causing problem.
40
     */
41
    public $schemas = [''];
42
43
44
    /**
45
     * @inheritdoc
46
     */
47 6
    public function beforeLoad()
48
    {
49 6
        $this->checkIntegrity(false);
50 6
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 6
    public function afterLoad()
56
    {
57 6
        $this->checkIntegrity(true);
58 6
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 6
    public function load()
64
    {
65 6
        $file = Yii::getAlias($this->initScript);
66 6
        if (is_file($file)) {
67
            require($file);
68
        }
69 6
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 6
    public function beforeUnload()
75
    {
76 6
        $this->checkIntegrity(false);
77 6
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82 6
    public function afterUnload()
83
    {
84 6
        $this->checkIntegrity(true);
85 6
    }
86
87
    /**
88
     * Toggles the DB integrity check.
89
     * @param boolean $check whether to turn on or off the integrity check.
90
     */
91 6
    public function checkIntegrity($check)
92
    {
93 6
        foreach ($this->schemas as $schema) {
94 6
            $this->db->createCommand()->checkIntegrity($check, $schema)->execute();
95 6
        }
96 6
    }
97
}
98