Skip to content

Commit

Permalink
add unit tests for ManifestOptions component
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Aug 27, 2024
1 parent c675733 commit 47e4dbe
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 18 deletions.
21 changes: 10 additions & 11 deletions src/Composer/ManifestOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 9 additions & 7 deletions src/Console/Command/Make.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
;

Expand All @@ -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(),
];

Expand Down
17 changes: 17 additions & 0 deletions tests/unit/ExternalDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/'];
}
}
143 changes: 143 additions & 0 deletions tests/unit/ManifestOptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php declare(strict_types=1);
/**
* This file is part of the BoxManifest package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Bartlett\BoxManifest\Tests;

use Bartlett\BoxManifest\Composer\ManifestOptions;
use Bartlett\BoxManifest\Console\Command\Make;
use Bartlett\BoxManifest\Helper\ManifestFormat;
use Bartlett\BoxManifest\Pipeline\StageInterface;

use Fidry\Console\IO;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProviderExternal;

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

/**
* Unit tests for ManifestOptions component of the Box Manifest
*
* @author Laurent Laville
*/
#[CoversClass(ManifestOptions::class)]
final class ManifestOptionsTest extends TestCase
{
#[DataProviderExternal(ExternalDataProvider::class, 'recognizedBuildStageOptions')]
public function testBuildStageOptions(
string $outputFormat,
array $resources,
?string $sbomSpec = null,
?string $outputFile = null,
?string $outputConfFile = null,
?string $bootstrap = null,
): void {
$parameters = [
'stages' => [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());
}
}
}

0 comments on commit 47e4dbe

Please sign in to comment.