From 9d91172532dbc217c737e3f7e4ba0a4df0bd582d Mon Sep 17 00:00:00 2001 From: mabolek Date: Sat, 7 Oct 2023 17:19:40 +0200 Subject: [PATCH] [TASK] Unit test for RemoveEmptyValuesFromRelationFieldArrays --- ...moveEmptyValuesFromRelationFieldArrays.php | 5 +- ...EmptyValuesFromRelationFieldArraysTest.php | 76 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 Tests/Unit/DataHandling/Operation/Event/Handler/RemoveEmptyValuesFromRelationFieldArraysTest.php diff --git a/Classes/DataHandling/Operation/Event/Handler/RemoveEmptyValuesFromRelationFieldArrays.php b/Classes/DataHandling/Operation/Event/Handler/RemoveEmptyValuesFromRelationFieldArrays.php index b4c7bed..b43fb36 100644 --- a/Classes/DataHandling/Operation/Event/Handler/RemoveEmptyValuesFromRelationFieldArrays.php +++ b/Classes/DataHandling/Operation/Event/Handler/RemoveEmptyValuesFromRelationFieldArrays.php @@ -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)) + ); } } } diff --git a/Tests/Unit/DataHandling/Operation/Event/Handler/RemoveEmptyValuesFromRelationFieldArraysTest.php b/Tests/Unit/DataHandling/Operation/Event/Handler/RemoveEmptyValuesFromRelationFieldArraysTest.php new file mode 100644 index 0000000..1429541 --- /dev/null +++ b/Tests/Unit/DataHandling/Operation/Event/Handler/RemoveEmptyValuesFromRelationFieldArraysTest.php @@ -0,0 +1,76 @@ +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); + } + } +}