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