From 86b9e481278cb1a20e4c7bf3935e091fdf659d36 Mon Sep 17 00:00:00 2001 From: Alexandre Parent Date: Thu, 21 Jan 2021 09:05:34 -0500 Subject: [PATCH 1/2] Fix console logger according to PSR-3 --- EventListener/ErrorListener.php | 4 ++-- Tests/EventListener/ErrorListenerTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/EventListener/ErrorListener.php b/EventListener/ErrorListener.php index a34075793..897d9853f 100644 --- a/EventListener/ErrorListener.php +++ b/EventListener/ErrorListener.php @@ -40,12 +40,12 @@ public function onConsoleError(ConsoleErrorEvent $event) $error = $event->getError(); if (!$inputString = $this->getInputString($event)) { - $this->logger->error('An error occurred while using the console. Message: "{message}"', ['exception' => $error, 'message' => $error->getMessage()]); + $this->logger->critical('An error occurred while using the console. Message: "{message}"', ['exception' => $error, 'message' => $error->getMessage()]); return; } - $this->logger->error('Error thrown while running command "{command}". Message: "{message}"', ['exception' => $error, 'command' => $inputString, 'message' => $error->getMessage()]); + $this->logger->critical('Error thrown while running command "{command}". Message: "{message}"', ['exception' => $error, 'command' => $inputString, 'message' => $error->getMessage()]); } public function onConsoleTerminate(ConsoleTerminateEvent $event) diff --git a/Tests/EventListener/ErrorListenerTest.php b/Tests/EventListener/ErrorListenerTest.php index 68b4b3a3e..0b9fb4d36 100644 --- a/Tests/EventListener/ErrorListenerTest.php +++ b/Tests/EventListener/ErrorListenerTest.php @@ -33,7 +33,7 @@ public function testOnConsoleError() $logger = $this->getLogger(); $logger ->expects($this->once()) - ->method('error') + ->method('critical') ->with('Error thrown while running command "{command}". Message: "{message}"', ['exception' => $error, 'command' => 'test:run --foo=baz buzz', 'message' => 'An error occurred']) ; @@ -48,7 +48,7 @@ public function testOnConsoleErrorWithNoCommandAndNoInputString() $logger = $this->getLogger(); $logger ->expects($this->once()) - ->method('error') + ->method('critical') ->with('An error occurred while using the console. Message: "{message}"', ['exception' => $error, 'message' => 'An error occurred']) ; From a5e89d57f178aef57b9b825808a56f55ca37c472 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Wed, 27 Jan 2021 14:04:45 +0100 Subject: [PATCH 2/2] Use createMock() instead of a getter --- Tests/EventListener/ErrorListenerTest.php | 30 ++++++++--------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/Tests/EventListener/ErrorListenerTest.php b/Tests/EventListener/ErrorListenerTest.php index ce3df1a0e..280395803 100644 --- a/Tests/EventListener/ErrorListenerTest.php +++ b/Tests/EventListener/ErrorListenerTest.php @@ -30,7 +30,7 @@ public function testOnConsoleError() { $error = new \TypeError('An error occurred'); - $logger = $this->getLogger(); + $logger = $this->createMock(LoggerInterface::class); $logger ->expects($this->once()) ->method('error') @@ -38,14 +38,14 @@ public function testOnConsoleError() ; $listener = new ErrorListener($logger); - $listener->onConsoleError(new ConsoleErrorEvent(new ArgvInput(['console.php', 'test:run', '--foo=baz', 'buzz']), $this->getOutput(), $error, new Command('test:run'))); + $listener->onConsoleError(new ConsoleErrorEvent(new ArgvInput(['console.php', 'test:run', '--foo=baz', 'buzz']), $this->createMock(OutputInterface::class), $error, new Command('test:run'))); } public function testOnConsoleErrorWithNoCommandAndNoInputString() { $error = new \RuntimeException('An error occurred'); - $logger = $this->getLogger(); + $logger = $this->createMock(LoggerInterface::class); $logger ->expects($this->once()) ->method('error') @@ -53,12 +53,12 @@ public function testOnConsoleErrorWithNoCommandAndNoInputString() ; $listener = new ErrorListener($logger); - $listener->onConsoleError(new ConsoleErrorEvent(new NonStringInput(), $this->getOutput(), $error)); + $listener->onConsoleError(new ConsoleErrorEvent(new NonStringInput(), $this->createMock(OutputInterface::class), $error)); } public function testOnConsoleTerminateForNonZeroExitCodeWritesToLog() { - $logger = $this->getLogger(); + $logger = $this->createMock(LoggerInterface::class); $logger ->expects($this->once()) ->method('debug') @@ -71,7 +71,7 @@ public function testOnConsoleTerminateForNonZeroExitCodeWritesToLog() public function testOnConsoleTerminateForZeroExitCodeDoesNotWriteToLog() { - $logger = $this->getLogger(); + $logger = $this->createMock(LoggerInterface::class); $logger ->expects($this->never()) ->method('debug') @@ -83,7 +83,7 @@ public function testOnConsoleTerminateForZeroExitCodeDoesNotWriteToLog() public function testGetSubscribedEvents() { - $this->assertEquals( + $this->assertSame( [ 'console.error' => ['onConsoleError', -128], 'console.terminate' => ['onConsoleTerminate', -128], @@ -94,7 +94,7 @@ public function testGetSubscribedEvents() public function testAllKindsOfInputCanBeLogged() { - $logger = $this->getLogger(); + $logger = $this->createMock(LoggerInterface::class); $logger ->expects($this->exactly(3)) ->method('debug') @@ -109,7 +109,7 @@ public function testAllKindsOfInputCanBeLogged() public function testCommandNameIsDisplayedForNonStringableInput() { - $logger = $this->getLogger(); + $logger = $this->createMock(LoggerInterface::class); $logger ->expects($this->once()) ->method('debug') @@ -120,19 +120,9 @@ public function testCommandNameIsDisplayedForNonStringableInput() $listener->onConsoleTerminate($this->getConsoleTerminateEvent($this->createMock(InputInterface::class), 255)); } - private function getLogger() - { - return $this->getMockForAbstractClass(LoggerInterface::class); - } - private function getConsoleTerminateEvent(InputInterface $input, $exitCode) { - return new ConsoleTerminateEvent(new Command('test:run'), $input, $this->getOutput(), $exitCode); - } - - private function getOutput() - { - return $this->createMock(OutputInterface::class); + return new ConsoleTerminateEvent(new Command('test:run'), $input, $this->createMock(OutputInterface::class), $exitCode); } }