From 47e4dbe158516be5e07e0623312dafd5af57fae4 Mon Sep 17 00:00:00 2001 From: Laurent Laville Date: Tue, 27 Aug 2024 14:42:40 +0000 Subject: [PATCH] add unit tests for ManifestOptions component --- src/Composer/ManifestOptions.php | 21 ++-- src/Console/Command/Make.php | 16 ++-- tests/unit/ExternalDataProvider.php | 17 ++++ tests/unit/ManifestOptionsTest.php | 143 ++++++++++++++++++++++++++++ 4 files changed, 179 insertions(+), 18 deletions(-) create mode 100644 tests/unit/ManifestOptionsTest.php diff --git a/src/Composer/ManifestOptions.php b/src/Composer/ManifestOptions.php index 951df33..9b43900 100644 --- a/src/Composer/ManifestOptions.php +++ b/src/Composer/ManifestOptions.php @@ -48,17 +48,6 @@ public function getFormat(bool $raw = false): string|null|ManifestFormat return ManifestFormat::tryFrom($rawFormat); } - public function getFormatDisplay(): string - { - // @phpstan-ignore return.type - return match ($this->getFormat()) { - ManifestFormat::auto => 'AUTO detection mode', - ManifestFormat::plain, ManifestFormat::consoleStyle, ManifestFormat::consoleTable => 'TEXT', - ManifestFormat::sbomXml, ManifestFormat::sbomJson => 'SBOM ' . $this->getSbomSpec(), - default => $this->getFormat(true), - }; - } - public function getSbomSpec(): string { return $this->io->getTypedOption(self::SBOM_SPEC_OPTION)->asString(); @@ -87,6 +76,16 @@ public function getOutputConfFile(): ?string return $this->io->getTypedOption(self::OUTPUT_CONF_OPTION)->asNullableString(); } + public function getTemplateFile(): ?string + { + return $this->io->getTypedOption(ManifestOptions::TEMPLATE_OPTION)->asNullableString(); + } + + public function getResourceDir(): string + { + return $this->io->getTypedOption(self::RESOURCE_DIR_OPTION)->asString(); + } + public function isImmutable(): bool { return $this->io->getTypedOption(self::IMMUTABLE_OPTION)->asBoolean(); diff --git a/src/Console/Command/Make.php b/src/Console/Command/Make.php index 2161e38..6c31c6f 100644 --- a/src/Console/Command/Make.php +++ b/src/Console/Command/Make.php @@ -212,7 +212,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io->getTypedOption(BoxHelper::NO_CONFIG_OPTION)->asBoolean() ); - $templatePath = $io->getTypedOption(ManifestOptions::TEMPLATE_OPTION)->asNullableString() + $makeOptions = new ManifestOptions($io); + + $templatePath = $makeOptions->getTemplateFile() ?? dirname(__DIR__, 3) . '/resources/default_stub.template' ; @@ -227,12 +229,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'template' => $templatePath, 'resources' => $resources, 'map' => $config->getFileMapper()->getMap(), - 'resourceDir' => $io->getTypedOption(ManifestOptions::RESOURCE_DIR_OPTION)->asString(), - 'sbomSpec' => (new ManifestOptions($io))->getSbomSpec(), - 'outputFormat' => (new ManifestOptions($io))->getFormat(true), - 'output' => (new ManifestOptions($io))->getOutputFile() ?? 'php://stdout', - 'outputStub' => (new ManifestOptions($io))->getOutputStubFile(), - 'outputConf' => (new ManifestOptions($io))->getOutputConfFile(), + 'resourceDir' => $makeOptions->getResourceDir(), + 'sbomSpec' => $makeOptions->getSbomSpec(), + 'outputFormat' => $makeOptions->getFormat(true), + 'output' => $makeOptions->getOutputFile() ?? 'php://stdout', + 'outputStub' => $makeOptions->getOutputStubFile(), + 'outputConf' => $makeOptions->getOutputConfFile(), 'configurationFile' => $config->getConfigurationFile(), ]; diff --git a/tests/unit/ExternalDataProvider.php b/tests/unit/ExternalDataProvider.php index d40a4bc..7d87051 100644 --- a/tests/unit/ExternalDataProvider.php +++ b/tests/unit/ExternalDataProvider.php @@ -62,4 +62,21 @@ public static function recognizedMimeType(): iterable yield ['sbom.cdx.xml', ManifestBuildStrategy::MIME_TYPE_SBOM_XML]; yield ['whatever.you.want', ManifestBuildStrategy::MIME_TYPE_OCTET_STREAM]; } + + public static function recognizedBuildStageOptions(): iterable + { + yield ['auto', ['plain.txt'], null, null, null, 'bootstrap.php']; + yield ['auto', ['plain.txt', 'console-table.txt', 'sbom.json']]; + yield ['auto', [], '1.5']; + yield ['auto', [], null, 'sbom.cdx.xml']; + yield ['auto', [], null, null, 'my-box.json.dist']; + + yield ['plain', []]; + } + + public static function recognizedStubStageOptions(): iterable + { + yield ['my-stub.php', null]; + yield [null, 'empty_stub.template', '.my.manifests/']; + } } diff --git a/tests/unit/ManifestOptionsTest.php b/tests/unit/ManifestOptionsTest.php new file mode 100644 index 0000000..a53a8b6 --- /dev/null +++ b/tests/unit/ManifestOptionsTest.php @@ -0,0 +1,143 @@ + [StageInterface::BUILD_STAGE], + '--immutable' => true, + ]; + + if (!empty($outputFormat)) { + $parameters['--output-format'] = $outputFormat; + } + if (!empty($resources)) { + $parameters['--resource'] = $resources; + } + if (!empty($sbomSpec)) { + $parameters['--sbom-spec'] = $sbomSpec; + } + if (!empty($outputFile)) { + $parameters['--output-file'] = $outputFile; + } + if (!empty($outputConfFile)) { + $parameters['--output-conf'] = $outputConfFile; + } + if (!empty($bootstrap)) { + $parameters['--bootstrap'] = $bootstrap; + } + + $io = new IO(new ArrayInput($parameters, (new Make())->getDefinition()), new NullOutput()); + $options = new ManifestOptions($io); + + // --output-format + $this->assertSame($outputFormat, $options->getFormat(true)); + + if (ManifestFormat::plain->value === $outputFormat) { + $this->assertSame(ManifestFormat::plain, $options->getFormat()); + } + + // --resource + $this->assertSame($resources, $options->getResources()); + + // --sbom-spec + if (!empty($sbomSpec)) { + $this->assertSame($sbomSpec, $options->getSbomSpec()); + } + + // --output-file + if (!empty($outputFile)) { + $this->assertSame($outputFile, $options->getOutputFile()); + } + + + // --output-conf + if (!empty($outputConfFile)) { + $this->assertSame($outputConfFile, $options->getOutputConfFile()); + } + + // --bootstrap + if (!empty($bootstrap)) { + $this->assertSame($bootstrap, $options->getBootstrap()); + } + + // --immutable + $this->assertTrue($options->isImmutable()); + } + + #[DataProviderExternal(ExternalDataProvider::class, 'recognizedStubStageOptions')] + public function testStubStageOptions( + ?string $outputStubFile = null, + ?string $templateFile = null, + ?string $resourceDir = null, + ): void { + $parameters = [ + 'stages' => [StageInterface::STUB_STAGE], + '--immutable' => true, + ]; + + if (!empty($outputStubFile)) { + $parameters['--output-stub'] = $outputStubFile; + } + + if (!empty($templateFile)) { + $parameters['--template'] = $templateFile; + } + + if (!empty($resourceDir)) { + $parameters['--resource-dir'] = $resourceDir; + } + + $io = new IO(new ArrayInput($parameters, (new Make())->getDefinition()), new NullOutput()); + $options = new ManifestOptions($io); + + // --output-stub + if (!empty($outputStubFile)) { + $this->assertSame($outputStubFile, $options->getOutputStubFile()); + } + + // --template + if (!empty($templateFile)) { + $this->assertSame($templateFile, $options->getTemplateFile()); + } + + // --resource-dir + if (!empty($resourceDir)) { + $this->assertSame($resourceDir, $options->getResourceDir()); + } + } +} \ No newline at end of file