Skip to content

Commit fdde9a0

Browse files
committed
[DoctrineBridge] Allow EntityValueResolver to return a list of entities
1 parent 7076052 commit fdde9a0

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

ArgumentResolver/EntityValueResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private function getCriteria(Request $request, MapEntity $options, ObjectManager
192192
return $criteria;
193193
}
194194

195-
private function findViaExpression(ObjectManager $manager, Request $request, MapEntity $options): ?object
195+
private function findViaExpression(ObjectManager $manager, Request $request, MapEntity $options): object|iterable|null
196196
{
197197
if (!$this->expressionLanguage) {
198198
throw new \LogicException(sprintf('You cannot use the "%s" if the ExpressionLanguage component is not available. Try running "composer require symfony/expression-language".', __CLASS__));

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Deprecate the `DoctrineExtractor::getTypes()` method, use `DoctrineExtractor::getType()` instead
8+
* Allow `EntityValueResolver` to return a list of entities
89

910
7.0
1011
---

Tests/ArgumentResolver/EntityValueResolverTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ public function testExpressionFailureReturns404()
298298

299299
$manager->expects($this->once())
300300
->method('getRepository')
301+
->with(\stdClass::class)
301302
->willReturn($repository);
302303

303304
$language->expects($this->once())
@@ -329,6 +330,7 @@ public function testExpressionMapsToArgument()
329330

330331
$manager->expects($this->once())
331332
->method('getRepository')
333+
->with(\stdClass::class)
332334
->willReturn($repository);
333335

334336
$language->expects($this->once())
@@ -343,6 +345,48 @@ public function testExpressionMapsToArgument()
343345
$this->assertSame([$object], $resolver->resolve($request, $argument));
344346
}
345347

348+
public function testExpressionMapsToIterableArgument()
349+
{
350+
$manager = $this->createMock(ObjectManager::class);
351+
$registry = $this->createRegistry($manager);
352+
$language = $this->createMock(ExpressionLanguage::class);
353+
$resolver = new EntityValueResolver($registry, $language);
354+
355+
$request = new Request();
356+
$request->attributes->set('id', 5);
357+
$request->query->set('sort', 'ASC');
358+
$request->query->set('limit', 10);
359+
$argument = $this->createArgument(
360+
'iterable',
361+
new MapEntity(
362+
class: \stdClass::class,
363+
expr: $expr = 'repository.findBy({"author": id}, {"createdAt": request.query.get("sort", "DESC")}, request.query.getInt("limit", 10))',
364+
),
365+
'arg1',
366+
);
367+
368+
$repository = $this->createMock(ObjectRepository::class);
369+
// find should not be attempted on this repository as a fallback
370+
$repository->expects($this->never())
371+
->method('find');
372+
373+
$manager->expects($this->once())
374+
->method('getRepository')
375+
->with(\stdClass::class)
376+
->willReturn($repository);
377+
378+
$language->expects($this->once())
379+
->method('evaluate')
380+
->with($expr, [
381+
'repository' => $repository,
382+
'request' => $request,
383+
'id' => 5,
384+
])
385+
->willReturn($objects = [new \stdClass(), new \stdClass()]);
386+
387+
$this->assertSame([$objects], $resolver->resolve($request, $argument));
388+
}
389+
346390
public function testExpressionSyntaxErrorThrowsException()
347391
{
348392
$manager = $this->getMockBuilder(ObjectManager::class)->getMock();
@@ -364,6 +408,7 @@ public function testExpressionSyntaxErrorThrowsException()
364408

365409
$manager->expects($this->once())
366410
->method('getRepository')
411+
->with(\stdClass::class)
367412
->willReturn($repository);
368413

369414
$language->expects($this->once())

0 commit comments

Comments
 (0)