1010 *
1111 * It records all records and gives you access to them for verification.
1212 *
13- * @psalm-type log_record_array array{level: string, message: string|\Stringable, context: mixed[]}
14- * @psalm-type has_record_array array{level?: string , message?: string|\Stringable, context?: array<array-key, mixed>}
13+ * @psalm-type log_record_array array{level: string|int , message: string|\Stringable, context: mixed[]}
14+ * @psalm-type has_record_array array{level?: mixed , message?: string|\Stringable, context?: array<array-key, mixed>}
1515 */
1616class TestLogger implements LoggerInterface
1717{
@@ -38,6 +38,8 @@ public function log($level, string|\Stringable $message, array $context = []): v
3838 $ message = $ this ->interpolate ($ message , $ context );
3939 }
4040
41+ $ level = self ::normalizeLevel ($ level );
42+
4143 $ record = [
4244 'level ' => $ level ,
4345 'message ' => $ message ,
@@ -49,17 +51,17 @@ public function log($level, string|\Stringable $message, array $context = []): v
4951 }
5052
5153 /**
52- * @param string $level
54+ * @param string|int|\Stringable|\UnitEnum $level
5355 * @return bool
5456 */
5557 public function hasRecords ($ level )
5658 {
57- return isset ($ this ->recordsByLevel [$ level ]);
59+ return isset ($ this ->recordsByLevel [self :: normalizeLevel ( $ level) ]);
5860 }
5961
6062 /**
6163 * @param has_record_array|string $record
62- * @param string $level
64+ * @param string|int|\Stringable|\UnitEnum $level
6365 * @return bool
6466 */
6567 public function hasRecord ($ record , $ level )
@@ -76,36 +78,38 @@ public function hasRecord($record, $level)
7678 return false ;
7779 }
7880 return true ;
79- }, $ level );
81+ }, self :: normalizeLevel ( $ level) );
8082 }
8183
8284 /**
8385 * @param string $message
84- * @param string $level
86+ * @param string|int|\Stringable|\UnitEnum $level
8587 * @return bool
8688 */
8789 public function hasRecordThatContains ($ message , $ level )
8890 {
89- return $ this ->hasRecordThatPasses (fn ($ rec ) => str_contains ($ rec ['message ' ], $ message ), $ level );
91+ return $ this ->hasRecordThatPasses (fn ($ rec ) => str_contains ($ rec ['message ' ], $ message ), self :: normalizeLevel ( $ level) );
9092 }
9193
9294 /**
9395 * @param string $regex
94- * @param string $level
96+ * @param string|int|\Stringable|\UnitEnum $level
9597 * @return bool
9698 */
9799 public function hasRecordThatMatches ($ regex , $ level )
98100 {
99- return $ this ->hasRecordThatPasses (fn ($ rec ) => preg_match ($ regex , $ rec ['message ' ]) > 0 , $ level );
101+ return $ this ->hasRecordThatPasses (fn ($ rec ) => preg_match ($ regex , $ rec ['message ' ]) > 0 , self :: normalizeLevel ( $ level) );
100102 }
101103
102104 /**
103105 * @param callable $predicate
104- * @param string $level
106+ * @param string|int|\Stringable|\UnitEnum $level
105107 * @return bool
106108 */
107109 public function hasRecordThatPasses (callable $ predicate , $ level )
108110 {
111+ $ level = self ::normalizeLevel ($ level );
112+
109113 if (!isset ($ this ->recordsByLevel [$ level ])) {
110114 return false ;
111115 }
@@ -408,7 +412,7 @@ public function reset()
408412 * @param array $context
409413 * @return string
410414 */
411- private function interpolate ($ message , array $ context = []) : string
415+ private function interpolate ($ message , array $ context = []): string
412416 {
413417 // build a replacement array with braces around the context keys
414418 $ replace = array ();
@@ -423,4 +427,23 @@ private function interpolate($message, array $context = []) : string
423427 return strtr ($ message , $ replace );
424428 }
425429
430+ /**
431+ * @param mixed $level
432+ */
433+ private static function normalizeLevel ($ level ): int |string
434+ {
435+ if ($ level instanceof \UnitEnum) {
436+ $ level = $ level ->value ;
437+ }
438+
439+ if ($ level instanceof \Stringable) {
440+ $ level = (string ) $ level ;
441+ }
442+
443+ if (is_string ($ level ) || is_int ($ level )) {
444+ return $ level ;
445+ }
446+
447+ throw new \InvalidArgumentException ('The given level of type " ' .gettype ($ level ).'" could not be normalized to a string or int. ' );
448+ }
426449}
0 commit comments