Skip to content

[Messenger] Testing LoggingMiddleware and minor improvements #26912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

<!-- Logging & Debug -->
<service id="messenger.middleware.debug.logging" class="Symfony\Component\Messenger\Debug\LoggingMiddleware">
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another workaround could be creating from scratch this service definition in MessengerPass, only if debug and logger are enabled/exists, wdyt?

Copy link
Contributor

@ogizanagi ogizanagi Apr 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We usually try as much as possible to declare all services in xml files so everything is in one place, favoring discovery/inspections and allowing to jump from classes to definitions with the PhpStorm Symfony plugin.

<argument type="service" id="logger" on-invalid="null" />
<argument type="service" id="logger" />

<tag name="messenger.bus_middleware" priority="10" />
<tag name="monolog.logger" channel="messenger" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
Expand Down Expand Up @@ -1089,6 +1090,7 @@ public function testEventDispatcherService()
{
$container = $this->createContainer(array('kernel.charset' => 'UTF-8', 'kernel.secret' => 'secret'));
$container->registerExtension(new FrameworkExtension());
$container->getCompilerPassConfig()->setBeforeOptimizationPasses(array(new LoggerPass()));
$this->loadFromFile($container, 'default_config');
$container
->register('foo', \stdClass::class)
Expand Down Expand Up @@ -1180,6 +1182,7 @@ protected function createContainerFromFile($file, $data = array(), $resetCompile
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
}
$container->getCompilerPassConfig()->setBeforeOptimizationPasses(array(new LoggerPass()));
$container->getCompilerPassConfig()->setBeforeRemovingPasses(array(new AddConstraintValidatorsPass(), new TranslatorPass('translator.default', 'translation.reader')));
$container->getCompilerPassConfig()->setAfterRemovingPasses(array(new AddAnnotationsCachedReaderPass()));

Expand Down
6 changes: 1 addition & 5 deletions src/Symfony/Component/Messenger/Debug/LoggingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LoggingMiddleware implements MiddlewareInterface
{
private $logger;

public function __construct(LoggerInterface $logger = null)
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
Expand All @@ -31,10 +31,6 @@ public function __construct(LoggerInterface $logger = null)
*/
public function handle($message, callable $next)
{
if (null === $this->logger) {
return $next($message);
}

$this->logger->debug('Starting handling message {class}', array(
'message' => $message,
'class' => \get_class($message),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function process(ContainerBuilder $container)
return;
}

if (!$container->getParameter('kernel.debug') || !$container->has('logger')) {
if (!$container->getParameter('kernel.debug') || !$container->hasAlias('logger')) {
$container->removeDefinition('messenger.middleware.debug.logging');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Tests\Debug;

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Debug\LoggingMiddleware;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;

class LoggingMiddlewareTest extends TestCase
{
public function testDebugLogAndNextMiddleware()
{
$message = new DummyMessage('Hey');

$logger = $this->createMock(LoggerInterface::class);
$logger
->expects($this->exactly(2))
->method('debug')
;
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
$next
->expects($this->once())
->method('__invoke')
->with($message)
->willReturn('Hello')
;

$result = (new LoggingMiddleware($logger))->handle($message, $next);

$this->assertSame('Hello', $result);
}

/**
* @expectedException \Exception
*/
public function testWarningLogOnException()
{
$message = new DummyMessage('Hey');

$logger = $this->createMock(LoggerInterface::class);
$logger
->expects($this->once())
->method('debug')
;
$logger
->expects($this->once())
->method('warning')
;
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
$next
->expects($this->once())
->method('__invoke')
->with($message)
->willThrowException(new \Exception())
;

(new LoggingMiddleware($logger))->handle($message, $next);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function testProcess()

(new MessengerPass())->process($container);

$this->assertFalse($container->hasDefinition('messenger.middleware.debug.logging'));

$handlerLocatorDefinition = $container->getDefinition($container->getDefinition('messenger.handler_resolver')->getArgument(0));
$this->assertSame(ServiceLocator::class, $handlerLocatorDefinition->getClass());
$this->assertEquals(
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Messenger/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
"php": "^7.1.3"
},
"require-dev": {
"symfony/serializer": "~3.4|~4.0",
"psr/log": "~1.0",
"symfony/dependency-injection": "~3.4.6|~4.0",
"symfony/http-kernel": "~3.4|~4.0",
"symfony/process": "~3.4|~4.0",
"symfony/property-access": "~3.4|~4.0",
"symfony/var-dumper": "~3.4|~4.0",
"symfony/property-access": "~3.4|~4.0",
"symfony/process": "~4.0",
"symfony/validator": "~4.0"
"symfony/serializer": "~3.4|~4.0",
"symfony/validator": "~3.4|~4.0",
"symfony/var-dumper": "~3.4|~4.0"
},
"suggest": {
"sroze/enqueue-bridge": "For using the php-enqueue library as an adapter."
Expand Down