Completed
Push — master ( 8bbc42...8e72cd )
by Michał
08:19
created

checkCacheServiceLoaded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
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\MonologSentryBundle;
8
use Dziki\MonologSentryBundle\Processor\TagAppending;
9
use Dziki\MonologSentryBundle\SubscribedProcessor\UserDataAppending;
10
use Dziki\MonologSentryBundle\UserAgent\CachedParser;
11
use Dziki\MonologSentryBundle\UserAgent\PhpUserAgentParser;
12
use Nyholm\BundleTest\AppKernel;
13
use Nyholm\BundleTest\BaseBundleTestCase;
14
use Nyholm\BundleTest\CompilerPass\PublicServicePass;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Bundle\MonologBundle\MonologBundle;
17
use Symfony\Bundle\SecurityBundle\SecurityBundle;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
21
22
class BundleInitializationTest extends BaseBundleTestCase
23
{
24
    /**
25
     * @test
26
     */
27
    public function checkDefaultServicesLoaded(): void
28
    {
29
        $kernel = $this->prepareKernel();
30
31
        $this->checkDefaultServices($kernel);
32
    }
33
34
    /**
35
     * @param string $configFilePath
36
     *
37
     * @return AppKernel
38
     */
39
    private function prepareKernel(string $configFilePath = 'config.yaml'): AppKernel
40
    {
41
        // Create a new Kernel
42
        $kernel = $this->createKernel();
43
44
        $kernel->addBundle(SecurityBundle::class);
45
        $kernel->addBundle(MonologBundle::class);
46
47
        $kernel->addConfigFile(__DIR__ . DIRECTORY_SEPARATOR . $configFilePath);
48
49
        // Make all services public
50
        $kernel->addCompilerPasses([new PublicServicePass()]);
51
52
        // Boot the kernel.
53
        $kernel->boot();
54
55
        return $kernel;
56
    }
57
58
    private function checkDefaultServices(AppKernel $kernel): void
59
    {
60
        $container = $this->getContainer();
61
62
        $this->logIn($container);
63
64
        $userDataAppending = $container->get('dziki.monolog_sentry_bundle.user_data_appending_subscribed_processor');
65
        $this->assertInstanceOf(UserDataAppending::class, $userDataAppending);
66
67
        $phpUserAgentParser = $container->get(PhpUserAgentParser::class);
68
        $this->assertInstanceOf(PhpUserAgentParser::class, $phpUserAgentParser);
69
70
        foreach (['symfony_version', 'commit', 'environment'] as $tagName) {
71
            $tagService = $container->get("dziki.monolog_sentry_bundle.{$tagName}_appending_processor");
72
            $this->assertInstanceOf(TagAppending::class, $tagService);
73
        }
74
75
        /** @var LoggerInterface $logger */
76
        $logger = $container->get('logger');
77
78
        $logger->info('some log');
79
80
        $kernel->handle(new Request());
81
82
        $logger->error('and now everything should be covered :)');
83
    }
84
85
    private function logIn(ContainerInterface $container): void
86
    {
87
        $session = $container->get('session');
88
89
        $firewallName = 'main';
90
        // if you don't define multiple connected firewalls, the context defaults to the firewall name
91
        // See https://symfony.com/doc/current/reference/configuration/security.html#firewall-context
92
        $firewallContext = $firewallName;
93
94
        // you may need to use a different token class depending on your application.
95
        // for example, when using Guard authentication you must instantiate PostAuthenticationGuardToken
96
        $token = new UsernamePasswordToken('test', 'test', $firewallName, ['ROLE_ADMIN']);
97
        $session->set('_security_' . $firewallContext, serialize($token));
98
        $session->save();
99
    }
100
101
    /**
102
     * @test
103
     */
104
    public function checkCacheServiceLoaded(): void
105
    {
106
        $kernel = $this->prepareKernel('config_with_cache.yaml');
107
108
        $cachedParser = $kernel->getContainer()->get(CachedParser::class);
109
        $this->assertInstanceOf(CachedParser::class, $cachedParser);
110
    }
111
112
    protected function getBundleClass(): string
113
    {
114
        return MonologSentryBundle::class;
115
    }
116
}
117