55namespace TimDev \StackLogger \Test ;
66
77use PHPUnit \Framework \TestCase ;
8- use TimDev \StackLogger \StackLogger ;
98use TimDev \StackLogger \Test \Support \TestStackLogger ;
109
1110/**
1716 */
1817abstract class BaseTestCase extends TestCase
1918{
20- abstract protected function makeTestSubject (): TestStackLogger ;
21-
2219 private TestStackLogger $ log ;
2320
2421 #[\Override]
25- public function setUp (): void
22+ protected function setUp (): void
2623 {
2724 $ this ->log = $ this ->makeTestSubject ();
2825 }
@@ -50,10 +47,7 @@ public function testAddsContext(): void
5047
5148 $ this ->log
5249 ->addContext (['even more ' => 'context ' ])
53- ->warning (
54- 'This message should get four context elements. ' ,
55- ['foo ' => 'bar ' ]
56- );
50+ ->warning ('This message should get four context elements. ' , ['foo ' => 'bar ' ]);
5751 self ::assertEquals (4 , $ this ->log ->contextCountAt (2 ));
5852
5953 $ this ->log ->debug ('Back to three! ' );
@@ -70,7 +64,7 @@ public function testCreateChildWithContext(): void
7064
7165 public function testAccumulatesContext (): void
7266 {
73- $ log = $ this ->log ->withContext (['initial ' => 'context ' ]);
67+ $ log = $ this ->log ->withContext (['initial ' => 'context ' ]);
7468 $ child = $ log ->withContext (['more ' => 'context ' ]);
7569
7670 // $child should have two context items.
@@ -87,10 +81,7 @@ public function testAccumulatesContext(): void
8781 $ log ->info ('I should have one context item (on my second record) ' );
8882
8983 self ::assertCount (1 , $ log ->contextAt (1 ));
90- self ::assertEquals (
91- ['initial ' ],
92- $ log ->contextKeysAt (1 )
93- );
84+ self ::assertEquals (['initial ' ], $ log ->contextKeysAt (1 ));
9485 }
9586
9687 public function testMergesContext (): void
@@ -101,29 +92,23 @@ public function testMergesContext(): void
10192 $ log
10293 ->withContext (['a ' => 'Allison ' , 'b ' => 'Bob ' ])
10394 ->emergency ('Allison and Bruno ' , ['b ' => 'Bruno ' ]);
104- self ::assertEquals (
105- ['Allison ' , 'Bruno ' ],
106- $ this ->log ->contextValuesAt (0 )
107- );
95+ self ::assertEquals (['Allison ' , 'Bruno ' ], $ this ->log ->contextValuesAt (0 ));
10896
10997 // But original logger should still have (only) Alice.
11098 $ log ->critical ('Alice alone. ' );
111- self ::assertEquals (
112- ['a ' => 'Alice ' ],
113- $ this ->log ->contextAt (1 )
114- );
99+ self ::assertEquals (['a ' => 'Alice ' ], $ this ->log ->contextAt (1 ));
115100 }
116101
117102 public function testInvokesCallables (): void
118103 {
119104 $ logger = $ this ->log ;
120- $ child = $ logger ->withContext (
105+ $ child = $ logger ->withContext (
121106 [
122107 // A callable context element that returns the number of
123108 // elements in the record's context.
124109 'counter ' => function (array $ ctx ) {
125110 return count ($ ctx );
126- }
111+ },
127112 ]
128113 );
129114 $ child ->notice ('Only one context element, the result of the callable. ' );
@@ -136,8 +121,8 @@ public function testInvokesCallables(): void
136121 $ child ->contextAt (1 )['counter ' ]
137122 );
138123
139- $ start = microtime (true );
140- $ child2 = $ logger ->withContext (['elapsed_micros ' => fn () => 1000000 * (microtime (true ) - $ start )]);
124+ $ start = microtime (true );
125+ $ child2 = $ logger ->withContext (['elapsed_micros ' => fn () => 1000000 * (microtime (true ) - $ start )]);
141126 usleep (1000 );
142127 $ child2 ->alert ('At least 1000 μ-sec have passed. ' );
143128 self ::assertGreaterThan (1000 , $ child2 ->contextAt (2 )['elapsed_micros ' ]);
@@ -149,15 +134,16 @@ public function testInvokesCallables(): void
149134 */
150135 public function testLongChain (): void
151136 {
152- $ logger = $ this ->log ;
137+ $ logger = $ this ->log ;
153138 $ numLoggers = 20 ;
154139 foreach (range (1 , $ numLoggers ) as $ i ) {
155140 $ logger = $ logger ->withContext (["gen {$ i }" => $ i ]);
156141 }
157- $ logger ->withContext ([" count " => fn (array $ ctx ) => count ($ ctx )]);
158- $ logger ->error (" I come from a long lineage " , ['final ' => 'I should be the 21st context element ' ]);
142+ $ logger ->withContext ([' count ' => fn (array $ ctx ) => count ($ ctx )]);
143+ $ logger ->error (' I come from a long lineage ' , ['final ' => 'I should be the 21st context element ' ]);
159144 self ::assertIsIterable ($ logger ->recordAt (0 )['context ' ]);
160145 self ::assertCount ($ numLoggers + 1 , $ logger ->recordAt (0 )['context ' ]);
161146 }
162147
148+ abstract protected function makeTestSubject (): TestStackLogger ;
163149}
0 commit comments