123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- <?php
- namespace League\CLImate\Tests;
- use League\CLImate\CLImate;
- use League\CLImate\Decorator\Style;
- use League\CLImate\Logger;
- use Mockery;
- use Mockery\Mock;
- use PHPUnit\Framework\TestCase;
- use Psr\Log\InvalidArgumentException;
- use Psr\Log\LoggerInterface;
- use Psr\Log\LogLevel;
- class LoggerTest extends TestCase
- {
- /**
- * @var Logger $logger The instance we are testing.
- */
- private $logger;
- /**
- * @var CLImate|Mock $cli A climate instance to test with.
- */
- private $cli;
- public function setUp(): void
- {
- $this->cli = Mockery::mock(CLImate::class);
- $style = Mockery::mock(Style::class);
- $style->shouldReceive("get")->andReturn(true);
- $this->cli->style = $style;
- $this->logger = new Logger(LogLevel::DEBUG, $this->cli);
- }
- public function tearDown(): void
- {
- Mockery::close();
- }
- public function testConstructor1()
- {
- $logger = new Logger();
- $this->assertInstanceOf(LoggerInterface::class, $logger);
- $this->assertInstanceOf(Logger::class, $logger);
- }
- public function testConstructor2()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage("Unknown log level: ");
- new Logger("");
- }
- public function testConstructor3()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage("Unknown log level: 15");
- new Logger(15);
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testEmergency()
- {
- $this->cli->shouldReceive("emergency")->once()->with("Testing emergency");
- $this->logger->emergency("Testing emergency");
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testAlert()
- {
- $this->cli->shouldReceive("alert")->once()->with("Testing alert");
- $this->logger->alert("Testing alert");
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testCritical()
- {
- $this->cli->shouldReceive("critical")->once()->with("Testing critical");
- $this->logger->critical("Testing critical");
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testError()
- {
- $this->cli->shouldReceive("error")->once()->with("Testing error");
- $this->logger->error("Testing error");
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testWarning()
- {
- $this->cli->shouldReceive("warning")->once()->with("Testing warning");
- $this->logger->warning("Testing warning");
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testNotice()
- {
- $this->cli->shouldReceive("notice")->once()->with("Testing notice");
- $this->logger->notice("Testing notice");
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testInfo()
- {
- $this->cli->shouldReceive("info")->once()->with("Testing info");
- $this->logger->info("Testing info");
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testDebug()
- {
- $this->cli->shouldReceive("debug")->once()->with("Testing debug");
- $this->logger->debug("Testing debug");
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testLog()
- {
- $this->cli->shouldReceive("critical")->once()->with("Testing log");
- $this->logger->log(LogLevel::CRITICAL, "Testing log");
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testLevelEmergency()
- {
- $this->cli->shouldReceive(LogLevel::EMERGENCY)->once()->with("Testing log");
- $this->logger->withLogLevel(LogLevel::EMERGENCY)->emergency("Testing log");
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testLevelAlert()
- {
- $this->cli->shouldReceive("alert")->never();
- $this->logger->withLogLevel(LogLevel::EMERGENCY)->alert("Testing log");
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testLevelNotice()
- {
- $this->cli->shouldReceive(LogLevel::NOTICE)->once()->with("Notice");
- $this->logger->withLogLevel(LogLevel::NOTICE)->notice("Notice");
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testLevelDebug()
- {
- $this->cli->shouldReceive(LogLevel::DEBUG)->once()->with("Debug");
- $this->logger->withLogLevel(LogLevel::DEBUG)->debug("Debug");
- }
- public function testWithInvalidLogLevel1()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage("Unknown log level: INVALID");
- $this->logger->withLogLevel("INVALID");
- }
- public function testWithInvalidLogLevel2()
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage("Unknown log level: 0");
- $this->logger->withLogLevel(0);
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testContext()
- {
- $this->cli->shouldReceive("info")->once()->with("With context");
- $this->cli->shouldReceive("tab")->with(1)->once()->andReturn($this->cli);
- $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
- $this->cli->shouldReceive("inline")->once()->with("context: ");
- $this->cli->shouldReceive("info")->once()->with("CONTEXT");
- $this->logger->info("With context", [
- "context" => "CONTEXT",
- ]);
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testEmptyContext()
- {
- $this->cli->shouldReceive("info")->once()->with("No context");
- $this->logger->info("No context", []);
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testPlaceholders()
- {
- $this->cli->shouldReceive("info")->once()->with("I am Spartacus!");
- $this->logger->info("I am {username}!", [
- "username" => "Spartacus",
- ]);
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testPlaceholdersAndContext()
- {
- $this->cli->shouldReceive("info")->once()->with("I am Spartacus!");
- $this->cli->shouldReceive("tab")->with(1)->once()->andReturn($this->cli);
- $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
- $this->cli->shouldReceive("inline")->once()->with("date: ");
- $this->cli->shouldReceive("info")->once()->with("2015-03-01");
- $this->logger->info("I am {username}!", [
- "username" => "Spartacus",
- "date" => "2015-03-01",
- ]);
- }
- /**
- * @doesNotPerformAssertions
- */
- public function testRecursiveContext()
- {
- $this->cli->shouldReceive("info")->once()->with("INFO");
- $this->cli->shouldReceive("tab")->with(1)->once()->andReturn($this->cli);
- $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
- $this->cli->shouldReceive("inline")->once()->with("data: ");
- $this->cli->shouldReceive("info")->once()->with("[");
- $this->cli->shouldReceive("tab")->with(2)->once()->andReturn($this->cli);
- $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
- $this->cli->shouldReceive("inline")->once()->with("field1: ");
- $this->cli->shouldReceive("info")->once()->with("One");
- $this->cli->shouldReceive("tab")->with(2)->once()->andReturn($this->cli);
- $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
- $this->cli->shouldReceive("inline")->once()->with("field2: ");
- $this->cli->shouldReceive("info")->once()->with("Two");
- $this->cli->shouldReceive("tab")->with(2)->once()->andReturn($this->cli);
- $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
- $this->cli->shouldReceive("inline")->once()->with("extra: ");
- $this->cli->shouldReceive("info")->once()->with("[");
- $this->cli->shouldReceive("tab")->with(3)->once()->andReturn($this->cli);
- $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
- $this->cli->shouldReceive("inline")->once()->with("0: ");
- $this->cli->shouldReceive("info")->once()->with("Three");
- $this->cli->shouldReceive("tab")->with(3)->once()->andReturn($this->cli);
- $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
- $this->cli->shouldReceive("inline")->once()->with("1: ");
- $this->cli->shouldReceive("info")->once()->with("Four");
- $this->cli->shouldReceive("tab")->with(2)->once()->andReturn($this->cli);
- $this->cli->shouldReceive("info")->once()->with("]");
- $this->cli->shouldReceive("tab")->with(1)->once()->andReturn($this->cli);
- $this->cli->shouldReceive("info")->once()->with("]");
- $this->logger->info("INFO", [
- "data" => [
- "field1" => "One",
- "field2" => "Two",
- "extra" => ["Three", "Four"],
- ],
- ]);
- }
- }
|