|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\FrameworkBundle\Tests\Command; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Command\EventDispatcherDebugCommand; |
| 16 | +use Symfony\Component\Console\Tester\CommandCompletionTester; |
| 17 | +use Symfony\Component\DependencyInjection\ServiceLocator; |
| 18 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 19 | + |
| 20 | +class EventDispatcherDebugCommandCompletionTest extends TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @dataProvider provideCompletionSuggestions |
| 24 | + */ |
| 25 | + public function testComplete(array $input, array $expectedSuggestions) |
| 26 | + { |
| 27 | + $tester = $this->createCommandCompletionTester(); |
| 28 | + |
| 29 | + $suggestions = $tester->complete($input); |
| 30 | + |
| 31 | + $this->assertSame($expectedSuggestions, $suggestions); |
| 32 | + } |
| 33 | + |
| 34 | + public function provideCompletionSuggestions() |
| 35 | + { |
| 36 | + yield 'event' => [[''], ['Symfony\Component\Mailer\Event\MessageEvent', 'console.command']]; |
| 37 | + yield 'event for other dispatcher' => [['--dispatcher', 'other_event_dispatcher', ''], ['other_event', 'App\OtherEvent']]; |
| 38 | + yield 'dispatcher' => [['--dispatcher='], ['event_dispatcher', 'other_event_dispatcher']]; |
| 39 | + yield 'format' => [['--format='], ['txt', 'xml', 'json', 'md']]; |
| 40 | + } |
| 41 | + |
| 42 | + private function createCommandCompletionTester(): CommandCompletionTester |
| 43 | + { |
| 44 | + $dispatcher = new EventDispatcher(); |
| 45 | + $otherDispatcher = new EventDispatcher(); |
| 46 | + |
| 47 | + $dispatcher->addListener('event', 'Listener'); |
| 48 | + $otherDispatcher->addListener('other_event', 'OtherListener'); |
| 49 | + |
| 50 | + $dispatchers = new ServiceLocator([ |
| 51 | + 'event_dispatcher' => function () { |
| 52 | + $dispatcher = new EventDispatcher(); |
| 53 | + $dispatcher->addListener('Symfony\Component\Mailer\Event\MessageEvent', 'var_dump'); |
| 54 | + $dispatcher->addListener('console.command', 'var_dump'); |
| 55 | + |
| 56 | + return $dispatcher; |
| 57 | + }, |
| 58 | + 'other_event_dispatcher' => function () { |
| 59 | + $dispatcher = new EventDispatcher(); |
| 60 | + $dispatcher->addListener('other_event', 'var_dump'); |
| 61 | + $dispatcher->addListener('App\OtherEvent', 'var_dump'); |
| 62 | + |
| 63 | + return $dispatcher; |
| 64 | + }, |
| 65 | + ]); |
| 66 | + $command = new EventDispatcherDebugCommand($dispatchers); |
| 67 | + |
| 68 | + return new CommandCompletionTester($command); |
| 69 | + } |
| 70 | +} |
0 commit comments