|
3 | 3 | namespace MalteHuebner\DataQueryBundle\Tests\Factory\ValueAssigner; |
4 | 4 |
|
5 | 5 | use Doctrine\Persistence\ManagerRegistry; |
| 6 | +use Doctrine\Persistence\ObjectRepository; |
| 7 | +use MalteHuebner\DataQueryBundle\Exception\EntityNotFoundException; |
6 | 8 | use MalteHuebner\DataQueryBundle\Factory\ValueAssigner\ValueAssigner; |
7 | 9 | use MalteHuebner\DataQueryBundle\Factory\ValueAssigner\ValueAssignerInterface; |
8 | 10 | use MalteHuebner\DataQueryBundle\Factory\ValueAssigner\ValueType; |
|
11 | 13 | use MalteHuebner\DataQueryBundle\Query\BoundingBoxQuery; |
12 | 14 | use MalteHuebner\DataQueryBundle\Parameter\SizeParameter; |
13 | 15 | use MalteHuebner\DataQueryBundle\RequestParameterList\RequestParameterList; |
| 16 | +use MalteHuebner\DataQueryBundle\Tests\Fixtures\SimpleEntity; |
| 17 | +use MalteHuebner\DataQueryBundle\Tests\Fixtures\SimpleEntityContainer; |
| 18 | +use MalteHuebner\DataQueryBundle\Tests\Fixtures\SimpleEntityContainerRepository; |
| 19 | +use MalteHuebner\DataQueryBundle\Tests\Fixtures\SimpleEntityQuery; |
14 | 20 | use PHPUnit\Framework\TestCase; |
15 | 21 |
|
16 | 22 | class ValueAssignerTest extends TestCase |
@@ -309,4 +315,130 @@ public function testConvertToIntTrimsWhitespaceForParameter(): void |
309 | 315 |
|
310 | 316 | $this->assertTrue(true); |
311 | 317 | } |
| 318 | + |
| 319 | + public function testAssignQueryEntityValueAssignsEntityFromRepository(): void |
| 320 | + { |
| 321 | + $entity = new SimpleEntity(); |
| 322 | + |
| 323 | + $repository = $this->createMock(ObjectRepository::class); |
| 324 | + $repository->method('find')->with('42')->willReturn($entity); |
| 325 | + |
| 326 | + $this->managerRegistry->method('getRepository')->with(SimpleEntity::class)->willReturn($repository); |
| 327 | + |
| 328 | + $list = new RequestParameterList(); |
| 329 | + $list->add('simpleEntityId', '42'); |
| 330 | + |
| 331 | + $query = new SimpleEntityQuery(); |
| 332 | + |
| 333 | + $queryField = new QueryField(); |
| 334 | + $queryField |
| 335 | + ->setMethodName('setSimpleEntity') |
| 336 | + ->setParameterName('simpleEntityId') |
| 337 | + ->setType(SimpleEntity::class); |
| 338 | + |
| 339 | + $this->valueAssigner->assignQueryPropertyValueFromRequest($list, $query, $queryField); |
| 340 | + |
| 341 | + $this->assertSame($entity, $query->getSimpleEntity()); |
| 342 | + } |
| 343 | + |
| 344 | + public function testAssignQueryEntityValueThrowsWhenEntityNotFound(): void |
| 345 | + { |
| 346 | + $repository = $this->createMock(ObjectRepository::class); |
| 347 | + $repository->method('find')->willReturn(null); |
| 348 | + |
| 349 | + $this->managerRegistry->method('getRepository')->willReturn($repository); |
| 350 | + |
| 351 | + $list = new RequestParameterList(); |
| 352 | + $list->add('simpleEntityId', '42'); |
| 353 | + |
| 354 | + $query = new SimpleEntityQuery(); |
| 355 | + |
| 356 | + $queryField = new QueryField(); |
| 357 | + $queryField |
| 358 | + ->setMethodName('setSimpleEntity') |
| 359 | + ->setParameterName('simpleEntityId') |
| 360 | + ->setType(SimpleEntity::class); |
| 361 | + |
| 362 | + $this->expectException(EntityNotFoundException::class); |
| 363 | + $this->expectExceptionMessage('Could not find entity for query parameter "simpleEntityId" with value "42"'); |
| 364 | + |
| 365 | + $this->valueAssigner->assignQueryPropertyValueFromRequest($list, $query, $queryField); |
| 366 | + } |
| 367 | + |
| 368 | + public function testAssignQueryEntityValueWithAccessorAssignsAccessedEntity(): void |
| 369 | + { |
| 370 | + $entity = new SimpleEntity(); |
| 371 | + $repository = new SimpleEntityContainerRepository(new SimpleEntityContainer($entity)); |
| 372 | + |
| 373 | + $this->managerRegistry->method('getRepository')->with(SimpleEntityContainer::class)->willReturn($repository); |
| 374 | + |
| 375 | + $list = new RequestParameterList(); |
| 376 | + $list->add('simpleEntitySlug', 'some-slug'); |
| 377 | + |
| 378 | + $query = new SimpleEntityQuery(); |
| 379 | + |
| 380 | + $queryField = new QueryField(); |
| 381 | + $queryField |
| 382 | + ->setMethodName('setSimpleEntity') |
| 383 | + ->setParameterName('simpleEntitySlug') |
| 384 | + ->setType(SimpleEntity::class) |
| 385 | + ->setRepository(SimpleEntityContainer::class) |
| 386 | + ->setRepositoryMethod('findOneBySlug') |
| 387 | + ->setAccessor('getSimpleEntity'); |
| 388 | + |
| 389 | + $this->valueAssigner->assignQueryPropertyValueFromRequest($list, $query, $queryField); |
| 390 | + |
| 391 | + $this->assertSame($entity, $query->getSimpleEntity()); |
| 392 | + } |
| 393 | + |
| 394 | + public function testAssignQueryEntityValueWithAccessorThrowsWhenLookupReturnsNull(): void |
| 395 | + { |
| 396 | + $repository = new SimpleEntityContainerRepository(null); |
| 397 | + |
| 398 | + $this->managerRegistry->method('getRepository')->willReturn($repository); |
| 399 | + |
| 400 | + $list = new RequestParameterList(); |
| 401 | + $list->add('simpleEntitySlug', 'unknown-slug'); |
| 402 | + |
| 403 | + $query = new SimpleEntityQuery(); |
| 404 | + |
| 405 | + $queryField = new QueryField(); |
| 406 | + $queryField |
| 407 | + ->setMethodName('setSimpleEntity') |
| 408 | + ->setParameterName('simpleEntitySlug') |
| 409 | + ->setType(SimpleEntity::class) |
| 410 | + ->setRepository(SimpleEntityContainer::class) |
| 411 | + ->setRepositoryMethod('findOneBySlug') |
| 412 | + ->setAccessor('getSimpleEntity'); |
| 413 | + |
| 414 | + $this->expectException(EntityNotFoundException::class); |
| 415 | + $this->expectExceptionMessage('Could not find entity for query parameter "simpleEntitySlug" with value "unknown-slug"'); |
| 416 | + |
| 417 | + $this->valueAssigner->assignQueryPropertyValueFromRequest($list, $query, $queryField); |
| 418 | + } |
| 419 | + |
| 420 | + public function testAssignQueryEntityValueThrowsWhenAccessorReturnsNull(): void |
| 421 | + { |
| 422 | + $repository = new SimpleEntityContainerRepository(new SimpleEntityContainer(null)); |
| 423 | + |
| 424 | + $this->managerRegistry->method('getRepository')->willReturn($repository); |
| 425 | + |
| 426 | + $list = new RequestParameterList(); |
| 427 | + $list->add('simpleEntitySlug', 'some-slug'); |
| 428 | + |
| 429 | + $query = new SimpleEntityQuery(); |
| 430 | + |
| 431 | + $queryField = new QueryField(); |
| 432 | + $queryField |
| 433 | + ->setMethodName('setSimpleEntity') |
| 434 | + ->setParameterName('simpleEntitySlug') |
| 435 | + ->setType(SimpleEntity::class) |
| 436 | + ->setRepository(SimpleEntityContainer::class) |
| 437 | + ->setRepositoryMethod('findOneBySlug') |
| 438 | + ->setAccessor('getSimpleEntity'); |
| 439 | + |
| 440 | + $this->expectException(EntityNotFoundException::class); |
| 441 | + |
| 442 | + $this->valueAssigner->assignQueryPropertyValueFromRequest($list, $query, $queryField); |
| 443 | + } |
312 | 444 | } |
0 commit comments