4
4
5
5
namespace App \Tests \Functional ;
6
6
7
+ use App \Log \Log ;
8
+ use Psr \Log \LoggerInterface ;
9
+ use ReflectionClass ;
10
+ use RuntimeException ;
7
11
use Symfony \Bundle \FrameworkBundle \Test \KernelTestCase ;
8
12
use Symfony \Component \Filesystem \Filesystem ;
9
13
@@ -31,7 +35,7 @@ protected function setUp(): void
31
35
}
32
36
}
33
37
34
- public function testLog (): void
38
+ public function testLogFile (): void
35
39
{
36
40
$ logger = self ::getContainer ()->get ('monolog.logger ' );
37
41
@@ -50,6 +54,84 @@ public function testLog(): void
50
54
}
51
55
}
52
56
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
+
53
135
protected function tearDown (): void
54
136
{
55
137
parent ::tearDown ();
0 commit comments