Skip to content

Commit

Permalink
Add a Logger class (bamarni#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Jul 8, 2022
1 parent 5222278 commit 09a868e
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 23 deletions.
61 changes: 38 additions & 23 deletions src/BinCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Composer\Command\BaseCommand;
use Composer\Factory;
use Composer\IO\IOInterface;
use Composer\IO\NullIO;
use ReflectionClass;
use ReflectionProperty;
use Symfony\Component\Console\Application;
Expand Down Expand Up @@ -35,10 +36,21 @@ class BinCommand extends BaseCommand

private const NAMESPACE_ARG = 'namespace';

/**
* @var Logger
*/
private $logger;

public function __construct(?Logger $logger = null)
{
parent::__construct('bin');

$this->logger = $logger ?? new Logger(new NullIO());
}

protected function configure(): void
{
$this
->setName('bin')
->setDescription('Run a command inside a bin namespace')
->setDefinition([
new InputArgument(self::NAMESPACE_ARG, InputArgument::REQUIRED),
Expand All @@ -47,6 +59,22 @@ protected function configure(): void
->ignoreValidationErrors();
}

public function setIO(IOInterface $io): void
{
parent::setIO($io);

$this->logger = new Logger($io);
}

public function getIO(): IOInterface
{
$io = parent::getIO();

$this->logger = new Logger($io);

return $io;
}

public function isProxyCommand(): bool
{
return true;
Expand All @@ -57,7 +85,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
$config = Config::fromComposer($this->requireComposer());
$currentWorkingDir = getcwd();

$this->log(
$this->logger->logDebug(
sprintf(
'Current working directory: <comment>%s</comment>',
$currentWorkingDir
Expand Down Expand Up @@ -117,10 +145,7 @@ private function executeAllNamespaces(
$namespaces = self::getBinNamespaces($binVendorRoot);

if (count($namespaces) === 0) {
$this->log(
'<warning>Could not find any bin namespace.</warning>',
false
);
$this->logger->logStandard('<warning>Could not find any bin namespace.</warning>');

// Is a valid scenario: the user may not have set up any bin
// namespace yet
Expand Down Expand Up @@ -149,7 +174,7 @@ private function executeInNamespace(
OutputInterface $output,
ReflectionProperty $commandsReflection
): int {
$this->log(
$this->logger->logDebug(
sprintf(
'Checking namespace <comment>%s</comment>',
$namespace
Expand All @@ -159,12 +184,11 @@ private function executeInNamespace(
try {
self::createNamespaceDirIfDoesNotExist($namespace);
} catch (CouldNotCreateNamespaceDir $exception) {
$this->log(
$this->logger->logStandard(
sprintf(
'<warning>%s</warning>',
$exception->getMessage()
),
false
)
);

return self::FAILURE;
Expand Down Expand Up @@ -197,7 +221,7 @@ private function executeInNamespace(

$namespaceInput = BinInputFactory::createNamespaceInput($input);

$this->log(
$this->logger->logDebug(
sprintf(
'Running <info>`@composer %s`</info>.',
$namespaceInput
Expand Down Expand Up @@ -249,7 +273,7 @@ private function configureBinLinksDir(Config $config): void
)
);

$this->log(
$this->logger->logDebug(
sprintf(
'Configuring bin directory to <comment>%s</comment>.',
$binDir
Expand All @@ -268,7 +292,7 @@ private function ensureComposerFileExists(): void

file_put_contents($namespaceComposerFile, '{}');

$this->log(
$this->logger->logDebug(
sprintf(
'Created the file <comment>%s</comment>.',
$namespaceComposerFile
Expand All @@ -291,20 +315,11 @@ private function chdir(string $dir): void
{
chdir($dir);

$this->log(
$this->logger->logDebug(
sprintf(
'Changed current directory to <comment>%s</comment>.',
$dir
)
);
}

private function log(string $message, bool $debug = true): void
{
$verbosity = $debug
? IOInterface::VERBOSE
: IOInterface::NORMAL;

$this->getIO()->writeError('[bamarni-bin-plugin] '.$message, true, $verbosity);
}
}
39 changes: 39 additions & 0 deletions src/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Bamarni\Composer\Bin;

use Composer\IO\IOInterface;

final class Logger
{
/**
* @var IOInterface
*/
private $io;

public function __construct(IOInterface $io)
{
$this->io = $io;
}

public function logStandard(string $message): void
{
$this->log($message, false);
}

public function logDebug(string $message): void
{
$this->log($message, true);
}

private function log(string $message, bool $debug): void
{
$verbosity = $debug
? IOInterface::VERBOSE
: IOInterface::NORMAL;

$this->io->writeError('[bamarni-bin-plugin] '.$message, true, $verbosity);
}
}
3 changes: 3 additions & 0 deletions tests/BinCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
use function realpath;
use function sys_get_temp_dir;

/**
* @covers \Bamarni\Composer\Bin\BinCommand
*/
class BinCommandTest extends TestCase
{
/**
Expand Down
3 changes: 3 additions & 0 deletions tests/BinInputFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\StringInput;

/**
* @covers \Bamarni\Composer\Bin\BinInputFactory
*/
final class BinInputFactoryTest extends TestCase
{
/**
Expand Down
1 change: 1 addition & 0 deletions tests/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

/**
* @group e2e
* @coversNothing
*/
final class EndToEndTest extends TestCase
{
Expand Down
105 changes: 105 additions & 0 deletions tests/LoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

declare(strict_types=1);

namespace Bamarni\Composer\Bin\Tests;

use Bamarni\Composer\Bin\Logger;
use Composer\IO\BufferIO;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\OutputInterface;
use function array_diff;
use const PHP_EOL;

/**
* @covers \Bamarni\Composer\Bin\Logger
*/
final class LoggerTest extends TestCase
{
private const VERBOSITIES = [
OutputInterface::VERBOSITY_QUIET,
OutputInterface::VERBOSITY_NORMAL,
OutputInterface::VERBOSITY_VERBOSE,
OutputInterface::VERBOSITY_VERY_VERBOSE,
OutputInterface::VERBOSITY_DEBUG,
];

/**
* @dataProvider standardMessageProvider
*/
public function test_it_can_log_standard_messages(
int $verbosity,
string $message,
string $expected
): void {
$io = new BufferIO('', $verbosity);
$logger = new Logger($io);

$logger->logStandard($message);

self::assertSame($expected, $io->getOutput());
}

public static function standardMessageProvider(): iterable
{
$notLoggedVerbosities = [
OutputInterface::VERBOSITY_QUIET,
];

$loggedVerbosities = array_diff(
self::VERBOSITIES,
$notLoggedVerbosities
);

$message = 'Hello world!';
$expected = '[bamarni-bin-plugin] Hello world!'.PHP_EOL;

foreach ($notLoggedVerbosities as $verbosity) {
yield [$verbosity, $message, ''];
}

foreach ($loggedVerbosities as $verbosity) {
yield [$verbosity, $message, $expected];
}
}

/**
* @dataProvider standardMessageProvider
*/
public function test_it_can_log_debug_messages(
int $verbosity,
string $message,
string $expected
): void {
$io = new BufferIO('', $verbosity);
$logger = new Logger($io);

$logger->logStandard($message);

self::assertSame($expected, $io->getOutput());
}

public static function debugMessageProvider(): iterable
{
$notLoggedVerbosities = [
OutputInterface::VERBOSITY_QUIET,
OutputInterface::VERBOSITY_NORMAL,
];

$loggedVerbosities = array_diff(
self::VERBOSITIES,
$notLoggedVerbosities
);

$message = 'Hello world!';
$expected = '[bamarni-bin-plugin] Hello world!'.PHP_EOL;

foreach ($notLoggedVerbosities as $verbosity) {
yield [$verbosity, $message, ''];
}

foreach ($loggedVerbosities as $verbosity) {
yield [$verbosity, $message, $expected];
}
}
}

0 comments on commit 09a868e

Please sign in to comment.