Passed
Pull Request — 1.x (#3)
by Kevin
02:01
created

Kernel::getLogDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Zenstruck\Browser\Tests\Fixture;
4
5
use Psr\Log\NullLogger;
6
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
7
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
8
use Symfony\Bundle\SecurityBundle\SecurityBundle;
9
use Symfony\Component\Config\Loader\LoaderInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\HttpFoundation\File\UploadedFile;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
17
use Symfony\Component\Mailer\MailerInterface;
18
use Symfony\Component\Mime\Address;
19
use Symfony\Component\Mime\Email;
20
use Symfony\Component\Routing\RouteCollectionBuilder;
21
use Symfony\Component\Security\Core\User\User;
22
use Symfony\Component\Security\Core\User\UserInterface;
23
24
/**
25
 * @author Kevin Bond <[email protected]>
26
 */
27
final class Kernel extends BaseKernel
28
{
29
    use MicroKernelTrait;
0 ignored issues
show
Bug introduced by
The trait Symfony\Bundle\Framework...Kernel\MicroKernelTrait requires the property $instanceof which is not provided by Zenstruck\Browser\Tests\Fixture\Kernel.
Loading history...
30
31
    public function page1(): Response
32
    {
33
        return new Response(\file_get_contents(__DIR__.'/files/page1.html'));
34
    }
35
36
    public function page2(): Response
37
    {
38
        return new Response('success');
39
    }
40
41
    public function text(): Response
42
    {
43
        return new Response('text content', 200, ['Content-Type' => 'text/plain']);
44
    }
45
46
    public function javascript(): Response
47
    {
48
        return new Response(\file_get_contents(__DIR__.'/files/javascript.html'));
49
    }
50
51
    public function submitForm(Request $request): JsonResponse
52
    {
53
        return new JsonResponse(\array_merge(
54
            $request->request->all(),
55
            \array_map(fn(UploadedFile $file) => $file->getClientOriginalName(), $request->files->all())
56
        ));
57
    }
58
59
    public function httpMethod(Request $request): Response
60
    {
61
        return new JsonResponse([
62
            'method' => $request->getMethod(),
63
            'headers' => $request->headers->all(),
64
            'query' => $request->query->all(),
65
            'attributes' => $request->attributes->all(),
66
            'files' => $request->files->all(),
67
            'server' => $request->server->all(),
68
            'request' => $request->query->all(),
69
            'content' => $request->getContent(),
70
            'ajax' => $request->isXmlHttpRequest(),
71
        ]);
72
    }
73
74
    public function json(Request $request): JsonResponse
75
    {
76
        return new JsonResponse($request->getContent(), 200, [], true);
77
    }
78
79
    public function exception(): void
80
    {
81
        throw new \Exception('exception thrown');
82
    }
83
84
    public function redirect1(): RedirectResponse
85
    {
86
        return new RedirectResponse('/redirect2');
87
    }
88
89
    public function redirect2(): RedirectResponse
90
    {
91
        return new RedirectResponse('/redirect3');
92
    }
93
94
    public function redirect3(): RedirectResponse
95
    {
96
        return new RedirectResponse('/page1');
97
    }
98
99
    public function sendEmail(): Response
100
    {
101
        $email = (new Email())
102
            ->from('[email protected]')
103
            ->to(new Address('[email protected]', 'Kevin'))
104
            ->cc('[email protected]')
105
            ->bcc('[email protected]')
106
            ->replyTo('[email protected]')
107
            ->attachFromPath(__DIR__.'/files/attachment.txt')
108
            ->subject('email subject')
109
            ->html('html body')
110
            ->text('text body')
111
        ;
112
113
        $email->getHeaders()->addTextHeader('X-PM-Tag', 'reset-password');
114
115
        $this->container->get('mailer')->send($email);
116
117
        return new Response('success');
118
    }
119
120
    public function user(?UserInterface $user = null): Response
121
    {
122
        return new Response($user ? "user: {$user->getUsername()}/{$user->getPassword()}" : 'anon');
123
    }
124
125
    public function registerBundles(): iterable
126
    {
127
        yield new FrameworkBundle();
0 ignored issues
show
Bug Best Practice introduced by
The expression yield new Symfony\Bundle...undle\FrameworkBundle() returns the type Generator which is incompatible with the return type mandated by Symfony\Component\HttpKe...face::registerBundles() of Symfony\Component\HttpKe...dleInterface[]|iterable.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
128
        yield new SecurityBundle();
129
    }
130
131
    protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void
0 ignored issues
show
Unused Code introduced by
The parameter $loader is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

131
    protected function configureContainer(ContainerBuilder $c, /** @scrutinizer ignore-unused */ LoaderInterface $loader): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
132
    {
133
        $c->loadFromExtension('framework', [
134
            'secret' => 'S3CRET',
135
            'router' => ['utf8' => true],
136
            'test' => true,
137
            'profiler' => ['enabled' => true, 'collect' => true],
138
            'mailer' => ['dsn' => 'null://null'],
139
            'session' => ['storage_id' => 'session.storage.mock_file'],
140
        ]);
141
        $c->loadFromExtension('security', [
142
            'encoders' => [User::class => 'plaintext'],
143
            'providers' => ['users' => ['memory' => ['users' => ['kevin' => ['password' => 'pass']]]]],
144
            'firewalls' => ['main' => ['anonymous' => true]],
145
        ]);
146
        $c->register('logger', NullLogger::class); // disable logging
147
        $c->setAlias('mailer', MailerInterface::class)->setPublic(true);
148
    }
149
150
    protected function configureRoutes(RouteCollectionBuilder $routes): void
151
    {
152
        $routes->add('/page1', 'kernel::page1');
153
        $routes->add('/page2', 'kernel::page2');
154
        $routes->add('/text', 'kernel::text');
155
        $routes->add('/submit-form', 'kernel::submitForm');
156
        $routes->add('/http-method', 'kernel::httpMethod');
157
        $routes->add('/exception', 'kernel::exception');
158
        $routes->add('/redirect1', 'kernel::redirect1');
159
        $routes->add('/redirect2', 'kernel::redirect2');
160
        $routes->add('/redirect3', 'kernel::redirect3');
161
        $routes->add('/send-email', 'kernel::sendEmail');
162
        $routes->add('/json', 'kernel::json');
163
        $routes->add('/javascript', 'kernel::javascript');
164
        $routes->add('/user', 'kernel::user');
165
    }
166
}
167