|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace EzSystems\Raml2Html\Command; |
| 6 | + |
| 7 | +use EzSystems\Raml2Html\Test\ReferenceTester; |
| 8 | +use Symfony\Component\Console\Command\Command; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Input\InputArgument; |
| 11 | +use Symfony\Component\Console\Input\InputOption; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | + |
| 14 | +final class TestCommand extends Command |
| 15 | +{ |
| 16 | + /** |
| 17 | + * {@inheritdoc} |
| 18 | + */ |
| 19 | + protected function configure(): void |
| 20 | + { |
| 21 | + $this->setName('test') |
| 22 | + ->setDescription('Compare REST API Reference with Ibexa DXP routing configuration under /api/ibexa/v2 prefix') |
| 23 | + ->setHelp('It is recommended to not use --console-path and --routing-file options while testing the Rest API Reference HTML file against configuration. Those options are used to test that the default configuration file list is up-to-date and other subtleties.') |
| 24 | + ->addArgument('rest-api-reference', InputArgument::REQUIRED, 'Path to the REST API Reference HTML file') |
| 25 | + ->addArgument('ibexa-dxp-root', InputArgument::REQUIRED, 'Path to an Ibexa DXP root directory') |
| 26 | + ->addOption('console-path', 'c', InputOption::VALUE_OPTIONAL, 'Path to the console relative to Ibexa DXP root directory (if no value, use `bin/console`)', false) |
| 27 | + ->addOption('routing-file', 'f', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'Path to a routing configuration YAML file relative to Ibexa DXP root directory', ReferenceTester::DEFAULT_FILE_LIST) |
| 28 | + ->addOption('tested-routes', 't', InputOption::VALUE_OPTIONAL, |
| 29 | + "ref: Test if reference routes are found in the configuration file;\n |
| 30 | + conf: Test if configuration routes are found in the reference file;\n |
| 31 | + both: Test both.", 'both'); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * {@inheritdoc} |
| 36 | + */ |
| 37 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 38 | + { |
| 39 | + $restApiReference = $input->getArgument('rest-api-reference'); |
| 40 | + if (!is_file($restApiReference)) { |
| 41 | + $output->writeln("<error>$restApiReference doesn't exist or is not a file.</error>"); |
| 42 | + return 1; |
| 43 | + } |
| 44 | + |
| 45 | + $dxpRoot = $input->getArgument('ibexa-dxp-root'); |
| 46 | + if (!is_dir($dxpRoot)) { |
| 47 | + $output->writeln("<error>$dxpRoot doesn't exist or is not a directory.</error>"); |
| 48 | + return 2; |
| 49 | + } |
| 50 | + |
| 51 | + $consolePath = $input->getOption('console-path'); |
| 52 | + if (null === $consolePath) { |
| 53 | + $consolePath = 'bin/console'; |
| 54 | + } |
| 55 | + |
| 56 | + $routingFiles = $input->getOption('routing-file'); |
| 57 | + |
| 58 | + $referenceTester = new ReferenceTester($restApiReference, $dxpRoot, $consolePath, $routingFiles); |
| 59 | + |
| 60 | + $testedRoutes = @[ |
| 61 | + 'ref' => ReferenceTester::TEST_REFERENCE_ROUTES, |
| 62 | + 'conf' => ReferenceTester::TEST_CONFIG_ROUTES, |
| 63 | + 'both' => ReferenceTester::TEST_ALL_ROUTES, |
| 64 | + ][$input->getOption('tested-routes')] ?? ReferenceTester::TEST_ALL_ROUTES; |
| 65 | + |
| 66 | + $referenceTester->run($testedRoutes); |
| 67 | + |
| 68 | + return 0; |
| 69 | + } |
| 70 | + |
| 71 | + private function createParserConfigurationFromInput(InputInterface $input): ParserConfiguration |
| 72 | + { |
| 73 | + $configuration = new ParserConfiguration(); |
| 74 | + |
| 75 | + $nonStandardHTTPMethods = $input->getOption(self::OPTION_NON_STANDARD_HTTP_METHODS); |
| 76 | + if ($nonStandardHTTPMethods !== null) { |
| 77 | + $configuration->setNonStandardHttpMethods(explode(',', $nonStandardHTTPMethods)); |
| 78 | + } |
| 79 | + |
| 80 | + return $configuration; |
| 81 | + } |
| 82 | + |
| 83 | + private function createGeneratorOptionsFromInput(InputInterface $input): GeneratorOptions |
| 84 | + { |
| 85 | + $generatorOptions = new GeneratorOptions(); |
| 86 | + $generatorOptions->setOutputDir($input->getOption(self::OPTION_OUTPUT_DIR)); |
| 87 | + $generatorOptions->setTheme($input->getOption(self::OPTION_THEME)); |
| 88 | + |
| 89 | + return $generatorOptions; |
| 90 | + } |
| 91 | +} |
0 commit comments