Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Composer commands #1083

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public function getHeader(): string
public function getCommands(): array
{
return [
new Command\Composer\ComposerCheckVersion(),
new Command\Composer\ComposerVendorDir(),
new Command\Compile($this->getHeader()),
new Command\Diff(),
new Command\Info(),
Expand Down
49 changes: 49 additions & 0 deletions src/Console/Command/Composer/ComposerCheckVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Command\Configuration;
use Fidry\Console\ExitCode;
use Fidry\Console\Input\IO;
use KevinGH\Box\Composer\ComposerOrchestrator;

/**
* @private
*/
final class ComposerCheckVersion extends ComposerCommand
{
public function getConfiguration(): Configuration
{
$parentConfig = parent::getConfiguration();

return new Configuration(
'composer:check-version',
'🎵 Checks if the Composer executable used is compatible with Box',
<<<'HELP'
The <info>%command.name%</info> command will look for the Composer binary (in the system if not configured
in the configuration file) and check if its version is compatible with Box.
HELP,
$parentConfig->getArguments(),
$parentConfig->getOptions(),
);
}

protected function orchestrate(ComposerOrchestrator $composerOrchestrator, IO $io): int
{
$composerOrchestrator->checkVersion();

return ExitCode::SUCCESS;
}
}
83 changes: 83 additions & 0 deletions src/Console/Command/Composer/ComposerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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\Command\Command;
use Fidry\Console\Command\Configuration;
use Fidry\Console\Input\IO;
use Fidry\FileSystem\FileSystem;
use KevinGH\Box\Composer\ComposerOrchestrator;
use KevinGH\Box\Composer\ComposerProcessFactory;
use Psr\Log\LogLevel;
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 COMPOSER_BIN_OPTION = 'composer-bin';

private const VERBOSITY_LEVEL_MAP = [
LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL,
LogLevel::DEBUG => OutputInterface::VERBOSITY_VERBOSE,
];

public function getConfiguration(): Configuration
{
return new Configuration(
'To configure.',
'To configure.',
'To configure.',
[],
[
new InputOption(
self::COMPOSER_BIN_OPTION,
null,
InputOption::VALUE_REQUIRED,
'Composer executable to use.',
),
],
);
}

final public function execute(IO $io): int
{
$composerOrchestrator = new ComposerOrchestrator(
ComposerProcessFactory::create(
self::getComposerExecutable($io),
$io,
),
new ConsoleLogger($io->getOutput(), self::VERBOSITY_LEVEL_MAP),
new FileSystem(),
);

return $this->orchestrate($composerOrchestrator, $io);
}

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

private static function getComposerExecutable(IO $io): ?string
{
$composerBin = $io->getOption(self::COMPOSER_BIN_OPTION)->asNullableNonEmptyString();

return null === $composerBin ? null : Path::makeAbsolute($composerBin, getcwd());
}
}
49 changes: 49 additions & 0 deletions src/Console/Command/Composer/ComposerVendorDir.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Command\Configuration;
use Fidry\Console\ExitCode;
use Fidry\Console\Input\IO;
use KevinGH\Box\Composer\ComposerOrchestrator;

/**
* @private
*/
final class ComposerVendorDir extends ComposerCommand
{
public function getConfiguration(): Configuration
{
$parentConfig = parent::getConfiguration();

return new Configuration(
'composer:vendor-dir',
'🎵 Shows the Composer vendor-dir configured',
<<<'HELP'
The <info>%command.name%</info> command will look for the Composer binary (in the system if not configured
in the configuration file) and print the vendor-dir found.
HELP,
$parentConfig->getArguments(),
$parentConfig->getOptions(),
);
}

protected function orchestrate(ComposerOrchestrator $composerOrchestrator, IO $io): int
{
$io->writeln($composerOrchestrator->getVendorDir());

return ExitCode::SUCCESS;
}
}
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".',
),
];
}
}
Loading