@@ -17,49 +17,59 @@ class SpoonDatabaseTest extends TestCase
1717 public function setup ()
1818 {
1919 // create database object
20- $ this ->db = new SpoonDatabase ('mysql ' , 'localhost ' , 'spoon ' , 'spoon ' , 'spoon_tests ' );
20+ $ this ->db = new SpoonDatabase ('mysql ' , '127.0.0.1 ' , 'root ' , 'spoon ' , 'spoon_tests ' );
2121 }
2222
2323 public function testExecute ()
2424 {
2525 // create database
26- try { $ this -> db -> execute ( ' CREATE DATABASE IF NOT EXISTS spoon_tests ' ); }
27- catch ( SpoondatabaseException $ e )
28- {
26+ try {
27+ $ this -> db -> execute ( ' CREATE DATABASE IF NOT EXISTS spoon_tests ' );
28+ } catch ( SpoondatabaseException $ e ) {
2929 $ this ->fail ('You should manually create a database "spoon_tests" ' );
3030 }
3131
3232 // clear all tables
33- if (count ($ this ->db ->getTables ()) != 0 ) $ this ->db ->drop ($ this ->db ->getTables ());
33+ if (count ($ this ->db ->getTables ()) != 0 ) {
34+ $ this ->db ->drop ($ this ->db ->getTables ());
35+ }
3436
3537 // create table users
36- $ this ->db ->execute ("
38+ $ this ->db ->execute (
39+ "
3740 CREATE TABLE `users` (
3841 `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
3942 `username` VARCHAR( 255 ) NOT NULL ,
4043 `email` VARCHAR( 255 ) NOT NULL ,
4144 `developer` ENUM( 'Y', 'N' ) NOT NULL
42- ) ENGINE = MYISAM; " );
45+ ) ENGINE = MYISAM; "
46+ );
4347
4448 // create dummy table
45- $ this ->db ->execute ("
49+ $ this ->db ->execute (
50+ "
4651 CREATE TABLE `test` (
4752 `id` int(11) NOT NULL auto_increment,
4853 `value` varchar(255) NOT NULL,
4954 PRIMARY KEY (`id`)
50- ) ENGINE=MyISAM; " );
55+ ) ENGINE=MyISAM; "
56+ );
5157
5258 // create table with datetime
53- $ this ->db ->execute ("
59+ $ this ->db ->execute (
60+ "
5461 CREATE TABLE `date_test` (
5562 `id` int(11) NOT NULL auto_increment,
5663 `date` DATETIME NOT NULL,
5764 PRIMARY KEY (`id`)
58- ) ENGINE=MyISAM; " );
65+ ) ENGINE=MyISAM; "
66+ );
5967
6068 // do nothing
6169 $ this ->db ->execute ('SELECT * FROM users LIMIT ? ' , 10 );
6270 $ this ->db ->execute ('SELECT * FROM users limit :limit ' , array (':limit ' => 10 ));
71+ $ this ->expectException (PDOException::class);
72+ $ this ->db ->execute ('SELECT * FROM non_existing limit :limit ' , array (':limit ' => 10 ));
6373 }
6474
6575 /**
@@ -113,8 +123,13 @@ public function testInsert()
113123 $ this ->db ->insert ('users ' , $ aData );
114124
115125 // insert 1000 records
116- for ($ i = 0 ; $ i < 1000 ; $ i ++) $ array [$ i ] = $ aData ;
126+ for ($ i = 0 ; $ i < 1000 ; $ i ++) {
127+ $ array [$ i ] = $ aData ;
128+ }
117129 $ this ->db ->insert ('users ' , $ array );
130+ $ aData ['id ' ] = 1 ;
131+ $ this ->expectException (PDOException::class);
132+ $ this ->db ->insert ('users ' , $ aData );
118133 }
119134
120135 /**
@@ -127,14 +142,20 @@ public function testInsertDate()
127142 $ this ->db ->insert ('date_test ' , $ aData );
128143
129144 // multiple rows data
130- $ aData = array (
131- array (
145+ $ aData = [
146+ [
132147 'date ' => new DateTime (),
133- ) ,
134- array (
148+ ] ,
149+ [
135150 'date ' => new DateTime (),
136- ),
137- );
151+ ],
152+ ];
153+ $ this ->db ->insert ('date_test ' , $ aData );
154+ $ this ->expectException (PDOException::class);
155+ $ aData = [
156+ 'id ' => 1 ,
157+ 'date ' => new DateTime (),
158+ ];
138159 $ this ->db ->insert ('date_test ' , $ aData );
139160 }
140161
@@ -165,11 +186,17 @@ public function testGetVar()
165186 {
166187 $ this ->assertEquals ('1001 ' , $ this ->db ->getVar ('SELECT COUNT(id) FROM users ' ));
167188 $ this ->assertEquals ('1001 ' , $ this ->db ->getVar ('SELECT COUNT(id) FROM users WHERE id != ? ' , 1337 ));
168- $ this ->assertEquals ('1001 ' , $ this ->db ->getVar ('SELECT COUNT(id) FROM users WHERE id != :id ' , array (':id ' => 1337 )));
189+ $ this ->assertEquals (
190+ '1001 ' ,
191+ $ this ->db ->getVar ('SELECT COUNT(id) FROM users WHERE id != :id ' , array (':id ' => 1337 ))
192+ );
169193 $ this ->assertEquals ('1 ' , $ this ->db ->getVar ('SELECT id FROM users ORDER BY id ASC LIMIT 1 ' ));
170194 $ this ->assertEquals ('1 ' , $ this ->db ->getVar ('SELECT id FROM users ORDER BY id ASC LIMIT ? ' , 1 ));
171195 $ this ->assertEquals ('1 ' , $ this ->db ->getVar ('SELECT id FROM users ORDER BY id ASC LIMIT ? ' , array (1 )));
172- $ this ->assertEquals ('1 ' , $ this ->db ->getVar ('SELECT id FROM users ORDER BY id ASC LIMIT :limit ' , array (':limit ' => 1 )));
196+ $ this ->assertEquals (
197+ '1 ' ,
198+ $ this ->db ->getVar ('SELECT id FROM users ORDER BY id ASC LIMIT :limit ' , array (':limit ' => 1 ))
199+ );
173200 }
174201
175202 /**
@@ -178,10 +205,27 @@ public function testGetVar()
178205 public function testGetPairs ()
179206 {
180207 $ this ->assertEquals (10 , count ($ this ->db ->getPairs ('SELECT id, username FROM users LIMIT 10; ' )));
181- $ this ->assertEquals (10 , count ($ this ->db ->getPairs ('SELECT id, username FROM users WHERE id != ? LIMIT 10 ' , 1337 )));
182- $ this ->assertEquals (10 , count ($ this ->db ->getPairs ('SELECT id, username FROM users WHERE id != ? LIMIT ? ' , array (1337 , 10 ))));
183- $ this ->assertEquals (10 , count ($ this ->db ->getPairs ('SELECT id, username FROM users WHERE id != :id LIMIT 10 ' , array (':id ' => 1337 ))));
184- $ this ->assertEquals (10 , count ($ this ->db ->getPairs ('SELECT id, username FROM users WHERE id != :id LIMIT :limit ' , array (':id ' => 1337 , ':limit ' => 10 ))));
208+ $ this ->assertEquals (
209+ 10 ,
210+ count ($ this ->db ->getPairs ('SELECT id, username FROM users WHERE id != ? LIMIT 10 ' , 1337 ))
211+ );
212+ $ this ->assertEquals (
213+ 10 ,
214+ count ($ this ->db ->getPairs ('SELECT id, username FROM users WHERE id != ? LIMIT ? ' , array (1337 , 10 )))
215+ );
216+ $ this ->assertEquals (
217+ 10 ,
218+ count ($ this ->db ->getPairs ('SELECT id, username FROM users WHERE id != :id LIMIT 10 ' , array (':id ' => 1337 )))
219+ );
220+ $ this ->assertEquals (
221+ 10 ,
222+ count (
223+ $ this ->db ->getPairs (
224+ 'SELECT id, username FROM users WHERE id != :id LIMIT :limit ' ,
225+ array (':id ' => 1337 , ':limit ' => 10 )
226+ )
227+ )
228+ );
185229 }
186230
187231 /**
@@ -206,13 +250,23 @@ public function testUpdate()
206250 $ this ->assertEquals (0 , $ this ->db ->getNumRows ('SELECT id FROM users WHERE id = ? ' , 1337 ));
207251
208252 // update record
209- $ this ->db ->update ('users ' , array ('id ' => 1337 , 'username ' => 'Bauffman ' , 'email ' => 'erik@bauffman.be ' , 'developer ' => 'Y ' ), 'id = ? ' , 2 );
253+ $ this ->db ->update (
254+ 'users ' ,
255+ array ('id ' => 1337 , 'username ' => 'Bauffman ' , 'email ' => 'erik@bauffman.be ' , 'developer ' => 'Y ' ),
256+ 'id = ? ' ,
257+ 2
258+ );
210259
211260 // 1 record with id 1337
212261 $ this ->assertEquals (1 , $ this ->db ->getNumRows ('SELECT id FROM users WHERE id = ? ' , 1337 ));
213262
214263 // update record
215- $ this ->db ->update ('users ' , array ('id ' => 1337 ), 'id = :leet AND id != :bauffman ' , array (':leet ' => 1337 , ':bauffman ' => 291 ));
264+ $ this ->db ->update (
265+ 'users ' ,
266+ array ('id ' => 1337 ),
267+ 'id = :leet AND id != :bauffman ' ,
268+ array (':leet ' => 1337 , ':bauffman ' => 291 )
269+ );
216270 }
217271
218272 /**
@@ -222,16 +276,16 @@ public function testUpdateDate()
222276 {
223277 // data
224278 $ aData ['date ' ] = new DateTime ();
225- $ this ->db ->update ('date_test ' , $ aData );
279+ self :: assertTrue ( is_int ( $ this ->db ->update ('date_test ' , $ aData)) );
226280 }
227281
228282 /**
229283 * @depends testExecute
230284 */
231285 public function testOptimize ()
232286 {
233- $ this ->db ->optimize ('users ' );
234- $ this ->db ->optimize (array ('users ' ));
287+ self :: assertArraySubset ([], $ this ->db ->optimize ('users ' ) );
288+ self :: assertArraySubset ([], $ this ->db ->optimize (array ('users ' ) ));
235289 }
236290
237291 /**
@@ -261,8 +315,14 @@ public function testGetRecord()
261315 $ data ['email ' ] = 'erik@bauffman.be ' ;
262316 $ data ['developer ' ] = 'Y ' ;
263317
264- $ this ->assertEquals ($ data , $ this ->db ->getRecord ('SELECT username, email, developer FROM users WHERE id = ? ' , 1337 ));
265- $ this ->assertEquals ($ data , $ this ->db ->getRecord ('SELECT username, email, developer FROM users WHERE id = :id ' , array (':id ' => 1337 )));
318+ $ this ->assertEquals (
319+ $ data ,
320+ $ this ->db ->getRecord ('SELECT username, email, developer FROM users WHERE id = ? ' , 1337 )
321+ );
322+ $ this ->assertEquals (
323+ $ data ,
324+ $ this ->db ->getRecord ('SELECT username, email, developer FROM users WHERE id = :id ' , array (':id ' => 1337 ))
325+ );
266326 }
267327
268328 /**
@@ -271,7 +331,10 @@ public function testGetRecord()
271331 public function testGetRecords ()
272332 {
273333 $ this ->assertEquals (100 , count ($ this ->db ->getRecords ('SELECT * FROM users WHERE id != ? LIMIT 100 ' , 1337 )));
274- $ this ->assertEquals (100 , count ($ this ->db ->getRecords ('SELECT * FROM users WHERE id != :id LIMIT 100 ' , array (':id ' => 1337 ))));
334+ $ this ->assertEquals (
335+ 100 ,
336+ count ($ this ->db ->getRecords ('SELECT * FROM users WHERE id != :id LIMIT 100 ' , array (':id ' => 1337 )))
337+ );
275338 }
276339
277340 /**
0 commit comments