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

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