1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DbTest4DoQuery |
4
|
|
|
* |
5
|
|
|
* Db::doQuery()用テストケース |
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 DbTest4DoQuery 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() |
|
|
|
|
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() |
|
|
|
|
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_DoQuery_NoArgs() |
99
|
|
|
* |
100
|
|
|
* doQuery()のテスト(引数なし) |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function test_DoQuery_NoArgs() |
|
|
|
|
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->doQuery() ); |
117
|
|
|
|
118
|
|
|
$instance->disConnect(); |
119
|
|
|
unset( $instance ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* test_DoQuery_WithSql() |
124
|
|
|
* |
125
|
|
|
* doQuery()のテスト(SQLを指定) |
126
|
|
|
*/ |
127
|
|
|
public function test_DoQuery_WithSql() |
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
|
|
|
$sql = 'SELECT id, column1, column2 FROM risoluto_db_test;'; |
139
|
|
|
|
140
|
|
|
$want = [ |
141
|
|
|
0 => [ 'id' => '1', 'column1' => 'id1:column1', 'column2' => 'id1:column2' ], |
142
|
|
|
1 => [ 'id' => '2', 'column1' => null, 'column2' => null ], |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
$instance = new Db; |
146
|
|
|
$instance->connect( $params ); |
147
|
|
|
|
148
|
|
|
$tmp_result = $instance->doQuery( $sql ); |
149
|
|
|
$this->assertEquals( $want, $tmp_result ); |
150
|
|
|
|
151
|
|
|
$instance->disConnect(); |
152
|
|
|
unset( $instance ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* test_DoQuery_WithSqlAndParam() |
158
|
|
|
* |
159
|
|
|
* doQuery()のテスト(SQLを指定) |
160
|
|
|
*/ |
161
|
|
|
public function test_DoQuery_WithSqlAndParam() |
162
|
|
|
{ |
163
|
|
|
$params = [ |
164
|
|
|
"driver" => $GLOBALS[ 'DB_DRIVER' ], |
165
|
|
|
"user" => $GLOBALS[ 'DB_USER' ], |
166
|
|
|
"pass" => $GLOBALS[ 'DB_PASSWORD' ], |
167
|
|
|
"dbname" => $GLOBALS[ 'DB_DBNAME' ], |
168
|
|
|
"host" => $GLOBALS[ 'DB_HOST' ], |
169
|
|
|
"persistent" => false, |
170
|
|
|
]; |
171
|
|
|
|
172
|
|
|
$sql = 'SELECT id, column1, column2 FROM risoluto_db_test WHERE id = :id;'; |
173
|
|
|
$param1 = [ |
174
|
|
|
[ 'id' => ':id', 'value' => 1, 'type' => \PDO::PARAM_INT ] |
175
|
|
|
]; |
176
|
|
|
$param2 = [ |
177
|
|
|
[ 'id' => ':id', 'value' => 2, 'type' => \PDO::PARAM_INT, 'length' => 1 ] |
178
|
|
|
]; |
179
|
|
|
|
180
|
|
|
$want1 = [ |
181
|
|
|
0 => [ 'id' => '1', 'column1' => 'id1:column1', 'column2' => 'id1:column2' ] |
182
|
|
|
]; |
183
|
|
|
$want2 = [ |
184
|
|
|
0 => [ 'id' => '2', 'column1' => null, 'column2' => null ] |
185
|
|
|
]; |
186
|
|
|
$want3 = [ |
187
|
|
|
0 => [ '0' => '2', '1' => null, '2' => null, 'id' => '2', 'column1' => null, 'column2' => null ] |
188
|
|
|
]; |
189
|
|
|
|
190
|
|
|
$instance = new Db; |
191
|
|
|
$instance->connect( $params ); |
192
|
|
|
|
193
|
|
|
// Begin, $sql and $param sets. |
194
|
|
|
$tmp_result = $instance->doQuery( $sql, $param1 ); |
195
|
|
|
$this->assertEquals( $want1, $tmp_result ); |
196
|
|
|
|
197
|
|
|
// Next, only $param sets. |
198
|
|
|
$tmp_result = $instance->doQuery( '', $param2 ); |
199
|
|
|
$this->assertEquals( $want2, $tmp_result ); |
200
|
|
|
|
201
|
|
|
// Final, $param and fetch_style set. |
202
|
|
|
$tmp_result = $instance->doQuery( '', $param2, [ ], \PDO::FETCH_BOTH ); |
203
|
|
|
$this->assertEquals( $want3, $tmp_result ); |
204
|
|
|
|
205
|
|
|
$instance->disConnect(); |
206
|
|
|
unset( $instance ); |
207
|
|
|
} |
208
|
|
|
} |
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.