‪TYPO3CMS  10.4
AbstractServiceProviderTest.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 ArrayObject;
21 use Psr\Container\ContainerInterface;
22 use Psr\Log\LoggerInterface;
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
35 class ‪AbstractServiceProviderTest extends UnitTestCase
36 {
41  {
42  $containerProphecy = $this->prophesize();
43  $containerProphecy->willImplement(ContainerInterface::class);
44 
45  $middlewares = new ArrayObject();
47  $containerProphecy->reveal(),
48  $middlewares
49  );
51  $containerProphecy->reveal(),
52  $middlewares
53  );
54 
55  $expected = new ArrayObject([
56  'testStack' => [
57  'firstMiddleware' => [
58  'target' => 'aClassName',
59  ],
60  'secondMiddleware' => [
61  'target' => 'anotherClassName',
62  ],
63  ],
64  ]);
65  self::assertEquals($expected, $middlewares);
66  }
67 
72  {
73  $containerProphecy = $this->prophesize();
74  $containerProphecy->willImplement(ContainerInterface::class);
75 
76  $package2 = $this->prophesize(Package::class);
77  $package2->getPackagePath()->willReturn(__DIR__ . '/../Http/Fixtures/Package2/');
78  $package2ServiceProvider = new ‪PseudoServiceProvider($package2->reveal());
79 
80  $middlewares = new ArrayObject();
82  $containerProphecy->reveal(),
83  $middlewares
84  );
85  $middlewares = $package2ServiceProvider->getExtensions()['middlewares'](
86  $containerProphecy->reveal(),
87  $middlewares
88  );
89 
90  $expected = new ArrayObject([
91  'testStack' => [
92  'firstMiddleware' => [
93  'target' => 'aClassName',
94  ],
95  'secondMiddleware' => [
96  'target' => 'anotherClassName',
97  ],
98  ],
99  ]);
100  self::assertEquals($expected, $middlewares);
101  }
102 
107  {
108  $containerProphecy = $this->prophesize();
109  $containerProphecy->willImplement(ContainerInterface::class);
110 
111  $package2 = $this->prophesize(Package::class);
112  $package2->getPackagePath()->willReturn(__DIR__ . '/../Http/Fixtures/Package2Disables1/');
113  $package2ServiceProvider = new ‪PseudoServiceProvider($package2->reveal());
114 
115  $middlewares = new ArrayObject();
117  $containerProphecy->reveal(),
118  $middlewares
119  );
120  $middlewares = $package2ServiceProvider->getExtensions()['middlewares'](
121  $containerProphecy->reveal(),
122  $middlewares
123  );
124 
125  $expected = new ArrayObject([
126  'testStack' => [
127  'firstMiddleware' => [
128  'target' => 'aClassName',
129  'disabled' => true,
130  ],
131  'secondMiddleware' => [
132  'target' => 'anotherClassName',
133  ],
134  ],
135  ]);
136  self::assertEquals($expected, $middlewares);
137  }
138 
143  {
144  $containerProphecy = $this->prophesize();
145  $containerProphecy->willImplement(ContainerInterface::class);
146 
147  $package2 = $this->prophesize(Package::class);
148  $package2->getPackagePath()->willReturn(__DIR__ . '/../Http/Fixtures/Package2Replaces1/');
149  $package2ServiceProvider = new ‪PseudoServiceProvider($package2->reveal());
150 
151  $middlewares = new ArrayObject();
153  $containerProphecy->reveal(),
154  $middlewares
155  );
156  $middlewares = $package2ServiceProvider->getExtensions()['middlewares'](
157  $containerProphecy->reveal(),
158  $middlewares
159  );
160 
161  $expected = new ArrayObject([
162  'testStack' => [
163  'firstMiddleware' => [
164  'target' => 'replacedClassName',
165  ],
166  'secondMiddleware' => [
167  'target' => 'anotherClassName',
168  ],
169  ],
170  ]);
171  self::assertEquals($expected, $middlewares);
172  }
173 
177  public function ‪newReturnsClassInstance()
178  {
179  $containerProphecy = $this->prophesize();
180  $containerProphecy->willImplement(ContainerInterface::class);
181 
182  $className = get_class($this->getMockBuilder('foo')->getMock());
183  $newClosure = $this->‪getClosureForNew();
184  $instance = $newClosure($containerProphecy->reveal(), $className);
185  self::assertTrue($instance instanceof $className);
186  }
187 
191  public function ‪newInjectsLogger()
192  {
193  $containerProphecy = $this->prophesize();
194  $containerProphecy->willImplement(ContainerInterface::class);
195 
196  $loggerProphecy = $this->prophesize();
197  $loggerProphecy->willImplement(LoggerInterface::class);
198 
199  $logManagerProphecy = $this->prophesize(LogManager::class);
200  $logManagerProphecy->getLogger(GeneralUtilityMakeInstanceInjectLoggerFixture::class)->willReturn($loggerProphecy->reveal());
201 
202  $containerProphecy->get(LogManager::class)->willReturn($logManagerProphecy->reveal());
203  $className = GeneralUtilityMakeInstanceInjectLoggerFixture::class;
204  $newClosure = $this->‪getClosureForNew();
205  $instance = $newClosure($containerProphecy->reveal(), $className);
206  self::assertInstanceOf(LoggerInterface::class, $instance->getLogger());
207  }
208 
212  protected function ‪getClosureForNew(): \Closure
213  {
214  return \Closure::bind(
215  function ($container, $className, $arguments = []) {
216  return ‪AbstractServiceProvider::new($container, $className, $arguments);
217  },
218  null,
219  AbstractServiceProvider::class
220  );
221  }
222 }
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\configureMiddlewaresReturnsMergedMiddlewaresWithOverrides
‪configureMiddlewaresReturnsMergedMiddlewaresWithOverrides()
Definition: AbstractServiceProviderTest.php:106
‪TYPO3\CMS\Core\Package\AbstractServiceProvider
Definition: AbstractServiceProvider.php:31
‪TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityMakeInstanceInjectLoggerFixture
Definition: GeneralUtilityMakeInstanceInjectLoggerFixture.php:28
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest
Definition: AbstractServiceProviderTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Package\Mocks\Package2ServiceProviderMock
Definition: Package2ServiceProviderMock.php:23
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\new
‪static mixed new(ContainerInterface $container, string $className, array $constructorArguments=[])
Definition: AbstractServiceProvider.php:116
‪TYPO3\CMS\Core\Tests\Unit\Package
Definition: AbstractServiceProviderTest.php:18
‪TYPO3\CMS\Core\Package\Package
Definition: Package.php:26
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\newReturnsClassInstance
‪newReturnsClassInstance()
Definition: AbstractServiceProviderTest.php:177
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\newInjectsLogger
‪newInjectsLogger()
Definition: AbstractServiceProviderTest.php:191
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\configureMiddlewares
‪static ArrayObject configureMiddlewares(ContainerInterface $container, ArrayObject $middlewares, string $path=null)
Definition: AbstractServiceProvider.php:63
‪TYPO3\CMS\Core\Package\PseudoServiceProvider
Definition: PseudoServiceProvider.php:26
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:30
‪TYPO3\CMS\Core\Tests\Unit\Package\Mocks\Package1ServiceProviderMock
Definition: Package1ServiceProviderMock.php:23
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\getClosureForNew
‪Closure getClosureForNew()
Definition: AbstractServiceProviderTest.php:212
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\configureMiddlewaresReturnsMergedMiddlewaresWithReplacements
‪configureMiddlewaresReturnsMergedMiddlewaresWithReplacements()
Definition: AbstractServiceProviderTest.php:142
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\configureMiddlewaresReturnsMergedMiddlewaresWithPseudoServiceProvider
‪configureMiddlewaresReturnsMergedMiddlewaresWithPseudoServiceProvider()
Definition: AbstractServiceProviderTest.php:71
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\configureMiddlewaresReturnsMergedMiddlewares
‪configureMiddlewaresReturnsMergedMiddlewares()
Definition: AbstractServiceProviderTest.php:40