Skip to content

Commit

Permalink
[TASK] Unit test for RemoveEmptyValuesFromRelationFieldArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
mabolek committed Oct 7, 2023
1 parent 49b3895 commit 9d91172
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public function __invoke(AbstractRecordOperationEvent $event): void
continue;
}

$event->getRecordOperation()->setDataFieldForDataHandler($fieldName, array_filter($fieldValue));
$event->getRecordOperation()->setDataFieldForDataHandler(
$fieldName,
array_values(array_filter($fieldValue))
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Pixelant\Interest\Tests\Unit\DataHandling\Operation\Event\Handler;

use Pixelant\Interest\DataHandling\Operation\CreateRecordOperation;
use Pixelant\Interest\DataHandling\Operation\DeleteRecordOperation;
use Pixelant\Interest\DataHandling\Operation\Event\Handler\MapNewUidToRemoteId;
use Pixelant\Interest\DataHandling\Operation\Event\Handler\RemoveEmptyValuesFromRelationFieldArrays;
use Pixelant\Interest\DataHandling\Operation\Event\RecordOperationInvocationEvent;
use Pixelant\Interest\DataHandling\Operation\Event\RecordOperationSetupEvent;
use Pixelant\Interest\DataHandling\Operation\UpdateRecordOperation;
use Pixelant\Interest\Domain\Model\Dto\RecordInstanceIdentifier;
use Pixelant\Interest\Domain\Model\Dto\RecordRepresentation;
use Pixelant\Interest\Domain\Repository\RemoteIdMappingRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

class RemoveEmptyValuesFromRelationFieldArraysTest extends UnitTestCase
{
/**
* @test
*/
public function returnEarlyIfDeleteOperation()
{
$mockOperation = $this->createMock(DeleteRecordOperation::class);

$mockOperation
->expects(self::never())
->method('getDataForDataHandler');

$event = new RecordOperationSetupEvent($mockOperation);

(new RemoveEmptyValuesFromRelationFieldArrays())($event);
}

/**
* @test
*/
public function correctlyRemovesEmptyValuesFromRelationArrays()
{
$dataForDataHandler = [
'nonRelationField' => 'nonRelationFieldValue',
'emptyRelationField' => [],
'relationFieldWithNoEmptyValues' => ['remoteId1', 'remoteId2', 'remoteId3'],
'relationFieldWithSomeEmptyValues' => ['remoteId4', null, 0, false, '0', '', 'remoteId5', 'remoteId6'],
'relationFieldWithOnlyEmptyValues' => [null, 0, false, '0', ''],
];

$expectedSetDataFieldForDataHandlerArguments = [
['emptyRelationField', []],
['relationFieldWithNoEmptyValues', ['remoteId1', 'remoteId2', 'remoteId3']],
['relationFieldWithSomeEmptyValues', ['remoteId4', 'remoteId5', 'remoteId6']],
['relationFieldWithOnlyEmptyValues', []]
];

foreach ([CreateRecordOperation::class, UpdateRecordOperation::class] as $operationClass) {
$mockOperation = $this->createMock($operationClass);

$mockOperation
->expects(self::once())
->method('getDataForDataHandler')
->willReturn($dataForDataHandler);

$mockOperation
->expects(self::exactly(count($expectedSetDataFieldForDataHandlerArguments)))
->method('setDataFieldForDataHandler')
->withConsecutive(... $expectedSetDataFieldForDataHandlerArguments);

$event = new RecordOperationSetupEvent($mockOperation);

(new RemoveEmptyValuesFromRelationFieldArrays())($event);
}
}
}

0 comments on commit 9d91172

Please sign in to comment.