Skip to content

Commit

Permalink
[TASK] Extract common site test aspects to trait
Browse files Browse the repository at this point in the history
Extracts TypoScript instruction handling to test trait, which enables
to reuse corresponding methods.

Resolves: #94354
Releases: master, 10.4, 9.5
Change-Id: I34f25070d926f1236b1f7bbd623e0060760b0819
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/69501
Tested-by: core-ci <typo3@b13.com>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
  • Loading branch information
ohader committed Jun 18, 2021
1 parent 2bfa16c commit 212aca3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 65 deletions.
Expand Up @@ -20,6 +20,10 @@
use TYPO3\CMS\Core\Configuration\SiteConfiguration;
use TYPO3\CMS\Core\Tests\Functional\Fixtures\Frontend\PhpError;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\AbstractInstruction;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\ArrayValueInstruction;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Internal\TypoScriptInstruction;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;

/**
* Trait used for test classes that want to set up (= write) site configuration files.
Expand Down Expand Up @@ -230,4 +234,69 @@ protected function resolveLanguagePreset(string $identifier)
}
return static::LANGUAGE_PRESETS[$identifier];
}

/**
* @param InternalRequest $request
* @param AbstractInstruction ...$instructions
* @return InternalRequest
*
* @todo Instruction handling should be part of Testing Framework (multiple instructions per identifier, merge in interface)
*/
protected function applyInstructions(InternalRequest $request, AbstractInstruction ...$instructions): InternalRequest
{
$modifiedInstructions = [];

foreach ($instructions as $instruction) {
$identifier = $instruction->getIdentifier();
if (isset($modifiedInstructions[$identifier]) || $request->getInstruction($identifier) !== null) {
$modifiedInstructions[$identifier] = $this->mergeInstruction(
$modifiedInstructions[$identifier] ?? $request->getInstruction($identifier),
$instruction
);
} else {
$modifiedInstructions[$identifier] = $instruction;
}
}

return $request->withInstructions($modifiedInstructions);
}

/**
* @param AbstractInstruction $current
* @param AbstractInstruction $other
* @return AbstractInstruction
*/
protected function mergeInstruction(AbstractInstruction $current, AbstractInstruction $other): AbstractInstruction
{
if (get_class($current) !== get_class($other)) {
throw new \LogicException('Cannot merge different instruction types', 1565863174);
}

if ($current instanceof TypoScriptInstruction) {
/** @var $other TypoScriptInstruction */
$typoScript = array_replace_recursive(
$current->getTypoScript() ?? [],
$other->getTypoScript() ?? []
);
$constants = array_replace_recursive(
$current->getConstants() ?? [],
$other->getConstants() ?? []
);
if ($typoScript !== []) {
$current = $current->withTypoScript($typoScript);
}
if ($constants !== []) {
$current = $current->withConstants($constants);
}
return $current;
}

if ($current instanceof ArrayValueInstruction) {
/** @var $other ArrayValueInstruction */
$array = array_merge_recursive($current->getArray(), $other->getArray());
return $current->withArray($array);
}

return $current;
}
}
Expand Up @@ -585,69 +585,4 @@ private function createParseFuncInstruction(array $parseFunc): TypoScriptInstruc
],
]);
}

/**
* @param InternalRequest $request
* @param AbstractInstruction ...$instructions
* @return InternalRequest
*
* @todo Instruction handling should be part of Testing Framework (multiple instructions per identifier, merge in interface)
*/
private function applyInstructions(InternalRequest $request, AbstractInstruction ...$instructions): InternalRequest
{
$modifiedInstructions = [];

foreach ($instructions as $instruction) {
$identifier = $instruction->getIdentifier();
if (isset($modifiedInstructions[$identifier]) || $request->getInstruction($identifier) !== null) {
$modifiedInstructions[$identifier] = $this->mergeInstruction(
$modifiedInstructions[$identifier] ?? $request->getInstruction($identifier),
$instruction
);
} else {
$modifiedInstructions[$identifier] = $instruction;
}
}

return $request->withInstructions($modifiedInstructions);
}

/**
* @param AbstractInstruction $current
* @param AbstractInstruction $other
* @return AbstractInstruction
*/
private function mergeInstruction(AbstractInstruction $current, AbstractInstruction $other): AbstractInstruction
{
if (get_class($current) !== get_class($other)) {
throw new \LogicException('Cannot merge different instruction types', 1565863174);
}

if ($current instanceof TypoScriptInstruction) {
/** @var $other TypoScriptInstruction */
$typoScript = array_replace_recursive(
$current->getTypoScript() ?? [],
$other->getTypoScript() ?? []
);
$constants = array_replace_recursive(
$current->getConstants() ?? [],
$other->getConstants() ?? []
);
if ($typoScript !== []) {
$current = $current->withTypoScript($typoScript);
}
if ($constants !== []) {
$current = $current->withConstants($constants);
}
return $current;
}

if ($current instanceof ArrayValueInstruction) {
/** @var $other ArrayValueInstruction */
$array = array_merge_recursive($current->getArray(), $other->getArray());
return $current->withArray($array);
}

return $current;
}
}

0 comments on commit 212aca3

Please sign in to comment.