1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DbTest4setAttribute |
4
|
|
|
* |
5
|
|
|
* Db::setAttribute()用テストケース |
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 DbTest4setAttribute 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_setAttribute_EmptyBoth() |
99
|
|
|
* |
100
|
|
|
* setAttribute()のテスト(引数両方未指定時) |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function test_setAttribute_EmptyBoth() |
|
|
|
|
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
|
|
|
$tmp_result = $instance->setAttribute( '', '' ); |
116
|
|
|
|
117
|
|
|
$this->assertFalse( $tmp_result ); |
118
|
|
|
|
119
|
|
|
$instance->disconnect(); |
120
|
|
|
unset( $instance ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* test_setAttribute_Empty1st() |
125
|
|
|
* |
126
|
|
|
* setAttribute()のテスト(第1引数未指定時) |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
public function test_setAttribute_Empty1st() |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$params = [ |
131
|
|
|
"driver" => $GLOBALS[ 'DB_DRIVER' ], |
132
|
|
|
"user" => $GLOBALS[ 'DB_USER' ], |
133
|
|
|
"pass" => $GLOBALS[ 'DB_PASSWORD' ], |
134
|
|
|
"dbname" => $GLOBALS[ 'DB_DBNAME' ], |
135
|
|
|
"host" => $GLOBALS[ 'DB_HOST' ], |
136
|
|
|
"persistent" => false, |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
$instance = new Db; |
140
|
|
|
$instance->connect( $params ); |
141
|
|
|
$tmp_result = $instance->setAttribute( '', 'BAR' ); |
142
|
|
|
|
143
|
|
|
$this->assertFalse( $tmp_result ); |
144
|
|
|
|
145
|
|
|
$instance->disconnect(); |
146
|
|
|
unset( $instance ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* test_setAttribute_Empty2nd() |
151
|
|
|
* |
152
|
|
|
* setAttribute()のテスト(第2引数未指定時) |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public function test_setAttribute_Empty2nd() |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$params = [ |
157
|
|
|
"driver" => $GLOBALS[ 'DB_DRIVER' ], |
158
|
|
|
"user" => $GLOBALS[ 'DB_USER' ], |
159
|
|
|
"pass" => $GLOBALS[ 'DB_PASSWORD' ], |
160
|
|
|
"dbname" => $GLOBALS[ 'DB_DBNAME' ], |
161
|
|
|
"host" => $GLOBALS[ 'DB_HOST' ], |
162
|
|
|
"persistent" => false, |
163
|
|
|
]; |
164
|
|
|
|
165
|
|
|
$instance = new Db; |
166
|
|
|
$instance->connect( $params ); |
167
|
|
|
$tmp_result = $instance->setAttribute( 'FOO', '' ); |
168
|
|
|
|
169
|
|
|
$this->assertFalse( $tmp_result ); |
170
|
|
|
|
171
|
|
|
$instance->disconnect(); |
172
|
|
|
unset( $instance ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* test_setAttribute_withArgs() |
177
|
|
|
* |
178
|
|
|
* setAttribute()のテスト(引数指定時) |
179
|
|
|
*/ |
180
|
|
|
public function test_setAttribute_withArgs() |
181
|
|
|
{ |
182
|
|
|
$params = [ |
183
|
|
|
"driver" => $GLOBALS[ 'DB_DRIVER' ], |
184
|
|
|
"user" => $GLOBALS[ 'DB_USER' ], |
185
|
|
|
"pass" => $GLOBALS[ 'DB_PASSWORD' ], |
186
|
|
|
"dbname" => $GLOBALS[ 'DB_DBNAME' ], |
187
|
|
|
"host" => $GLOBALS[ 'DB_HOST' ], |
188
|
|
|
"persistent" => false, |
189
|
|
|
]; |
190
|
|
|
|
191
|
|
|
$instance = new Db; |
192
|
|
|
$instance->connect( $params ); |
193
|
|
|
$before_val = $instance->getAttribute( 'FETCH_MODE' ); |
194
|
|
|
$tmp_result = $instance->setAttribute( \PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ ); |
195
|
|
|
$after_val = $instance->getAttribute( 'FETCH_MODE' ); |
196
|
|
|
|
197
|
|
|
$this->assertTrue( $tmp_result ); |
198
|
|
|
$this->assertNotEquals( $before_val, $after_val ); |
199
|
|
|
$this->assertNotEquals( \PDO::FETCH_OBJ, $after_val ); |
200
|
|
|
|
201
|
|
|
$instance->disconnect(); |
202
|
|
|
unset( $instance ); |
203
|
|
|
} |
204
|
|
|
} |
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.