@@ -295,6 +295,105 @@ public function testSelecting()
295295 }
296296 $ this ->assertEquals (0 , $ this ->object ->query ('DROP TABLE unit_test ' ));
297297 }
298+
299+ /**
300+ * @covers ezsql\Database\ez_pdo::commit
301+ * @covers ezsql\Database\ez_pdo::beginTransaction
302+ * @covers ezsql\Database\ez_pdo::query
303+ * @covers ezsql\Database\ez_pdo::processQuery
304+ * @covers ezsql\Database\ez_pdo::processResult
305+ * @covers ezsql\Database\ez_pdo::prepareValues
306+ * @covers ezsql\Database\ez_pdo::query_prepared
307+ * @covers \select
308+ */
309+ public function testBeginTransactionCommit ()
310+ {
311+ $ this ->object ->connect ();
312+ $ this ->object ->query ('CREATE TABLE unit_test(id integer, test_key varchar(50), PRIMARY KEY (ID)) ' );
313+
314+ $ commit = null ;
315+ try {
316+ $ commit = true ;
317+ $ this ->object ->beginTransaction ();
318+ $ this ->object ->insert ('unit_test ' , array ('id ' =>'1 ' , 'test_key ' =>'testing 1 ' ));
319+ $ this ->object ->insert ('unit_test ' , array ('id ' =>'2 ' , 'test_key ' =>'testing 2 ' ));
320+ $ this ->object ->insert ('unit_test ' , array ('id ' =>'3 ' , 'test_key ' =>'testing 3 ' ));
321+ $ this ->object ->commit ();
322+ } catch (\PDOException $ ex ) {
323+ $ this ->object ->rollback ();
324+ $ this ->fail ("Error! This message shouldn't have been displayed. " );
325+ }
326+
327+ if ($ commit ) {
328+ $ result = $ this ->object ->selecting ('unit_test ' );
329+ $ i = 1 ;
330+ foreach ($ result as $ row ) {
331+ $ this ->assertEquals ($ i , $ row ->id );
332+ $ this ->assertEquals ('testing ' . $ i , $ row ->test_key );
333+ ++$ i ;
334+ }
335+
336+ $ where = array ('test_key ' , '= ' , 'testing 2 ' );
337+ $ result = select ('unit_test ' , 'id ' , $ where );
338+ foreach ($ result as $ row ) {
339+ $ this ->assertEquals (2 , $ row ->id );
340+ }
341+
342+ $ result = $ this ->object ->selecting ('unit_test ' , 'test_key ' , array ( 'id ' , '= ' , '3 ' ));
343+ foreach ($ result as $ row ) {
344+ $ this ->assertEquals ('testing 3 ' , $ row ->test_key );
345+ }
346+
347+ $ result = $ this ->object ->selecting ('unit_test ' , array ('test_key ' ), eq ('id ' , 1 ));
348+ foreach ($ result as $ row ) {
349+ $ this ->assertEquals ('testing 1 ' , $ row ->test_key );
350+ }
351+
352+ $ this ->assertEquals (0 , $ this ->object ->query ('DROP TABLE unit_test ' ));
353+ }
354+ }
355+
356+ /**
357+ * @covers ezsql\Database\ez_pdo::rollback
358+ * @covers ezsql\Database\ez_pdo::beginTransaction
359+ * @covers ezsql\Database\ez_pdo::query
360+ * @covers ezsql\Database\ez_pdo::processQuery
361+ * @covers ezsql\Database\ez_pdo::processResult
362+ * @covers ezsql\Database\ez_pdo::prepareValues
363+ * @covers ezsql\Database\ez_pdo::query_prepared
364+ * @covers \select
365+ */
366+ public function testBeginTransactionRollback ()
367+ {
368+ $ this ->object ->connect ();
369+ $ this ->object ->query ('CREATE TABLE unit_test(id integer, test_key varchar(50), PRIMARY KEY (ID)) ' );
370+
371+ $ commit = null ;
372+ try {
373+ $ commit = true ;
374+ $ this ->object ->beginTransaction ();
375+ $ this ->object ->insert ('unit_test ' , array ( 0 => 3 , 'test_key ' =>'testing 3 ' ));
376+ $ this ->object ->commit ();
377+ } catch (\PDOException $ ex ) {
378+ $ commit = false ;
379+ $ this ->object ->rollback ();
380+ }
381+
382+ if ($ commit ) {
383+ //echo ("Error! This message shouldn't have been displayed.");
384+ $ result = $ this ->object ->selecting ('unit_test ' );
385+ $ i = 1 ;
386+ foreach ($ result as $ row ) {
387+ $ this ->assertEquals ('should be seen ' . $ i , $ row ->test_key );
388+ ++$ i ;
389+ }
390+ $ this ->assertEquals (0 , $ this ->object ->query ('DROP TABLE unit_test ' ));
391+ } else {
392+ $ result = $ this ->object ->selecting ('unit_test ' );
393+ $ this ->assertEquals (0 , $ result );
394+ $ this ->assertEquals (0 , $ this ->object ->query ('DROP TABLE unit_test ' ));
395+ }
396+ }
298397
299398 /**
300399 * @covers ezsql\Database\ez_pdo::disconnect
0 commit comments