api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MysqlTest.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. require_once(__DIR__ . '/Tests.php');
  3. class MysqlTest extends Tests
  4. {
  5. /**
  6. * Gets the name of the database engine
  7. *
  8. * @return string Name of the database engine
  9. */
  10. public function getEngineName()
  11. {
  12. return 'MySQL';
  13. }
  14. /**
  15. * Connects to the Database
  16. *
  17. * @return object Database connection
  18. */
  19. public function connect($config)
  20. {
  21. $db = mysqli_connect(
  22. $config['hostname'],
  23. $config['username'],
  24. $config['password'],
  25. $config['database']
  26. );
  27. if (mysqli_connect_errno()) {
  28. die("Connect failed: ".mysqli_connect_error()."\n");
  29. }
  30. mysqli_set_charset($db,'utf8');
  31. return $db;
  32. }
  33. /**
  34. * Disconnects from the Database
  35. *
  36. * @return boolean Success
  37. */
  38. public function disconnect($db)
  39. {
  40. return mysqli_close($db);
  41. }
  42. /**
  43. * Checks the version of the Database
  44. *
  45. * @return void
  46. */
  47. public function checkVersion($db)
  48. {
  49. $major = 5;
  50. $minor = 5;
  51. $version = mysqli_get_server_version($db);
  52. $v = array(floor($version/10000),floor(($version%10000)/100));
  53. if ($v[0]<$major || ($v[0]==$major && $v[1]<$minor)) {
  54. die("Detected MySQL $v[0].$v[1], but only $major.$minor and up are supported\n");
  55. }
  56. }
  57. /**
  58. * Gets the capabilities of the Database
  59. *
  60. * @return int Capabilites
  61. */
  62. public function getCapabilities($db)
  63. {
  64. $capabilities = 0;
  65. $version = mysqli_get_server_version($db);
  66. if ($version>=50600) {
  67. $capabilities |= self::GIS;
  68. }
  69. if ($version>=50700) {
  70. $capabilities |= self::JSON;
  71. }
  72. return $capabilities;
  73. }
  74. /**
  75. * Seeds the database for this connection
  76. *
  77. * @return void
  78. */
  79. public function seedDatabase($db, $capabilities)
  80. {
  81. $fixture = __DIR__.'/data/blog_mysql.sql';
  82. $contents = file_get_contents($fixture);
  83. if (!($capabilities & self::GIS)) {
  84. $contents = preg_replace('/(POINT|POLYGON) NOT NULL/i','text NOT NULL',$contents);
  85. $contents = preg_replace('/ST_GeomFromText/i','concat',$contents);
  86. }
  87. if (!($capabilities & self::JSON)) {
  88. $contents = preg_replace('/JSON NOT NULL/i','text NOT NULL',$contents);
  89. }
  90. $i=0;
  91. if (mysqli_multi_query($db, $contents)) {
  92. do { $i++; mysqli_next_result($db); } while (mysqli_more_results($db));
  93. }
  94. if (mysqli_errno($db)) {
  95. die("Loading '$fixture' failed on statemement #$i with error:\n".mysqli_error($db)."\n");
  96. }
  97. }
  98. }