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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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(Api $api, 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($api, $file, $category);
  29. $total += 1;
  30. } elseif (is_dir($file)) {
  31. $statistics = runDir($api, $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(Api $api, 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. $out = $api->handle(Request::fromString($in));
  62. if ($recording) {
  63. $parts[$i + 1] = $out;
  64. } else if ($out != $exp) {
  65. echo "$line1\n$exp\n$line2\n$out\n$line2\n";
  66. $success = 0;
  67. }
  68. }
  69. if ($dirty) {
  70. file_put_contents($file, implode("===\n", $parts));
  71. }
  72. return $success;
  73. }
  74. function loadFixture(String $dir, Config $config)
  75. {
  76. $driver = $config->getDriver();
  77. $filename = "$dir/fixtures/blog_$driver.sql";
  78. $file = file_get_contents($filename);
  79. $db = new GenericDB(
  80. $config->getDriver(),
  81. $config->getAddress(),
  82. $config->getPort(),
  83. $config->getDatabase(),
  84. $config->getUsername(),
  85. $config->getPassword()
  86. );
  87. $pdo = $db->pdo();
  88. $file = preg_replace('/--.*$/m', '', $file);
  89. if ($driver == 'sqlsrv') {
  90. $statements = preg_split('/\n\s*GO\s*\n/s', $file);
  91. } else {
  92. $statements = preg_split('/(?<=;)\n/s', $file);
  93. }
  94. foreach ($statements as $i => $statement) {
  95. $statement = trim($statement);
  96. if ($statement) {
  97. try {
  98. $pdo->exec($statement);
  99. } catch (\PDOException $e) {
  100. $error = print_r($pdo->errorInfo(), true);
  101. $statement = var_export($statement, true);
  102. echo "Loading '$filename' failed on statemement #$i:\n$statement\nwith error:\n$error\n";
  103. exit(1);
  104. }
  105. }
  106. }
  107. }
  108. function run(array $drivers, String $dir, array $matches)
  109. {
  110. foreach ($drivers as $driver) {
  111. if (!extension_loaded("pdo_$driver")) {
  112. echo sprintf("%s: skipped, driver not loaded\n", $driver);
  113. continue;
  114. }
  115. $settings = [];
  116. include "$dir/config/base.php";
  117. include sprintf("$dir/config/%s.php", $driver);
  118. $config = new Config($settings);
  119. loadFixture($dir, $config);
  120. $start = microtime(true);
  121. $api = new Api($config);
  122. $stats = runDir($api, "$dir/functional", $matches, '');
  123. $end = microtime(true);
  124. $time = ($end - $start) * 1000;
  125. $total = $stats['total'];
  126. $failed = $stats['failed'];
  127. echo sprintf("%s: %d tests ran in %d ms, %d failed\n", $driver, $total, $time, $failed);
  128. }
  129. }
  130. run(['mysql', 'pgsql', 'sqlsrv'], __DIR__ . '/tests', array_slice($argv, 1));