|
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\BrowserDataAppending; |
|
10
|
|
|
use Dziki\MonologSentryBundle\SubscribedProcessor\UserDataAppending; |
|
11
|
|
|
use Dziki\MonologSentryBundle\UserAgent\CachedParser; |
|
12
|
|
|
use Dziki\MonologSentryBundle\UserAgent\NativeParser; |
|
13
|
|
|
use Dziki\MonologSentryBundle\UserAgent\ParserInterface; |
|
14
|
|
|
use Dziki\MonologSentryBundle\UserAgent\PhpUserAgentParser; |
|
15
|
|
|
use Dziki\MonologSentryBundle\UserAgent\UserAgent; |
|
16
|
|
|
use Nyholm\BundleTest\AppKernel; |
|
17
|
|
|
use Nyholm\BundleTest\BaseBundleTestCase; |
|
18
|
|
|
use Nyholm\BundleTest\CompilerPass\PublicServicePass; |
|
19
|
|
|
use Psr\Log\LoggerInterface; |
|
20
|
|
|
use Symfony\Bundle\MonologBundle\MonologBundle; |
|
21
|
|
|
use Symfony\Bundle\SecurityBundle\SecurityBundle; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
24
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
|
25
|
|
|
|
|
26
|
|
|
class BundleInitializationTest extends BaseBundleTestCase |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @test |
|
30
|
|
|
*/ |
|
31
|
|
|
public function checkPhpUserAgentParserLoaded(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$kernel = $this->prepareKernel(); |
|
34
|
|
|
|
|
35
|
|
|
$this->checkDefaultServices($kernel); |
|
36
|
|
|
|
|
37
|
|
|
$phpUserAgentParser = $kernel->getContainer()->get(PhpUserAgentParser::class); |
|
38
|
|
|
$this->assertInstanceOf(PhpUserAgentParser::class, $phpUserAgentParser); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $configFilePath |
|
43
|
|
|
* |
|
44
|
|
|
* @return AppKernel |
|
45
|
|
|
*/ |
|
46
|
|
|
private function prepareKernel(string $configFilePath = 'config.yaml'): AppKernel |
|
47
|
|
|
{ |
|
48
|
|
|
// Create a new Kernel |
|
49
|
|
|
$kernel = $this->createKernel(); |
|
50
|
|
|
|
|
51
|
|
|
$kernel->addBundle(SecurityBundle::class); |
|
52
|
|
|
$kernel->addBundle(MonologBundle::class); |
|
53
|
|
|
|
|
54
|
|
|
$kernel->addConfigFile(__DIR__ . DIRECTORY_SEPARATOR . $configFilePath); |
|
55
|
|
|
|
|
56
|
|
|
// Make all services public |
|
57
|
|
|
$kernel->addCompilerPasses([new PublicServicePass()]); |
|
58
|
|
|
|
|
59
|
|
|
// Boot the kernel. |
|
60
|
|
|
$kernel->boot(); |
|
61
|
|
|
|
|
62
|
|
|
return $kernel; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function checkDefaultServices(AppKernel $kernel): void |
|
66
|
|
|
{ |
|
67
|
|
|
$container = $this->getContainer(); |
|
68
|
|
|
|
|
69
|
|
|
$this->logIn($container); |
|
70
|
|
|
|
|
71
|
|
|
$userDataAppending = $container->get('dziki.monolog_sentry_bundle.user_data_appending_subscribed_processor'); |
|
72
|
|
|
$this->assertInstanceOf(UserDataAppending::class, $userDataAppending); |
|
73
|
|
|
|
|
74
|
|
|
foreach (['symfony_version', 'commit', 'environment'] as $tagName) { |
|
75
|
|
|
$tagService = $container->get("dziki.monolog_sentry_bundle.{$tagName}_appending_processor"); |
|
76
|
|
|
$this->assertInstanceOf(TagAppending::class, $tagService); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** @var LoggerInterface $logger */ |
|
80
|
|
|
$logger = $container->get('logger'); |
|
81
|
|
|
|
|
82
|
|
|
$logger->info('some log'); |
|
83
|
|
|
|
|
84
|
|
|
$kernel->handle(new Request()); |
|
85
|
|
|
|
|
86
|
|
|
$logger->error('and now everything should be covered :)'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function logIn(ContainerInterface $container): void |
|
90
|
|
|
{ |
|
91
|
|
|
$session = $container->get('session'); |
|
92
|
|
|
|
|
93
|
|
|
$firewallName = 'main'; |
|
94
|
|
|
// if you don't define multiple connected firewalls, the context defaults to the firewall name |
|
95
|
|
|
// See https://symfony.com/doc/current/reference/configuration/security.html#firewall-context |
|
96
|
|
|
$firewallContext = $firewallName; |
|
97
|
|
|
|
|
98
|
|
|
// you may need to use a different token class depending on your application. |
|
99
|
|
|
// for example, when using Guard authentication you must instantiate PostAuthenticationGuardToken |
|
100
|
|
|
$token = new UsernamePasswordToken('test', 'test', $firewallName, ['ROLE_ADMIN']); |
|
101
|
|
|
$session->set('_security_' . $firewallContext, serialize($token)); |
|
102
|
|
|
$session->save(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @test |
|
107
|
|
|
*/ |
|
108
|
|
|
public function checkCacheServiceLoaded(): void |
|
109
|
|
|
{ |
|
110
|
|
|
$kernel = $this->prepareKernel('config_with_cache.yaml'); |
|
111
|
|
|
|
|
112
|
|
|
$cachedParser = $kernel->getContainer()->get(CachedParser::class); |
|
113
|
|
|
$this->assertInstanceOf(CachedParser::class, $cachedParser); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @test |
|
118
|
|
|
*/ |
|
119
|
|
|
public function checkCustomParserLoadedWhenServiceNameProvided(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$kernel = $this->prepareKernel('config_with_custom_parser.yaml'); |
|
122
|
|
|
|
|
123
|
|
|
$processorWithCustomParser = $kernel->getContainer()->get('dziki.monolog_sentry_bundle.browser_data_appending_subscribed_processor'); |
|
124
|
|
|
$this->assertInstanceOf(BrowserDataAppending::class, $processorWithCustomParser); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @test |
|
129
|
|
|
*/ |
|
130
|
|
|
public function checkNativeParserLoaded(): void |
|
131
|
|
|
{ |
|
132
|
|
|
if (!ini_get('browscap')) { |
|
133
|
|
|
$this->markTestSkipped( |
|
134
|
|
|
'The browscap.ini directive not set, skipped.' |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$kernel = $this->prepareKernel('config_with_native_parser.yaml'); |
|
139
|
|
|
|
|
140
|
|
|
$nativeParser = $kernel->getContainer()->get(NativeParser::class); |
|
141
|
|
|
$this->assertInstanceOf(NativeParser::class, $nativeParser); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected function getBundleClass(): string |
|
145
|
|
|
{ |
|
146
|
|
|
return MonologSentryBundle::class; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
class CustomParser implements ParserInterface { |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
public function parse(string $userAgent): UserAgent |
|
153
|
|
|
{ |
|
154
|
|
|
return UserAgent::create('', '', ''); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.