Skip to content

Commit

Permalink
Avoid to pass empty array as argument 3 for implementations of `Model…
Browse files Browse the repository at this point in the history
…ManagerInterface::addIdentifiersToQuery()`
  • Loading branch information
phansys authored and jordisala1991 committed Oct 20, 2020
1 parent 622d7b8 commit 8e9583b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Form/DataTransformer/ModelsToArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array-key, T> $collection */
$collection = TraversableToCollection::transform($result);
Expand Down
24 changes: 24 additions & 0 deletions tests/Form/DataTransformer/ModelsToArrayTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 8e9583b

Please sign in to comment.