1--TEST--
2mysqli_begin_transaction()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once __DIR__ . '/test_setup/test_helpers.inc';;
8$link = mysqli_connect_or_skip();
9if (!have_innodb($link))
10    die(sprintf("skip Needs InnoDB support, [%d] %s", $link->errno, $link->error));
11?>
12--FILE--
13<?php
14    require_once 'connect.inc';
15    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
16        printf("[004] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
17            $host, $user, $db, $port, $socket);
18
19    if (!mysqli_query($link, 'DROP TABLE IF EXISTS test'))
20        printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
21
22    if (!mysqli_query($link, 'CREATE TABLE test(id INT) ENGINE = InnoDB'))
23        printf("[009] Cannot create test table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
24
25    if (true !== ($tmp = mysqli_autocommit($link, true)))
26        printf("[010] Cannot turn on autocommit, expecting true, got %s/%s\n", gettype($tmp), $tmp);
27
28    /* overrule autocommit */
29    if (true !== ($tmp = mysqli_begin_transaction($link)))
30        printf("[011] Got %s - [%d] %s\n", $tmp, mysqli_errno($link), mysqli_error($link));
31
32    if (!mysqli_query($link, 'INSERT INTO test(id) VALUES (1)'))
33        printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
34
35    $tmp = mysqli_rollback($link);
36    if ($tmp !== true)
37        printf("[013] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
38
39    /* empty */
40    $res = mysqli_query($link, "SELECT * FROM test");
41    var_dump($res->fetch_assoc());
42
43    /* valid flags */
44    $flags = array(MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT);
45
46    if (mysqli_get_server_version($link) >= 50605) {
47        $flags[] = MYSQLI_TRANS_START_READ_WRITE;
48        $flags[] = MYSQLI_TRANS_START_READ_ONLY;
49    }
50
51    /* just coverage */
52    foreach ($flags as $flag) {
53        if (!mysqli_begin_transaction($link, $flag, sprintf("flag %d", $flag))) {
54            printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
55        }
56        if (!mysqli_query($link, 'SELECT * FROM test') || !mysqli_rollback($link)) {
57            printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
58        }
59    }
60
61    /* does it really set a flag? */
62    if (mysqli_get_server_version($link) >= 50605) {
63        if (!mysqli_begin_transaction($link, MYSQLI_TRANS_START_READ_ONLY, sprintf("flag %d", $flag))) {
64            printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
65        }
66        if (mysqli_query($link, "INSERT INTO test(id) VALUES (2)")) {
67            printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
68        } else if (!mysqli_commit($link)) {
69            printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
70        } else {
71            mysqli_query($link, "SELECT id FROM test WHERE id = 2");
72        }
73    }
74
75    try {
76        mysqli_begin_transaction($link, -1);
77        printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
78    } catch (\ValueError $e) {
79        echo $e->getMessage() . \PHP_EOL;
80    }
81
82    if (mysqli_get_server_version($link) >= 50605) {
83        /* does it like stupid names? */
84        if (@!$link->begin_transaction(MYSQLI_TRANS_START_READ_WRITE, "*/trick me?\n\0"))
85                printf("[020] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
86
87        /* does it like stupid names? */
88        if (@!$link->begin_transaction(MYSQLI_TRANS_START_READ_WRITE, "az09"))
89            printf("[021] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
90    }
91
92    print "done!";
93?>
94--CLEAN--
95<?php
96    require_once 'clean_table.inc';
97?>
98--EXPECT--
99NULL
100mysqli_begin_transaction(): Argument #2 ($flags) must be one of the MYSQLI_TRANS_* constants
101done!
102