Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Oct 17, 2023
1 parent cb8ea3c commit 09bcc05
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/Composer/ComposerOrchestrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function getVersion(): string

$output = $getVersionProcess->getOutput();

if (1 !== preg_match('/^Composer version ([^\\s]+)/', $output, $match)) {
if (1 !== preg_match('/Composer version ([^\\s]+?) /', $output, $match)) {
throw UndetectableComposerVersion::forOutput(
$getVersionProcess,
$output,
Expand Down
10 changes: 0 additions & 10 deletions src/Console/Command/Composer/ComposerCheckVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,10 @@

namespace KevinGH\Box\Console\Command\Composer;

use Fidry\Console\Command\Command;
use Fidry\Console\Command\Configuration;
use Fidry\Console\ExitCode;
use Fidry\Console\Input\IO;
use Fidry\FileSystem\FileSystem;
use KevinGH\Box\Composer\ComposerOrchestrator;
use KevinGH\Box\Composer\ComposerProcessFactory;
use KevinGH\Box\Console\ConfigurationLoader;
use KevinGH\Box\Console\ConfigurationLocator;
use Psr\Log\LogLevel;
use RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @private
Expand Down
36 changes: 14 additions & 22 deletions src/Console/Command/Composer/ComposerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@

use Fidry\Console\Command\Command;
use Fidry\Console\Command\Configuration;
use Fidry\Console\ExitCode;
use Fidry\Console\Input\IO;
use Fidry\FileSystem\FileSystem;
use KevinGH\Box\Composer\ComposerOrchestrator;
use KevinGH\Box\Composer\ComposerProcessFactory;
use KevinGH\Box\Console\ConfigurationLoader;
use KevinGH\Box\Console\ConfigurationLocator;
use Psr\Log\LogLevel;
use RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Path;
use function Safe\getcwd;

/**
* @private
*/
abstract class ComposerCommand implements Command
{
private const FILE_ARGUMENT = 'file';
private const COMPOSER_BIN_OPTION = 'composer-bin';

private const VERBOSITY_LEVEL_MAP = [
LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
Expand All @@ -48,11 +46,13 @@ public function getConfiguration(): Configuration
'To configure.',
'To configure.',
'To configure.',
[],
[
new InputArgument(
self::FILE_ARGUMENT,
InputArgument::OPTIONAL,
'The configuration file. (default: box.json, box.json.dist)',
new InputOption(
self::COMPOSER_BIN_OPTION,
null,
InputOption::VALUE_REQUIRED,
'Composer executable to use.',
),
],
);
Expand All @@ -62,7 +62,7 @@ final public function execute(IO $io): int
{
$composerOrchestrator = new ComposerOrchestrator(
ComposerProcessFactory::create(
$this->getComposerExecutable($io),
self::getComposerExecutable($io),
$io,
),
new ConsoleLogger($io->getOutput(), self::VERBOSITY_LEVEL_MAP),
Expand All @@ -74,18 +74,10 @@ final public function execute(IO $io): int

abstract protected function orchestrate(ComposerOrchestrator $composerOrchestrator, IO $io): int;

private function getComposerExecutable(IO $io): ?string
private static function getComposerExecutable(IO $io): ?string
{
try {
$config = ConfigurationLoader::getConfig(
$io->getArgument(self::FILE_ARGUMENT)->asNullableNonEmptyString() ?? ConfigurationLocator::findDefaultPath(),
$io,
false,
);
$composerBin = $io->getOption(self::COMPOSER_BIN_OPTION)->asNullableNonEmptyString();

return $config->getComposerBin();
} catch (RuntimeException) {
return null;
}
return null === $composerBin ? null : Path::makeAbsolute($composerBin, getcwd());
}
}
10 changes: 0 additions & 10 deletions src/Console/Command/Composer/ComposerVendorDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,10 @@

namespace KevinGH\Box\Console\Command\Composer;

use Fidry\Console\Command\Command;
use Fidry\Console\Command\Configuration;
use Fidry\Console\ExitCode;
use Fidry\Console\Input\IO;
use Fidry\FileSystem\FileSystem;
use KevinGH\Box\Composer\ComposerOrchestrator;
use KevinGH\Box\Composer\ComposerProcessFactory;
use KevinGH\Box\Console\ConfigurationLoader;
use KevinGH\Box\Console\ConfigurationLocator;
use Psr\Log\LogLevel;
use RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @private
Expand Down
142 changes: 142 additions & 0 deletions tests/Console/Command/Composer/ComposerCheckVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box\Console\Command\Composer;

use Exception;
use Fidry\Console\ExitCode;
use Fidry\Console\Test\CommandTester;
use Fidry\Console\Test\OutputAssertions;
use KevinGH\Box\Composer\IncompatibleComposerVersion;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Path;
use function Safe\chdir;
use function Safe\getcwd;

/**
* @covers \KevinGH\Box\Console\Command\Composer\ComposerCheckVersion
*
* @internal
*/
class ComposerCheckVersionTest extends TestCase
{
private CommandTester $commandTester;
private string $cwd;

protected function setUp(): void
{
$this->commandTester = CommandTester::fromConsoleCommand(new ComposerCheckVersion());

$this->cwd = getcwd();
chdir(__DIR__);
}

protected function tearDown(): void
{
chdir($this->cwd);
}

/**
* @dataProvider compatibleComposerExecutableProvider
*/
public function test_it_succeeds_the_check_when_the_composer_version_is_compatible(
array $input,
array $options,
string $expectedOutput,
int $expectedStatusCode,
): void {
$input['command'] = 'composer:check-version';

$this->commandTester->execute($input, $options);

OutputAssertions::assertSameOutput(
$expectedOutput,
$expectedStatusCode,
$this->commandTester,
);
}

public static function compatibleComposerExecutableProvider(): iterable
{
$compatibleComposerPath = Path::normalize(__DIR__.'/compatible-composer.phar');

yield 'normal verbosity' => [
[
'--composer-bin' => 'compatible-composer.phar',
],
[],
<<<OUTPUT
[info] '{$compatibleComposerPath}' '--version' '--no-ansi'
[info] Version detected: 2.6.3 (Box requires ^2.2.0)
OUTPUT,
ExitCode::SUCCESS,
];

yield 'quiet verbosity' => [
[
'--composer-bin' => 'compatible-composer.phar',
],
['verbosity' => OutputInterface::VERBOSITY_QUIET],
'',
ExitCode::SUCCESS,
];

yield 'no custom composer' => [
[],
['verbosity' => OutputInterface::VERBOSITY_QUIET],
'',
ExitCode::SUCCESS,
];
}

/**
* @dataProvider incompatibleComposerExecutableProvider
*/
public function test_it_fails_the_check_when_the_composer_version_is_incompatible(
array $input,
array $options,
Exception $expected,
): void {
$input['command'] = 'composer:check-version';

$this->expectExceptionObject($expected);

$this->commandTester->execute($input, $options);
}

public static function incompatibleComposerExecutableProvider(): iterable
{
yield 'normal verbosity' => [
[
'--composer-bin' => 'incompatible-composer.phar',
],
[],
new IncompatibleComposerVersion(
'The Composer version "2.0.14" does not satisfy the constraint "^2.2.0".',
),
];

yield 'quiet verbosity' => [
[
'--composer-bin' => 'incompatible-composer.phar',
],
['verbosity' => OutputInterface::VERBOSITY_QUIET],
new IncompatibleComposerVersion(
'The Composer version "2.0.14" does not satisfy the constraint "^2.2.0".',
),
];
}
}
113 changes: 113 additions & 0 deletions tests/Console/Command/Composer/ComposerVendorDirTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box\Console\Command\Composer;

use Fidry\Console\ExitCode;
use Fidry\Console\Test\CommandTester;
use Fidry\Console\Test\OutputAssertions;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Path;
use function Safe\chdir;
use function Safe\getcwd;

/**
* @covers \KevinGH\Box\Console\Command\Composer\ComposerVendorDir
*
* @internal
*/
class ComposerVendorDirTest extends TestCase
{
private CommandTester $commandTester;
private string $cwd;

protected function setUp(): void
{
$this->commandTester = CommandTester::fromConsoleCommand(new ComposerVendorDir());

$this->cwd = getcwd();
chdir(__DIR__);
}

protected function tearDown(): void
{
chdir($this->cwd);
}

/**
* @dataProvider composerExecutableProvider
*/
public function test_it_retrieves_the_vendor_bin_directory_path(
array $input,
array $options,
string $expectedOutput,
int $expectedStatusCode,
): void {
$input['command'] = 'composer:vendor-dir';

$this->commandTester->execute($input, $options);

OutputAssertions::assertSameOutput(
$expectedOutput,
$expectedStatusCode,
$this->commandTester,
);
}

public static function composerExecutableProvider(): iterable
{
$compatibleComposerPath = Path::normalize(__DIR__.'/compatible-composer.phar');
$incompatibleComposerPath = Path::normalize(__DIR__.'/incompatible-composer.phar');

yield 'normal verbosity' => [
[
'--composer-bin' => 'compatible-composer.phar',
],
[],
<<<OUTPUT
[info] '{$compatibleComposerPath}' 'config' 'vendor-dir' '--no-ansi'
vendor
OUTPUT,
ExitCode::SUCCESS,
];

yield 'quiet verbosity' => [
[
'--composer-bin' => 'compatible-composer.phar',
],
['verbosity' => OutputInterface::VERBOSITY_QUIET],
'',
ExitCode::SUCCESS,
];

yield 'no custom composer' => [
[],
['verbosity' => OutputInterface::VERBOSITY_QUIET],
'',
ExitCode::SUCCESS,
];

yield 'incompatible composer executable; quiet verbosity' => [
[
'--composer-bin' => 'incompatible-composer.phar',
],
['verbosity' => OutputInterface::VERBOSITY_QUIET],
// The output would be too unstable to test in normal verbosity
'',
ExitCode::SUCCESS,
];
}
}
5 changes: 5 additions & 0 deletions tests/Console/Command/Composer/box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "../../../../res/schema.json",

"composerBin": "./composer.phar"
}
Binary file not shown.
1 change: 1 addition & 0 deletions tests/Console/Command/Composer/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Binary file not shown.

0 comments on commit 09bcc05

Please sign in to comment.