From 46aada53275c3ee64ec94781918e98b485d581ce Mon Sep 17 00:00:00 2001 From: Hoja Mustaffa Abdul Latheef Date: Wed, 4 Dec 2024 11:34:06 +0100 Subject: [PATCH] [TASK] Fixed Functional Test Cases --- .../CurrentWeatherRepositoryTest.php | 5 ++ .../Repository/DwdWarnCellRepositoryTest.php | 5 ++ .../Repository/WeatherAlertRepositoryTest.php | 42 +------------- .../InitializeFrontendControllerMockTrait.php | 55 +++++++++++++++++++ 4 files changed, 68 insertions(+), 39 deletions(-) create mode 100644 Tests/Functional/Traits/InitializeFrontendControllerMockTrait.php diff --git a/Tests/Functional/Domain/Repository/CurrentWeatherRepositoryTest.php b/Tests/Functional/Domain/Repository/CurrentWeatherRepositoryTest.php index 4004760..af66668 100644 --- a/Tests/Functional/Domain/Repository/CurrentWeatherRepositoryTest.php +++ b/Tests/Functional/Domain/Repository/CurrentWeatherRepositoryTest.php @@ -13,6 +13,7 @@ use JWeiland\Weather2\Domain\Model\CurrentWeather; use JWeiland\Weather2\Domain\Repository\CurrentWeatherRepository; +use JWeiland\Weather2\Tests\Functional\Traits\InitializeFrontendControllerMockTrait; use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; /** @@ -20,6 +21,8 @@ */ class CurrentWeatherRepositoryTest extends FunctionalTestCase { + use InitializeFrontendControllerMockTrait; + /** * @var CurrentWeatherRepository */ @@ -32,8 +35,10 @@ class CurrentWeatherRepositoryTest extends FunctionalTestCase protected function setUp(): void { parent::setUp(); + $this->importCSVDataSet(__DIR__ . '/../../Fixtures/tx_weather2_domain_model_currentweather.csv'); $this->subject = $this->getContainer()->get(CurrentWeatherRepository::class); + $this->createFrontendControllerMock(); } /** diff --git a/Tests/Functional/Domain/Repository/DwdWarnCellRepositoryTest.php b/Tests/Functional/Domain/Repository/DwdWarnCellRepositoryTest.php index 3a9d4bd..550e2dc 100644 --- a/Tests/Functional/Domain/Repository/DwdWarnCellRepositoryTest.php +++ b/Tests/Functional/Domain/Repository/DwdWarnCellRepositoryTest.php @@ -12,6 +12,7 @@ namespace JWeiland\Weather2\Tests\Functional\Domain\Repository; use JWeiland\Weather2\Domain\Repository\DwdWarnCellRepository; +use JWeiland\Weather2\Tests\Functional\Traits\InitializeFrontendControllerMockTrait; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; @@ -20,6 +21,8 @@ */ class DwdWarnCellRepositoryTest extends FunctionalTestCase { + use InitializeFrontendControllerMockTrait; + /** * @var DwdWarnCellRepository */ @@ -36,6 +39,8 @@ protected function setUp(): void $this->importCSVDataSet(__DIR__ . '/../../Fixtures/tx_weather2_domain_model_dwdwarncell.csv'); $this->subject = GeneralUtility::makeInstance(DwdWarnCellRepository::class); + + $this->createFrontendControllerMock(); } protected function tearDown(): void diff --git a/Tests/Functional/Domain/Repository/WeatherAlertRepositoryTest.php b/Tests/Functional/Domain/Repository/WeatherAlertRepositoryTest.php index 0aa08fd..174b751 100644 --- a/Tests/Functional/Domain/Repository/WeatherAlertRepositoryTest.php +++ b/Tests/Functional/Domain/Repository/WeatherAlertRepositoryTest.php @@ -13,16 +13,10 @@ use JWeiland\Weather2\Domain\Model\WeatherAlert; use JWeiland\Weather2\Domain\Repository\WeatherAlertRepository; +use JWeiland\Weather2\Tests\Functional\Traits\InitializeFrontendControllerMockTrait; use PHPUnit\Framework\MockObject\Exception; use Psr\Http\Message\ServerRequestInterface; -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\ArrayUtility; use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; -use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; /** @@ -30,6 +24,8 @@ */ class WeatherAlertRepositoryTest extends FunctionalTestCase { + use InitializeFrontendControllerMockTrait; + protected WeatherAlertRepository $subject; protected array $testExtensionsToLoad = [ @@ -113,36 +109,4 @@ public function findByUserSelectionWillReturnWeatherAlert(): void $firstWeatherAlert->getEndDate(), ); } - - /** - * Create a TypoScriptFrontendController mock instance. - * - * @throws Exception - */ - protected function createFrontendControllerMock(array $config = []): void - { - $controllerMock = $this->createMock(TypoScriptFrontendController::class); - $controllerMock->cObj = new ContentObjectRenderer($controllerMock); - $controllerMock->cObj->data = [ - 'uid' => 1, - 'pid' => 0, - 'title' => 'Startpage', - 'nav_title' => 'Car', - ]; - - // Set the configuration - $configProperty = new \ReflectionProperty($controllerMock, 'config'); - $configProperty->setAccessible(true); - ArrayUtility::mergeRecursiveWithOverrule($controllerMock->config, $config); - - $frontendTypoScript = new FrontendTypoScript(new RootNode(), [], [], []); - $frontendTypoScript->setSetupArray([]); - - $controllerMock->config = $config; - - $this->request = (new ServerRequest()) - ->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_FE) - ->withAttribute('frontend.controller', $controllerMock) - ->withAttribute('frontend.typoscript', $frontendTypoScript); - } } diff --git a/Tests/Functional/Traits/InitializeFrontendControllerMockTrait.php b/Tests/Functional/Traits/InitializeFrontendControllerMockTrait.php new file mode 100644 index 0000000..0dba675 --- /dev/null +++ b/Tests/Functional/Traits/InitializeFrontendControllerMockTrait.php @@ -0,0 +1,55 @@ +createMock(TypoScriptFrontendController::class); + $controllerMock->cObj = new ContentObjectRenderer($controllerMock); + $controllerMock->cObj->data = [ + 'uid' => 1, + 'pid' => 0, + 'title' => 'Startpage', + 'nav_title' => 'Car', + ]; + + // Set the configuration + $configProperty = new \ReflectionProperty($controllerMock, 'config'); + $configProperty->setAccessible(true); + ArrayUtility::mergeRecursiveWithOverrule($controllerMock->config, $config); + + $frontendTypoScript = new FrontendTypoScript(new RootNode(), [], [], []); + $frontendTypoScript->setSetupArray([]); + + $controllerMock->config = $config; + + $this->request = (new ServerRequest()) + ->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_FE) + ->withAttribute('frontend.controller', $controllerMock) + ->withAttribute('frontend.typoscript', $frontendTypoScript); + + $GLOBALS['TYPO3_REQUEST'] = $this->request; + } +}