Skip to content

Commit efd2f71

Browse files
committed
test: test bundle in framework
1 parent fa2f7a4 commit efd2f71

File tree

4 files changed

+163
-13
lines changed

4 files changed

+163
-13
lines changed

test/Fixtures/MyException.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace Pitch\AdrBundle\Fixtures;
3+
4+
use Exception;
5+
6+
class MyException extends Exception
7+
{
8+
}

test/Fixtures/MyResponseHandler.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace Pitch\AdrBundle\Fixtures;
3+
4+
use Pitch\AdrBundle\Responder\ResponseHandlerInterface;
5+
use Pitch\AdrBundle\Responder\ResponsePayloadEvent;
6+
7+
class MyResponseHandler implements ResponseHandlerInterface
8+
{
9+
public function getSupportedPayloadTypes(): array
10+
{
11+
return [
12+
MyResponseHandler::class,
13+
];
14+
}
15+
16+
public function handleResponsePayload(ResponsePayloadEvent $payloadEvent)
17+
{
18+
$payloadEvent->payload = 'foo';
19+
}
20+
}

test/Fixtures/MyResponsePayload.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace Pitch\AdrBundle\Fixtures;
3+
4+
class MyResponsePayload
5+
{
6+
}

test/PitchAdrBundleTest.php

+129-13
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,145 @@
11
<?php
22
namespace Pitch\AdrBundle;
33

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;
413
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;
723

8-
class PitchAdrBundleTest extends \PHPUnit\Framework\TestCase
24+
class PitchAdrBundleTest extends KernelTestCase
925
{
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()
1185
{
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()());
13117

14-
$container->expects($this->once())->method('addCompilerPass')
15-
->with($this->isInstanceOf(ResponseHandlerPass::class));
118+
$event = $event = $this->dispatchControllerArgumentsEvent([$controller, 'throwMyException']);
16119

17-
$bundle = new PitchAdrBundle();
120+
$this->assertInstanceOf(ActionProxy::class, $event->getController());
18121

19-
/** @var ContainerBuilder $container */
20-
$bundle->build($container);
122+
$this->expectException(MyException::class);
123+
$event->getController()();
21124
}
22125

23-
public function testGetContainerExtension()
126+
protected function dispatchViewEvent($payload, Request $request = null)
24127
{
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');
26142

27-
$this->assertInstanceOf(PitchAdrExtension::class, $bundle->getContainerExtension());
143+
$this->assertEquals(['value' => 'foo'], $event->getControllerResult());
28144
}
29145
}

0 commit comments

Comments
 (0)