Completed
Push — master ( c3f0e5...6743bb )
by Michał
01:39
created

checkCompilerPassReplaceRavenHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dziki\MonologSentryBundle\Tests\Functional;
6
7
use Dziki\MonologSentryBundle\Handler\Raven;
8
use Dziki\MonologSentryBundle\MonologSentryBundle;
9
use Dziki\MonologSentryBundle\Processor\TagAppending;
10
use Dziki\MonologSentryBundle\SubscribedProcessor\BrowserDataAppending;
11
use Dziki\MonologSentryBundle\SubscribedProcessor\UserDataAppending;
12
use Dziki\MonologSentryBundle\UserAgent\CachedParser;
13
use Dziki\MonologSentryBundle\UserAgent\NativeParser;
14
use Dziki\MonologSentryBundle\UserAgent\PhpUserAgentParser;
15
use Nyholm\BundleTest\AppKernel;
16
use Nyholm\BundleTest\BaseBundleTestCase;
17
use Nyholm\BundleTest\CompilerPass\PublicServicePass;
18
use Psr\Log\LoggerInterface;
19
use Symfony\Bundle\MonologBundle\MonologBundle;
20
use Symfony\Bundle\SecurityBundle\SecurityBundle;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
24
25
/**
26
 * @covers \Dziki\MonologSentryBundle\MonologSentryBundle
27
 * @covers \Dziki\MonologSentryBundle\DependencyInjection\Configuration
28
 * @covers \Dziki\MonologSentryBundle\DependencyInjection\MonologSentryExtension
29
 *
30
 * @uses \Dziki\MonologSentryBundle\Handler\Raven
31
 * @uses \Dziki\MonologSentryBundle\DependencyInjection\Compiler\MonologHandlerOverridePass
32
 * @uses \Dziki\MonologSentryBundle\Processor\TagAppending
33
 * @uses \Dziki\MonologSentryBundle\UserAgent\PhpUserAgentParser
34
 * @uses \Dziki\MonologSentryBundle\UserAgent\UserAgent
35
 * @uses \Dziki\MonologSentryBundle\SubscribedProcessor\BrowserDataAppending
36
 * @uses \Dziki\MonologSentryBundle\SubscribedProcessor\UserDataAppending
37
 * @uses \Dziki\MonologSentryBundle\UserAgent\CachedParser
38
 */
39
class BundleInitializationTest extends BaseBundleTestCase
40
{
41
    /**
42
     * @test
43
     */
44
    public function checkPhpUserAgentParserLoaded(): void
45
    {
46
        $kernel = $this->prepareKernel();
47
48
        $this->checkDefaultServices($kernel);
49
50
        $phpUserAgentParser = $kernel->getContainer()->get(PhpUserAgentParser::class);
51
        $this->assertInstanceOf(PhpUserAgentParser::class, $phpUserAgentParser);
52
    }
53
54
    /**
55
     * @param string $configFilePath
56
     *
57
     * @return AppKernel
58
     */
59
    private function prepareKernel(string $configFilePath = 'config.yaml'): AppKernel
60
    {
61
        // Create a new Kernel
62
        $kernel = $this->createKernel();
63
64
        $kernel->addBundle(SecurityBundle::class);
65
        $kernel->addBundle(MonologBundle::class);
66
67
        $kernel->addConfigFile(__DIR__.DIRECTORY_SEPARATOR.$configFilePath);
68
69
        // Make all services public
70
        $kernel->addCompilerPasses([new PublicServicePass()]);
71
72
        // Boot the kernel.
73
        $kernel->boot();
74
75
        return $kernel;
76
    }
77
78
    private function checkDefaultServices(AppKernel $kernel): void
79
    {
80
        $container = $this->getContainer();
81
82
        $this->logIn($container);
83
84
        $userDataAppending = $container->get('dziki.monolog_sentry_bundle.user_data_appending_subscribed_processor');
85
        $this->assertInstanceOf(UserDataAppending::class, $userDataAppending);
86
87
        foreach (['symfony_version', 'commit', 'environment'] as $tagName) {
88
            $tagService = $container->get("dziki.monolog_sentry_bundle.{$tagName}_appending_processor");
89
            $this->assertInstanceOf(TagAppending::class, $tagService);
90
        }
91
92
        /** @var LoggerInterface $logger */
93
        $logger = $container->get('logger');
94
95
        $logger->info('some log');
96
97
        $kernel->handle(new Request());
98
99
        $logger->error('and now everything should be covered :)');
100
    }
101
102
    private function logIn(ContainerInterface $container): void
103
    {
104
        $session = $container->get('session');
105
106
        $firewallName = 'main';
107
        // if you don't define multiple connected firewalls, the context defaults to the firewall name
108
        // See https://symfony.com/doc/current/reference/configuration/security.html#firewall-context
109
        $firewallContext = $firewallName;
110
111
        // you may need to use a different token class depending on your application.
112
        // for example, when using Guard authentication you must instantiate PostAuthenticationGuardToken
113
        $token = new UsernamePasswordToken('test', 'test', $firewallName, ['ROLE_ADMIN']);
114
        $session->set('_security_'.$firewallContext, serialize($token));
115
        $session->save();
116
    }
117
118
    /**
119
     * @test
120
     */
121
    public function checkCacheServiceLoaded(): void
122
    {
123
        $kernel = $this->prepareKernel('config_with_cache.yaml');
124
125
        $cachedParser = $kernel->getContainer()->get(CachedParser::class);
126
        $this->assertInstanceOf(CachedParser::class, $cachedParser);
127
    }
128
129
    /**
130
     * @test
131
     */
132
    public function checkCustomParserLoadedWhenServiceNameProvided(): void
133
    {
134
        $kernel = $this->prepareKernel('config_with_custom_parser.yaml');
135
136
        $processorWithCustomParser = $kernel->getContainer()->get('dziki.monolog_sentry_bundle.browser_data_appending_subscribed_processor');
137
        $this->assertInstanceOf(BrowserDataAppending::class, $processorWithCustomParser);
138
    }
139
140
    /**
141
     * @test
142
     */
143
    public function checkNativeParserLoaded(): void
144
    {
145
        if (!ini_get('browscap')) {
146
            $this->markTestSkipped(
147
                'The browscap.ini directive not set, skipped.'
148
            );
149
        }
150
151
        $kernel = $this->prepareKernel('config_with_native_parser.yaml');
152
153
        $nativeParser = $kernel->getContainer()->get(NativeParser::class);
154
        $this->assertInstanceOf(NativeParser::class, $nativeParser);
155
    }
156
157
    /**
158
     * @test
159
     */
160
    public function checkCompilerPassReplaceRavenHandler(): void
161
    {
162
        $kernel = $this->prepareKernel('config.yaml');
163
164
        $ravenHandler = $kernel->getContainer()->get('monolog.handler.sentry');
165
166
        $this->assertInstanceOf(Raven::class, $ravenHandler);
167
    }
168
169
    protected function getBundleClass(): string
170
    {
171
        return MonologSentryBundle::class;
172
    }
173
}
174