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 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\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. $out = $api->handle(Request::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));