|
11 | 11 |
|
12 | 12 | namespace Symfony\Bridge\Doctrine\Tests\Console\ArgumentResolver; |
13 | 13 |
|
| 14 | +use Doctrine\DBAL\Types\ConversionException; |
14 | 15 | use Doctrine\Persistence\ManagerRegistry; |
15 | 16 | use Doctrine\Persistence\ObjectManager; |
16 | 17 | use Doctrine\Persistence\ObjectRepository; |
|
24 | 25 | use Symfony\Component\Console\Input\ArrayInput; |
25 | 26 | use Symfony\Component\Console\Input\InputArgument; |
26 | 27 | use Symfony\Component\Console\Input\InputDefinition; |
| 28 | +use Symfony\Component\Console\Input\InputInterface; |
27 | 29 | use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
28 | 30 |
|
29 | 31 | class EntityValueResolverTest extends TestCase |
@@ -250,6 +252,65 @@ public function testResolveWithExpression() |
250 | 252 | $this->assertSame([$object], iterator_to_array($resolver->resolve('entity', $input, $member))); |
251 | 253 | } |
252 | 254 |
|
| 255 | + public function testResolveWithClosure() |
| 256 | + { |
| 257 | + $manager = $this->createMock(ObjectManager::class); |
| 258 | + $registry = $this->createRegistry($manager); |
| 259 | + $resolver = new EntityValueResolver($registry, null, new MapEntity(expr: static fn (InputInterface $input, ObjectRepository $repository) => $repository->findOneBy(['id' => $input->getArgument('entity')]))); |
| 260 | + |
| 261 | + $input = new ArrayInput(['entity' => 1], new InputDefinition([ |
| 262 | + new InputArgument('entity'), |
| 263 | + ])); |
| 264 | + |
| 265 | + $repository = $this->createMock(ObjectRepository::class); |
| 266 | + $repository->expects($this->never()) |
| 267 | + ->method('find'); |
| 268 | + $repository->expects($this->once()) |
| 269 | + ->method('findOneBy') |
| 270 | + ->with(['id' => 1]) |
| 271 | + ->willReturn($object = new \stdClass()); |
| 272 | + |
| 273 | + $manager->expects($this->once()) |
| 274 | + ->method('getRepository') |
| 275 | + ->with(\stdClass::class) |
| 276 | + ->willReturn($repository); |
| 277 | + |
| 278 | + $member = $this->createMember('entity', \stdClass::class); |
| 279 | + |
| 280 | + $this->assertSame([$object], iterator_to_array($resolver->resolve('entity', $input, $member))); |
| 281 | + } |
| 282 | + |
| 283 | + public function testResolveWithClosureFailureThrowsException() |
| 284 | + { |
| 285 | + $manager = $this->createMock(ObjectManager::class); |
| 286 | + $registry = $this->createRegistry($manager); |
| 287 | + $resolver = new EntityValueResolver($registry, null, new MapEntity(expr: static function (InputInterface $input, ObjectRepository $repository) { |
| 288 | + $repository->findOneBy(['id' => $input->getArgument('entity')]); |
| 289 | + |
| 290 | + throw new ConversionException(); |
| 291 | + })); |
| 292 | + |
| 293 | + $input = new ArrayInput(['entity' => 1], new InputDefinition([ |
| 294 | + new InputArgument('entity'), |
| 295 | + ])); |
| 296 | + |
| 297 | + $repository = $this->createMock(ObjectRepository::class); |
| 298 | + $repository->expects($this->once()) |
| 299 | + ->method('findOneBy') |
| 300 | + ->with(['id' => 1]); |
| 301 | + |
| 302 | + $manager->expects($this->once()) |
| 303 | + ->method('getRepository') |
| 304 | + ->with(\stdClass::class) |
| 305 | + ->willReturn($repository); |
| 306 | + |
| 307 | + $member = $this->createMember('entity', \stdClass::class); |
| 308 | + |
| 309 | + $this->expectException(RuntimeException::class); |
| 310 | + |
| 311 | + iterator_to_array($resolver->resolve('entity', $input, $member)); |
| 312 | + } |
| 313 | + |
253 | 314 | public function testResolveWithStripNull() |
254 | 315 | { |
255 | 316 | $manager = $this->createMock(ObjectManager::class); |
|
0 commit comments