1--TEST--
2PDO::ATTR_ERRMODE
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once __DIR__ . '/inc/mysql_pdo_test.inc';
8MySQLPDOTest::skip();
9?>
10--INI--
11error_reporting=E_ALL
12--FILE--
13<?php
14    require_once __DIR__ . '/inc/mysql_pdo_test.inc';
15    $db = MySQLPDOTest::factory();
16
17    try {
18        $db->setAttribute(PDO::ATTR_ERRMODE, []);
19    } catch (\Error $e) {
20        echo get_class($e), ': ', $e->getMessage(), \PHP_EOL;
21    }
22    try {
23        $db->setAttribute(PDO::ATTR_ERRMODE, new stdClass());
24    } catch (\Error $e) {
25        echo get_class($e), ': ', $e->getMessage(), \PHP_EOL;
26    }
27    try {
28        $db->setAttribute(PDO::ATTR_ERRMODE, 'pdo');
29    } catch (\Error $e) {
30        echo get_class($e), ': ', $e->getMessage(), \PHP_EOL;
31    }
32    try {
33        $db->setAttribute(PDO::ATTR_ERRMODE, 1000);
34    } catch (\Error $e) {
35        echo get_class($e), ': ', $e->getMessage(), \PHP_EOL;
36    }
37
38    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
39    // no message for any PDO call but...
40    $db->query('THIS IS NOT VALID SQL');
41    // ... still messages for everything else
42    $code = $db->errorCode();
43    $info = $db->errorInfo();
44
45    if ($code != '42000')
46        printf("[005] Expecting SQL code 42000 got '%s'\n", $code);
47    if ($code !== $info[0])
48        printf("[006] Code and info should be identical, got errorCode() = %s, errorInfo()[0] = %s\n",
49            $code, $info[0]);
50    if ('' == $info[1])
51        printf("[007] Driver specific error code not set\n");
52    if ('' == $info[2])
53        printf("[008] Driver specific error message not set\n");
54
55    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
56    $db->query('THIS IS NOT VALID SQL');
57
58    $code = $db->errorCode();
59    $info = $db->errorInfo();
60
61    if ($code != '42000')
62        printf("[009] Expecting SQL code 42000 got '%s'\n", $code);
63    if ($code !== $info[0])
64        printf("[010] Code and info should be identical, got errorCode() = %s, errorInfo()[0] = %s\n",
65            $code, $info[0]);
66    if ('' == $info[1])
67        printf("[011] Driver specific error code not set\n");
68    if ('' == $info[2])
69        printf("[012] Driver specific error message not set\n");
70
71    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
72    try {
73        $line = __LINE__ + 1;
74        $db->query('THIS IS NOT VALID SQL');
75    } catch (PDOException $e) {
76        $code = $db->errorCode();
77        $info = $db->errorInfo();
78
79        if ($code != '42000')
80            printf("[013] Expecting SQL code 42000 got '%s'\n", $code);
81        if ($code !== $info[0])
82            printf("[014] Code and info should be identical, got errorCode() = %s, errorInfo()[0] = %s\n",
83                $code, $info[0]);
84        if ('' == $info[1])
85            printf("[015] Driver specific error code not set\n");
86        if ('' == $info[2])
87            printf("[016] Driver specific error message not set\n");
88
89        if ($e->getCode() !== $code)
90            printf("[017] Exception code '%s' differs from errorCode '%s'\n",
91                $e->getCode(), $code);
92
93        $msg = $e->getMessage();
94        foreach ($info as $k => $v) {
95            if (false === stristr($msg, (string)$v)) {
96                printf("[018] Cannot find all parts of the error info ('%s') in the exception message '%s'\n",
97                    $v, $msg);
98            }
99        }
100
101        if ($e->getLine() !== $line)
102            printf("[019] Exception has been thrown in line %d, exception object reports line %d\n",
103                $line, $e->getLine());
104
105        if ($e->getFile() !== __FILE__)
106            printf("[020] Exception has been thrown in file '%s', exception object reports file '%s'\n",
107                __FILE__, $e->getFile());
108    }
109
110    function my_handler($e) {
111        global $db, $line;
112
113        $code = $db->errorCode();
114        $info = $db->errorInfo();
115
116        if ($code != '42000')
117            printf("[021] Expecting SQL code 42000 got '%s'\n", $code);
118        if ($code !== $info[0])
119            printf("[022] Code and info should be identical, got errorCode() = %s, errorInfo()[0] = %s\n",
120                $code, $info[0]);
121        if ('' == $info[1])
122            printf("[023] Driver specific error code not set\n");
123        if ('' == $info[2])
124            printf("[024] Driver specific error message not set\n");
125
126        if ($e->getCode() !== $code)
127            printf("[025] Exception code '%s' differs from errorCode '%s'\n",
128                $e->getCode(), $code);
129
130        $msg = $e->getMessage();
131        foreach ($info as $k => $v) {
132            if (false === stristr($msg, (string)$v)) {
133                printf("[026] Cannot find all parts of the error info ('%s') in the exception message '%s'\n",
134                    $v, $msg);
135            }
136        }
137
138        if ($e->getLine() !== $line)
139            printf("[027] Exception has been thrown in line %d, exception object reports line %d\n",
140                $line, $e->getLine());
141
142        if ($e->getFile() !== __FILE__)
143            printf("[028] Exception has been thrown in file '%s', exception object reports file '%s'\n",
144                __FILE__, $e->getFile());
145
146        if (get_class($e) != 'PDOException')
147            printf("[029] Expecting PDO exception got exception of type '%s'\n", get_class($e));
148
149        print "\nend of execution";
150    }
151    set_exception_handler('my_handler');
152    $line = __LINE__ + 1;
153    $db->query('THIS IS NOT VALID SQL');
154
155    print "done!\n";
156?>
157--EXPECTF--
158TypeError: Attribute value must be of type int for selected attribute, array given
159TypeError: Attribute value must be of type int for selected attribute, stdClass given
160TypeError: Attribute value must be of type int for selected attribute, string given
161ValueError: Error mode must be one of the PDO::ERRMODE_* constants
162
163Warning: PDO::query(): SQLSTATE[42000]: Syntax error or access violation: %d You have an error in your SQL syntax; check the manual that corresponds to your %s server version for the right syntax to use near '%s' at line %d in %s on line %d
164
165end of execution
166