[FEATURE] Add symfony dependency injection for core and extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Http / MiddlewareStackResolverTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Http;
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
18 use Prophecy\Argument;
19 use Psr\Container\ContainerInterface;
20 use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend;
21 use TYPO3\CMS\Core\Http\MiddlewareStackResolver;
22 use TYPO3\CMS\Core\Service\DependencyOrderingService;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24
25 /**
26 * Test case
27 */
28 class MiddlewareStackResolverTest extends UnitTestCase
29 {
30 /**
31 * @test
32 */
33 public function resolveReturnsMiddlewareStack()
34 {
35 $middlewares = array_replace_recursive(
36 [],
37 require __DIR__ . '/Fixtures/Package1/Configuration/RequestMiddlewares.php',
38 require __DIR__ . '/Fixtures/Package2/Configuration/RequestMiddlewares.php'
39 );
40 $containerProphecy = $this->prophesize();
41 $containerProphecy->willImplement(ContainerInterface::class);
42 $containerProphecy->get('middlewares')->willReturn($middlewares);
43 $dependencyOrderingServiceProphecy = $this->prophesize(DependencyOrderingService::class);
44 $dependencyOrderingServiceProphecy->orderByDependencies(Argument::cetera())->willReturnArgument(0);
45 $phpFrontendCacheProphecy = $this->prophesize(PhpFrontend::class);
46 $phpFrontendCacheProphecy->has(Argument::cetera())->willReturn(false);
47 $phpFrontendCacheProphecy->set(Argument::cetera())->willReturn(false);
48
49 $subject = new MiddlewareStackResolver(
50 $containerProphecy->reveal(),
51 $dependencyOrderingServiceProphecy->reveal(),
52 $phpFrontendCacheProphecy->reveal()
53 );
54 $expected = [
55 'secondMiddleware' => 'anotherClassName',
56 'firstMiddleware' => 'aClassName',
57 ];
58 $this->assertEquals($expected, $subject->resolve('testStack'));
59 }
60
61 /**
62 * @test
63 */
64 public function resolveReturnsEmptyMiddlewareStackForZeroPackages()
65 {
66 $middlewares = [];
67 $containerProphecy = $this->prophesize();
68 $containerProphecy->willImplement(ContainerInterface::class);
69 $containerProphecy->get('middlewares')->willReturn($middlewares);
70 $dependencyOrderingServiceProphecy = $this->prophesize(DependencyOrderingService::class);
71 $dependencyOrderingServiceProphecy->orderByDependencies(Argument::cetera())->willReturnArgument(0);
72 $phpFrontendCacheProphecy = $this->prophesize(PhpFrontend::class);
73 $phpFrontendCacheProphecy->has(Argument::cetera())->willReturn(false);
74 $phpFrontendCacheProphecy->set(Argument::cetera())->willReturn(false);
75
76 $subject = new MiddlewareStackResolver(
77 $containerProphecy->reveal(),
78 $dependencyOrderingServiceProphecy->reveal(),
79 $phpFrontendCacheProphecy->reveal()
80 );
81 // empty array expected
82 $expected = [];
83 $this->assertEquals($expected, $subject->resolve('testStack'));
84 }
85
86 /**
87 * @test
88 */
89 public function resolveAllowsDisablingAMiddleware()
90 {
91 $middlewares = array_replace_recursive(
92 [],
93 require __DIR__ . '/Fixtures/Package1/Configuration/RequestMiddlewares.php',
94 require __DIR__ . '/Fixtures/Package2Disables1/Configuration/RequestMiddlewares.php'
95 );
96 $containerProphecy = $this->prophesize();
97 $containerProphecy->willImplement(ContainerInterface::class);
98 $containerProphecy->get('middlewares')->willReturn($middlewares);
99 $dependencyOrderingServiceProphecy = $this->prophesize(DependencyOrderingService::class);
100 $dependencyOrderingServiceProphecy->orderByDependencies(Argument::cetera())->willReturnArgument(0);
101 $phpFrontendCacheProphecy = $this->prophesize(PhpFrontend::class);
102 $phpFrontendCacheProphecy->has(Argument::cetera())->willReturn(false);
103 $phpFrontendCacheProphecy->set(Argument::cetera())->willReturn(false);
104
105 $subject = new MiddlewareStackResolver(
106 $containerProphecy->reveal(),
107 $dependencyOrderingServiceProphecy->reveal(),
108 $phpFrontendCacheProphecy->reveal()
109 );
110 $expected = [
111 // firstMiddleware is missing, RequestMiddlewares.php of Package2 sets disables=true on firstMiddleware
112 'secondMiddleware' => 'anotherClassName',
113 ];
114 $this->assertEquals($expected, $subject->resolve('testStack'));
115 }
116
117 /**
118 * @test
119 */
120 public function resolveAllowsReplacingAMiddleware()
121 {
122 $middlewares = array_replace_recursive(
123 [],
124 require __DIR__ . '/Fixtures/Package1/Configuration/RequestMiddlewares.php',
125 require __DIR__ . '/Fixtures/Package2Replaces1/Configuration/RequestMiddlewares.php'
126 );
127 $containerProphecy = $this->prophesize();
128 $containerProphecy->willImplement(ContainerInterface::class);
129 $containerProphecy->get('middlewares')->willReturn($middlewares);
130 $dependencyOrderingServiceProphecy = $this->prophesize(DependencyOrderingService::class);
131 $dependencyOrderingServiceProphecy->orderByDependencies(Argument::cetera())->willReturnArgument(0);
132 $phpFrontendCacheProphecy = $this->prophesize(PhpFrontend::class);
133 $phpFrontendCacheProphecy->has(Argument::cetera())->willReturn(false);
134 $phpFrontendCacheProphecy->set(Argument::cetera())->willReturn(false);
135
136 $subject = new MiddlewareStackResolver(
137 $containerProphecy->reveal(),
138 $dependencyOrderingServiceProphecy->reveal(),
139 $phpFrontendCacheProphecy->reveal()
140 );
141 $expected = [
142 // firstMiddleware has been replaced, RequestMiddlewares.php of $package2 sets a new value for firstMiddleware
143 'firstMiddleware' => 'replacedClassName',
144 'secondMiddleware' => 'anotherClassName',
145 ];
146 $this->assertEquals($expected, $subject->resolve('testStack'));
147 }
148 }