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
Ви не можете вибрати більше 25 тем Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. spl_autoload_register(function ($class) {
  8. include str_replace('\\', '/', "src\\$class.php");
  9. });
  10. function runDir(Config $config, string $dir, array $matches, string $category): array
  11. {
  12. $success = 0;
  13. $total = 0;
  14. $entries = scandir($dir);
  15. foreach ($entries as $entry) {
  16. if ($entry === '.' || $entry === '..') {
  17. continue;
  18. }
  19. if (isset($matches[0])) {
  20. if (!preg_match('/' . $matches[0] . '/', $entry)) {
  21. continue;
  22. }
  23. }
  24. $file = "$dir/$entry";
  25. if (is_file($file)) {
  26. if (substr($entry, -4) != '.log') {
  27. continue;
  28. }
  29. $success += runTest($config, $file, $category);
  30. $total += 1;
  31. } elseif (is_dir($file)) {
  32. $statistics = runDir($config, $file, array_slice($matches, 1), "$category/$entry");
  33. $total += $statistics['total'];
  34. $success += $statistics['success'];
  35. }
  36. }
  37. $failed = $total - $success;
  38. return compact('total', 'success', 'failed');
  39. }
  40. function runTest(Config $config, string $file, string $category): int
  41. {
  42. $title = ucwords(str_replace('_', ' ', $category)) . '/';
  43. $title .= ucwords(str_replace('_', ' ', substr(basename($file), 0, -4)));
  44. $line1 = "=====[$title]=====";
  45. $len = strlen($line1);
  46. $line2 = str_repeat("=", $len);
  47. $parts = preg_split('/^[=]+([\r\n]+|$)/m', file_get_contents($file));
  48. $dirty = false;
  49. $success = 1;
  50. for ($i = 0; $i < count($parts); $i += 2) {
  51. $recording = false;
  52. if (empty($parts[$i + 1])) {
  53. if (substr($parts[$i], -1) != "\n") {
  54. $parts[$i] .= "\n";
  55. }
  56. $parts[$i + 1] = '';
  57. $recording = true;
  58. $dirty = true;
  59. }
  60. $in = $parts[$i];
  61. $exp = $parts[$i + 1];
  62. $api = new Api($config);
  63. $_SERVER['REMOTE_ADDR'] = 'TEST_IP';
  64. $out = ResponseUtils::toString($api->handle(RequestFactory::fromString($in)));
  65. if ($recording) {
  66. $parts[$i + 1] = $out;
  67. } else if ($out != $exp) {
  68. echo "$line1\n$exp\n$line2\n$out\n$line2\n";
  69. $success = 0;
  70. }
  71. }
  72. if ($dirty) {
  73. file_put_contents($file, implode("===\n", $parts));
  74. }
  75. return $success;
  76. }
  77. function loadFixture(string $dir, Config $config)
  78. {
  79. $driver = $config->getDriver();
  80. $filename = "$dir/fixtures/blog_$driver.sql";
  81. $file = file_get_contents($filename);
  82. $db = new GenericDB(
  83. $config->getDriver(),
  84. $config->getAddress(),
  85. $config->getPort(),
  86. $config->getDatabase(),
  87. $config->getUsername(),
  88. $config->getPassword()
  89. );
  90. $pdo = $db->pdo();
  91. $file = preg_replace('/--.*$/m', '', $file);
  92. if ($driver == 'sqlsrv') {
  93. $statements = preg_split('/\n\s*GO\s*\n/s', $file);
  94. } else {
  95. $statements = preg_split('/(?<=;)\n/s', $file);
  96. }
  97. foreach ($statements as $i => $statement) {
  98. $statement = trim($statement);
  99. if ($statement) {
  100. try {
  101. $pdo->exec($statement);
  102. } catch (\PDOException $e) {
  103. $error = print_r($pdo->errorInfo(), true);
  104. $statement = var_export($statement, true);
  105. echo "Loading '$filename' failed on statemement #$i:\n$statement\nwith error:\n$error\n";
  106. exit(1);
  107. }
  108. }
  109. }
  110. }
  111. function run(array $drivers, string $dir, array $matches)
  112. {
  113. foreach ($drivers as $driver) {
  114. if (isset($matches[0])) {
  115. if (!preg_match('/' . $matches[0] . '/', $driver)) {
  116. continue;
  117. }
  118. }
  119. if (!extension_loaded("pdo_$driver")) {
  120. echo sprintf("%s: skipped, driver not loaded\n", $driver);
  121. continue;
  122. }
  123. $settings = [];
  124. include "$dir/config/base.php";
  125. include sprintf("$dir/config/%s.php", $driver);
  126. $config = new Config($settings);
  127. loadFixture($dir, $config);
  128. $start = microtime(true);
  129. $stats = runDir($config, "$dir/functional", array_slice($matches, 1), '');
  130. $end = microtime(true);
  131. $time = ($end - $start) * 1000;
  132. $total = $stats['total'];
  133. $failed = $stats['failed'];
  134. echo sprintf("%s: %d tests ran in %d ms, %d failed\n", $driver, $total, $time, $failed);
  135. }
  136. }
  137. run(['mysql', 'pgsql', 'sqlsrv'], __DIR__ . '/tests', array_slice($argv, 1));