Skip to content

Commit d43dd05

Browse files
minor symfony#58098 [HttpKernel] Fix method naming collision of dummy logger implementations (derrabus)
This PR was merged into the 7.2 branch. Discussion ---------- [HttpKernel] Fix method naming collision of dummy logger implementations | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | N/A | License | MIT In HttpKernel's test suite, we use a dummy logger implementation which exposes a `getLogs()` method. Our own `DebugLoggerInterface` also defines a `getLogs()` method with a different signature and different semantics. Then there's also a `TestLogger` class which extends our dummy logger _and_ implements that interface which leads to a weird naming collision. In this PR, I'm renaming the dummy's `getLogs()` method to `getLogsForLevel()` to resolve this situation. While I'm at it, I'm also simplifying the implementation by leveraging the `AbstractLogger` class from the psr/log contracts. Since I'm only changing test classes, I believe that the method signature changes should not cause any trouble. Commits ------- ec9f13e [HttpKernel] Fix method naming collision
2 parents 123aac4 + ec9f13e commit d43dd05

File tree

2 files changed

+18
-53
lines changed

2 files changed

+18
-53
lines changed

src/Symfony/Component/HttpKernel/Tests/EventListener/ErrorListenerTest.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function testHandleWithLogger($event, $event2)
100100
}
101101

102102
$this->assertEquals(3, $logger->countErrors());
103-
$logs = $logger->getLogs('critical');
103+
$logs = $logger->getLogsForLevel('critical');
104104
$this->assertCount(3, $logs);
105105
$this->assertStringStartsWith('Uncaught PHP Exception Exception: "foo" at ErrorListenerTest.php line', $logs[0]);
106106
$this->assertStringStartsWith('Uncaught PHP Exception Exception: "foo" at ErrorListenerTest.php line', $logs[1]);
@@ -124,8 +124,8 @@ public function testHandleWithLoggerAndCustomConfiguration()
124124
$this->assertEquals(new Response('foo', 401), $event->getResponse());
125125

126126
$this->assertEquals(0, $logger->countErrors());
127-
$this->assertCount(0, $logger->getLogs('critical'));
128-
$this->assertCount(1, $logger->getLogs('warning'));
127+
$this->assertCount(0, $logger->getLogsForLevel('critical'));
128+
$this->assertCount(1, $logger->getLogsForLevel('warning'));
129129
}
130130

131131
public function testHandleWithLogLevelAttribute()
@@ -139,8 +139,8 @@ public function testHandleWithLogLevelAttribute()
139139
$l->onKernelException($event);
140140

141141
$this->assertEquals(0, $logger->countErrors());
142-
$this->assertCount(0, $logger->getLogs('critical'));
143-
$this->assertCount(1, $logger->getLogs('warning'));
142+
$this->assertCount(0, $logger->getLogsForLevel('critical'));
143+
$this->assertCount(1, $logger->getLogsForLevel('warning'));
144144
}
145145

146146
public function testHandleClassImplementingInterfaceWithLogLevelAttribute()
@@ -154,8 +154,8 @@ public function testHandleClassImplementingInterfaceWithLogLevelAttribute()
154154
$l->onKernelException($event);
155155

156156
$this->assertEquals(0, $logger->countErrors());
157-
$this->assertCount(0, $logger->getLogs('critical'));
158-
$this->assertCount(1, $logger->getLogs('warning'));
157+
$this->assertCount(0, $logger->getLogsForLevel('critical'));
158+
$this->assertCount(1, $logger->getLogsForLevel('warning'));
159159
}
160160

161161
public function testHandleWithLogLevelAttributeAndCustomConfiguration()
@@ -173,8 +173,8 @@ public function testHandleWithLogLevelAttributeAndCustomConfiguration()
173173
$l->onKernelException($event);
174174

175175
$this->assertEquals(0, $logger->countErrors());
176-
$this->assertCount(0, $logger->getLogs('warning'));
177-
$this->assertCount(1, $logger->getLogs('info'));
176+
$this->assertCount(0, $logger->getLogsForLevel('warning'));
177+
$this->assertCount(1, $logger->getLogsForLevel('info'));
178178
}
179179

180180
/**
@@ -327,6 +327,11 @@ public function countErrors(?Request $request = null): int
327327
{
328328
return \count($this->logs['critical']);
329329
}
330+
331+
public function getLogs(?Request $request = null): array
332+
{
333+
return [];
334+
}
330335
}
331336

332337
class TestKernel implements HttpKernelInterface

src/Symfony/Component/HttpKernel/Tests/Logger.php

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
namespace Symfony\Component\HttpKernel\Tests;
1313

14-
use Psr\Log\LoggerInterface;
14+
use Psr\Log\AbstractLogger;
1515

16-
class Logger implements LoggerInterface
16+
class Logger extends AbstractLogger
1717
{
1818
protected array $logs;
1919

@@ -22,9 +22,9 @@ public function __construct()
2222
$this->clear();
2323
}
2424

25-
public function getLogs($level = false): array
25+
public function getLogsForLevel(string $level): array
2626
{
27-
return false === $level ? $this->logs : $this->logs[$level];
27+
return $this->logs[$level];
2828
}
2929

3030
public function clear(): void
@@ -45,44 +45,4 @@ public function log($level, $message, array $context = []): void
4545
{
4646
$this->logs[$level][] = $message;
4747
}
48-
49-
public function emergency($message, array $context = []): void
50-
{
51-
$this->log('emergency', $message, $context);
52-
}
53-
54-
public function alert($message, array $context = []): void
55-
{
56-
$this->log('alert', $message, $context);
57-
}
58-
59-
public function critical($message, array $context = []): void
60-
{
61-
$this->log('critical', $message, $context);
62-
}
63-
64-
public function error($message, array $context = []): void
65-
{
66-
$this->log('error', $message, $context);
67-
}
68-
69-
public function warning($message, array $context = []): void
70-
{
71-
$this->log('warning', $message, $context);
72-
}
73-
74-
public function notice($message, array $context = []): void
75-
{
76-
$this->log('notice', $message, $context);
77-
}
78-
79-
public function info($message, array $context = []): void
80-
{
81-
$this->log('info', $message, $context);
82-
}
83-
84-
public function debug($message, array $context = []): void
85-
{
86-
$this->log('debug', $message, $context);
87-
}
8848
}

0 commit comments

Comments
 (0)