1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DbTest4Connect |
4
|
|
|
* |
5
|
|
|
* Db::connect()用テストケース |
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 DbTest4Connect 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_Connect_Success1() |
99
|
|
|
* |
100
|
|
|
* connect()成功時のテスト(DSN未指定時) |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function test_Connect_Success1() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$params = [ |
105
|
|
|
'dsn' => '', |
106
|
|
|
'driver' => $GLOBALS[ 'DB_DRIVER' ], |
107
|
|
|
'user' => $GLOBALS[ 'DB_USER' ], |
108
|
|
|
'pass' => $GLOBALS[ 'DB_PASSWORD' ], |
109
|
|
|
'dbname' => $GLOBALS[ 'DB_DBNAME' ], |
110
|
|
|
'host' => $GLOBALS[ 'DB_HOST' ], |
111
|
|
|
'persistent' => false, |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
$instance = new Db; |
115
|
|
|
$this->assertTrue( $instance->connect( $params ) ); |
116
|
|
|
unset( $instance ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* test_Connect_Success2() |
121
|
|
|
* |
122
|
|
|
* connect()成功時のテスト(DSN指定時) |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function test_Connect_Success2() |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$params = [ |
127
|
|
|
'dsn' => $GLOBALS[ 'DB_DSN' ], |
128
|
|
|
'driver' => $GLOBALS[ 'DB_DRIVER' ], |
129
|
|
|
'user' => $GLOBALS[ 'DB_USER' ], |
130
|
|
|
'pass' => $GLOBALS[ 'DB_PASSWORD' ], |
131
|
|
|
'dbname' => $GLOBALS[ 'DB_DBNAME' ], |
132
|
|
|
'host' => $GLOBALS[ 'DB_HOST' ], |
133
|
|
|
'persistent' => false, |
134
|
|
|
]; |
135
|
|
|
|
136
|
|
|
$instance = new Db; |
137
|
|
|
$this->assertTrue( $instance->connect( $params ) ); |
138
|
|
|
unset( $instance ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* test_Connect_Success3() |
143
|
|
|
* |
144
|
|
|
* connect()成功時のテスト(DSN未指定、オプション指定) |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function test_Connect_Success3() |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$params = [ |
149
|
|
|
'dsn' => '', |
150
|
|
|
'driver' => $GLOBALS[ 'DB_DRIVER' ], |
151
|
|
|
'user' => $GLOBALS[ 'DB_USER' ], |
152
|
|
|
'pass' => $GLOBALS[ 'DB_PASSWORD' ], |
153
|
|
|
'dbname' => $GLOBALS[ 'DB_DBNAME' ], |
154
|
|
|
'host' => $GLOBALS[ 'DB_HOST' ], |
155
|
|
|
'persistent' => false, |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
$instance = new Db; |
159
|
|
|
$this->assertTrue( $instance->connect( $params, [ \PDO::ATTR_PERSISTENT => false ] ) ); |
160
|
|
|
unset( $instance ); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* test_Connect_Failure() |
165
|
|
|
* |
166
|
|
|
* connect()失敗時のテスト |
167
|
|
|
*/ |
168
|
|
|
public function test_Connect_Failure() |
169
|
|
|
{ |
170
|
|
|
$params = [ |
171
|
|
|
'dsn' => '', |
172
|
|
|
'driver' => '', |
173
|
|
|
'user' => '', |
174
|
|
|
'pass' => '', |
175
|
|
|
'dbname' => '', |
176
|
|
|
'host' => '', |
177
|
|
|
'persistent' => false, |
178
|
|
|
]; |
179
|
|
|
|
180
|
|
|
$instance = new Db; |
181
|
|
|
$this->assertFalse( $instance->connect( $params ) ); |
182
|
|
|
unset( $instance ); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
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.