DbTest4ExecAndTransactions   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 201
Duplicated Lines 23.88 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 3 Features 2
Metric Value
wmc 13
c 4
b 3
f 2
lcom 1
cbo 1
dl 48
loc 201
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataSet() 0 4 1
A testPreCondition() 0 4 1
C setUp() 22 22 7
A getconnection() 7 7 1
A test_ExecAndTransaction_ExecWithNoArgs() 19 19 1
B test_ExecAndTransaction_ExecWithNoTrans() 0 29 1
A test_ExecAndTransaction_ExecWithInTrans() 0 59 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * DbTest4ExecAndTransactions
4
 *
5
 * Db::exec()/beginTransaction()/inTransaction()/commit()/rollBack()/lastInsertId()用テストケース
6
 *
7
 * @package           risoluto
8
 * @author            Risoluto Developers
9
 * @license           http://opensource.org/licenses/bsd-license.php new BSD license
10
 * @copyright     (C) 2008-2015 Risoluto Developers / All Rights Reserved.
11
 */
12
13
//------------------------------------------------------//
14
// 名前空間の定義
15
//------------------------------------------------------//
16
namespace Risoluto;
17
18
//------------------------------------------------------//
19
// テストクラス定義
20
//------------------------------------------------------//
21
class DbTest4ExecAndTransactions extends \PHPUnit_Extensions_Database_TestCase
22
{
23
    //------------------------------------------------------//
24
    // テストクラス変数定義
25
    //------------------------------------------------------//
26
    /**
27
     * $instance
28
     * @access protected
29
     * @var    object    テスト対象インスタンスを保持
30
     */
31
    protected $instance;
32
33
    //------------------------------------------------------//
34
    // テストメソッド定義
35
    //------------------------------------------------------//
36
    /**
37
     * setUp()
38
     *
39
     * テストに必要な準備を実施
40
     */
41 View Code Duplication
    protected function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        // 拡張モジュールがロードされているかをチェック
44
        if (!extension_loaded( 'mysqli' )) {
45
            $this->markTestSkipped( 'Cannot use mysqli expansion module.' );
46
        }
47
48
        if (!isset( $GLOBALS[ 'DB_DRIVER' ] )) {
49
            $this->markTestSkipped( 'DB_DRIVER was not defined. Check phpunit.xml' );
50
        } elseif (!isset( $GLOBALS[ 'DB_USER' ] )) {
51
            $this->markTestSkipped( 'DB_USER was not defined. Check phpunit.xml' );
52
        } elseif (!isset( $GLOBALS[ 'DB_PASSWORD' ] )) {
53
            $this->markTestSkipped( 'DB_PASSWORD was not defined. Check phpunit.xml' );
54
        } elseif (!isset( $GLOBALS[ 'DB_DBNAME' ] )) {
55
            $this->markTestSkipped( 'DB_DBNAME was not defined. Check phpunit.xml' );
56
        } elseif (!isset( $GLOBALS[ 'DB_HOST' ] )) {
57
            $this->markTestSkipped( 'DB_HOST was not defined. Check phpunit.xml' );
58
        }
59
60
        // DB周りの初期化を行う為に元々のsetUp()をコールする
61
        parent::setUp();
62
    }
63
64
    /**
65
     * getconnection()
66
     *
67
     * DBテストに必要な接続を実施
68
     */
69 View Code Duplication
    public function getconnection()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $dsn = $GLOBALS[ 'DB_DRIVER' ] . ':dbname=' . $GLOBALS[ 'DB_DBNAME' ] . ';host=' . $GLOBALS[ 'DB_HOST' ];
72
        $pdo = new \PDO( $dsn, $GLOBALS[ 'DB_USER' ], $GLOBALS[ 'DB_PASSWORD' ] );
73
74
        return $this->createDefaultDBconnection( $pdo, $GLOBALS[ 'DB_DBNAME' ] );
75
    }
76
77
    /**
78
     * getDataSet()
79
     *
80
     * DBテストに必要なデータセットを実施
81
     */
82
    public function getDataSet()
83
    {
84
        return $this->createXMLDataSet( dirname( __FILE__ ) . '/../../../risoluto_db_test.xml' );
85
    }
86
87
    /**
88
     * testPreCondition()
89
     *
90
     * テスト開始前に前提条件をチェックする
91
     */
92
    public function testPreCondition()
93
    {
94
        $this->assertEquals( 2, $this->getconnection()->getRowCount( 'risoluto_db_test' ) );
95
    }
96
97
    /**
98
     * test_ExecAndTransaction_ExecWithNoArgs()
99
     *
100
     * exec()のテスト(引数なし)
101
     */
102 View Code Duplication
    public function test_ExecAndTransaction_ExecWithNoArgs()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        $params = [
105
            "driver" => $GLOBALS[ 'DB_DRIVER' ],
106
            "user" => $GLOBALS[ 'DB_USER' ],
107
            "pass" => $GLOBALS[ 'DB_PASSWORD' ],
108
            "dbname" => $GLOBALS[ 'DB_DBNAME' ],
109
            "host" => $GLOBALS[ 'DB_HOST' ],
110
            "persistent" => false,
111
        ];
112
113
        $instance = new Db;
