| @@ 193-215 (lines=23) @@ | ||
| 190 | /** |
|
| 191 | * @test |
|
| 192 | */ |
|
| 193 | public function shouldCheckIfAccessIsGranted() |
|
| 194 | { |
|
| 195 | /** @var Request $request */ |
|
| 196 | $request = $this->createMock(Request::class); |
|
| 197 | ||
| 198 | $this->expectException(AccessDeniedException::class); |
|
| 199 | ||
| 200 | $this->controllerHelper->expects($this->once())->method('denyAccessUnlessGranted')->with( |
|
| 201 | $this->equalTo('some-attribute'), |
|
| 202 | $this->identicalTo($request) |
|
| 203 | )->will($this->returnCallback( |
|
| 204 | function () { |
|
| 205 | throw new AccessDeniedException('Lorem ipsum!'); |
|
| 206 | } |
|
| 207 | )); |
|
| 208 | ||
| 209 | $controller = new GenericEntityListingController($this->controllerHelper, $this->argumentCompiler, [ |
|
| 210 | 'entity-class' => SampleEntity::class, |
|
| 211 | 'authorization-attribute' => 'some-attribute', |
|
| 212 | ]); |
|
| 213 | ||
| 214 | $controller->listEntities($request); |
|
| 215 | } |
|
| 216 | ||
| 217 | /** |
|
| 218 | * @test |
|
| @@ 387-410 (lines=24) @@ | ||
| 384 | /** |
|
| 385 | * @test |
|
| 386 | */ |
|
| 387 | public function shouldRejectNonExistingFactory() |
|
| 388 | { |
|
| 389 | $controller = new GenericEntityCreateController( |
|
| 390 | $this->controllerHelper, |
|
| 391 | $this->argumentBuilder, |
|
| 392 | $this->container, |
|
| 393 | [ |
|
| 394 | 'entity-class' => SampleEntity::class, |
|
| 395 | 'factory' => '@some_factory_service::serialize' |
|
| 396 | ] |
|
| 397 | ); |
|
| 398 | ||
| 399 | $this->container->expects($this->once())->method('get')->with( |
|
| 400 | $this->equalTo('some_factory_service') |
|
| 401 | )->willReturn(null); |
|
| 402 | ||
| 403 | /** @var Request $request */ |
|
| 404 | $request = $this->createMock(Request::class); |
|
| 405 | ||
| 406 | $this->expectException(InvalidArgumentException::class); |
|
| 407 | $this->expectExceptionMessage("Did not find service 'some_factory_service'!"); |
|
| 408 | ||
| 409 | $controller->createEntity($request); |
|
| 410 | } |
|
| 411 | ||
| 412 | /** |
|
| 413 | * @test |
|
| @@ 438-463 (lines=26) @@ | ||
| 435 | /** |
|
| 436 | * @test |
|
| 437 | */ |
|
| 438 | public function shouldRejectNonExistingFactoryMethod() |
|
| 439 | { |
|
| 440 | $controller = new GenericEntityCreateController( |
|
| 441 | $this->controllerHelper, |
|
| 442 | $this->argumentBuilder, |
|
| 443 | $this->container, |
|
| 444 | [ |
|
| 445 | 'entity-class' => SampleEntity::class, |
|
| 446 | 'factory' => '@some_factory_service::MethodDoesNotExist' |
|
| 447 | ] |
|
| 448 | ); |
|
| 449 | ||
| 450 | /** @var Serializable $factoryMock */ |
|
| 451 | $factoryMock = $this->createMock(Serializable::class); |
|
| 452 | ||
| 453 | $this->container->expects($this->once())->method('get')->with( |
|
| 454 | $this->equalTo('some_factory_service') |
|
| 455 | )->willReturn($factoryMock); |
|
| 456 | ||
| 457 | /** @var Request $request */ |
|
| 458 | $request = $this->createMock(Request::class); |
|
| 459 | ||
| 460 | $this->expectException(ReflectionException::class); |
|
| 461 | ||
| 462 | $controller->createEntity($request); |
|
| 463 | } |
|
| 464 | ||
| 465 | /** |
|
| 466 | * @test |
|
| @@ 617-640 (lines=24) @@ | ||
| 614 | /** |
|
| 615 | * @test |
|
| 616 | */ |
|
| 617 | public function shouldCreateAnEntityUsingAStaticFactory() |
|
| 618 | { |
|
| 619 | $controller = new GenericEntityCreateController( |
|
| 620 | $this->controllerHelper, |
|
| 621 | $this->argumentBuilder, |
|
| 622 | $this->container, |
|
| 623 | [ |
|
| 624 | 'entity-class' => SampleEntity::class, |
|
| 625 | 'factory' => 'someStaticFactory' |
|
| 626 | ] |
|
| 627 | ); |
|
| 628 | ||
| 629 | $this->controllerHelper->expects($this->once())->method('persistEntity')->with( |
|
| 630 | $this->equalTo(new SampleEntity("static foo")) |
|
| 631 | ); |
|
| 632 | ||
| 633 | /** @var Request $request */ |
|
| 634 | $request = $this->createMock(Request::class); |
|
| 635 | ||
| 636 | /** @var Response $response */ |
|
| 637 | $response = $controller->createEntity($request); |
|
| 638 | ||
| 639 | $this->assertEquals(200, $response->getStatusCode()); |
|
| 640 | } |
|
| 641 | ||
| 642 | /** |
|
| 643 | * @test |
|
| @@ 645-668 (lines=24) @@ | ||
| 642 | /** |
|
| 643 | * @test |
|
| 644 | */ |
|
| 645 | public function shouldCreateAnEntityUsingRegularFunction() |
|
| 646 | { |
|
| 647 | $controller = new GenericEntityCreateController( |
|
| 648 | $this->controllerHelper, |
|
| 649 | $this->argumentBuilder, |
|
| 650 | $this->container, |
|
| 651 | [ |
|
| 652 | 'entity-class' => SampleEntity::class, |
|
| 653 | 'factory' => 'someStaticFactory' |
|
| 654 | ] |
|
| 655 | ); |
|
| 656 | ||
| 657 | $this->controllerHelper->expects($this->once())->method('persistEntity')->with( |
|
| 658 | $this->equalTo(new SampleEntity("static foo")) |
|
| 659 | ); |
|
| 660 | ||
| 661 | /** @var Request $request */ |
|
| 662 | $request = $this->createMock(Request::class); |
|
| 663 | ||
| 664 | /** @var Response $response */ |
|
| 665 | $response = $controller->createEntity($request); |
|
| 666 | ||
| 667 | $this->assertEquals(200, $response->getStatusCode()); |
|
| 668 | } |
|
| 669 | ||
| 670 | } |
|
| 671 | ||