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