-
-
Notifications
You must be signed in to change notification settings - Fork 264
/
Copy pathConsoleEventsTest.php
99 lines (86 loc) · 3.43 KB
/
ConsoleEventsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Console\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConsoleEventsTest extends TestCase
{
protected function tearDown(): void
{
if (\function_exists('pcntl_signal')) {
pcntl_async_signals(false);
// We reset all signals to their default value to avoid side effects
pcntl_signal(\SIGINT, \SIG_DFL);
pcntl_signal(\SIGTERM, \SIG_DFL);
pcntl_signal(\SIGUSR1, \SIG_DFL);
pcntl_signal(\SIGUSR2, \SIG_DFL);
pcntl_signal(\SIGALRM, \SIG_DFL);
}
}
public function testEventAliases()
{
$container = new ContainerBuilder();
$container->setParameter('event_dispatcher.event_aliases', ConsoleEvents::ALIASES);
$container->addCompilerPass(new RegisterListenersPass());
$container->register('event_dispatcher', EventDispatcher::class);
$container->register('tracer', EventTraceSubscriber::class)
->setPublic(true)
->addTag('kernel.event_subscriber');
$container->register('failing_command', FailingCommand::class);
$container->register('application', Application::class)
->setPublic(true)
->addMethodCall('setAutoExit', [false])
->addMethodCall('setDispatcher', [new Reference('event_dispatcher')])
->addMethodCall('add', [new Reference('failing_command')])
;
$container->compile();
$tester = new ApplicationTester($container->get('application'));
$tester->run(['fail']);
$this->assertSame([ConsoleCommandEvent::class, ConsoleErrorEvent::class, ConsoleTerminateEvent::class], $container->get('tracer')->observedEvents);
}
}
class EventTraceSubscriber implements EventSubscriberInterface
{
public array $observedEvents = [];
public static function getSubscribedEvents(): array
{
return [
ConsoleCommandEvent::class => 'observe',
ConsoleErrorEvent::class => 'observe',
ConsoleTerminateEvent::class => 'observe',
];
}
public function observe(object $event): void
{
$this->observedEvents[] = get_debug_type($event);
}
}
#[AsCommand(name: 'fail')]
class FailingCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
throw new \RuntimeException('I failed. Sorry.');
}
}