Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Commit

Permalink
Improve tests and some small fixes after mutation testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mleczakm committed Sep 12, 2018
1 parent c3f0e56 commit 6743bbb
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 23 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->children()
->scalarNode('value')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('name')
->defaultNull()
Expand Down
6 changes: 4 additions & 2 deletions DependencyInjection/MonologSentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function load(array $configs, ContainerBuilder $container): void
'dziki.monolog_sentry_bundle.user_data_appending_subscribed_processor',
new Definition(UserDataAppending::class, [new Reference(TokenStorageInterface::class)])
)
->setPrivate(true)
->addTag('kernel.event_subscriber')
->addTag('monolog.processor')
;
Expand Down Expand Up @@ -66,7 +67,8 @@ public function load(array $configs, ContainerBuilder $container): void
new Reference($parserClass),
]
)
);
)->setPrivate(true)
;

$parserClass = CachedParser::class;
}
Expand All @@ -83,7 +85,7 @@ public function load(array $configs, ContainerBuilder $container): void

if (\is_array($configs['tags'])) {
foreach ($configs['tags'] as $tag => ['value' => $value, 'name' => $name]) {
$tagName = $name ?: (string) $tag;
$tagName = $name ?: $tag;
$container->setDefinition(
"dziki.monolog_sentry_bundle.{$tag}_appending_processor",
new Definition(
Expand Down
2 changes: 0 additions & 2 deletions MonologSentryBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class MonologSentryBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new MonologHandlerOverridePass());
}
}
19 changes: 15 additions & 4 deletions Tests/Functional/BundleInitializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

namespace Dziki\MonologSentryBundle\Tests\Functional;

use Dziki\MonologSentryBundle\Handler\Raven;
use Dziki\MonologSentryBundle\MonologSentryBundle;
use Dziki\MonologSentryBundle\Processor\TagAppending;
use Dziki\MonologSentryBundle\SubscribedProcessor\BrowserDataAppending;
use Dziki\MonologSentryBundle\SubscribedProcessor\UserDataAppending;
use Dziki\MonologSentryBundle\UserAgent\CachedParser;
use Dziki\MonologSentryBundle\UserAgent\NativeParser;
use Dziki\MonologSentryBundle\UserAgent\ParserInterface;
use Dziki\MonologSentryBundle\UserAgent\PhpUserAgentParser;
use Dziki\MonologSentryBundle\UserAgent\UserAgent;
use Nyholm\BundleTest\AppKernel;
use Nyholm\BundleTest\BaseBundleTestCase;
use Nyholm\BundleTest\CompilerPass\PublicServicePass;
Expand Down Expand Up @@ -65,7 +64,7 @@ private function prepareKernel(string $configFilePath = 'config.yaml'): AppKerne
$kernel->addBundle(SecurityBundle::class);
$kernel->addBundle(MonologBundle::class);

$kernel->addConfigFile(__DIR__ . DIRECTORY_SEPARATOR . $configFilePath);
$kernel->addConfigFile(__DIR__.DIRECTORY_SEPARATOR.$configFilePath);

// Make all services public
$kernel->addCompilerPasses([new PublicServicePass()]);
Expand Down Expand Up @@ -112,7 +111,7 @@ private function logIn(ContainerInterface $container): void
// you may need to use a different token class depending on your application.
// for example, when using Guard authentication you must instantiate PostAuthenticationGuardToken
$token = new UsernamePasswordToken('test', 'test', $firewallName, ['ROLE_ADMIN']);
$session->set('_security_' . $firewallContext, serialize($token));
$session->set('_security_'.$firewallContext, serialize($token));
$session->save();
}

Expand Down Expand Up @@ -155,6 +154,18 @@ public function checkNativeParserLoaded(): void
$this->assertInstanceOf(NativeParser::class, $nativeParser);
}

/**
* @test
*/
public function checkCompilerPassReplaceRavenHandler(): void
{
$kernel = $this->prepareKernel('config.yaml');

$ravenHandler = $kernel->getContainer()->get('monolog.handler.sentry');

$this->assertInstanceOf(Raven::class, $ravenHandler);
}

