Skip to content

Commit

Permalink
[TASK] Cover NormalizedData scope with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Nov 5, 2022
1 parent 039af27 commit bdfd9d9
Show file tree
Hide file tree
Showing 10 changed files with 600 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ protected function synchroniseConfigurationRecords(array $dataSource): void

if (empty($sheetData)) {
$label = $sheetConfiguration['ROOT']['TCEforms']['sheetTitle'];
$sheetData = array(
$sheetData = [
'pid' => $this->record['pid'],
'name' => $sheetName,
'sheet_label' => empty($label) ? $sheetName : $label,
'source_table' => $this->table,
'source_field' => $this->field,
'source_uid' => $this->record['uid']
);
];
$sheetData['uid'] = $this->insertSheetData($sheetData);
}
$sheetUid = (int) $sheetData['uid'];
Expand Down Expand Up @@ -148,11 +148,14 @@ protected function synchroniseConfigurationRecords(array $dataSource): void

protected function assertArrayHasKey(array $array, string $path): bool
{
$segments = GeneralUtility::trimExplode('.', $path);
$lastSegment = array_pop($segments);
if ($lastSegment === null) {
if (empty($array)) {
return false;
}
$segments = GeneralUtility::trimExplode('.', $path, true);
if (count($segments) === 0) {
return false;
}
$lastSegment = array_pop($segments);
foreach ($segments as $segment) {
$array = $array[$segment];
}
Expand Down Expand Up @@ -189,32 +192,35 @@ protected function resolveDataSourceDefinition(array $structure): ?array
* @param array $data
* @param string $name
* @param mixed $value
* @return void
* @return array
*/
protected function assignVariableByDottedPath(array &$data, string $name, $value): void
protected function assignVariableByDottedPath(array $data, string $name, $value): array
{
if (!strpos($name, '.')) {
$data[$name] = $value;
} else {
$assignIn = &$data;
$segments = explode('.', $name);
$last = array_pop($segments);
foreach ($segments as $segment) {
if (!array_key_exists($segment, $assignIn)) {
$assignIn[$segment] = [];
}
$assignIn = &$assignIn[$segment];
$assignIn = &$data;
$segments = GeneralUtility::trimExplode('.', $name, true);
$last = array_pop($segments);
foreach ($segments as $segment) {
if (!array_key_exists($segment, $assignIn)) {
$assignIn[$segment] = [];
}
$assignIn[$last] = $value;
$assignIn = &$assignIn[$segment];
}
$assignIn[$last] = $value;
return $data;
}

/**
* @codeCoverageIgnore
*/
protected function updateFieldData(int $sheetUid, string $fieldName, array $fieldData): void
{
$connection = $this->createConnectionForTable('flux_field');
$connection->update('flux_field', $fieldData, ['sheet' => $sheetUid, 'field_name' => $fieldName]);
}

/**
* @codeCoverageIgnore
*/
protected function insertFieldData(array $fieldData): int
{
$connection = $this->createConnectionForTable('flux_field');
Expand All @@ -223,6 +229,9 @@ protected function insertFieldData(array $fieldData): int
return (int) $connection->lastInsertId('flux_field');
}

/**
* @codeCoverageIgnore
*/
protected function insertSheetData(array $sheetData): int
{
$connection = $this->createConnectionForTable('flux_sheet');
Expand All @@ -231,6 +240,9 @@ protected function insertSheetData(array $sheetData): int
return (int) $connection->lastInsertId('flux_sheet');
}

/**
* @codeCoverageIgnore
*/
protected function fetchFieldData(int $uid): array
{
$settings = [];
Expand All @@ -240,11 +252,18 @@ protected function fetchFieldData(int $uid): array
$queryBuilder->expr()->eq('sheet', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT))
)->execute()->fetchAllAssociative();
foreach ($result as $fieldRecord) {
$this->assignVariableByDottedPath($settings, $fieldRecord['field_name'], $fieldRecord['field_value']);
$settings = $this->assignVariableByDottedPath(
$settings,
$fieldRecord['field_name'],
$fieldRecord['field_value']
);
}
return $settings;
}

/**
* @codeCoverageIgnore
*/
protected function fetchConfigurationRecords(): array
{
$queryBuilder = $this->createQueryBuilderForTable('flux_sheet');
Expand All @@ -260,6 +279,9 @@ protected function fetchConfigurationRecords(): array
)->execute()->fetchAllAssociative();
}

/**
* @codeCoverageIgnore
*/
protected function fetchSheetRecord(string $sheetName): ?array
{
$queryBuilder = $this->createQueryBuilderForTable('flux_sheet');
Expand All @@ -268,13 +290,19 @@ protected function fetchSheetRecord(string $sheetName): ?array
)->execute()->fetchAssociative() ?: null;
}

