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