Skip to content

Commit f8a3020

Browse files
authored
Add tests to Log class (#656)
1 parent 2c15302 commit f8a3020

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

tests/Functional/LogTest.php

+83-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
namespace App\Tests\Functional;
66

7+
use App\Log\Log;
8+
use Psr\Log\LoggerInterface;
9+
use ReflectionClass;
10+
use RuntimeException;
711
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
812
use Symfony\Component\Filesystem\Filesystem;
913

@@ -31,7 +35,7 @@ protected function setUp(): void
3135
}
3236
}
3337

34-
public function testLog(): void
38+
public function testLogFile(): void
3539
{
3640
$logger = self::getContainer()->get('monolog.logger');
3741

@@ -50,6 +54,84 @@ public function testLog(): void
5054
}
5155
}
5256

57+
public function testInitLogger(): void
58+
{
59+
$logger = $this->createMock(LoggerInterface::class);
60+
61+
Log::init($logger);
62+
63+
$this->assertInstanceOf(LoggerInterface::class, $this->getPrivateStaticProperty(Log::class, 'logger'));
64+
}
65+
66+
public function testGetLoggerThrowsExceptionWhenNotInitialized(): void
67+
{
68+
$this->resetLogger();
69+
$this->expectException(RuntimeException::class);
70+
$this->expectExceptionMessage('Logger has not been initialized.');
71+
72+
$this->invokePrivateStaticMethod(Log::class, 'getLogger');
73+
}
74+
75+
public function testCriticalMethodCallsLogger(): void
76+
{
77+
$this->assertLogMethodCalled('critical');
78+
}
79+
80+
public function testDebugMethodCallsLogger(): void
81+
{
82+
$this->assertLogMethodCalled('debug');
83+
}
84+
85+
public function testErrorMethodCallsLogger(): void
86+
{
87+
$this->assertLogMethodCalled('error');
88+
}
89+
90+
public function testInfoMethodCallsLogger(): void
91+
{
92+
$this->assertLogMethodCalled('info');
93+
}
94+
95+
private function assertLogMethodCalled(string $method): void
96+
{
97+
$logger = $this->createMock(LoggerInterface::class);
98+
$logger->expects($this->once())
99+
->method($method)
100+
->with(
101+
$this->equalTo('Test message'),
102+
$this->equalTo(['key' => 'value'])
103+
);
104+
105+
Log::init($logger);
106+
Log::$method('Test message', ['key' => 'value']);
107+
}
108+
109+
private function getPrivateStaticProperty(string $class, string $property): mixed
110+
{
111+
$reflection = new ReflectionClass($class);
112+
$prop = $reflection->getProperty($property);
113+
$prop->setAccessible(true);
114+
115+
return $prop->getValue();
116+
}
117+
118+
private function invokePrivateStaticMethod(string $class, string $method): mixed
119+
{
120+
$reflection = new ReflectionClass($class);
121+
$method = $reflection->getMethod($method);
122+
$method->setAccessible(true);
123+
124+
return $method->invoke(null);
125+
}
126+
127+
private function resetLogger(): void
128+
{
129+
$reflection = new ReflectionClass(Log::class);
130+
$prop = $reflection->getProperty('logger');
131+
$prop->setAccessible(true);
132+
$prop->setValue(null);
133+
}
134+
53135
protected function tearDown(): void
54136
{
55137
parent::tearDown();

0 commit comments

Comments
 (0)