forked from zenstruck/filesystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestKernel.php
More file actions
201 lines (182 loc) · 6.93 KB
/
TestKernel.php
File metadata and controls
201 lines (182 loc) · 6.93 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/*
* This file is part of the zenstruck/filesystem package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zenstruck\Tests\Fixtures;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use League\Glide\Urls\UrlBuilder;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Zenstruck\Filesystem\Glide\GlideTransformUrlGenerator;
use Zenstruck\Filesystem\Node\Mapping;
use Zenstruck\Filesystem\Symfony\Form\PendingFileType;
use Zenstruck\Filesystem\Symfony\Form\PendingImageType;
use Zenstruck\Filesystem\Symfony\ZenstruckFilesystemBundle;
use Zenstruck\Foundry\ZenstruckFoundryBundle;
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class TestKernel extends Kernel
{
use MicroKernelTrait;
public function submitForm(Request $request, FormFactoryInterface $factory): Response
{
$form = $factory->createBuilder()
->add('file', $request->query->has('image') ? PendingImageType::class : PendingFileType::class, [
'multiple' => $request->query->has('multiple'),
])
->getForm()
->submit($request->files->all())
;
if (!$form->isSubmitted() || !$form->isValid()) {
return new Response('Not submitted and valid.');
}
$file = $form->getData()['file'];
if (\is_array($file)) {
foreach ($file as $f) {
if (!\file_exists($f)) {
return new Response('File does not exist.');
}
}
return new JsonResponse(\array_map(fn($f) => \get_debug_type($f), $file));
}
if (!\file_exists($file)) {
return new Response('File does not exist.');
}
return new Response(\get_debug_type($file));
}
public function registerBundles(): iterable
{
yield new FrameworkBundle();
yield new DoctrineBundle();
yield new TwigBundle();
yield new ZenstruckFoundryBundle();
yield new ZenstruckFilesystemBundle();
}
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void
{
$c->loadFromExtension('framework', [
'http_method_override' => false,
'secret' => 'S3CRET',
'router' => ['utf8' => true],
'test' => true,
]);
$c->loadFromExtension('twig', [
'default_path' => __DIR__.'/templates',
]);
$c->loadFromExtension('zenstruck_foundry', [
'auto_refresh_proxies' => false,
]);
$c->loadFromExtension('doctrine', [
'dbal' => ['url' => 'sqlite:///%kernel.project_dir%/var/data.db'],
'orm' => [
'mappings' => [
'Test' => [
'is_bundle' => false,
'type' => 'attribute',
'dir' => '%kernel.project_dir%/tests/Fixtures/Entity',
'prefix' => 'Zenstruck\Tests\Fixtures\Entity',
'alias' => 'Test',
],
],
],
]);
$c->loadFromExtension('zenstruck_filesystem', [
'filesystems' => [
'public' => [
'dsn' => '%kernel.project_dir%/var/public',
'public_url' => [
'prefix' => '/prefix',
'version' => 'size',
],
'temporary_url' => self::VERSION_ID < 70100 ? null : 'route:public_temp',
'image_url' => 'route:public_transform',
'reset_before_tests' => true,
'events' => true,
'cache' => [
'metadata' => [Mapping::LAST_MODIFIED, Mapping::SIZE],
],
],
'private' => [
'dsn' => '%kernel.project_dir%/var/private',
'public_url' => [
'route' => [
'name' => 'private_public',
'sign' => true,
],
],
'image_url' => '@'.GlideTransformUrlGenerator::class,
'reset_before_tests' => true,
],
'no_reset' => [
'dsn' => '%kernel.project_dir%/var/no_reset',
],
'scoped' => 'scoped:public:some/prefix',
'static' => 'static-in-memory',
'cached' => [
'dsn' => '%kernel.project_dir%/var/cached',
'cache' => [
'metadata' => [Mapping::LAST_MODIFIED, Mapping::SIZE, Mapping::DIMENSIONS],
],
],
],
'default_filesystem' => 'public',
]);
$c->register(UrlBuilder::class)
->addArgument('/glide')
;
$c->register(GlideTransformUrlGenerator::class)
->setAutowired(true)
;
$c->register(Service::class)
->setPublic(true)
->setAutowired(true)
->setAutoconfigured(true)
;
$c->register(CustomPathGenerator::class)
->setAutowired(true)
->setAutoconfigured(true)
;
$c->register(CustomObjectPathGenerator::class)
->setAutowired(true)
->setAutoconfigured(true)
;
$c->register(FilesystemEventSubscriber::class)
->setAutowired(true)
->setAutoconfigured(true)
;
$c->register('logger', NullLogger::class); // disable logging
}
protected function configureRoutes(RoutingConfigurator $routes): void
{
$routes->add('submit_form', '/submit-form')
->controller([$this, 'submitForm'])
;
$routes->add('public_temp', '/temp/{path}')
->requirements(['path' => '.+'])
;
$routes->add('public_transform', '/transform/{path}')
->requirements(['path' => '.+'])
;
$routes->add('private_public', '/private/{path}')
->requirements(['path' => '.+'])
;
$routes->import(__DIR__.'/Controller/ArgumentResolverController.php', 'attribute');
$routes->import(__DIR__.'/Controller/ValidatedArgumentResolverController.php', 'attribute');
}
}