From cbdfeb717c2adbbcd7bfc1486d3a808901dc146c Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 20 Oct 2020 08:09:00 -0300 Subject: [PATCH] Avoid to pass empty array as argument 3 for implementations of `ModelManagerInterface::addIdentifiersToQuery()` --- .../ModelsToArrayTransformer.php | 13 ++++++---- .../ModelsToArrayTransformerTest.php | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Form/DataTransformer/ModelsToArrayTransformer.php b/src/Form/DataTransformer/ModelsToArrayTransformer.php index 86c17df438..46e79654a8 100644 --- a/src/Form/DataTransformer/ModelsToArrayTransformer.php +++ b/src/Form/DataTransformer/ModelsToArrayTransformer.php @@ -167,11 +167,14 @@ public function reverseTransform($value) throw new UnexpectedTypeException($value, 'array'); } - $value = array_map('strval', $value); - - $query = $this->modelManager->createQuery($this->class); - $this->modelManager->addIdentifiersToQuery($this->class, $query, $value); - $result = $this->modelManager->executeQuery($query); + if ([] === $value) { + $result = $value; + } else { + $value = array_map('strval', $value); + $query = $this->modelManager->createQuery($this->class); + $this->modelManager->addIdentifiersToQuery($this->class, $query, $value); + $result = $this->modelManager->executeQuery($query); + } /** @phpstan-var ArrayCollection $collection */ $collection = TraversableToCollection::transform($result); diff --git a/tests/Form/DataTransformer/ModelsToArrayTransformerTest.php b/tests/Form/DataTransformer/ModelsToArrayTransformerTest.php index 83463f607b..7b79e5e51f 100644 --- a/tests/Form/DataTransformer/ModelsToArrayTransformerTest.php +++ b/tests/Form/DataTransformer/ModelsToArrayTransformerTest.php @@ -90,6 +90,30 @@ public function reverseTransformProvider(): iterable yield [null]; } + public function testReverseTransformWithEmptyArray(): void + { + $modelManager = $this->createStub(ModelManagerInterface::class); + $modelManager + ->expects($this->never()) + ->method('createQuery'); + $modelManager + ->expects($this->never()) + ->method('addIdentifiersToQuery'); + $modelManager + ->expects($this->never()) + ->method('executeQuery'); + + $transformer = new ModelsToArrayTransformer( + $modelManager, + Foo::class + ); + + $result = $transformer->reverseTransform([]); + + $this->assertInstanceOf(Collection::class, $result); + $this->assertCount(0, $result); + } + public function testReverseTransformUnexpectedType(): void { $value = 'unexpected';