@@ -54,19 +54,32 @@ private function assertIsType( $type, $value, $message = '' ) {
54
54
}
55
55
}
56
56
57
+ /**
58
+ * Reset the logger singleton to ensure fresh state per test.
59
+ */
60
+ private function reset_logger_singleton () {
61
+ if ( class_exists ( 'Feedzy_Rss_Feeds_Log ' ) ) {
62
+ $ ref = new ReflectionClass ( 'Feedzy_Rss_Feeds_Log ' );
63
+ if ( $ ref ->hasProperty ( 'instance ' ) ) {
64
+ $ prop = $ ref ->getProperty ( 'instance ' );
65
+ $ prop ->setAccessible ( true );
66
+ $ prop ->setValue ( null , null );
67
+ }
68
+ }
69
+ }
70
+
57
71
/**
58
72
* Set up test environment.
59
73
*/
60
74
public function setUp (): void {
61
75
parent ::setUp ();
62
76
63
- // Create a fresh logger instance for each test
77
+ // Ensure fresh singleton for each test.
78
+ $ this ->reset_logger_singleton ();
64
79
$ this ->logger = Feedzy_Rss_Feeds_Log::get_instance ();
65
80
66
- // Set up test log directory
67
- $ upload_dir = wp_upload_dir ();
68
- $ test_log_dir = $ upload_dir ['basedir ' ] . '/feedzy-logs ' ;
69
- $ this ->test_log_path = $ test_log_dir . '/feedzy.jsonl ' ;
81
+ // Use the class API to determine the log path.
82
+ $ this ->test_log_path = $ this ->logger ->get_log_file_path ();
70
83
71
84
// Clean up any existing log files
72
85
if ( file_exists ( $ this ->test_log_path ) ) {
@@ -92,6 +105,9 @@ public function tearDown(): void {
92
105
// Reset options
93
106
delete_option ( Feedzy_Rss_Feeds_Log::STATS_OPTION_KEY );
94
107
delete_option ( 'feedzy-settings ' );
108
+
109
+ // Reset the singleton to avoid side effects between tests.
110
+ $ this ->reset_logger_singleton ();
95
111
96
112
parent ::tearDown ();
97
113
}
@@ -219,6 +235,10 @@ public function test_context_setting() {
219
235
$ logs = $ this ->logger ->get_recent_logs ( 1 );
220
236
$ this ->assertEquals ( 123 , $ logs [0 ]['context ' ]['import_id ' ] );
221
237
$ this ->assertEquals ( 'https://example.com/feed.xml ' , $ logs [0 ]['context ' ]['feed_url ' ] );
238
+
239
+ // New: origin should be auto-injected.
240
+ $ this ->assertArrayHasKey ( 'function ' , $ logs [0 ]['context ' ] );
241
+ $ this ->assertArrayHasKey ( 'line ' , $ logs [0 ]['context ' ] );
222
242
}
223
243
224
244
/**
@@ -237,8 +257,8 @@ public function test_log_entry_structure() {
237
257
$ this ->assertArrayHasKey ( 'message ' , $ log );
238
258
$ this ->assertArrayHasKey ( 'context ' , $ log );
239
259
240
- // Validate timestamp format (ISO 8601)
241
- $ this ->assertMatchesRegularExpression ( '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2}$/ ' , $ log ['timestamp ' ] );
260
+ // Validate timestamp format (ISO 8601, allow Z or explicit offset )
261
+ $ this ->assertMatchesRegularExpression ( '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?: \+\d{2}:\d{2}|Z) $/ ' , $ log ['timestamp ' ] );
242
262
}
243
263
244
264
/**
@@ -376,8 +396,9 @@ public function test_email_configuration() {
376
396
);
377
397
update_option ( 'feedzy-settings ' , $ settings );
378
398
379
- // Recreate logger to pick up new settings
380
- $ this ->logger = new Feedzy_Rss_Feeds_Log ();
399
+ // Recreate logger singleton to pick up new settings
400
+ $ this ->reset_logger_singleton ();
401
+ $ this ->logger = Feedzy_Rss_Feeds_Log::get_instance ();
381
402
382
403
$ this ->assertTrue ( $ this ->logger ->can_send_email () );
383
404
$ this ->
assertEquals (
'[email protected] ' ,
$ this ->
logger ->
get_email_address () );
@@ -428,7 +449,8 @@ public function test_settings_initialization() {
428
449
update_option ( 'feedzy-settings ' , $ settings );
429
450
430
451
// Create new logger instance to test initialization
431
- $ logger = new Feedzy_Rss_Feeds_Log ();
452
+ $ this ->reset_logger_singleton ();
453
+ $ logger = Feedzy_Rss_Feeds_Log::get_instance ();
432
454
433
455
$ this ->assertEquals ( Feedzy_Rss_Feeds_Log::DEBUG , $ logger ->level_threshold );
434
456
$ this ->assertTrue ( $ logger ->can_send_email );
@@ -447,7 +469,8 @@ public function test_invalid_settings_values() {
447
469
);
448
470
update_option ( 'feedzy-settings ' , $ settings );
449
471
450
- $ logger = new Feedzy_Rss_Feeds_Log ();
472
+ $ this ->reset_logger_singleton ();
473
+ $ logger = Feedzy_Rss_Feeds_Log::get_instance ();
451
474
452
475
// Should fallback to default ERROR level
453
476
$ this ->assertEquals ( Feedzy_Rss_Feeds_Log::ERROR , $ logger ->level_threshold );
0 commit comments