Skip to content

Commit

Permalink
Use createMock() instead of a getter
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark authored and chalasr committed Jan 28, 2021
1 parent 24026c4 commit a5e89d5
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions Tests/EventListener/ErrorListenerTest.php
Original file line number Diff line number Diff line change
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')
->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')
->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 a5e89d5

Please sign in to comment.