/**
* @codeCoverageIgnore
*/
protected function createConnectionForTable(string $table): Connection
{
/** @var ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
return $connectionPool->getConnectionForTable($table);
}

/**
* @codeCoverageIgnore
*/
protected function createQueryBuilderForTable(string $table): QueryBuilder
{
return $this->createConnectionForTable($table)->createQueryBuilder();
Expand Down
1 change: 0 additions & 1 deletion Classes/Integration/NormalizedData/DataAccessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

trait DataAccessTrait
{

/**
* @var ConfigurationManagerInterface;
*/
Expand Down
10 changes: 3 additions & 7 deletions Classes/Integration/NormalizedData/ImplementationRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ImplementationRegistry
public static function registerImplementation(string $implementationClassName, array $settings = []): void
{
foreach (static::$implementations as $implementationData) {
list ($registeredClassName, $registeredSettings) = $implementationData;
[$registeredClassName, $registeredSettings] = $implementationData;
if ($registeredClassName === $implementationClassName && $registeredSettings == $settings) {
return;
}
Expand All @@ -44,13 +44,9 @@ public static function resolveImplementations(string $table, string $field, arra
{
$implementations = [];
foreach (static::$implementations as $implementationData) {
[$registeredClassName, $registeredSettings] = $implementationData;
/** @var ImplementationInterface $instance */
if (count($implementationData) === 3) {
list (, , $instance) = $implementationData;
} else {
list ($registeredClassName, $registeredSettings) = $implementationData;
$instance = GeneralUtility::makeInstance($registeredClassName, $registeredSettings);
}
$instance = GeneralUtility::makeInstance($registeredClassName, $registeredSettings);
if ($instance->appliesToTableField($table, $field) && $instance->appliesToRecord($record)) {
$implementations[] = $instance;
}
Expand Down
1 change: 1 addition & 0 deletions Tests/Unit/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ protected function setUp(): void
define('TYPO3_version', '9.5.0');
}

$GLOBALS['EXEC_TIME'] = time();
$GLOBALS['LANG'] = (object) ['csConvObj' => new CharsetConverter()];
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['preProcessors'] = [];
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['interceptors'] = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace FluidTYPO3\Flux\Tests\Unit\Integration\NormalizedData\Converter;

/*
* This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Flux\Form;
use FluidTYPO3\Flux\Integration\HookSubscribers\PagePreviewRenderer;
use FluidTYPO3\Flux\Integration\NormalizedData\AbstractImplementation;
use FluidTYPO3\Flux\Integration\NormalizedData\Converter\InlineRecordDataConverter;
use FluidTYPO3\Flux\Provider\Provider;
use FluidTYPO3\Flux\Provider\ProviderInterface;
use FluidTYPO3\Flux\Tests\Unit\AbstractTestCase;
use TYPO3\CMS\Backend\Controller\PageLayoutController;
use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class AbstractImplementationTest extends AbstractTestCase
{
public function testAppliesToRecord(): void
{
$subject = $this->getMockBuilder(AbstractImplementation::class)
->setConstructorArgs([])
->getMockForAbstractClass();
self::assertTrue($subject->appliesToRecord(['uid' => 123]));
}
}
Loading

0 comments on commit bdfd9d9

Please sign in to comment.