|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Usage: |
| 5 | + * php tests/generate-case.php --case-name=Issue777 |
| 6 | + * php tests/generate-case.php --case-name=Issue888 --template=Issue777 |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +\error_reporting(E_ALL | E_STRICT); |
| 12 | +\ini_set('display_errors', '1'); |
| 13 | + |
| 14 | +//Composer |
| 15 | +require_once \dirname(__DIR__) . '/vendor/autoload.php'; |
| 16 | + |
| 17 | +$integrationDir = __DIR__ . '/Behavior/Functional/Driver/Common/Integration'; |
| 18 | + |
| 19 | +function defaultCaseName(string $integrationDir): string |
| 20 | +{ |
| 21 | + $cases = 0; |
| 22 | + |
| 23 | + foreach (\scandir($integrationDir) as $dirName) { |
| 24 | + if (\sscanf($dirName, 'Case%d', $last) === 1) { |
| 25 | + $cases = \max($cases, $last); |
| 26 | + } |
| 27 | + } |
| 28 | + ++$cases; |
| 29 | + |
| 30 | + return $integrationDir . '/Case' . $cases; |
| 31 | +} |
| 32 | + |
| 33 | +function copyTemplateFiles(string $copyDir, string $caseTemplateDirName): void |
| 34 | +{ |
| 35 | + $caseTemplateDir = __DIR__ . '/Behavior/Functional/Driver/Common/Integration/' . $caseTemplateDirName; |
| 36 | + if (!\file_exists($caseTemplateDir) || !\is_dir($caseTemplateDir)) { |
| 37 | + echo "Error. template folder '$caseTemplateDir' does not exists\n"; |
| 38 | + exit(1); |
| 39 | + } |
| 40 | + |
| 41 | + echo \sprintf("Using template files from '%s'...\n", $caseTemplateDir); |
| 42 | + |
| 43 | + $rii = new RecursiveIteratorIterator( |
| 44 | + new RecursiveDirectoryIterator($caseTemplateDir, FilesystemIterator::SKIP_DOTS), |
| 45 | + ); |
| 46 | + foreach ($rii as $file) { |
| 47 | + $filePath = $file->getRealPath(); |
| 48 | + $target = \substr($filePath, \strlen($caseTemplateDir)); |
| 49 | + |
| 50 | + // creating directory... |
| 51 | + $dirName = \dirname($copyDir . $target); |
| 52 | + if (!\is_dir($dirName)) { |
| 53 | + \mkdir($dirName, recursive: true); |
| 54 | + } |
| 55 | + |
| 56 | + $contents = \str_replace($caseTemplateDirName, \basename($copyDir), \file_get_contents($filePath)); |
| 57 | + \file_put_contents($copyDir . $target, $contents); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +$options = \getopt('', [ |
| 62 | + 'case-name:', |
| 63 | + 'template:', |
| 64 | +]); |
| 65 | + |
| 66 | +$copyDir = isset($options['case-name']) |
| 67 | + ? $integrationDir . DIRECTORY_SEPARATOR . $options['case-name'] |
| 68 | + : defaultCaseName($integrationDir); |
| 69 | + |
| 70 | +if (\file_exists($copyDir)) { |
| 71 | + echo "Error. Tests folder `$copyDir` already exists\n"; |
| 72 | + exit(1); |
| 73 | +} |
| 74 | + |
| 75 | +echo \sprintf("Generating new test case '%s'... \n", \basename($copyDir)); |
| 76 | + |
| 77 | +\mkdir($copyDir); |
| 78 | +$copyDir = \realpath($copyDir); |
| 79 | + |
| 80 | +copyTemplateFiles($copyDir, $options['template'] ?? 'CaseTemplate'); |
| 81 | + |
| 82 | +require 'generate.php'; |
| 83 | + |
| 84 | +echo "Done. New test case is here:\n$copyDir\n"; |
0 commit comments