protected function getBundleClass(): string
{
return MonologSentryBundle::class;
Expand Down
114 changes: 105 additions & 9 deletions Tests/Functional/MonologSentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@
namespace Dziki\MonologSentryBundle\Tests\Functional;

use Dziki\MonologSentryBundle\DependencyInjection\MonologSentryExtension;
use Dziki\MonologSentryBundle\Processor\TagAppending;
use Dziki\MonologSentryBundle\SubscribedProcessor\BrowserDataAppending;
use Dziki\MonologSentryBundle\SubscribedProcessor\UserDataAppending;
use Dziki\MonologSentryBundle\UserAgent\CachedParser;
use Dziki\MonologSentryBundle\UserAgent\NativeParser;
use Dziki\MonologSentryBundle\UserAgent\PhpUserAgentParser;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;

use Symfony\Component\Cache\Simple\ArrayCache;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class MonologSentryExtensionTest extends AbstractExtensionTestCase
{
/**
* @test
* @covers \Dziki\MonologSentryBundle\DependencyInjection\MonologSentryExtension::load
*
* @uses \Dziki\MonologSentryBundle\DependencyInjection\Configuration
*/
public function checkIfServicesDefinedAccordingToConfig(): void
public function checkIfServicesDefinedAndPrivate(): void
{
$this->load(
[
Expand All @@ -32,18 +42,104 @@ public function checkIfServicesDefinedAccordingToConfig(): void
);

$defaultServices = [
'dziki.monolog_sentry_bundle.user_data_appending_subscribed_processor',
'Dziki\MonologSentryBundle\UserAgent\PhpUserAgentParser',
'dziki.monolog_sentry_bundle.browser_data_appending_subscribed_processor',
'dziki.monolog_sentry_bundle.test_appending_processor',
'dziki.monolog_sentry_bundle.test2_appending_processor',
'dziki.monolog_sentry_bundle.user_data_appending_subscribed_processor' => [
'class' => UserDataAppending::class,
],
PhpUserAgentParser::class => [
'class' => PhpUserAgentParser::class,
],
'dziki.monolog_sentry_bundle.browser_data_appending_subscribed_processor' => [
'class' => BrowserDataAppending::class,
],
'dziki.monolog_sentry_bundle.test_appending_processor' => [
'class' => TagAppending::class,
],
'dziki.monolog_sentry_bundle.test2_appending_processor' => [
'class' => TagAppending::class,
],
];

foreach ($defaultServices as $defaultService) {
$this->assertContainerBuilderHasService($defaultService);
$this->registerService(TokenStorageInterface::class, new TokenStorage());
$this->compile();

$this->assertServicesDefinedAndPrivate($defaultServices);
}

private function assertServicesDefinedAndPrivate($defaultServices): void
{
foreach ($defaultServices as $defaultService => $data) {
$this->assertContainerBuilderHasService($defaultService, $data['class']);
try {
$service = $this->container->get($defaultService);
$this->fail(sprintf('Service "%s" (%s) should be private', \get_class($service), $defaultService));
} catch (ServiceNotFoundException $exception) {
$this->assertContains($defaultService, $exception->getMessage());
}
}
}

/**
* @test
* @covers \Dziki\MonologSentryBundle\DependencyInjection\MonologSentryExtension::load
*
* @uses \Dziki\MonologSentryBundle\DependencyInjection\Configuration
*/
public function checkIfNativeUserAgentParserServiceDefinedAndPrivate(): void
{
if (!ini_get('browscap')) {
$this->markTestSkipped(
'The browscap.ini directive not set, skipped.'
);
}

$this->load(
[
'user_context' => false,
'user_agent_parser' => 'native',
]
);

$defaultServices = [
NativeParser::class => [
'class' => NativeParser::class,
],
];
$this->compile();

$this->assertServicesDefinedAndPrivate($defaultServices);
}

/**
* @test
* @covers \Dziki\MonologSentryBundle\DependencyInjection\MonologSentryExtension::load
*
* @uses \Dziki\MonologSentryBundle\DependencyInjection\Configuration
*/
public function checkIfCachedParserServiceDefinedAndPrivate(): void
{
$this->load(
[
'user_context' => false,
'user_agent_parser' => 'phpuseragent',
'cache' => 'app.cache.simple',
]
);

$defaultServices = [
PhpUserAgentParser::class => [
'class' => PhpUserAgentParser::class,
],
CachedParser::class => [
'class' => CachedParser::class,
],
];

$this->registerService('app.cache.simple', new ArrayCache());
$this->compile();

$this->assertServicesDefinedAndPrivate($defaultServices);
}

protected function getContainerExtensions()
{
return [
Expand Down
5 changes: 5 additions & 0 deletions Tests/Functional/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ monolog_sentry:
symfony_version: !php/const Symfony\Component\HttpKernel\Kernel::VERSION
commit: '37bf8629120469e164477a8d5046967bf3235988'
environment: 'test'
1: '2'
'true': 'false'
real tag name (empty name will be ignored):
name:
value: 'non empty value'

monolog:
handlers:
Expand Down
13 changes: 7 additions & 6 deletions Tests/Unit/Handler/RavenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,20 @@ protected function getHandler($ravenClient)
}

/**
* @param int $level
* @param int $level
* @param string $message
* @param array $context
*
* @param array $context
* @param string $channel
* @param array $extra
* @param array $extra
*
* @return array Record
*
* @throws \Exception
*/
protected function getRecord($level = Logger::WARNING, $message = 'test', array $context = [], $channel = 'test', $extra = []): array
{
return [
'message' => (string)$message,
'message' => (string) $message,
'context' => $context,
'level' => $level,
'level_name' => Logger::getLevelName($level),
Expand Down Expand Up @@ -196,7 +197,7 @@ public function testException()
$handler->handle($record);
}

$this->assertEquals('[test] ' . $record['message'], $ravenClient->lastData['message']);
$this->assertEquals('[test] '.$record['message'], $ravenClient->lastData['message']);
}

private function methodThatThrowsAnException()
Expand Down

0 comments on commit 6743bbb

Please sign in to comment.