Skip to content

Commit

Permalink
[TASK] Make Request emulation more automatic and v12-compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Jul 3, 2023
1 parent b6cc61c commit 28515b9
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 37 deletions.
21 changes: 18 additions & 3 deletions Classes/Utility/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
* LICENSE.md file that was distributed with this source code.
*/

use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\TypoScript\AST\Node\RootNode;
use TYPO3\CMS\Core\TypoScript\FrontendTypoScript;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Extbase\Mvc\Request;
Expand All @@ -24,7 +27,6 @@ public function buildRequestFor(
string $pluginName,
array $arguments = []
): RequestInterface {
/** @var ServerRequest $serverRequest */
$serverRequest = $this->getServerRequest();

$controllerExtensionName = ExtensionNamingUtility::getExtensionName($extensionIdentity);
Expand Down Expand Up @@ -71,10 +73,23 @@ public function buildRequestFor(
return $request;
}

private function getServerRequest(): ?ServerRequest
public function getServerRequest(): ServerRequest
{
/** @var ServerRequest|null $request */
$request = $GLOBALS['TYPO3_REQUEST'] ?? null;
$request = $GLOBALS['TYPO3_REQUEST'] ?? (new ServerRequest())->withAttribute(
'applicationType',
defined('TYPO3_REQUESTTYPE') ? constant('TYPO3_REQUESTTYPE') : SystemEnvironmentBuilder::REQUESTTYPE_FE
);

if (class_exists(FrontendTypoScript::class) && !$request->getAttribute('frontend.typoscript')) {
$frontendTypoScript = GeneralUtility::makeInstance(
FrontendTypoScript::class,
GeneralUtility::makeInstance(RootNode::class),
[]
);
$frontendTypoScript->setSetupArray([]);
$request = $request->withAttribute('frontend.typoscript', $frontendTypoScript);
}
return $request;
}

Expand Down
1 change: 0 additions & 1 deletion Tests/Unit/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
use TYPO3\CMS\Core\Charset\CharsetConverter;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Parser\Interceptor\Escape;

abstract class AbstractTestCase extends TestCase
{
Expand Down
12 changes: 0 additions & 12 deletions Tests/Unit/Controller/AbstractFluxControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use TYPO3\CMS\Core\Http\ResponseFactory;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
use TYPO3\CMS\Extbase\Mvc\Response;
Expand Down Expand Up @@ -68,16 +66,6 @@ protected function setUp(): void

GeneralUtility::addInstance(RenderingContextBuilder::class, $renderingContextBuilder);

$GLOBALS['TYPO3_REQUEST'] = $this->getMockBuilder(ServerRequest::class)
->setMethods(['getAttribute'])
->disableOriginalConstructor()
->getMock();
if (class_exists(ExtbaseRequestParameters::class)) {
$GLOBALS['TYPO3_REQUEST']->method('getAttribute')
->with('extbase')
->willReturn(new ExtbaseRequestParameters($this->createInstanceClassName()));
}

$requestBuilder = $this->getMockBuilder(RequestBuilder::class)
->setMethods(['getEnvironmentVariable'])
->getMock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Integration\Event\BeforeFlexFormDataStructureIdentifierInitializedEventListener;
use FluidTYPO3\Flux\Integration\Event\BeforeFlexFormDataStructureParsedEventListener;
use FluidTYPO3\Flux\Integration\FlexFormBuilder;
use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use FluidTYPO3\Flux\Integration\Configuration\SpooledConfigurationApplicator;
use FluidTYPO3\Flux\Integration\Event\BootCompletedEventListener;
use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase;
use TYPO3\CMS\Core\Configuration\Event\BeforeFlexFormDataStructureParsedEvent;
use TYPO3\CMS\Core\Core\Event\BootCompletedEvent;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase;
use TYPO3\CMS\Backend\View\Event\PageContentPreviewRenderingEvent;
use TYPO3\CMS\Backend\View\PageLayoutContext;
use TYPO3\CMS\Core\Core\Event\BootCompletedEvent;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class PageContentPreviewRenderingEventListenerTest extends AbstractTestCase
Expand Down
13 changes: 0 additions & 13 deletions Tests/Unit/Integration/NormalizedData/DataAccessTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase;
use FluidTYPO3\Flux\Utility\ExtensionConfigurationUtility;
use FluidTYPO3\Flux\Utility\RequestBuilder;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

class DataAccessTraitTest extends AbstractTestCase
Expand All @@ -45,17 +43,6 @@ protected function setUp(): void
$this->singletonInstances[FluxService::class] = $this->getMockBuilder(FluxService::class)
->disableOriginalConstructor()
->getMock();

$GLOBALS['TYPO3_REQUEST'] = $this->getMockBuilder(ServerRequest::class)
->setMethods(['getAttribute'])
->disableOriginalConstructor()
->getMock();
if (class_exists(ExtbaseRequestParameters::class)) {
$GLOBALS['TYPO3_REQUEST']->method('getAttribute')
->with('extbase')
->willReturn(new ExtbaseRequestParameters($this->createInstanceClassName()));
}

$requestBuilder = $this->getMockBuilder(RequestBuilder::class)
->setMethods(['getEnvironmentVariable'])
->getMock();
Expand Down
5 changes: 0 additions & 5 deletions Tests/Unit/Integration/RenderingContextBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@
use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase;
use FluidTYPO3\Flux\Utility\RenderingContextBuilder;
use FluidTYPO3\Flux\Utility\RequestBuilder;
use Psr\Container\ContainerInterface;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory;
use TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperResolver;
use TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperResolverFactoryInterface;
use TYPO3\CMS\Fluid\View\TemplatePaths;
use TYPO3Fluid\Fluid\Core\Cache\FluidCacheInterface;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;

class RenderingContextBuilderTest extends AbstractTestCase
Expand Down

0 comments on commit 28515b9

Please sign in to comment.