forked from symfony/framework-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicroKernelTraitTest.php
172 lines (138 loc) · 5.4 KB
/
MicroKernelTraitTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\Kernel;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
require_once __DIR__.'/flex-style/src/FlexStyleMicroKernel.php';
class MicroKernelTraitTest extends TestCase
{
private ?Kernel $kernel = null;
protected function tearDown(): void
{
if ($this->kernel) {
$kernel = $this->kernel;
$this->kernel = null;
$fs = new Filesystem();
$fs->remove($kernel->getCacheDir());
}
}
public function test()
{
$kernel = $this->kernel = new ConcreteMicroKernel('test', false);
$kernel->boot();
$request = Request::create('/');
$response = $kernel->handle($request);
$this->assertEquals('halloween', $response->getContent());
$this->assertEquals('Have a great day!', $kernel->getContainer()->getParameter('halloween'));
$this->assertInstanceOf(\stdClass::class, $kernel->getContainer()->get('halloween'));
}
public function testAsEventSubscriber()
{
$kernel = $this->kernel = new ConcreteMicroKernel('test', false);
$kernel->boot();
$request = Request::create('/danger');
$response = $kernel->handle($request);
$this->assertSame('It\'s dangerous to go alone. Take this ⚔', $response->getContent());
}
public function testRoutingRouteLoaderTagIsAdded()
{
$frameworkExtension = $this->createMock(ExtensionInterface::class);
$frameworkExtension
->expects($this->atLeastOnce())
->method('getAlias')
->willReturn('framework');
$container = new ContainerBuilder();
$container->registerExtension($frameworkExtension);
$kernel = $this->kernel = new ConcreteMicroKernel('test', false);
$kernel->registerContainerConfiguration(new ClosureLoader($container));
$this->assertTrue($container->getDefinition('kernel')->hasTag('routing.route_loader'));
}
public function testFlexStyle()
{
$kernel = new FlexStyleMicroKernel('test', false);
$kernel->boot();
$request = Request::create('/');
$response = $kernel->handle($request);
$this->assertEquals('Have a great day!', $response->getContent());
$request = Request::create('/h');
$response = $kernel->handle($request);
$this->assertEquals('Have a great day!', $response->getContent());
$request = Request::create('/easter');
$response = $kernel->handle($request);
$this->assertSame('easter', $response->getContent());
}
public function testSecretLoadedFromExtension()
{
$kernel = $this->kernel = new ConcreteMicroKernel('test', false);
$kernel->boot();
self::assertSame('$ecret', $kernel->getContainer()->getParameter('kernel.secret'));
}
public function testAnonymousMicroKernel()
{
$kernel = $this->kernel = new class('anonymous_kernel') extends MinimalKernel {
public function helloAction(): Response
{
return new Response('Hello World!');
}
protected function configureContainer(ContainerConfigurator $c): void
{
$c->extension('framework', [
'annotations' => false,
'http_method_override' => false,
'handle_all_throwables' => true,
'php_errors' => ['log' => true],
'router' => ['utf8' => true],
]);
$c->services()->set('logger', NullLogger::class);
}
protected function configureRoutes(RoutingConfigurator $routes): void
{
$routes->add('hello', '/')->controller($this->helloAction(...));
}
};
$request = Request::create('/');
$response = $kernel->handle($request, HttpKernelInterface::MAIN_REQUEST, false);
$this->assertSame('Hello World!', $response->getContent());
}
}
abstract class MinimalKernel extends Kernel
{
use MicroKernelTrait;
private string $cacheDir;
public function __construct(string $cacheDir)
{
parent::__construct('test', false);
$this->cacheDir = sys_get_temp_dir().'/'.$cacheDir;
}
public function registerBundles(): iterable
{
yield new FrameworkBundle();
}
public function getCacheDir(): string
{
return $this->cacheDir;
}
public function getLogDir(): string
{
return $this->cacheDir;
}
}