‪TYPO3CMS  10.4
ListenerProviderPassTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Symfony\Component\Config\FileLocator;
21 use Symfony\Component\DependencyInjection\ContainerBuilder;
22 use Symfony\Component\DependencyInjection\Definition;
23 use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
24 use Symfony\Component\DependencyInjection\Reference;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
29 class ‪ListenerProviderPassTest extends UnitTestCase
30 {
31  protected function ‪getContainerWithListenerProvider(array $packages = [])
32  {
33  $container = new ContainerBuilder();
34 
35  foreach ($packages as $package) {
36  $yamlFileLoader = new YamlFileLoader($container, new FileLocator($package . '/Configuration'));
37  $yamlFileLoader->load('Services.yaml');
38  }
39 
40  $listenerProvider = new Definition(ListenerProvider::class);
41  $listenerProvider->setPublic(true);
42  $listenerProvider->setArguments([
43  new Reference('service_container')
44  ]);
45 
46  $container->setDefinition(ListenerProvider::class, $listenerProvider);
47 
48  $container->addCompilerPass(new ‪ListenerProviderPass('event.listener'));
49  $container->compile();
50 
51  return $container;
52  }
53 
55  {
56  $container = $this->‪getContainerWithListenerProvider([
57  __DIR__ . '/Fixtures/Package1',
58  __DIR__ . '/Fixtures/Package2',
59  __DIR__ . '/Fixtures/Package3',
60  ]);
61 
62  $listenerProvider = $container->get(ListenerProvider::class);
63  $listeners = $listenerProvider->getAllListenerDefinitions();
64 
65  self::assertEquals(
66  [
67  'TYPO3\\CMS\\Core\\Mail\\Event\\AfterMailerInitializationEvent' => [
68  [
69  'service' => 'package2.listener',
70  'method' => 'onEvent',
71  ],
72  [
73  'service' => 'package1.listener1',
74  'method' => null,
75  ]
76  ],
77  'TYPO3\\CMS\\Core\\Foo\\Event\\TestEvent' => [
78  [
79  'service' => 'package3.listener',
80  'method' => null,
81  ]
82  ],
83  ],
84  $listeners
85  );
86  }
87 
88  public function ‪testCycleException()
89  {
90  $this->expectException(\UnexpectedValueException::class);
91  $this->expectExceptionMessage('Your dependencies have cycles. That will not work out. Cycles found: legacy-hook->package4.listener, package4.listener->legacy-hook');
92 
93  $container = $this->‪getContainerWithListenerProvider([
94  __DIR__ . '/Fixtures/Package1',
95  __DIR__ . '/Fixtures/Package4Cycle',
96  ]);
97  }
98 
99  public function ‪testWithoutConfiguration()
100  {
101  $container = $this->‪getContainerWithListenerProvider([]);
102 
103  $listenerProvider = $container->get(ListenerProvider::class);
104  $listeners = $listenerProvider->getAllListenerDefinitions();
105 
106  self::assertEquals([], $listeners);
107  }
108 }
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ListenerProviderPassTest\testCycleException
‪testCycleException()
Definition: ListenerProviderPassTest.php:88
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ListenerProviderPassTest\testWithoutConfiguration
‪testWithoutConfiguration()
Definition: ListenerProviderPassTest.php:99
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection
Definition: FailsafeContainerTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ListenerProviderPassTest
Definition: ListenerProviderPassTest.php:30
‪TYPO3\CMS\Core\DependencyInjection\ListenerProviderPass
Definition: ListenerProviderPass.php:30
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ListenerProviderPassTest\getContainerWithListenerProvider
‪getContainerWithListenerProvider(array $packages=[])
Definition: ListenerProviderPassTest.php:31
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider
Definition: ListenerProvider.php:30
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ListenerProviderPassTest\testSimpleChainsAndDependencies
‪testSimpleChainsAndDependencies()
Definition: ListenerProviderPassTest.php:54