Skip to content

Commit ca55458

Browse files
committed
Make logger arg a hard dependency, remove dead code and add tests
1 parent 6f50efd commit ca55458

File tree

5 files changed

+78
-11
lines changed

5 files changed

+78
-11
lines changed

Debug/LoggingMiddleware.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class LoggingMiddleware implements MiddlewareInterface
2121
{
2222
private $logger;
2323

24-
public function __construct(LoggerInterface $logger = null)
24+
public function __construct(LoggerInterface $logger)
2525
{
2626
$this->logger = $logger;
2727
}
@@ -31,10 +31,6 @@ public function __construct(LoggerInterface $logger = null)
3131
*/
3232
public function handle($message, callable $next)
3333
{
34-
if (null === $this->logger) {
35-
return $next($message);
36-
}
37-
3834
$this->logger->debug('Starting handling message {class}', array(
3935
'message' => $message,
4036
'class' => \get_class($message),

DependencyInjection/MessengerPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function process(ContainerBuilder $container)
4949
return;
5050
}
5151

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

Tests/Debug/LoggingMiddlewareTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Tests\Debug;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Psr\Log\LoggerInterface;
16+
use Symfony\Component\Messenger\Debug\LoggingMiddleware;
17+
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
18+
19+
class LoggingMiddlewareTest extends TestCase
20+
{
21+
public function testDebugLogAndNextMiddleware()
22+
{
23+
$message = new DummyMessage('Hey');
24+
25+
$logger = $this->createMock(LoggerInterface::class);
26+
$logger
27+
->expects($this->exactly(2))
28+
->method('debug')
29+
;
30+
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
31+
$next
32+
->expects($this->once())
33+
->method('__invoke')
34+
->with($message)
35+
->willReturn('Hello')
36+
;
37+
38+
$result = (new LoggingMiddleware($logger))->handle($message, $next);
39+
40+
$this->assertSame('Hello', $result);
41+
}
42+
43+
/**
44+
* @expectedException \Exception
45+
*/
46+
public function testWarningLogOnException()
47+
{
48+
$message = new DummyMessage('Hey');
49+
50+
$logger = $this->createMock(LoggerInterface::class);
51+
$logger
52+
->expects($this->once())
53+
->method('debug')
54+
;
55+
$logger
56+
->expects($this->once())
57+
->method('warning')
58+
;
59+
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
60+
$next
61+
->expects($this->once())
62+
->method('__invoke')
63+
->with($message)
64+
->willThrowException(new \Exception())
65+
;
66+
67+
(new LoggingMiddleware($logger))->handle($message, $next);
68+
}
69+
}

Tests/DependencyInjection/MessengerPassTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function testProcess()
4444

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

47+
$this->assertFalse($container->hasDefinition('messenger.middleware.debug.logging'));
48+
4749
$handlerLocatorDefinition = $container->getDefinition($container->getDefinition('messenger.handler_resolver')->getArgument(0));
4850
$this->assertSame(ServiceLocator::class, $handlerLocatorDefinition->getClass());
4951
$this->assertEquals(

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
"php": "^7.1.3"
2020
},
2121
"require-dev": {
22-
"symfony/serializer": "~3.4|~4.0",
22+
"psr/log": "~1.0",
2323
"symfony/dependency-injection": "~3.4.6|~4.0",
2424
"symfony/http-kernel": "~3.4|~4.0",
25+
"symfony/process": "~3.4|~4.0",
2526
"symfony/property-access": "~3.4|~4.0",
26-
"symfony/var-dumper": "~3.4|~4.0",
27-
"symfony/property-access": "~3.4|~4.0",
28-
"symfony/process": "~4.0",
29-
"symfony/validator": "~4.0"
27+
"symfony/serializer": "~3.4|~4.0",
28+
"symfony/validator": "~3.4|~4.0",
29+
"symfony/var-dumper": "~3.4|~4.0"
3030
},
3131
"suggest": {
3232
"sroze/enqueue-bridge": "For using the php-enqueue library as an adapter."

0 commit comments

Comments
 (0)