‪TYPO3CMS  10.4
ServiceProviderRegistryTest.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 Prophecy\Argument;
21 use Psr\Container\ContainerInterface;
24 use TYPO3\CMS\Core\Package\PackageManager;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
29 class ‪ServiceProviderRegistryTest extends UnitTestCase
30 {
35 
39  protected function ‪setUp(): void
40  {
41  $this->packageManagerProphecy = $this->prophesize(PackageManager::class);
42  $this->packageManagerProphecy->isPackageActive(Argument::any())->willReturn(false);
43  $this->packageManagerProphecy->getActivePackages()->willReturn([]);
44  }
45 
46  protected function ‪mockPackages($packages)
47  {
48  $active = [];
49  foreach ($packages as $packageKey => $serviceProvider) {
50  $this->packageManagerProphecy->isPackageActive($packageKey)->willReturn(true);
51 
52  $package = $this->prophesize(Package::class);
53  $package->getPackageKey()->willReturn($packageKey);
54  $package->getServiceProvider()->willReturn($serviceProvider);
55 
56  $this->packageManagerProphecy->getPackage($packageKey)->willReturn($package->reveal());
57  $active[] = $package->reveal();
58  }
59 
60  $this->packageManagerProphecy->getActivePackages()->willReturn($active);
61  }
62 
63  public function ‪testRegistry()
64  {
65  $this->‪mockPackages(['core' => TestRegistryServiceProvider::class]);
66  $registry = new ‪ServiceProviderRegistry($this->packageManagerProphecy->reveal());
67 
68  self::assertEquals(new ‪TestRegistryServiceProvider(), $registry->get('core'));
69  }
70 
71  public function ‪testRegistryCaches()
72  {
73  $this->‪mockPackages(['core' => TestRegistryServiceProvider::class]);
74  $registry = new ‪ServiceProviderRegistry($this->packageManagerProphecy->reveal());
75 
76  self::assertEquals(new ‪TestRegistryServiceProvider(), $registry->get('core'));
77  self::assertSame($registry->get('core'), $registry->get('core'));
78  }
79 
81  {
82  $this->‪mockPackages(['core' => TestStatefulServiceProvider::class]);
83  $registry = new ‪ServiceProviderRegistry($this->packageManagerProphecy->reveal());
84 
85  self::assertInstanceOf(TestStatefulServiceProvider::class, $registry->get('core'));
86  self::assertInstanceOf(Package::class, $registry->get('core')->package);
87  }
88 
89  public function ‪testGetException()
90  {
91  $this->expectException(\InvalidArgumentException::class);
92  $this->‪mockPackages(['core' => TestRegistryServiceProvider::class]);
93  $registry = new ‪ServiceProviderRegistry($this->packageManagerProphecy->reveal());
94 
95  $registry->get('backend');
96  }
97 
98  public function ‪testGetServices()
99  {
100  $this->‪mockPackages(['core' => TestRegistryServiceProvider::class]);
101  $registry = new ‪ServiceProviderRegistry($this->packageManagerProphecy->reveal());
102 
103  $services = $registry->getFactories('core');
104  self::assertArrayHasKey('serviceA', $services);
105 
106  $services2 = $registry->getFactories('core');
107 
108  self::assertSame($services['serviceA'], $services2['serviceA']);
109  }
110 
111  public function ‪testExtendServices()
112  {
113  $this->‪mockPackages(['core' => TestRegistryServiceProvider::class]);
114  $registry = new ‪ServiceProviderRegistry($this->packageManagerProphecy->reveal());
115 
116  $services = $registry->getExtensions('core');
117  self::assertArrayHasKey('serviceB', $services);
118 
119  $services2 = $registry->getExtensions('core');
120 
121  self::assertSame($services['serviceB'], $services2['serviceB']);
122  }
123 
124  public function ‪testGetServiceFactory()
125  {
126  $this->‪mockPackages(['core' => TestRegistryServiceProvider::class]);
127  $registry = new ‪ServiceProviderRegistry($this->packageManagerProphecy->reveal());
128 
129  $containerProphecy = $this->prophesize(ContainerInterface::class);
130  $service = $registry->createService('core', 'param', $containerProphecy->reveal());
131 
132  self::assertEquals(42, $service);
133  }
134 
135  public function ‪testGetServiceExtension()
136  {
137  $this->‪mockPackages(['core' => TestRegistryServiceProvider::class]);
138  $registry = new ‪ServiceProviderRegistry($this->packageManagerProphecy->reveal());
139 
140  $containerProphecy = $this->prophesize(ContainerInterface::class);
141  $service = $registry->extendService('core', 'serviceB', $containerProphecy->reveal(), null);
142 
143  self::assertInstanceOf(\stdClass::class, $service);
144  }
145 
146  public function ‪testIterator()
147  {
148  $packages = [
149  'core' => TestRegistryServiceProvider::class,
150  'backend' => TestRegistryServiceProvider::class
151  ];
152  $this->‪mockPackages($packages);
153  $registry = new ‪ServiceProviderRegistry($this->packageManagerProphecy->reveal());
154 
155  $i = 0;
156  foreach ($registry as $key => $provider) {
157  self::assertEquals(array_keys($packages)[$i], $key);
158  self::assertInstanceOf(TestRegistryServiceProvider::class, $registry->get($key));
159  $i++;
160  }
161  }
162 }
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\Fixtures\TestRegistryServiceProvider
Definition: TestRegistryServiceProvider.php:23
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ServiceProviderRegistryTest\testRegistryPassesPackageAsConstructorArgument
‪testRegistryPassesPackageAsConstructorArgument()
Definition: ServiceProviderRegistryTest.php:79
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ServiceProviderRegistryTest\testExtendServices
‪testExtendServices()
Definition: ServiceProviderRegistryTest.php:110
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ServiceProviderRegistryTest\testRegistryCaches
‪testRegistryCaches()
Definition: ServiceProviderRegistryTest.php:70
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ServiceProviderRegistryTest\testIterator
‪testIterator()
Definition: ServiceProviderRegistryTest.php:145
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ServiceProviderRegistryTest
Definition: ServiceProviderRegistryTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ServiceProviderRegistryTest\testGetServiceExtension
‪testGetServiceExtension()
Definition: ServiceProviderRegistryTest.php:134
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ServiceProviderRegistryTest\testGetServices
‪testGetServices()
Definition: ServiceProviderRegistryTest.php:97
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ServiceProviderRegistryTest\testGetServiceFactory
‪testGetServiceFactory()
Definition: ServiceProviderRegistryTest.php:123
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ServiceProviderRegistryTest\testGetException
‪testGetException()
Definition: ServiceProviderRegistryTest.php:88
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection
Definition: FailsafeContainerTest.php:18
‪TYPO3\CMS\Core\Package\Package
Definition: Package.php:26
‪TYPO3\CMS\Core\DependencyInjection\ServiceProviderRegistry
Definition: ServiceProviderRegistry.php:31
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ServiceProviderRegistryTest\mockPackages
‪mockPackages($packages)
Definition: ServiceProviderRegistryTest.php:45
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ServiceProviderRegistryTest\testRegistry
‪testRegistry()
Definition: ServiceProviderRegistryTest.php:62
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ServiceProviderRegistryTest\setUp
‪setUp()
Definition: ServiceProviderRegistryTest.php:38
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\Fixtures\TestStatefulServiceProvider
Definition: TestStatefulServiceProvider.php:24
‪TYPO3\CMS\Core\Tests\Unit\DependencyInjection\ServiceProviderRegistryTest\$packageManagerProphecy
‪PackageManager Prophecy Prophecy ObjectProphecy $packageManagerProphecy
Definition: ServiceProviderRegistryTest.php:33