1--TEST--
2mysqli_begin_transaction()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8
9require_once('connect.inc');
10if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
11	die(sprintf("skip Cannot connect, [%d] %s", mysqli_connect_errno(), mysqli_connect_error()));
12
13if (!have_innodb($link))
14	die(sprintf("skip Needs InnoDB support, [%d] %s", $link->errno, $link->error));
15?>
16--FILE--
17<?php
18	require_once("connect.inc");
19	/* {{{ proto bool mysqli_begin_transaction(object link, [int flags [, string name]]) */
20	$tmp    = NULL;
21	$link   = NULL;
22
23	if (!is_null($tmp = @mysqli_begin_transaction()))
24		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
25
26	if (!is_null($tmp = @mysqli_begin_transaction($link)))
27		printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
28
29	if (!is_null($tmp = @mysqli_begin_transaction($link, $link)))
30		printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
31
32	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
33		printf("[004] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
34			$host, $user, $db, $port, $socket);
35
36	if (!is_null($tmp = @mysqli_begin_transaction($link, $link)))
37		printf("[005] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
38
39	if (!is_null($tmp = @mysqli_begin_transaction($link, 0, $link)))
40		printf("[006] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
41
42	if (!is_null($tmp = @mysqli_begin_transaction($link, 0, "mytrx", $link)))
43		printf("[007] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
44
45	if (!mysqli_query($link, 'DROP TABLE IF EXISTS test'))
46		printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
47
48	if (!mysqli_query($link, 'CREATE TABLE test(id INT) ENGINE = InnoDB'))
49		printf("[009] Cannot create test table, [%d] %s\n", mysqli_errno($link), mysqli_error($link));
50
51	if (true !== ($tmp = mysqli_autocommit($link, true)))
52		printf("[010] Cannot turn on autocommit, expecting true, got %s/%s\n", gettype($tmp), $tmp);
53
54	/* overrule autocommit */
55	if (true !== ($tmp = mysqli_begin_transaction($link)))
56		printf("[011] Got %s - [%d] %s\n", var_dump($tmp, true), mysqli_errno($link), mysqli_error($link));
57
58	if (!mysqli_query($link, 'INSERT INTO test(id) VALUES (1)'))
59		printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
60
61	$tmp = mysqli_rollback($link);
62	if ($tmp !== true)
63		printf("[013] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
64
65	/* empty */
66	$res = mysqli_query($link, "SELECT * FROM test");
67	var_dump($res->fetch_assoc());
68
69	/* valid flags */
70	$flags = array(MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT);
71
72	if (mysqli_get_server_version($link) >= 50605) {
73		$flags[] = MYSQLI_TRANS_START_READ_WRITE;
74		$flags[] = MYSQLI_TRANS_START_READ_ONLY;
75	}
76
77	/* just coverage */
78	foreach ($flags as $flag) {
79		if (!mysqli_begin_transaction($link, $flag, sprintf("flag %d", $flag))) {
80			printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
81		}
82		if (!mysqli_query($link, 'SELECT * FROM test') || !mysqli_rollback($link)) {
83			printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
84		}
85	}
86
87	/* does it really set a flag? */
88	if (mysqli_get_server_version($link) >= 50605) {
89		if (!mysqli_begin_transaction($link, MYSQLI_TRANS_START_READ_ONLY, sprintf("flag %d", $flag))) {
90			printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
91		}
92		if (mysqli_query($link, "INSERT INTO test(id) VALUES (2)")) {
93			printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
94		} else if (!mysqli_commit($link)) {
95			printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
96		} else {
97			$res = mysqli_query($link, "SELECT id FROM test WHERE id = 2");
98		}
99	}
100
101	if (!mysqli_begin_transaction($link, -1)) {
102			printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
103	}
104
105	if (mysqli_get_server_version($link) >= 50605) {
106		/* does it like stupid names? */
107		if (@!$link->begin_transaction(MYSQLI_TRANS_START_READ_WRITE, "*/trick me?\n\0"))
108				printf("[020] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
109
110		/* does it like stupid names? */
111		if (@!$link->begin_transaction(MYSQLI_TRANS_START_READ_WRITE, "az09"))
112			printf("[021] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
113	}
114
115	print "done!";
116?>
117--CLEAN--
118<?php
119	require_once("clean_table.inc");
120?>
121--EXPECTF--
122NULL
123
124Warning: mysqli_begin_transaction(): Invalid value for parameter flags (-1) in %s on line %d
125[019] [%d]%A
126done!
127