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