|
1 | 1 | <?php
|
2 | 2 | namespace Pitch\AdrBundle;
|
3 | 3 |
|
| 4 | +use Pitch\AdrBundle\Action\ActionProxy; |
| 5 | +use Pitch\AdrBundle\Fixtures\MyException; |
| 6 | +use Pitch\AdrBundle\Fixtures\MyResponseHandler; |
| 7 | +use Pitch\AdrBundle\PitchAdrBundle; |
| 8 | +use Pitch\Annotation\PitchAnnotationBundle; |
| 9 | +use RuntimeException; |
| 10 | +use Symfony\Bundle\FrameworkBundle\FrameworkBundle; |
| 11 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 12 | +use Symfony\Component\Config\Loader\LoaderInterface; |
4 | 13 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
5 |
| -use Pitch\AdrBundle\DependencyInjection\Compiler\ResponseHandlerPass; |
6 |
| -use Pitch\AdrBundle\DependencyInjection\PitchAdrExtension; |
| 14 | +use Symfony\Component\DependencyInjection\Definition; |
| 15 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | +use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent; |
| 18 | +use Symfony\Component\HttpKernel\Event\ControllerEvent; |
| 19 | +use Symfony\Component\HttpKernel\Event\ViewEvent; |
| 20 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 21 | +use Symfony\Component\HttpKernel\Kernel; |
| 22 | +use Symfony\Component\HttpKernel\KernelEvents; |
7 | 23 |
|
8 |
| -class PitchAdrBundleTest extends \PHPUnit\Framework\TestCase |
| 24 | +class PitchAdrBundleTest extends KernelTestCase |
9 | 25 | {
|
10 |
| - public function testAddCompilerPass() |
| 26 | + protected EventDispatcher $dispatcher; |
| 27 | + |
| 28 | + protected static function getKernelClass() |
| 29 | + { |
| 30 | + return get_class(new class('', true) extends Kernel |
| 31 | + { |
| 32 | + public function getProjectDir() |
| 33 | + { |
| 34 | + return $this->dir ??= sys_get_temp_dir() . '/PitchForm-' . uniqid() . '/'; |
| 35 | + } |
| 36 | + |
| 37 | + public function registerBundles(): iterable |
| 38 | + { |
| 39 | + return [ |
| 40 | + new FrameworkBundle(), |
| 41 | + new PitchAnnotationBundle(), |
| 42 | + new PitchAdrBundle(), |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + public function registerContainerConfiguration(LoaderInterface $loader) |
| 47 | + { |
| 48 | + $loader->load(function (ContainerBuilder $containerBuilder) { |
| 49 | + $containerBuilder->setParameter('kernel.secret', 'secret'); |
| 50 | + }); |
| 51 | + |
| 52 | + $loader->load(function (ContainerBuilder $containerBuilder) { |
| 53 | + $containerBuilder->setDefinition( |
| 54 | + 'myHandler', |
| 55 | + new Definition(MyResponseHandler::class), |
| 56 | + ); |
| 57 | + }); |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + protected function boot(): void |
| 63 | + { |
| 64 | + self::bootKernel(); |
| 65 | + |
| 66 | + $this->dispatcher = self::$kernel->getContainer()->get('event_dispatcher'); |
| 67 | + } |
| 68 | + |
| 69 | + protected function dispatchControllerArgumentsEvent( |
| 70 | + callable $controller |
| 71 | + ): ControllerArgumentsEvent { |
| 72 | + $request = new Request(); |
| 73 | + $controllerEvent = new ControllerEvent(self::$kernel, $controller, $request, null); |
| 74 | + |
| 75 | + $this->dispatcher->dispatch($controllerEvent, KernelEvents::CONTROLLER); |
| 76 | + |
| 77 | + $event = new ControllerArgumentsEvent(self::$kernel, $controllerEvent->getController(), [], $request, null); |
| 78 | + |
| 79 | + $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER_ARGUMENTS); |
| 80 | + |
| 81 | + return $event; |
| 82 | + } |
| 83 | + |
| 84 | + public function testGracefulExceptions() |
11 | 85 | {
|
12 |
| - $container = $this->createMock(ContainerBuilder::class); |
| 86 | + $this->boot(); |
| 87 | + $controller = new class { |
| 88 | + /** |
| 89 | + * @\Pitch\AdrBundle\Configuration\Graceful(MyException::class) |
| 90 | + */ |
| 91 | + public function throwMyExceptionGraceful() |
| 92 | + { |
| 93 | + throw new MyException(); |
| 94 | + } |
| 95 | + |
| 96 | + public function throwRuntimeException() |
| 97 | + { |
| 98 | + // this is graceful per default global graceful |
| 99 | + throw new RuntimeException(); |
| 100 | + } |
| 101 | + |
| 102 | + public function throwMyException() |
| 103 | + { |
| 104 | + throw new MyException(); |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + $event = $this->dispatchControllerArgumentsEvent([$controller, 'throwMyExceptionGraceful']); |
| 109 | + |
| 110 | + $this->assertInstanceOf(ActionProxy::class, $event->getController()); |
| 111 | + $this->assertInstanceOf(MyException::class, $event->getController()()); |
| 112 | + |
| 113 | + $event = $this->dispatchControllerArgumentsEvent([$controller, 'throwRuntimeException']); |
| 114 | + |
| 115 | + $this->assertInstanceOf(ActionProxy::class, $event->getController()); |
| 116 | + $this->assertInstanceOf(RuntimeException::class, $event->getController()()); |
13 | 117 |
|
14 |
| - $container->expects($this->once())->method('addCompilerPass') |
15 |
| - ->with($this->isInstanceOf(ResponseHandlerPass::class)); |
| 118 | + $event = $event = $this->dispatchControllerArgumentsEvent([$controller, 'throwMyException']); |
16 | 119 |
|
17 |
| - $bundle = new PitchAdrBundle(); |
| 120 | + $this->assertInstanceOf(ActionProxy::class, $event->getController()); |
18 | 121 |
|
19 |
| - /** @var ContainerBuilder $container */ |
20 |
| - $bundle->build($container); |
| 122 | + $this->expectException(MyException::class); |
| 123 | + $event->getController()(); |
21 | 124 | }
|
22 | 125 |
|
23 |
| - public function testGetContainerExtension() |
| 126 | + protected function dispatchViewEvent($payload, Request $request = null) |
24 | 127 | {
|
25 |
| - $bundle = new PitchAdrBundle(); |
| 128 | + $request ??= new Request(); |
| 129 | + |
| 130 | + $event = new ViewEvent(self::$kernel, $request, HttpKernelInterface::MASTER_REQUEST, $payload); |
| 131 | + |
| 132 | + $this->dispatcher->dispatch($event, KernelEvents::VIEW); |
| 133 | + |
| 134 | + return $event; |
| 135 | + } |
| 136 | + |
| 137 | + public function testResponder() |
| 138 | + { |
| 139 | + $this->boot(); |
| 140 | + |
| 141 | + $event = $this->dispatchViewEvent('foo'); |
26 | 142 |
|
27 |
| - $this->assertInstanceOf(PitchAdrExtension::class, $bundle->getContainerExtension()); |
| 143 | + $this->assertEquals(['value' => 'foo'], $event->getControllerResult()); |
28 | 144 | }
|
29 | 145 | }
|
0 commit comments