SqlParser\Tools\TestGenerator::buildAll PHP Method

buildAll() public static method

Generates recursively all tests preserving the directory structure.
public static buildAll ( string $input, string $output, $debug = null ) : void
$input string The input directory.
$output string The output directory.
return void
    public static function buildAll($input, $output, $debug = null)
    {
        $files = scandir($input);
        foreach ($files as $file) {
            // Skipping current and parent directories.
            if ($file === '.' || $file === '..') {
                continue;
            }
            // Appending the filename to directories.
            $inputFile = $input . '/' . $file;
            $outputFile = $output . '/' . $file;
            $debugFile = $debug !== null ? $debug . '/' . $file : null;
            if (is_dir($inputFile)) {
                // Creating required directories to maintain the structure.
                // Ignoring errors if the folder structure exists already.
                if (!is_dir($outputFile)) {
                    mkdir($outputFile);
                }
                if ($debug !== null && !is_dir($debugFile)) {
                    mkdir($debugFile);
                }
                // Generating tests recursively.
                static::buildAll($inputFile, $outputFile, $debugFile);
            } elseif (substr($inputFile, -3) === '.in') {
                // Generating file names by replacing `.in` with `.out` and
                // `.debug`.
                $outputFile = substr($outputFile, 0, -3) . '.out';
                if ($debug !== null) {
                    $debugFile = substr($debugFile, 0, -3) . '.debug';
                }
                // Building the test.
                if (!file_exists($outputFile)) {
                    sprintf("Building test for %s...\n", $inputFile);
                    static::build(strpos($inputFile, 'lex') !== false ? 'lexer' : 'parser', $inputFile, $outputFile, $debugFile);
                } else {
                    sprintf("Test for %s already built!\n", $inputFile);
                }
            }
        }
    }

Usage Example

Example #1
0
                }
            }
        }
    }
}
// Test generator.
//
// Example of usage:
//
//      php TestGenerator.php ../tests/data ../tests/data
//
// Input data must be in the `../tests/data` folder.
// The output will be generated in the same `../tests/data` folder.
//
if (count($argv) >= 3) {
    // Extracting directories' name from command line and trimming unnecessary
    // slashes at the end.
    $input = rtrim($argv[1], '/');
    $output = rtrim($argv[2], '/');
    $debug = empty($argv[3]) ? null : rtrim($argv[3], '/');
    // Checking if all directories are valid.
    if (!is_dir($input)) {
        throw new \Exception('The input directory does not exist.');
    } elseif (!is_dir($output)) {
        throw new \Exception('The output directory does not exist.');
    } elseif ($debug !== null && !is_dir($debug)) {
        throw new \Exception('The debug directory does not exist.');
    }
    // Finally, building the tests.
    TestGenerator::buildAll($input, $output, $debug);
}