Passed
Push — 1.x ( a74d24...5e9553 )
by Kevin
01:49
created

Kernel::text()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
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\Component\Config\Loader\LoaderInterface;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\HttpFoundation\File\UploadedFile;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
16
use Symfony\Component\Mailer\MailerInterface;
17
use Symfony\Component\Mime\Address;
18
use Symfony\Component\Mime\Email;
19
use Symfony\Component\Routing\RouteCollectionBuilder;
20
21
/**
22
 * @author Kevin Bond <[email protected]>
23
 */
24
final class Kernel extends BaseKernel
25
{
26
    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...
27
28
    public function page1(): Response
29
    {
30
        return new Response(\file_get_contents(__DIR__.'/files/page1.html'));
31
    }
32
33
    public function page2(): Response
34
    {
35
        return new Response('success');
36
    }
37
38
    public function text(): Response
39
    {
40
        return new Response('text content', 200, ['Content-Type' => 'text/plain']);
41
    }
42
43
    public function javascript(): Response
44
    {
45
        return new Response(\file_get_contents(__DIR__.'/files/javascript.html'));
46
    }
47
48
    public function submitForm(Request $request): JsonResponse
49
    {
50
        return new JsonResponse(\array_merge(
51
            $request->request->all(),
52
            \array_map(fn(UploadedFile $file) => $file->getClientOriginalName(), $request->files->all())
53
        ));
54
    }
55
56
    public function httpMethod(Request $request): Response
57
    {
58
        return new JsonResponse([
59
            'method' => $request->getMethod(),
60
            'headers' => $request->headers->all(),
61
            'query' => $request->query->all(),
62
            'attributes' => $request->attributes->all(),
63
            'files' => $request->files->all(),
64
            'server' => $request->server->all(),
65
            'request' => $request->query->all(),
66
            'content' => $request->getContent(),
67
            'ajax' => $request->isXmlHttpRequest(),
68
        ]);
69
    }
70
71
    public function json(Request $request): JsonResponse
72
    {
73
        return new JsonResponse($request->getContent(), 200, [], true);
74
    }
75
76
    public function exception(): void
77
    {
78
        throw new \Exception('exception thrown');
79
    }
80
81
    public function redirect1(): RedirectResponse
82
    {
83
        return new RedirectResponse('/redirect2');
84
    }
85
86
    public function redirect2(): RedirectResponse
87
    {
88
        return new RedirectResponse('/redirect3');
89
    }
90
91
    public function redirect3(): RedirectResponse
92
    {
93
        return new RedirectResponse('/page1');
94
    }
95
96
    public function sendEmail(): Response
97
    {
98
        $email = (new Email())
99
            ->from('[email protected]')
100
            ->to(new Address('[email protected]', 'Kevin'))
101
            ->cc('[email protected]')
102
            ->bcc('[email protected]')
103
            ->replyTo('[email protected]')
104
            ->attachFromPath(__DIR__.'/files/attachment.txt')
105
            ->subject('email subject')
106
            ->html('html body')
107
            ->text('text body')
108
        ;
109
110
        $email->getHeaders()->addTextHeader('X-PM-Tag', 'reset-password');
111
112
        $this->container->get('mailer')->send($email);
113
114
        return new Response('success');
115
    }
116
117
    public function registerBundles(): iterable
118
    {
119
        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...
120
    }
121
122
    public function getLogDir(): string
123
    {
124
        return \sys_get_temp_dir().'/zenstruck-browser/logs';
125
    }
126
127
    public function getCacheDir(): string
128
    {
129
        return \sys_get_temp_dir().'/zenstruck-browser/cache';
130
    }
131
132
    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

132
    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...
133
    {
134
        $c->loadFromExtension('framework', [
135
            'secret' => 'S3CRET',
136
            'router' => ['utf8' => true],
137
            'test' => true,
138
            'profiler' => ['enabled' => true, 'collect' => true],
139
            'mailer' => ['dsn' => 'null://null'],
140
        ]);
141
        $c->register('logger', NullLogger::class); // disable logging
142
        $c->setAlias('mailer', MailerInterface::class)->setPublic(true);
143
    }
144
145
    protected function configureRoutes(RouteCollectionBuilder $routes): void
146
    {
147
        $routes->add('/page1', 'kernel::page1');
148
        $routes->add('/page2', 'kernel::page2');
149
        $routes->add('/text', 'kernel::text');
150
        $routes->add('/submit-form', 'kernel::submitForm');
151
        $routes->add('/http-method', 'kernel::httpMethod');
152
        $routes->add('/exception', 'kernel::exception');
153
        $routes->add('/redirect1', 'kernel::redirect1');
154
        $routes->add('/redirect2', 'kernel::redirect2');
155
        $routes->add('/redirect3', 'kernel::redirect3');
156
        $routes->add('/send-email', 'kernel::sendEmail');
157
        $routes->add('/json', 'kernel::json');
158
        $routes->add('/javascript', 'kernel::javascript');
159
    }
160
}
161