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
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

test.php 4.5KB

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