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