1--TEST--
2MySQL PDO->__construct(), options
3--SKIPIF--
4<?php
5require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
6require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7MySQLPDOTest::skip();
8?>
9--FILE--
10<?php
11	require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
12
13	function set_option_and_check($offset, $option, $value, $option_desc) {
14
15		$dsn = MySQLPDOTest::getDSN();
16		$user = PDO_MYSQL_TEST_USER;
17		$pass = PDO_MYSQL_TEST_PASS;
18
19		try {
20			$db = new PDO($dsn, $user, $pass, array($option => $value));
21			if (!is_object($db) || ($value !== ($tmp = @$db->getAttribute($option))))
22				printf("[%03d] Execting '%s'/%s got '%s'/%s' for options '%s'\n",
23					$offset,
24					$value, gettype($value),
25					$tmp, gettype($tmp),
26					$option_desc);
27		} catch (PDOException $e) {
28			printf("[%03d] %s\n", $offset, $e->getMessage());
29		}
30
31	}
32
33	try {
34
35		$dsn = MySQLPDOTest::getDSN();
36		$user = PDO_MYSQL_TEST_USER;
37		$pass = PDO_MYSQL_TEST_PASS;
38
39		$valid_options = array(
40			/* pdo_dbh.c */
41			PDO::ATTR_PERSISTENT									=> 'PDO::ATTR_PERSISTENT',
42			PDO::ATTR_AUTOCOMMIT									=> 'PDO::ATTR_AUTOCOMMIT',
43			/* mysql_driver.c */
44			/* TODO Possible bug PDO::ATTR_TIMEOUT != MYSQLI_OPT_CONNECT_TIMEOUT*/
45			PDO::ATTR_TIMEOUT 										=> 'PDO::ATTR_TIMEOUT',
46			PDO::ATTR_EMULATE_PREPARES						=> 'PDO::ATTR_EMULATE_PREPARES',
47
48			PDO::MYSQL_ATTR_USE_BUFFERED_QUERY		=> 'PDO::MYSQL_ATTR_USE_BUFFERED_QUERY',
49			PDO::MYSQL_ATTR_LOCAL_INFILE					=> 'PDO::MYSQL_ATTR_LOCAL_INFILE',
50			PDO::MYSQL_ATTR_DIRECT_QUERY					=> 'PDO::MYSQL_ATTR_DIRECT_QUERY',
51
52			PDO::MYSQL_ATTR_INIT_COMMAND					=> 'PDO::MYSQL_ATTR_INIT_COMMAND',
53		);
54
55		$defaults = array(
56			PDO::ATTR_PERSISTENT									=> false,
57			PDO::ATTR_AUTOCOMMIT									=> 1,
58			/* TODO - why is this a valid option if getAttribute() does not support it?! */
59			PDO::ATTR_TIMEOUT 										=> false,
60			PDO::ATTR_EMULATE_PREPARES						=> 1,
61			PDO::MYSQL_ATTR_USE_BUFFERED_QUERY		=> 1,
62			/* TODO getAttribute() does not handle it */
63			PDO::MYSQL_ATTR_LOCAL_INFILE					=> false,
64			/* TODO getAttribute() does not handle it */
65			PDO::MYSQL_ATTR_DIRECT_QUERY					=> 1,
66			PDO::MYSQL_ATTR_INIT_COMMAND					=> '',
67		);
68
69		if (NULL !== ($db = @new PDO($dsn, $user, $pass, 'wrong type')))
70			printf("[001] Expecting NULL got %s/%s\n", gettype($db), $db);
71
72		if (!is_object($db = new PDO($dsn, $user, $pass, array())))
73			printf("[002] Expecting object got %s/%s¸\n", gettype($db), $db);
74
75		do {
76			$invalid = mt_rand(-1000, 1000);
77		} while (isset($valid_options[$invalid]));
78		if (is_object($db = new PDO($dsn, $user, $pass, array($invalid => true))))
79			printf("[003] [TODO][CHANGEREQUEST] Please, lets not ignore invalid options and bail out!\n");
80
81		$db = new PDO($dsn, $user, $pass);
82		foreach ($valid_options as $option => $name) {
83			/* TODO getAttribute() is pretty poor in supporting the options, suppress errors */
84			$tmp = @$db->getAttribute($option);
85			if ($tmp !== $defaults[$option])
86				printf("[003a] Expecting default value for '%s' of '%s'/%s, getAttribute() reports setting '%s'/%s\n",
87					$name, $defaults[$option], gettype($defaults[$option]),
88					$tmp, gettype($tmp));
89		}
90
91		$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_AUTOCOMMIT => true));
92		if (!is_object($db) || !$db->getAttribute(PDO::ATTR_AUTOCOMMIT))
93			printf("[004] Autocommit should be on\n");
94
95		$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_AUTOCOMMIT => false));
96		if (!is_object($db) || $db->getAttribute(PDO::ATTR_AUTOCOMMIT))
97			printf("[005] Autocommit should be off\n");
98
99		/* TODO: no way to check ATTR_TIMEOUT settings */
100		if (!is_object($db = new PDO($dsn, $user, $pass, array(PDO::ATTR_TIMEOUT => 10))))
101			printf("[006] ATTR_TIMEOUT should be accepted\n");
102
103		if (!is_object($db = new PDO($dsn, $user, $pass, array(PDO::ATTR_TIMEOUT => PHP_INT_MAX))))
104			printf("[007] ATTR_TIMEOUT should be accepted\n");
105
106		if (!is_object($db = new PDO($dsn, $user, $pass, array(PDO::ATTR_TIMEOUT => -PHP_INT_MAX))))
107			printf("[008] ATTR_TIMEOUT should be accepted\n");
108
109		/* TODO: Its ugly that PDO::ATTR_EMULATE_PREPARES == PDO::MYSQL_ATTR_DIRECT_QUERY */
110		$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => true));
111		if (!is_object($db))
112			printf("[009] ATTR_EMULATE_PREPARES should be accepted and on\n");
113		if (!$db->getAttribute(PDO::ATTR_EMULATE_PREPARES))
114			printf("[010] [TODO][CHANGEREQUEST] ATTR_EMULATE_PREPARES should be on\n");
115		if (!$db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
116			printf("[011] As PDO::MYSQL_ATTR_DIRECT_QUERY == PDO::ATTR_EMULATE_PREPARES
117				and PDO::ATTR_EMULATE_PREPARES overrules the other, PDO::MYSQL_ATTR_DIRECT_QUERY should be on\n");
118
119		$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => false));
120		if (!is_object($db))
121			printf("[012] ATTR_EMULATE_PREPARES should be accepted and on\n");
122		if ($db->getAttribute(PDO::ATTR_EMULATE_PREPARES))
123			printf("[013] [TODO][CHANGEREQUEST] ATTR_EMULATE_PREPARES should be off\n");
124		if ($db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
125			printf("[014] As PDO::MYSQL_ATTR_DIRECT_QUERY == PDO::ATTR_EMULATE_PREPARES
126				and PDO::ATTR_EMULATE_PREPARES overrules the other, PDO::MYSQL_ATTR_DIRECT_QUERY should be off\n");
127
128		// PDO::ATTR_EMULATE_PREPARES overrules PDO::MYSQL_ATTR_DIRECT_QUERY
129		// TODO: is it clever that a generic setting overrules a specific setting?
130		$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => true, PDO::MYSQL_ATTR_DIRECT_QUERY => false));
131		if (!$db->getAttribute(PDO::ATTR_EMULATE_PREPARES))
132			printf("[015] PDO::ATTR_EMULATE_PREPARES should be on\n");
133		if (!$db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
134			printf("[016] PDO::MYSQL_ATTR_DIRECT_QUERY should be on\n");
135
136		$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::MYSQL_ATTR_DIRECT_QUERY => true));
137		if ($db->getAttribute(PDO::ATTR_EMULATE_PREPARES))
138			printf("[017] PDO::ATTR_EMULATE_PREPARES should be off\n");
139		if ($db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
140			printf("[018] PDO::MYSQL_ATTR_DIRECT_QUERY should be off\n");
141
142		set_option_and_check(19, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 1, 'PDO::MYSQL_ATTR_USE_BUFFERED_QUERY');
143		set_option_and_check(20, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, 0, 'PDO::MYSQL_ATTR_USE_BUFFERED_QUERY');
144
145		set_option_and_check(21, PDO::MYSQL_ATTR_LOCAL_INFILE, true, 'PDO::MYSQL_ATTR_LOCAL_INFILE');
146		set_option_and_check(22, PDO::MYSQL_ATTR_LOCAL_INFILE, false, 'PDO::MYSQL_ATTR_LOCAL_INFILE');
147
148		set_option_and_check(23, PDO::MYSQL_ATTR_INIT_COMMAND, 'SET @a=1', 'PDO::MYSQL_ATTR_INIT_COMMAND');
149		set_option_and_check(24, PDO::MYSQL_ATTR_INIT_COMMAND, '', 'PDO::MYSQL_ATTR_INIT_COMMAND');
150		set_option_and_check(25, PDO::MYSQL_ATTR_INIT_COMMAND, 'INSERT INTO nonexistent(invalid) VALUES (1)', 'PDO::MYSQL_ATTR_INIT_COMMAND');
151
152		set_option_and_check(33, PDO::MYSQL_ATTR_DIRECT_QUERY, 1, 'PDO::MYSQL_ATTR_DIRECT_QUERY');
153		set_option_and_check(34, PDO::MYSQL_ATTR_DIRECT_QUERY, 0, 'PDO::MYSQL_ATTR_DIRECT_QUERY');
154
155	} catch (PDOException $e) {
156		printf("[001] %s, [%s] %s\n",
157			$e->getMessage(),
158			(is_object($db)) ? $db->errorCode() : 'n/a',
159			(is_object($db)) ? implode(' ', $db->errorInfo()) : 'n/a');
160	}
161
162	print "done!";
163?>
164--EXPECTF--
165[003] [TODO][CHANGEREQUEST] Please, lets not ignore invalid options and bail out!
166[003a] Expecting default value for 'PDO::ATTR_EMULATE_PREPARES' of '1'/integer, getAttribute() reports setting ''/boolean
167[003a] Expecting default value for 'PDO::MYSQL_ATTR_INIT_COMMAND' of ''/string, getAttribute() reports setting ''/boolean
168
169Warning: PDO::getAttribute(): SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute in %s on line %d
170[010] [TODO][CHANGEREQUEST] ATTR_EMULATE_PREPARES should be on
171
172Warning: PDO::getAttribute(): SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute in %s on line %d
173
174Warning: PDO::getAttribute(): SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute in %s on line %d
175[015] PDO::ATTR_EMULATE_PREPARES should be on
176[016] PDO::MYSQL_ATTR_DIRECT_QUERY should be on
177
178Warning: PDO::getAttribute(): SQLSTATE[IM001]: Driver does not support this function: driver does not support that attribute in %s on line %d
179[018] PDO::MYSQL_ATTR_DIRECT_QUERY should be off
180[021] Execting '1'/boolean got ''/boolean' for options 'PDO::MYSQL_ATTR_LOCAL_INFILE'
181[023] Execting 'SET @a=1'/string got ''/boolean' for options 'PDO::MYSQL_ATTR_INIT_COMMAND'
182[024] SQLSTATE[42000] [1065] Query was empty
183[025] SQLSTATE[42S02] [1146] Table '%s.nonexistent' doesn't exist
184done!
185