‪TYPO3CMS  ‪main
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 Psr\Container\ContainerInterface;
21 use Psr\Log\LoggerInterface;
22 use Psr\Log\NullLogger;
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
32 final class ‪AbstractServiceProviderTest extends UnitTestCase
33 {
38  {
39  $containerMock = $this->createMock(ContainerInterface::class);
40 
41  $middlewares = new \ArrayObject();
42  $middlewares = ‪Package1ServiceProviderMock::configureMiddlewares($containerMock, $middlewares);
43  $middlewares = ‪Package2ServiceProviderMock::configureMiddlewares($containerMock, $middlewares);
44 
45  $expected = new \ArrayObject([
46  'testStack' => [
47  'firstMiddleware' => [
48  'target' => 'aClassName',
49  ],
50  'secondMiddleware' => [
51  'target' => 'anotherClassName',
52  ],
53  ],
54  ]);
55  self::assertEquals($expected, $middlewares);
56  }
57 
62  {
63  $containerMock = $this->createMock(ContainerInterface::class);
64 
65  $package2 = $this->createMock(Package::class);
66  $package2->method('getPackagePath')->willReturn(__DIR__ . '/../Http/Fixtures/Package2/');
67  $package2->method('getValueFromComposerManifest')->with('name')->willReturn('typo3/cms-testing');
68  $package2ServiceProvider = new ‪PseudoServiceProvider($package2);
69 
70  $middlewares = new \ArrayObject();
71  $middlewares = ‪Package1ServiceProviderMock::configureMiddlewares($containerMock, $middlewares);
72  $middlewares = $package2ServiceProvider->getExtensions()['middlewares']($containerMock, $middlewares);
73 
74  $expected = new \ArrayObject([
75  'testStack' => [
76  'firstMiddleware' => [
77  'target' => 'aClassName',
78  ],
79  'secondMiddleware' => [
80  'target' => 'anotherClassName',
81  ],
82  ],
83  ]);
84  self::assertEquals($expected, $middlewares);
85  }
86 
91  {
92  $containerMock = $this->createMock(ContainerInterface::class);
93 
94  $package2 = $this->createMock(Package::class);
95  $package2->method('getPackagePath')->willReturn(__DIR__ . '/../Http/Fixtures/Package2Disables1/');
96  $package2->method('getValueFromComposerManifest')->with('name')->willReturn('typo3/cms-testing');
97  $package2ServiceProvider = new ‪PseudoServiceProvider($package2);
98 
99  $middlewares = new \ArrayObject();
100  $middlewares = ‪Package1ServiceProviderMock::configureMiddlewares($containerMock, $middlewares);
101  $middlewares = $package2ServiceProvider->getExtensions()['middlewares']($containerMock, $middlewares);
102 
103  $expected = new \ArrayObject([
104  'testStack' => [
105  'firstMiddleware' => [
106  'target' => 'aClassName',
107  'disabled' => true,
108  ],
109  'secondMiddleware' => [
110  'target' => 'anotherClassName',
111  ],
112  ],
113  ]);
114  self::assertEquals($expected, $middlewares);
115  }
116 
121  {
122  $containerMock = $this->createMock(ContainerInterface::class);
123 
124  $package2 = $this->createMock(Package::class);
125  $package2->method('getPackagePath')->willReturn(__DIR__ . '/../Http/Fixtures/Package2Replaces1/');
126  $package2->method('getValueFromComposerManifest')->with('name')->willReturn('typo3/cms-testing');
127  $package2ServiceProvider = new ‪PseudoServiceProvider($package2);
128 
129  $middlewares = new \ArrayObject();
130  $middlewares = ‪Package1ServiceProviderMock::configureMiddlewares($containerMock, $middlewares);
131  $middlewares = $package2ServiceProvider->getExtensions()['middlewares']($containerMock, $middlewares);
132 
133  $expected = new \ArrayObject([
134  'testStack' => [
135  'firstMiddleware' => [
136  'target' => 'replacedClassName',
137  ],
138  'secondMiddleware' => [
139  'target' => 'anotherClassName',
140  ],
141  ],
142  ]);
143  self::assertEquals($expected, $middlewares);
144  }
145 
149  public function ‪newReturnsClassInstance(): void
150  {
151  $newClosure = $this->‪getClosureForNew();
152  $instance = $newClosure($this->createMock(ContainerInterface::class), \stdClass::class);
153  self::assertInstanceOf(\stdClass::class, $instance);
154  }
155 
159  public function ‪newInjectsLogger(): void
160  {
161  $containerMock = $this->createMock(ContainerInterface::class);
162 
163  $logManagerMock = $this->createMock(LogManager::class);
164  $logManagerMock->method('getLogger')->with(GeneralUtilityMakeInstanceInjectLoggerFixture::class)
165  ->willReturn(new NullLogger());
166  $containerMock->method('get')->with(LogManager::class)->willReturn($logManagerMock);
167  $className = GeneralUtilityMakeInstanceInjectLoggerFixture::class;
168  $newClosure = $this->‪getClosureForNew();
169  $instance = $newClosure($containerMock, $className);
170  self::assertInstanceOf(LoggerInterface::class, $instance->getLogger());
171  }
172 
173  protected function ‪getClosureForNew(): \Closure
174  {
175  return \Closure::bind(
176  static function ($container, $className, $arguments = []) {
177  return ‪AbstractServiceProvider::new($container, $className, $arguments);
178  },
179  null,
180  AbstractServiceProvider::class
181  );
182  }
183 }
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\configureMiddlewares
‪static configureMiddlewares(ContainerInterface $container, \ArrayObject $middlewares, string $path=null)
Definition: AbstractServiceProvider.php:66
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\configureMiddlewaresReturnsMergedMiddlewaresWithOverrides
‪configureMiddlewaresReturnsMergedMiddlewaresWithOverrides()
Definition: AbstractServiceProviderTest.php:90
‪TYPO3\CMS\Core\Package\AbstractServiceProvider
Definition: AbstractServiceProvider.php:35
‪TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityMakeInstanceInjectLoggerFixture
Definition: GeneralUtilityMakeInstanceInjectLoggerFixture.php:28
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest
Definition: AbstractServiceProviderTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Package\Mocks\Package2ServiceProviderMock
Definition: Package2ServiceProviderMock.php:23
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\getClosureForNew
‪getClosureForNew()
Definition: AbstractServiceProviderTest.php:173
‪TYPO3\CMS\Core\Package\AbstractServiceProvider\new
‪static mixed new(ContainerInterface $container, string $className, array $constructorArguments=[])
Definition: AbstractServiceProvider.php:183
‪TYPO3\CMS\Core\Tests\Unit\Package
Definition: AbstractServiceProviderTest.php:18
‪TYPO3\CMS\Core\Package\Package
Definition: Package.php:30
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\newReturnsClassInstance
‪newReturnsClassInstance()
Definition: AbstractServiceProviderTest.php:149
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\newInjectsLogger
‪newInjectsLogger()
Definition: AbstractServiceProviderTest.php:159
‪TYPO3\CMS\Core\Package\PseudoServiceProvider
Definition: PseudoServiceProvider.php:26
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:33
‪TYPO3\CMS\Core\Tests\Unit\Package\Mocks\Package1ServiceProviderMock
Definition: Package1ServiceProviderMock.php:23
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\configureMiddlewaresReturnsMergedMiddlewaresWithReplacements
‪configureMiddlewaresReturnsMergedMiddlewaresWithReplacements()
Definition: AbstractServiceProviderTest.php:120
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\configureMiddlewaresReturnsMergedMiddlewaresWithPseudoServiceProvider
‪configureMiddlewaresReturnsMergedMiddlewaresWithPseudoServiceProvider()
Definition: AbstractServiceProviderTest.php:61
‪TYPO3\CMS\Core\Tests\Unit\Package\AbstractServiceProviderTest\configureMiddlewaresReturnsMergedMiddlewares
‪configureMiddlewaresReturnsMergedMiddlewares()
Definition: AbstractServiceProviderTest.php:37