forked from bamarni/composer-bin-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
189 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
|
||
/** | ||
* @group e2e | ||
* @coversNothing | ||
*/ | ||
final class EndToEndTest extends TestCase | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
} |