@@ -420,6 +420,93 @@ public function testSelecting()
420420 }
421421 }
422422
423+ /**
424+ * @covers ezsql\Database\ez_mysqli::commit
425+ * @covers ezsql\Database\ez_mysqli::beginTransaction
426+ * @covers ezsql\Database\ez_mysqli::query
427+ * @covers ezsql\Database\ez_mysqli::processQueryResult
428+ */
429+ public function testBeginTransactionCommit ()
430+ {
431+ $ this ->object ->connect ();
432+ $ this ->object ->query ('CREATE TABLE unit_test(id int(11) NOT NULL AUTO_INCREMENT, test_key varchar(50), PRIMARY KEY (ID))ENGINE=MyISAM DEFAULT CHARSET=utf8 ' );
433+ $ this ->object ->query ('"ALTER TABLE unit_test Type=InnoDB" ' );
434+
435+ $ commit = null ;
436+ try {
437+ $ commit = true ;
438+ $ this ->object ->beginTransaction ();
439+ $ this ->object ->insert ('unit_test ' , array ('id ' =>'1 ' , 'test_key ' =>'testing 1 ' ));
440+ $ this ->object ->insert ('unit_test ' , array ('id ' =>'2 ' , 'test_key ' =>'testing 2 ' ));
441+ $ this ->object ->insert ('unit_test ' , array ('id ' =>'3 ' , 'test_key ' =>'testing 3 ' ));
442+ $ this ->object ->commit ();
443+ } catch (\Exception $ ex ) {
444+ $ commit = false ;
445+ $ this ->object ->rollback ();
446+ echo ("Error! This rollback message shouldn't have been displayed: " ).$ ex ->getMessage ();
447+ }
448+
449+ if ($ commit ) {
450+ $ result = $ this ->object ->selecting ('unit_test ' );
451+ $ i = 1 ;
452+ foreach ($ result as $ row ) {
453+ $ this ->assertEquals ($ i , $ row ->id );
454+ $ this ->assertEquals ('testing ' . $ i , $ row ->test_key );
455+ ++$ i ;
456+ }
457+
458+ $ this ->assertEquals (0 , $ this ->object ->drop ('unit_test ' ));
459+ }
460+ }
461+
462+ /**
463+ * @covers ezsql\Database\ez_mysqli::rollback
464+ * @covers ezsql\Database\ez_mysqli::beginTransaction
465+ * @covers ezsql\Database\ez_mysqli::query
466+ * @covers ezsql\Database\ez_mysqli::processQueryResult
467+ */
468+ public function testBeginTransactionRollback ()
469+ {
470+ $ this ->object ->connect ();
471+ $ this ->object ->query ('CREATE TABLE unit_test(id int(11) NOT NULL AUTO_INCREMENT, test_key varchar(50), PRIMARY KEY (ID) ' );
472+ $ this ->object ->query ('"ALTER TABLE unit_test Type=InnoDB" ' );
473+
474+ $ commit = null ;
475+ try {
476+ $ commit = true ;
477+ $ this ->object ->beginTransaction ();
478+ $ this ->object ->insert ('unit_test ' , array ('id ' =>'1 ' , 'test_key ' =>'testing 1 ' ));
479+ $ this ->object ->insert ('unit_test ' , array ('id ' =>'2 ' , 'test_key ' =>'testing 2 ' ));
480+ $ this ->object ->insert ('unit_test ' , array ('idx ' => 1 , 'test_key2 ' =>'testing 1 ' ));
481+ $ this ->object ->commit ();
482+ } catch (\Exception $ ex ) {
483+ $ commit = false ;
484+ $ this ->object ->rollback ();
485+ }
486+
487+ if ($ commit ) {
488+ echo ("Error! This message shouldn't have been displayed. " );
489+ $ result = $ this ->object ->selecting ('unit_test ' );
490+ $ i = 1 ;
491+ foreach ($ result as $ row ) {
492+ $ this ->assertEquals ('should not be seen ' . $ i , $ row ->test_key );
493+ ++$ i ;
494+ }
495+ $ this ->object ->drop ('unit_test ' );
496+ } else {
497+ //echo ("Error! rollback.");
498+ $ result = $ this ->object ->selecting ('unit_test ' );
499+ $ i = 1 ;
500+ foreach ($ result as $ row ) {
501+ $ this ->assertEquals ('should not be seen ' . $ i , $ row ->test_key );
502+ ++$ i ;
503+ }
504+
505+ $ this ->assertEquals (0 , $ result );
506+ $ this ->object ->drop ('unit_test ' );
507+ }
508+ }
509+
423510 /**
424511 * @covers ezsql\Database\ez_mysqli::query
425512 * @covers ezsql\Database\ez_mysqli::processQueryResult
0 commit comments