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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. if ($key == "skip-always") {
  64. $skipped = 1;
  65. $success = 0;
  66. }
  67. }
  68. if (!$skipped) {
  69. $dirty = false;
  70. for ($i = 1; $i < count($parts); $i += 2) {
  71. $recording = false;
  72. if (empty($parts[$i + 1])) {
  73. if (substr($parts[$i], -1) != "\n") {
  74. $parts[$i] .= "\n";
  75. }
  76. $parts[$i + 1] = '';
  77. $recording = true;
  78. $dirty = true;
  79. }
  80. $in = $parts[$i];
  81. $exp = $parts[$i + 1];
  82. $api = new Api($config);
  83. $_SERVER['REMOTE_ADDR'] = 'TEST_IP';
  84. $out = ResponseUtils::toString($api->handle(RequestFactory::fromString($in)));
  85. if ($recording) {
  86. $parts[$i + 1] = $out;
  87. } else if ($out != $exp) {
  88. echo "$line1\n$exp\n$line2\n$out\n$line2\n";
  89. $failed = 1;
  90. $success = 0;
  91. }
  92. }
  93. if ($dirty) {
  94. file_put_contents($file, implode("===\n", $parts));
  95. }
  96. }
  97. return compact('success', 'skipped', 'failed');
  98. }
  99. function getDatabase(Config $config)
  100. {
  101. if (!isset($config->getMiddlewares()['reconnect']['databaseHandler'])) {
  102. return $config->getDatabase();
  103. }
  104. return $config->getMiddlewares()['reconnect']['databaseHandler']();
  105. }
  106. function getTables(Config $config)
  107. {
  108. if (!isset($config->getMiddlewares()['reconnect']['tablesHandler'])) {
  109. return $config->getTables();
  110. }
  111. return $config->getMiddlewares()['reconnect']['tablesHandler']();
  112. }
  113. function getUsername(Config $config)
  114. {
  115. if (!isset($config->getMiddlewares()['reconnect']['usernameHandler'])) {
  116. return $config->getUsername();
  117. }
  118. return $config->getMiddlewares()['reconnect']['usernameHandler']();
  119. }
  120. function getPassword(Config $config)
  121. {
  122. if (!isset($config->getMiddlewares()['reconnect']['passwordHandler'])) {
  123. return $config->getPassword();
  124. }
  125. return $config->getMiddlewares()['reconnect']['passwordHandler']();
  126. }
  127. function loadFixture(string $dir, Config $config)
  128. {
  129. $driver = $config->getDriver();
  130. $filename = "$dir/fixtures/blog_$driver.sql";
  131. $file = file_get_contents($filename);
  132. $db = new GenericDB(
  133. $config->getDriver(),
  134. $config->getAddress(),
  135. $config->getPort(),
  136. getDatabase($config),
  137. getTables($config),
  138. getUsername($config),
  139. getPassword($config)
  140. );
  141. $pdo = $db->pdo();
  142. $file = preg_replace('/--.*$/m', '', $file);
  143. if ($driver == 'sqlsrv') {
  144. $statements = preg_split('/\n\s*GO\s*\n/s', $file);
  145. } else {
  146. $statements = preg_split('/(?<=;)\n/s', $file);
  147. }
  148. foreach ($statements as $i => $statement) {
  149. $statement = trim($statement);
  150. if ($statement) {
  151. try {
  152. $pdo->exec($statement);
  153. } catch (\PDOException $e) {
  154. $error = print_r($pdo->errorInfo(), true);
  155. $statement = var_export($statement, true);
  156. echo "Loading '$filename' failed on statemement #$i:\n$statement\nwith error:\n$error\n";
  157. exit(1);
  158. }
  159. }
  160. }
  161. }
  162. function run(array $drivers, string $dir, array $matches)
  163. {
  164. foreach ($drivers as $driver) {
  165. if (isset($matches[0])) {
  166. if (!preg_match('/' . $matches[0] . '/', $driver)) {
  167. continue;
  168. }
  169. }
  170. if (!extension_loaded("pdo_$driver")) {
  171. echo sprintf("%s: skipped, driver not loaded\n", $driver);
  172. continue;
  173. }
  174. $settings = [];
  175. include "$dir/config/base.php";
  176. include sprintf("$dir/config/%s.php", $driver);
  177. $config = new Config($settings);
  178. loadFixture($dir, $config);
  179. $start = microtime(true);
  180. $statistics = runDir($config, "$dir/functional", array_slice($matches, 1), '');
  181. $end = microtime(true);
  182. $time = ($end - $start) * 1000;
  183. $success = $statistics['success'];
  184. $skipped = $statistics['skipped'];
  185. $failed = $statistics['failed'];
  186. $total = $success + $skipped + $failed;
  187. echo sprintf("%s: %d tests ran in %d ms, %d skipped, %d failed\n", $driver, $total, $time, $skipped, $failed);
  188. }
  189. }
  190. run(['mysql', 'pgsql', 'sqlsrv', 'sqlite'], __DIR__ . '/tests', array_slice($argv, 1));