Skip to content

Commit

Permalink
[TASK] Fixed Functional Test Cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hojalatheef committed Dec 4, 2024
1 parent f96cfe7 commit 46aada5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@

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;

/**
* Test case.
*/
class CurrentWeatherRepositoryTest extends FunctionalTestCase
{
use InitializeFrontendControllerMockTrait;

/**
* @var CurrentWeatherRepository
*/
Expand All @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,6 +21,8 @@
*/
class DwdWarnCellRepositoryTest extends FunctionalTestCase
{
use InitializeFrontendControllerMockTrait;

/**
* @var DwdWarnCellRepository
*/
Expand All @@ -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
Expand Down
42 changes: 3 additions & 39 deletions Tests/Functional/Domain/Repository/WeatherAlertRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,19 @@

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;

/**
* Test case.
*/
class WeatherAlertRepositoryTest extends FunctionalTestCase
{
use InitializeFrontendControllerMockTrait;

protected WeatherAlertRepository $subject;

protected array $testExtensionsToLoad = [
Expand Down Expand Up @@ -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);
}
}
55 changes: 55 additions & 0 deletions Tests/Functional/Traits/InitializeFrontendControllerMockTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package jweiland/weather2.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace JWeiland\Weather2\Tests\Functional\Traits;

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\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* Trait to initialize frontend controller mock
*/
trait InitializeFrontendControllerMockTrait
{
public 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);

$GLOBALS['TYPO3_REQUEST'] = $this->request;
}
}

0 comments on commit 46aada5

Please sign in to comment.