Skip to content

Commit

Permalink
fix exceptions listener event name
Browse files Browse the repository at this point in the history
  • Loading branch information
VasekPurchart committed May 12, 2022
1 parent 3e77254 commit 317f600
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/Console/ConsoleExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ public function __construct(
$this->logLevel = $logLevel;
}

/**
* Keeping this method for BC (of this package) - was renamed to onConsoleError() as part of a bugfix
*
* @param \Symfony\Component\Console\Event\ConsoleErrorEvent $event
*/
public function onConsoleException(ConsoleErrorEvent $event): void
{
$this->onConsoleError($event);
}

public function onConsoleError(ConsoleErrorEvent $event): void
{
$command = $event->getCommand();
$exception = $event->getError();
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/config/exception_listener.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ services:
tags:
-
name: kernel.event_listener
event: console.exception
event: console.error
priority: '%vasek_purchart.console_errors.exception.listener_priority%'
33 changes: 30 additions & 3 deletions tests/Console/ConsoleExceptionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace VasekPurchart\ConsoleErrorsBundle\Console;

use Closure;
use Generator;
use Psr\Log\LogLevel;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
Expand All @@ -14,7 +16,30 @@
class ConsoleExceptionListenerTest extends \PHPUnit\Framework\TestCase
{

public function testLogError(): void
/**
* @return mixed[]|\Generator
*/
public function eventMethodProvider(): Generator
{
yield 'onConsoleError' => [
'methodCallback' => static function (ConsoleExceptionListener $listener, ConsoleErrorEvent $event): void {
$listener->onConsoleError($event);
},
];

yield 'onConsoleException (for BC)' => [
'methodCallback' => static function (ConsoleExceptionListener $listener, ConsoleErrorEvent $event): void {
$listener->onConsoleException($event);
},
];
}

/**
* @dataProvider eventMethodProvider
*
* @param \Closure $methodCallback
*/
public function testLogError(Closure $methodCallback): void
{
$commandName = 'hello:world';
$message = 'Foobar!';
Expand All @@ -35,8 +60,10 @@ public function testLogError(): void
$output = $this->createMock(OutputInterface::class);
$event = new ConsoleErrorEvent($input, $output, $exception, $command);

$listener = new ConsoleExceptionListener($logger, $logLevel);
$listener->onConsoleException($event);
$methodCallback(
new ConsoleExceptionListener($logger, $logLevel),
$event
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testExceptionsEnabledByDefault(): void

$this->assertContainerBuilderHasService('vasek_purchart.console_errors.console.console_exception_listener', ConsoleExceptionListener::class);
$this->assertContainerBuilderHasServiceDefinitionWithTag('vasek_purchart.console_errors.console.console_exception_listener', 'kernel.event_listener', [
'event' => 'console.exception',
'event' => 'console.error',
'priority' => '%' . ConsoleErrorsExtension::CONTAINER_PARAMETER_EXCEPTION_LISTENER_PRIORITY . '%',
]);

Expand Down Expand Up @@ -55,7 +55,7 @@ public function testExceptionsEnabled(): void

$this->assertContainerBuilderHasService('vasek_purchart.console_errors.console.console_exception_listener', ConsoleExceptionListener::class);
$this->assertContainerBuilderHasServiceDefinitionWithTag('vasek_purchart.console_errors.console.console_exception_listener', 'kernel.event_listener', [
'event' => 'console.exception',
'event' => 'console.error',
'priority' => '%' . ConsoleErrorsExtension::CONTAINER_PARAMETER_EXCEPTION_LISTENER_PRIORITY . '%',
]);

Expand Down

0 comments on commit 317f600

Please sign in to comment.