pdo_mysql_commit.phpt 3.2 KB

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