114
        $instance->connect( $params );
115
116
        $this->assertFalse( $instance->exec( '' ) );
117
118
        $instance->disConnect();
119
        unset( $instance );
120
    }
121
122
    /**
123
     * test_ExecAndTransaction_ExecWithNoTrans()
124
     *
125
     * exec()のテスト(トランザクションなし)
126
     */
127
    public function test_ExecAndTransaction_ExecWithNoTrans()
128
    {
129
        $params = [
130
            "driver" => $GLOBALS[ 'DB_DRIVER' ],
131
            "user" => $GLOBALS[ 'DB_USER' ],
132
            "pass" => $GLOBALS[ 'DB_PASSWORD' ],
133
            "dbname" => $GLOBALS[ 'DB_DBNAME' ],
134
            "host" => $GLOBALS[ 'DB_HOST' ],
135
            "persistent" => false,
136
        ];
137
138
        $instance = new Db;
139
        $instance->connect( $params );
140
141
        $this->assertFalse( $instance->inTransaction() );
142
143
        $this->assertEquals( 1,
144
            $instance->exec( 'INSERT INTO risoluto_db_test(id, column1, column2) values ("10", "TEST_A", "TEST_B");' ) );
145
        $this->assertEquals( 3, $this->getconnection()->getRowCount( 'risoluto_db_test' ) );
146
147
        $this->assertEquals( 10, $instance->lastInsertId() );
148
        $this->assertEquals( 10, $instance->lastInsertId( 'id' ) );
149
150
        $this->assertEquals( 1, $instance->exec( 'DELETE FROM risoluto_db_test WHERE id="10";' ) );
151
        $this->assertEquals( 2, $this->getconnection()->getRowCount( 'risoluto_db_test' ) );
152
153
        $instance->disConnect();
154
        unset( $instance );
155
    }
156
157
    /**
158
     * test_ExecAndTransaction_ExecWithInTrans()
159
     *
160
     * exec()のテスト(トランザクションあり)
161
     */
162
    public function test_ExecAndTransaction_ExecWithInTrans()
163
    {
164
        $params = [
165
            "driver" => $GLOBALS[ 'DB_DRIVER' ],
166
            "user" => $GLOBALS[ 'DB_USER' ],
167
            "pass" => $GLOBALS[ 'DB_PASSWORD' ],
168
            "dbname" => $GLOBALS[ 'DB_DBNAME' ],
169
            "host" => $GLOBALS[ 'DB_HOST' ],
170
            "persistent" => false,
171
        ];
172
173
        $instance = new Db;
174
        $instance->connect( $params );
175
176
        // commit pattern
177
        $this->assertTrue( $instance->beginTransaction() );
178
        $this->assertTrue( $instance->inTransaction() );
179
180
        $this->assertEquals( 1,
181
            $instance->exec( 'INSERT INTO risoluto_db_test(id, column1, column2) values ("10", "TEST_A", "TEST_B");' ) );
182
        $this->assertEquals( 2, $this->getconnection()->getRowCount( 'risoluto_db_test' ) );
183
184
        $this->assertEquals( 10, $instance->lastInsertId() );
185
        $this->assertEquals( 10, $instance->lastInsertId( 'id' ) );
186
187
        $this->assertTrue( $instance->commit() );
188
        $this->assertFalse( $instance->inTransaction() );
189
190
        $this->assertEquals( 3, $this->getconnection()->getRowCount( 'risoluto_db_test' ) );
191
192
193
        // Rollback pattern
194
        $before_val = $this->getconnection()->createQueryTable( 'risoluto_db_test',
195
            'SELECT id, column1, column2 FROM risoluto_db_test WHERE id="10";' );
196
        $this->assertTrue( $instance->beginTransaction() );
197
        $this->assertTrue( $instance->inTransaction() );
198
199
        $this->assertEquals( 1,
200
            $instance->exec( 'UPDATE risoluto_db_test SET column1="TEST_C", column2="TEST_C" WHERE id="10";' ) );
201
        $this->assertEquals( 3, $this->getconnection()->getRowCount( 'risoluto_db_test' ) );
202
203
        $this->assertEquals( 1, $instance->exec( 'DELETE FROM risoluto_db_test WHERE id="10";' ) );
204
        $this->assertEquals( 3, $this->getconnection()->getRowCount( 'risoluto_db_test' ) );
205
206
        $this->assertTrue( $instance->rollBack() );
207
        $this->assertFalse( $instance->inTransaction() );
208
209
        $after_val = $this->getconnection()->createQueryTable( 'risoluto_db_test',
210
            'SELECT id, column1, column2 FROM risoluto_db_test WHERE id="10";' );
211
        $this->assertEquals( 3, $this->getconnection()->getRowCount( 'risoluto_db_test' ) );
212
        $this->assertTablesEqual( $before_val, $after_val );
213
214
        // Cleaning
215
        $this->assertEquals( 1, $instance->exec( 'DELETE FROM risoluto_db_test WHERE id="10";' ) );
216
        $this->assertEquals( 2, $this->getconnection()->getRowCount( 'risoluto_db_test' ) );
217
218
        $instance->disConnect();
219
        unset( $instance );
220
    }
221
}