@@ 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 |
@@ 392-415 (lines=24) @@ | ||
389 | /** |
|
390 | * @test |
|
391 | */ |
|
392 | public function shouldRejectNonExistingFactory() |
|
393 | { |
|
394 | $controller = new GenericEntityCreateController( |
|
395 | $this->controllerHelper, |
|
396 | $this->argumentBuilder, |
|
397 | $this->container, |
|
398 | [ |
|
399 | 'entity-class' => SampleEntity::class, |
|
400 | 'factory' => '@some_factory_service::serialize' |
|
401 | ] |
|
402 | ); |
|
403 | ||
404 | $this->container->expects($this->once())->method('get')->with( |
|
405 | $this->equalTo('some_factory_service') |
|
406 | )->willReturn(null); |
|
407 | ||
408 | /** @var Request $request */ |
|
409 | $request = $this->createMock(Request::class); |
|
410 | ||
411 | $this->expectException(InvalidArgumentException::class); |
|
412 | $this->expectExceptionMessage("Did not find service 'some_factory_service'!"); |
|
413 | ||
414 | $controller->createEntity($request); |
|
415 | } |
|
416 | ||
417 | /** |
|
418 | * @test |
|
@@ 443-468 (lines=26) @@ | ||
440 | /** |
|
441 | * @test |
|
442 | */ |
|
443 | public function shouldRejectNonExistingFactoryMethod() |
|
444 | { |
|
445 | $controller = new GenericEntityCreateController( |
|
446 | $this->controllerHelper, |
|
447 | $this->argumentBuilder, |
|
448 | $this->container, |
|
449 | [ |
|
450 | 'entity-class' => SampleEntity::class, |
|
451 | 'factory' => '@some_factory_service::MethodDoesNotExist' |
|
452 | ] |
|
453 | ); |
|
454 | ||
455 | /** @var Serializable $factoryMock */ |
|
456 | $factoryMock = $this->createMock(Serializable::class); |
|
457 | ||
458 | $this->container->expects($this->once())->method('get')->with( |
|
459 | $this->equalTo('some_factory_service') |
|
460 | )->willReturn($factoryMock); |
|
461 | ||
462 | /** @var Request $request */ |
|
463 | $request = $this->createMock(Request::class); |
|
464 | ||
465 | $this->expectException(ReflectionException::class); |
|
466 | ||
467 | $controller->createEntity($request); |
|
468 | } |
|
469 | ||
470 | /** |
|
471 | * @test |
|
@@ 622-645 (lines=24) @@ | ||
619 | /** |
|
620 | * @test |
|
621 | */ |
|
622 | public function shouldCreateAnEntityUsingAStaticFactory() |
|
623 | { |
|
624 | $controller = new GenericEntityCreateController( |
|
625 | $this->controllerHelper, |
|
626 | $this->argumentBuilder, |
|
627 | $this->container, |
|
628 | [ |
|
629 | 'entity-class' => SampleEntity::class, |
|
630 | 'factory' => 'someStaticFactory' |
|
631 | ] |
|
632 | ); |
|
633 | ||
634 | $this->controllerHelper->expects($this->once())->method('persistEntity')->with( |
|
635 | $this->equalTo(new SampleEntity("static foo")) |
|
636 | ); |
|
637 | ||
638 | /** @var Request $request */ |
|
639 | $request = $this->createMock(Request::class); |
|
640 | ||
641 | /** @var Response $response */ |
|
642 | $response = $controller->createEntity($request); |
|
643 | ||
644 | $this->assertEquals(200, $response->getStatusCode()); |
|
645 | } |
|
646 | ||
647 | /** |
|
648 | * @test |
|
@@ 650-673 (lines=24) @@ | ||
647 | /** |
|
648 | * @test |
|
649 | */ |
|
650 | public function shouldCreateAnEntityUsingStaticFactory() |
|
651 | { |
|
652 | $controller = new GenericEntityCreateController( |
|
653 | $this->controllerHelper, |
|
654 | $this->argumentBuilder, |
|
655 | $this->container, |
|
656 | [ |
|
657 | 'entity-class' => SampleEntity::class, |
|
658 | 'factory' => 'someStaticFactory' |
|
659 | ] |
|
660 | ); |
|
661 | ||
662 | $this->controllerHelper->expects($this->once())->method('persistEntity')->with( |
|
663 | $this->equalTo(new SampleEntity("static foo")) |
|
664 | ); |
|
665 | ||
666 | /** @var Request $request */ |
|
667 | $request = $this->createMock(Request::class); |
|
668 | ||
669 | /** @var Response $response */ |
|
670 | $response = $controller->createEntity($request); |
|
671 | ||
672 | $this->assertEquals(200, $response->getStatusCode()); |
|
673 | } |
|
674 | ||
675 | /** |
|
676 | * @test |
|
@@ 678-702 (lines=25) @@ | ||
675 | /** |
|
676 | * @test |
|
677 | */ |
|
678 | public function shouldCreateAnEntityUsingRegularFunction() |
|
679 | { |
|
680 | /** @var mixed $controller */ |
|
681 | $controller = new GenericEntityCreateController( |
|
682 | $this->controllerHelper, |
|
683 | $this->argumentBuilder, |
|
684 | $this->container, |
|
685 | [ |
|
686 | 'entity-class' => SampleEntity::class, |
|
687 | 'factory' => __NAMESPACE__ . '\\createSampleEntity' |
|
688 | ] |
|
689 | ); |
|
690 | ||
691 | $this->controllerHelper->expects($this->once())->method('persistEntity')->with( |
|
692 | $this->equalTo(new SampleEntity("function foo")) |
|
693 | ); |
|
694 | ||
695 | /** @var Request $request */ |
|
696 | $request = $this->createMock(Request::class); |
|
697 | ||
698 | /** @var Response $response */ |
|
699 | $response = $controller->createEntity($request); |
|
700 | ||
701 | $this->assertEquals(200, $response->getStatusCode()); |
|
702 | } |
|
703 | ||
704 | } |
|
705 |