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
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

test.php 4.5KB

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