PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/bad/ext/mysqli/tests/mysqli_report.php

http://github.com/facebook/hiphop-php
PHP | 272 lines | 159 code | 56 blank | 57 comment | 38 complexity | 7220f2cf5c2323a4f2be71b86e1042ea MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. require_once("connect.inc");
  3. $tmp = NULL;
  4. $link = NULL;
  5. if (true !== ($tmp = mysqli_report(-1)))
  6. printf("[002] Expecting boolean/true even for invalid flags, got %s/%s\n", gettype($tmp), $tmp);
  7. if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_ERROR)))
  8. printf("[003] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  9. if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_STRICT)))
  10. printf("[004] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  11. if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_INDEX)))
  12. printf("[005] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  13. if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_ALL)))
  14. printf("[007] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  15. if (true !== ($tmp = mysqli_report(MYSQLI_REPORT_OFF)))
  16. printf("[008] Expecting boolean/true, got %s/%s\n", gettype($tmp), $tmp);
  17. $test_table_name = 'test_mysqli_report_table_1'; require('table.inc');
  18. /*
  19. Internal macro MYSQL_REPORT_ERROR
  20. */
  21. mysqli_report(MYSQLI_REPORT_ERROR);
  22. mysqli_multi_query($link, "BAR; FOO;");
  23. mysqli_query($link, "FOO");
  24. mysqli_kill($link, -1);
  25. // mysqli_ping() cannot be tested, because one would need to cause an error inside the C function to test it
  26. mysqli_prepare($link, "FOO");
  27. mysqli_real_query($link, "FOO");
  28. if (@mysqli_select_db($link, "Oh lord, let this be an unknown database name"))
  29. printf("[009] select_db should have failed\n");
  30. // mysqli_store_result() and mysqli_use_result() cannot be tested, because one would need to cause an error inside the C function to test it
  31. // Check that none of the above would have caused any error messages if MYSQL_REPORT_ERROR would
  32. // not have been set. If that would be the case, the test would be broken.
  33. mysqli_report(MYSQLI_REPORT_OFF);
  34. mysqli_multi_query($link, "BAR; FOO;");
  35. mysqli_query($link, "FOO");
  36. mysqli_kill($link, -1);
  37. mysqli_prepare($link, "FOO");
  38. mysqli_real_query($link, "FOO");
  39. mysqli_select_db($link, "Oh lord, let this be an unknown database name");
  40. /*
  41. Internal macro MYSQL_REPORT_STMT_ERROR
  42. */
  43. mysqli_report(MYSQLI_REPORT_ERROR);
  44. $stmt = mysqli_stmt_init($link);
  45. mysqli_stmt_prepare($stmt, "FOO");
  46. $stmt = mysqli_stmt_init($link);
  47. mysqli_stmt_prepare($stmt, "SELECT id FROM test_mysqli_report_table_1 WHERE id > ?");
  48. $id = 1;
  49. mysqli_kill($link, mysqli_thread_id($link));
  50. mysqli_stmt_bind_param($stmt, "i", $id);
  51. mysqli_stmt_close($stmt);
  52. mysqli_close($link);
  53. /* mysqli_stmt_execute() = mysql_stmt_execute cannot be tested from PHP */
  54. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  55. printf("[008] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  56. $stmt = mysqli_stmt_init($link);
  57. mysqli_stmt_prepare($stmt, "SELECT id FROM test_mysqli_report_table_1 WHERE id > ?");
  58. $id = 1;
  59. mysqli_stmt_bind_param($stmt, "i", $id);
  60. // mysqli_kill($link, mysqli_thread_id($link));
  61. mysqli_stmt_execute($stmt);
  62. mysqli_stmt_close($stmt);
  63. mysqli_close($link);
  64. /* mysqli_kill() "trick" does not work for any of the following because of an E_COMMANDS_OUT_OF_SYNC */
  65. /* mysqli_stmt_bind_result() = mysql_stmt_bind_result() cannot be tested from PHP */
  66. /* mysqli_stmt_fetch() = mysql_stmt_fetch() cannot be tested from PHP */
  67. /* mysqli_stmt_result_metadata() = mysql_stmt_result_metadata() cannot be tested from PHP */
  68. /* mysqli_stmt_store_result() = mysql_stmt_store_result() cannot be tested from PHP */
  69. // Check
  70. mysqli_report(MYSQLI_REPORT_OFF);
  71. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  72. printf("[010] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  73. $stmt = mysqli_stmt_init($link);
  74. mysqli_stmt_prepare($stmt, "FOO");
  75. $stmt = mysqli_stmt_init($link);
  76. mysqli_stmt_prepare($stmt, "SELECT id FROM test_mysqli_report_table_1 WHERE id > ?");
  77. $id = 1;
  78. mysqli_kill($link, mysqli_thread_id($link));
  79. mysqli_stmt_bind_param($stmt, "i", $id);
  80. mysqli_stmt_close($stmt);
  81. mysqli_close($link);
  82. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  83. printf("[011] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  84. $stmt = mysqli_stmt_init($link);
  85. mysqli_stmt_prepare($stmt, "SELECT id FROM test_mysqli_report_table_1 WHERE id > ?");
  86. $id = 1;
  87. mysqli_stmt_bind_param($stmt, "i", $id);
  88. mysqli_kill($link, mysqli_thread_id($link));
  89. mysqli_stmt_execute($stmt);
  90. mysqli_stmt_close($stmt);
  91. mysqli_close($link);
  92. /*
  93. MYSQLI_REPORT_STRICT
  94. MYSQLI_REPORT_STRICT --->
  95. php_mysqli_report_error() ->
  96. MYSQLI_REPORT_MYSQL_ERROR,
  97. MYSQLI_REPORT_STMT_ERROR ->
  98. already tested
  99. php_mysqli_throw_sql_exception() ->
  100. my_mysqli_real_connect()
  101. my_mysqli_connect()
  102. can't be tested: mysqli_query() via mysql_use_result()/mysql_store_result()
  103. */
  104. mysqli_report(MYSQLI_REPORT_OFF);
  105. mysqli_report(MYSQLI_REPORT_STRICT);
  106. try {
  107. if ($link = my_mysqli_connect($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket))
  108. printf("[012] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
  109. $host, $user . 'unknown_really', $db, $port, $socket);
  110. mysqli_close($link);
  111. } catch (mysqli_sql_exception $e) {
  112. printf("[013] %s\n", $e->getMessage());
  113. }
  114. try {
  115. if (!$link = mysqli_init())
  116. printf("[014] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  117. if ($link = my_mysqli_real_connect($link, $host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket))
  118. printf("[015] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
  119. $host, $user . 'unknown_really', $db, $port, $socket);
  120. mysqli_close($link);
  121. } catch (mysqli_sql_exception $e) {
  122. printf("[016] %s\n", $e->getMessage());
  123. }
  124. /*
  125. MYSQLI_REPORT_INDEX --->
  126. mysqli_query()
  127. mysqli_stmt_execute()
  128. mysqli_prepare()
  129. mysqli_real_query()
  130. mysqli_store_result()
  131. mysqli_use_result()
  132. No test, because of to many prerequisites:
  133. - Server needs to be started with and
  134. --log-slow-queries --log-queries-not-using-indexes
  135. - query must cause the warning on all MySQL versions
  136. TODO:
  137. */
  138. $log_slow_queries = false;
  139. $log_queries_not_using_indexes = false;
  140. mysqli_report(MYSQLI_REPORT_OFF);
  141. mysqli_report(MYSQLI_REPORT_INDEX);
  142. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  143. printf("[017] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  144. if (mysqli_get_server_version($link) <= 50600) {
  145. // this might cause a warning - no index used
  146. if (!$res = @mysqli_query($link, "SHOW VARIABLES LIKE 'log_slow_queries'"))
  147. printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  148. if (!$row = mysqli_fetch_assoc($res))
  149. printf("[019] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  150. $log_slow_query = ('ON' == $row['Value']);
  151. if (mysqli_get_server_version($link) >= 50111) {
  152. // this might cause a warning - no index used
  153. if (!$res = @mysqli_query($link, "SHOW VARIABLES LIKE 'log_queries_not_using_indexes'"))
  154. printf("[020] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  155. if (!$row = mysqli_fetch_assoc($res))
  156. printf("[021] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  157. $log_queries_not_using_indexes = ('ON' == $row['Value']);
  158. if ($log_slow_queries && $log_queries_not_using_indexes) {
  159. for ($i = 100; $i < 20000; $i++) {
  160. if (!mysqli_query($link, "INSERT INTO test_mysqli_report_table_1(id, label) VALUES ($i, 'z')"))
  161. printf("[022 - %d] [%d] %s\n", $i - 99, mysqli_errno($link), mysqli_error($link));
  162. }
  163. // this might cause a warning - no index used
  164. if (!$res = @mysqli_query($link, "SELECT id, label FROM test_mysqli_report_table_1 WHERE id = 1323"))
  165. printf("[023] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  166. mysqli_free_result($res);
  167. }
  168. }
  169. }
  170. // Maybe we've provoked an index message, maybe not.
  171. // All we can do is make a few dummy calls to ensure that all codes gets executed which
  172. // checks the flag. Functions to check: mysqli_query() - done above,
  173. // mysqli_stmt_execute(), mysqli_prepare(), mysqli_real_query(), mysqli_store_result()
  174. // mysqli_use_result(), mysqli_thread_safe(), mysqli_thread_id()
  175. mysqli_report(MYSQLI_REPORT_OFF);
  176. mysqli_close($link);
  177. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  178. printf("[024] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
  179. if (!$stmt = mysqli_stmt_init($link))
  180. printf("[025] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  181. if (!mysqli_stmt_prepare($stmt, 'SELECT id, label FROM test_mysqli_report_table_1'))
  182. printf("[026] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  183. if (!mysqli_stmt_execute($stmt))
  184. printf("[027] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
  185. mysqli_stmt_close($stmt);
  186. if (!mysqli_real_query($link, 'SELECT label, id FROM test_mysqli_report_table_1'))
  187. printf("[028] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  188. if (!$res = mysqli_use_result($link))
  189. printf("[029] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  190. mysqli_free_result($res);
  191. if (!mysqli_real_query($link, 'SELECT label, id FROM test_mysqli_report_table_1'))
  192. printf("[030] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  193. if (!$res = mysqli_store_result($link))
  194. printf("[031] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  195. mysqli_free_result($res);
  196. if (!$stmt = mysqli_prepare($link, 'SELECT id * 3 FROM test_mysqli_report_table_1'))
  197. printf("[032] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  198. else
  199. mysqli_stmt_close($stmt);
  200. if (!mysqli_query($link, "INSERT INTO test_mysqli_report_table_1(id, label) VALUES (100, 'z')", MYSQLI_USE_RESULT) ||
  201. !mysqli_query($link, 'DELETE FROM test_mysqli_report_table_1 WHERE id > 50', MYSQLI_USE_RESULT))
  202. printf("[033] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  203. $tmp = mysqli_thread_safe($link);
  204. $tmp = mysqli_thread_id($link);
  205. mysqli_close($link);
  206. print "done!";
  207. ?>
  208. <?php error_reporting(0); ?>
  209. <?php
  210. $test_table_name = 'test_mysqli_report_table_1'; require_once("clean_table.inc");
  211. ?>