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.

test.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. use Tqdev\PhpCrudApi\Api;
  3. use Tqdev\PhpCrudApi\Config;
  4. use Tqdev\PhpCrudApi\Database\GenericDB;
  5. use Tqdev\PhpCrudApi\RequestFactory;
  6. use Tqdev\PhpCrudApi\ResponseUtils;
  7. require 'vendor/autoload.php';
  8. function runDir(Config $config, string $dir, array $matches, string $category): array
  9. {
  10. $success = 0;
  11. $skipped = 0;
  12. $failed = 0;
  13. $entries = scandir($dir);
  14. foreach ($entries as $entry) {
  15. if ($entry === '.' || $entry === '..') {
  16. continue;
  17. }
  18. if (isset($matches[0])) {
  19. if (!preg_match('/' . $matches[0] . '/', $entry)) {
  20. continue;
  21. }
  22. }
  23. $file = "$dir/$entry";
  24. if (is_file($file)) {
  25. if (substr($entry, -4) != '.log') {
  26. continue;
  27. }
  28. $statistics = runTest($config, $file, $category);
  29. $success += $statistics['success'];
  30. $skipped += $statistics['skipped'];
  31. $failed += $statistics['failed'];
  32. } elseif (is_dir($file)) {
  33. $statistics = runDir($config, $file, array_slice($matches, 1), "$category/$entry");
  34. $success += $statistics['success'];
  35. $skipped += $statistics['skipped'];
  36. $failed += $statistics['failed'];
  37. }
  38. }
  39. return compact('success', 'skipped', 'failed');
  40. }
  41. function runTest(Config $config, string $file, string $category): array
  42. {
  43. $success = 1;
  44. $skipped = 0;
  45. $failed = 0;
  46. $title = ucwords(str_replace('_', ' ', $category)) . '/';
  47. $title .= ucwords(str_replace('_', ' ', substr(basename($file), 0, -4)));
  48. $line1 = "=====[$title]=====";
  49. $len = strlen($line1);
  50. $line2 = str_repeat("=", $len);
  51. $parts = preg_split('/^[=]+([\r\n]+|$)/m', file_get_contents($file));
  52. $headers = explode("\n", $parts[0]);
  53. $driver = $config->getDriver();
  54. foreach ($headers as $header) {
  55. if (!strpos($header, ':')) {
  56. continue;
  57. }
  58. list($key, $value) = explode(':', strtolower($header));
  59. if ($key == "skip-for-$driver") {
  60. $skipped = 1;
  61. $success = 0;
  62. }
  63. }
  64. if (!$skipped) {
  65. $dirty = false;
  66. for ($i = 1; $i < count($parts); $i += 2) {
  67. $recording = false;
  68. if (empty($parts[$i + 1])) {
  69. if (substr($parts[$i], -1) != "\n") {
  70. $parts[$i] .= "\n";
  71. }
  72. $parts[$i + 1] = '';
  73. $recording = true;
  74. $dirty = true;
  75. }
  76. $in = $parts[$i];
  77. $exp = $parts[$i + 1];
  78. $api = new Api($config);
  79. $_SERVER['REMOTE_ADDR'] = 'TEST_IP';
  80. $out = ResponseUtils::toString($api->handle(RequestFactory::fromString($in)));
  81. if ($recording) {
  82. $parts[$i + 1] = $out;
  83. } else if ($out != $exp) {
  84. echo "$line1\n$exp\n$line2\n$out\n$line2\n";
  85. $failed = 1;
  86. $success = 0;
  87. }
  88. }
  89. if ($dirty) {
  90. file_put_contents($file, implode("===\n", $parts));
  91. }
  92. }
  93. return compact('success', 'skipped', 'failed');
  94. }
  95. function getDatabase(Config $config)
  96. {
  97. if (!isset($config->getMiddlewares()['reconnect']['databaseHandler'])) {
  98. return $config->getDatabase();
  99. }
  100. return $config->getMiddlewares()['reconnect']['databaseHandler']();
  101. }
  102. function getTables(Config $config)
  103. {
  104. if (!isset($config->getMiddlewares()['reconnect']['tablesHandler'])) {
  105. return $config->getTables();
  106. }
  107. return $config->getMiddlewares()['reconnect']['tablesHandler']();
  108. }
  109. function getUsername(Config $config)
  110. {
  111. if (!isset($config->getMiddlewares()['reconnect']['usernameHandler'])) {
  112. return $config->getUsername();
  113. }
  114. return $config->getMiddlewares()['reconnect']['usernameHandler']();
  115. }
  116. function getPassword(Config $config)
  117. {
  118. if (!isset($config->getMiddlewares()['reconnect']['passwordHandler'])) {
  119. return $config->getPassword();
  120. }
  121. return $config->getMiddlewares()['reconnect']['passwordHandler']();
  122. }
  123. function loadFixture(string $dir, Config $config)
  124. {
  125. $driver = $config->getDriver();
  126. $filename = "$dir/fixtures/blog_$driver.sql";
  127. $file = file_get_contents($filename);
  128. $db = new GenericDB(
  129. $config->getDriver(),
  130. $config->getAddress(),
  131. $config->getPort(),
  132. getDatabase($config),
  133. getTables($config),
  134. getUsername($config),
  135. getPassword($config)
  136. );
  137. $pdo = $db->pdo();
  138. $file = preg_replace('/--.*$/m', '', $file);
  139. if ($driver == 'sqlsrv') {
  140. $statements = preg_split('/\n\s*GO\s*\n/s', $file);
  141. } else {
  142. $statements = preg_split('/(?<=;)\n/s', $file);
  143. }
  144. foreach ($statements as $i => $statement) {
  145. $statement = trim($statement);
  146. if ($statement) {
  147. try {
  148. $pdo->exec($statement);
  149. } catch (\PDOException $e) {
  150. $error = print_r($pdo->errorInfo(), true);
  151. $statement = var_export($statement, true);
  152. echo "Loading '$filename' failed on statemement #$i:\n$statement\nwith error:\n$error\n";
  153. exit(1);
  154. }
  155. }
  156. }
  157. }
  158. function run(array $drivers, string $dir, array $matches)
  159. {
  160. foreach ($drivers as $driver) {
  161. if (isset($matches[0])) {
  162. if (!preg_match('/' . $matches[0] . '/', $driver)) {
  163. continue;
  164. }
  165. }
  166. if (!extension_loaded("pdo_$driver")) {
  167. echo sprintf("%s: skipped, driver not loaded\n", $driver);
  168. continue;
  169. }
  170. $settings = [];
  171. include "$dir/config/base.php";
  172. include sprintf("$dir/config/%s.php", $driver);
  173. $config = new Config($settings);
  174. loadFixture($dir, $config);
  175. $start = microtime(true);
  176. $statistics = runDir($config, "$dir/functional", array_slice($matches, 1), '');
  177. $end = microtime(true);
  178. $time = ($end - $start) * 1000;
  179. $success = $statistics['success'];
  180. $skipped = $statistics['skipped'];
  181. $failed = $statistics['failed'];
  182. $total = $success + $skipped + $failed;
  183. echo sprintf("%s: %d tests ran in %d ms, %d skipped, %d failed\n", $driver, $total, $time, $skipped, $failed);
  184. }
  185. }
  186. run(['mysql', 'pgsql', 'sqlsrv', 'sqlite'], __DIR__ . '/tests', array_slice($argv, 1));