1--TEST-- 2MySQL PDO->commit() 3--EXTENSIONS-- 4pdo_mysql 5--SKIPIF-- 6<?php 7require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); 8MySQLPDOTest::skip(); 9$db = MySQLPDOTest::factory(); 10if (false == MySQLPDOTest::detect_transactional_mysql_engine($db)) 11 die("skip Transactional engine not found"); 12?> 13--FILE-- 14<?php 15 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc'); 16 $db = MySQLPDOTest::factory(); 17 MySQLPDOTest::createTestTable($db, MySQLPDOTest::detect_transactional_mysql_engine($db)); 18 19 try { 20 if (true !== ($tmp = $db->beginTransaction())) { 21 printf("[001] Expecting true, got %s/%s\n", gettype($tmp), $tmp); 22 } 23 24 // DDL will issue an implicit commit 25 $db->exec(sprintf('DROP TABLE IF EXISTS test_commit')); 26 $db->exec(sprintf('CREATE TABLE test_commit(id INT) ENGINE=%s', MySQLPDOTest::detect_transactional_mysql_engine($db))); 27 try { 28 $db->commit(); 29 $failed = false; 30 } catch (PDOException $e) { 31 $failed = true; 32 } 33 if (!$failed) { 34 printf("[002] Commit should have failed\n"); 35 } 36 37 // pdo_transaction_transitions should check this as well... 38 // ... just to be sure the most basic stuff really works we check it again... 39 if (1 !== ($tmp = $db->getAttribute(PDO::ATTR_AUTOCOMMIT))) 40 printf("[003] According to the manual we should be back to autocommit mode, got %s/%s\n", 41 gettype($tmp), var_export($tmp, true)); 42 43 if (true !== ($tmp = $db->beginTransaction())) 44 printf("[004] Expecting true, got %s/%s\n", gettype($tmp), $tmp); 45 46 $db->exec("INSERT INTO test(id, label) VALUES (100, 'z')"); 47 48 if (true !== ($tmp = $db->commit())) 49 printf("[005] No commit allowed? [%s] %s\n", 50 $db->errorCode(), implode(' ', $db->errorInfo())); 51 52 // a weak test without unicode etc. - lets leave that to dedicated tests 53 $stmt = $db->query('SELECT id, label FROM test WHERE id = 100'); 54 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 55 if (!isset($rows[0]['label']) || ($rows[0]['label'] != 'z')) { 56 printf("[006] Record data is strange, dumping rows\n"); 57 var_dump($rows); 58 } 59 60 // Ok, lets check MyISAM resp. any other non-transactional engine 61 // pdo_mysql_begin_transaction has more on this, quick check only 62 $db = MySQLPDOTest::factory(); 63 MySQLPDOTest::createTestTable($db, 'MyISAM'); 64 65 if (true !== ($tmp = $db->beginTransaction())) 66 printf("[007] Expecting true, got %s/%s\n", gettype($tmp), $tmp); 67 68 $db->exec("INSERT INTO test(id, label) VALUES (100, 'z')"); 69 if (true !== ($tmp = $db->commit())) 70 printf("[008] No commit allowed? [%s] %s\n", 71 $db->errorCode(), implode(' ', $db->errorInfo())); 72 73 // a weak test without unicode etc. - lets leave that to dedicated tests 74 $stmt = $db->query('SELECT id, label FROM test WHERE id = 100'); 75 $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 76 if (!isset($rows[0]['label']) || ($rows[0]['label'] != 'z')) { 77 printf("[009] Record data is strange, dumping rows\n"); 78 var_dump($rows); 79 } 80 81 } catch (PDOException $e) { 82 printf("[002] %s, [%s] %s\n", 83 $e->getMessage(), 84 $db->errorCode(), implode(' ', $db->errorInfo())); 85 } 86 87 print "done!"; 88--CLEAN-- 89<?php 90require __DIR__ . '/mysql_pdo_test.inc'; 91$db = MySQLPDOTest::factory(); 92$db->exec('DROP TABLE IF EXISTS test_commit'); 93MySQLPDOTest::dropTestTable($db); 94?> 95--EXPECT-- 96done! 97