1--TEST--
2MySQL PDO->commit()
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once __DIR__ . '/inc/mysql_pdo_test.inc';
8MySQLPDOTest::skip();
9MySQLPDOTest::skipNotTransactionalEngine();
10--FILE--
11<?php
12    require_once __DIR__ . '/inc/mysql_pdo_test.inc';
13    $db = MySQLPDOTest::factory();
14
15    $table = 'pdo_mysql_commit';
16    MySQLPDOTest::createTestTable($table, $db, MySQLPDOTest::detect_transactional_mysql_engine($db));
17
18    $table2 = 'pdo_mysql_commit_2';
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('CREATE TABLE %s (id INT) ENGINE=%s', $table2, 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 (true !== ($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 {$table} (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 {$table} 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($table, $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 {$table} (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 {$table} 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?>
88--CLEAN--
89<?php
90require_once __DIR__ . '/inc/mysql_pdo_test.inc';
91$db = MySQLPDOTest::factory();
92$db->exec('DROP TABLE IF EXISTS pdo_mysql_commit');
93$db->exec('DROP TABLE IF EXISTS pdo_mysql_commit_2');
94?>
95--EXPECT--
96done!
97