Skip to content

Commit

Permalink
Merge branch '4.4' into 5.2
Browse files Browse the repository at this point in the history
* 4.4:
  Use createMock() instead of a getter
  [ErrorHandler] Fix strpos error when trying to call a method without a name
  use proper keys to not override appended files
  Fix console logger according to PSR-3
  • Loading branch information
jderusse committed Jan 28, 2021
2 parents d62ec79 + 1a9570e commit 89d4b17
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
4 changes: 2 additions & 2 deletions EventListener/ErrorListener.php
Expand Up @@ -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)
Expand Down
34 changes: 12 additions & 22 deletions Tests/EventListener/ErrorListenerTest.php
Expand Up @@ -30,35 +30,35 @@ public function testOnConsoleError()
{
$error = new \TypeError('An error occurred');

$logger = $this->getLogger();
$logger = $this->createMock(LoggerInterface::class);
$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'])
;

$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')
->method('critical')
->with('An error occurred while using the console. Message: "{message}"', ['exception' => $error, 'message' => 'An error occurred'])
;

$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')
Expand All @@ -71,7 +71,7 @@ public function testOnConsoleTerminateForNonZeroExitCodeWritesToLog()

public function testOnConsoleTerminateForZeroExitCodeDoesNotWriteToLog()
{
$logger = $this->getLogger();
$logger = $this->createMock(LoggerInterface::class);
$logger
->expects($this->never())
->method('debug')
Expand All @@ -83,7 +83,7 @@ public function testOnConsoleTerminateForZeroExitCodeDoesNotWriteToLog()

public function testGetSubscribedEvents()
{
$this->assertEquals(
$this->assertSame(
[
'console.error' => ['onConsoleError', -128],
'console.terminate' => ['onConsoleTerminate', -128],
Expand All @@ -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')
Expand All @@ -109,7 +109,7 @@ public function testAllKindsOfInputCanBeLogged()

public function testCommandNameIsDisplayedForNonStringableInput()
{
$logger = $this->getLogger();
$logger = $this->createMock(LoggerInterface::class);
$logger
->expects($this->once())
->method('debug')
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 89d4b17

Please sign in to comment.