|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of contao-community-alliance/dc-general. |
| 5 | + * |
| 6 | + * (c) 2013-2026 Contao Community Alliance. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * This project is provided in good faith and hope to be usable by anyone. |
| 12 | + * |
| 13 | + * @package contao-community-alliance/dc-general |
| 14 | + * @author Ingolf Steinhardt <info@e-spin.de> |
| 15 | + * @copyright 2013-2026 Contao Community Alliance. |
| 16 | + * @license https://github.com/contao-community-alliance/dc-general/blob/master/LICENSE LGPL-3.0-or-later |
| 17 | + * @filesource |
| 18 | + */ |
| 19 | + |
| 20 | +declare(strict_types=1); |
| 21 | + |
| 22 | +namespace ContaoCommunityAlliance\DcGeneral\Test\Contao; |
| 23 | + |
| 24 | +use Contao\CoreBundle\Routing\ScopeMatcher; |
| 25 | +use ContaoCommunityAlliance\DcGeneral\Contao\RequestScopeDeterminator; |
| 26 | +use ContaoCommunityAlliance\DcGeneral\Test\TestCase; |
| 27 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 28 | +use Symfony\Component\HttpFoundation\Request; |
| 29 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 30 | + |
| 31 | +/** |
| 32 | + * This tests the request scope determinator. |
| 33 | + */ |
| 34 | +#[CoversClass(RequestScopeDeterminator::class)] |
| 35 | +final class RequestScopeDeterminatorTest extends TestCase |
| 36 | +{ |
| 37 | + /** |
| 38 | + * Test that the answer is obtained from the matcher and passed through. |
| 39 | + * |
| 40 | + * @return void |
| 41 | + */ |
| 42 | + public function testDeterminesTheScope(): void |
| 43 | + { |
| 44 | + $request = $this->mockRequest('backend'); |
| 45 | + $matcher = $this->mockMatcher(backend: true, frontend: false, contao: true); |
| 46 | + |
| 47 | + $determinator = new RequestScopeDeterminator($matcher, $this->mockStack($request)); |
| 48 | + |
| 49 | + self::assertTrue($determinator->currentScopeIsBackend()); |
| 50 | + self::assertFalse($determinator->currentScopeIsFrontend()); |
| 51 | + self::assertFalse($determinator->currentScopeIsUnknown()); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Test that the matcher is consulted only once per request and answer. |
| 56 | + * |
| 57 | + * @return void |
| 58 | + */ |
| 59 | + public function testAsksTheMatcherOnlyOnce(): void |
| 60 | + { |
| 61 | + $request = $this->mockRequest('backend'); |
| 62 | + |
| 63 | + $matcher = $this->getMockBuilder(ScopeMatcher::class)->disableOriginalConstructor()->getMock(); |
| 64 | + $matcher->expects(self::once())->method('isBackendRequest')->with($request)->willReturn(true); |
| 65 | + |
| 66 | + $determinator = new RequestScopeDeterminator($matcher, $this->mockStack($request)); |
| 67 | + |
| 68 | + for ($i = 0; $i < 100; $i++) { |
| 69 | + self::assertTrue($determinator->currentScopeIsBackend()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Test that a different request is determined anew instead of reusing the previous answer. |
| 75 | + * |
| 76 | + * @return void |
| 77 | + */ |
| 78 | + public function testDeterminesEachRequestOnItsOwn(): void |
| 79 | + { |
| 80 | + $backendRequest = $this->mockRequest('backend'); |
| 81 | + $frontendRequest = $this->mockRequest('frontend'); |
| 82 | + |
| 83 | + $stack = $this->createStub(RequestStack::class); |
| 84 | + $stack->method('getCurrentRequest')->willReturnOnConsecutiveCalls( |
| 85 | + $backendRequest, |
| 86 | + $frontendRequest, |
| 87 | + $backendRequest |
| 88 | + ); |
| 89 | + |
| 90 | + $matcher = $this->createStub(ScopeMatcher::class); |
| 91 | + $matcher->method('isBackendRequest')->willReturnCallback( |
| 92 | + static fn (Request $request): bool => 'backend' === $request->attributes->get('_scope') |
| 93 | + ); |
| 94 | + |
| 95 | + $determinator = new RequestScopeDeterminator($matcher, $stack); |
| 96 | + |
| 97 | + self::assertTrue($determinator->currentScopeIsBackend()); |
| 98 | + self::assertFalse($determinator->currentScopeIsBackend()); |
| 99 | + self::assertTrue($determinator->currentScopeIsBackend()); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Test that a scope assigned after the first question invalidates the remembered answer. |
| 104 | + * |
| 105 | + * This is what happens when something asks before the router has run. |
| 106 | + * |
| 107 | + * @return void |
| 108 | + */ |
| 109 | + public function testForgetsTheAnswerWhenTheScopeIsAssignedLater(): void |
| 110 | + { |
| 111 | + $request = $this->mockRequest(null); |
| 112 | + |
| 113 | + $matcher = $this->createStub(ScopeMatcher::class); |
| 114 | + $matcher->method('isBackendRequest')->willReturnCallback( |
| 115 | + static fn (Request $request): bool => 'backend' === $request->attributes->get('_scope') |
| 116 | + ); |
| 117 | + |
| 118 | + $determinator = new RequestScopeDeterminator($matcher, $this->mockStack($request)); |
| 119 | + |
| 120 | + self::assertFalse($determinator->currentScopeIsBackend()); |
| 121 | + |
| 122 | + $request->attributes->set('_scope', 'backend'); |
| 123 | + |
| 124 | + self::assertTrue($determinator->currentScopeIsBackend()); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Test that the answers without a request stay as they were. |
| 129 | + * |
| 130 | + * @return void |
| 131 | + */ |
| 132 | + public function testAnswersWithoutRequest(): void |
| 133 | + { |
| 134 | + $stack = $this->createStub(RequestStack::class); |
| 135 | + $stack->method('getCurrentRequest')->willReturn(null); |
| 136 | + |
| 137 | + $matcher = $this->getMockBuilder(ScopeMatcher::class)->disableOriginalConstructor()->getMock(); |
| 138 | + $matcher->expects(self::never())->method('isBackendRequest'); |
| 139 | + |
| 140 | + $determinator = new RequestScopeDeterminator($matcher, $stack); |
| 141 | + |
| 142 | + self::assertTrue($determinator->currentScopeIsUnknown()); |
| 143 | + self::assertTrue($determinator->currentScopeIsBackend()); |
| 144 | + self::assertFalse($determinator->currentScopeIsFrontend()); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Build a request carrying the passed scope. |
| 149 | + * |
| 150 | + * @param string|null $scope The scope to set, null to leave it unset. |
| 151 | + * |
| 152 | + * @return Request |
| 153 | + */ |
| 154 | + private function mockRequest(?string $scope): Request |
| 155 | + { |
| 156 | + $request = new Request(); |
| 157 | + if (null !== $scope) { |
| 158 | + $request->attributes->set('_scope', $scope); |
| 159 | + } |
| 160 | + |
| 161 | + return $request; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Build a request stack always returning the passed request. |
| 166 | + * |
| 167 | + * @param Request $request The request to return. |
| 168 | + * |
| 169 | + * @return RequestStack&\PHPUnit\Framework\MockObject\Stub |
| 170 | + */ |
| 171 | + private function mockStack(Request $request): RequestStack |
| 172 | + { |
| 173 | + $stack = $this->createStub(RequestStack::class); |
| 174 | + $stack->method('getCurrentRequest')->willReturn($request); |
| 175 | + |
| 176 | + return $stack; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Build a scope matcher answering as passed. |
| 181 | + * |
| 182 | + * @param bool $backend The answer for backend requests. |
| 183 | + * @param bool $frontend The answer for frontend requests. |
| 184 | + * @param bool $contao The answer for Contao requests. |
| 185 | + * |
| 186 | + * @return ScopeMatcher&\PHPUnit\Framework\MockObject\Stub |
| 187 | + */ |
| 188 | + private function mockMatcher(bool $backend, bool $frontend, bool $contao): ScopeMatcher |
| 189 | + { |
| 190 | + $matcher = $this->createStub(ScopeMatcher::class); |
| 191 | + $matcher->method('isBackendRequest')->willReturn($backend); |
| 192 | + $matcher->method('isFrontendRequest')->willReturn($frontend); |
| 193 | + $matcher->method('isContaoRequest')->willReturn($contao); |
| 194 | + |
| 195 | + return $matcher; |
| 196 | + } |
| 197 | +} |
0 commit comments