From 09a868e3d716b0565daf1f2937f27ea0ab490588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Fri, 8 Jul 2022 21:24:31 +0200 Subject: [PATCH] Add a Logger class (#99) --- src/BinCommand.php | 61 ++++++++++++-------- src/Logger.php | 39 +++++++++++++ tests/BinCommandTest.php | 3 + tests/BinInputFactoryTest.php | 3 + tests/EndToEndTest.php | 1 + tests/LoggerTest.php | 105 ++++++++++++++++++++++++++++++++++ 6 files changed, 189 insertions(+), 23 deletions(-) create mode 100644 src/Logger.php create mode 100644 tests/LoggerTest.php diff --git a/src/BinCommand.php b/src/BinCommand.php index 3b0733d..73c5da3 100644 --- a/src/BinCommand.php +++ b/src/BinCommand.php @@ -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; @@ -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), @@ -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; @@ -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: %s', $currentWorkingDir @@ -117,10 +145,7 @@ private function executeAllNamespaces( $namespaces = self::getBinNamespaces($binVendorRoot); if (count($namespaces) === 0) { - $this->log( - 'Could not find any bin namespace.', - false - ); + $this->logger->logStandard('Could not find any bin namespace.'); // Is a valid scenario: the user may not have set up any bin // namespace yet @@ -149,7 +174,7 @@ private function executeInNamespace( OutputInterface $output, ReflectionProperty $commandsReflection ): int { - $this->log( + $this->logger->logDebug( sprintf( 'Checking namespace %s', $namespace @@ -159,12 +184,11 @@ private function executeInNamespace( try { self::createNamespaceDirIfDoesNotExist($namespace); } catch (CouldNotCreateNamespaceDir $exception) { - $this->log( + $this->logger->logStandard( sprintf( '%s', $exception->getMessage() - ), - false + ) ); return self::FAILURE; @@ -197,7 +221,7 @@ private function executeInNamespace( $namespaceInput = BinInputFactory::createNamespaceInput($input); - $this->log( + $this->logger->logDebug( sprintf( 'Running `@composer %s`.', $namespaceInput @@ -249,7 +273,7 @@ private function configureBinLinksDir(Config $config): void ) ); - $this->log( + $this->logger->logDebug( sprintf( 'Configuring bin directory to %s.', $binDir @@ -268,7 +292,7 @@ private function ensureComposerFileExists(): void file_put_contents($namespaceComposerFile, '{}'); - $this->log( + $this->logger->logDebug( sprintf( 'Created the file %s.', $namespaceComposerFile @@ -291,20 +315,11 @@ private function chdir(string $dir): void { chdir($dir); - $this->log( + $this->logger->logDebug( sprintf( 'Changed current directory to %s.', $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); - } } diff --git a/src/Logger.php b/src/Logger.php new file mode 100644 index 0000000..a71f857 --- /dev/null +++ b/src/Logger.php @@ -0,0 +1,39 @@ +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); + } +} diff --git a/tests/BinCommandTest.php b/tests/BinCommandTest.php index c91dc0a..462bbec 100644 --- a/tests/BinCommandTest.php +++ b/tests/BinCommandTest.php @@ -22,6 +22,9 @@ use function realpath; use function sys_get_temp_dir; +/** + * @covers \Bamarni\Composer\Bin\BinCommand + */ class BinCommandTest extends TestCase { /** diff --git a/tests/BinInputFactoryTest.php b/tests/BinInputFactoryTest.php index 52ad36b..e43c9a3 100644 --- a/tests/BinInputFactoryTest.php +++ b/tests/BinInputFactoryTest.php @@ -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 { /** diff --git a/tests/EndToEndTest.php b/tests/EndToEndTest.php index cf3c096..22b93b7 100644 --- a/tests/EndToEndTest.php +++ b/tests/EndToEndTest.php @@ -19,6 +19,7 @@ /** * @group e2e + * @coversNothing */ final class EndToEndTest extends TestCase { diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php new file mode 100644 index 0000000..6452f69 --- /dev/null +++ b/tests/LoggerTest.php @@ -0,0 +1,105 @@ +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]; + } + } +}