PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/test/zend/bad/ext/pdo_mysql/tests/pdo_mysql_attr_errmode.php

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