|
Line 0
Link Here
|
| 0 |
- |
1 |
use Modern::Perl; |
|
|
2 |
use Test::More tests => 10; |
| 3 |
use Test::Warn; |
| 4 |
|
| 5 |
# Module under test |
| 6 |
use t::lib::Mocks::Logger; |
| 7 |
use Koha::Logger; |
| 8 |
|
| 9 |
# Test instantiation |
| 10 |
my $logger; |
| 11 |
subtest 'Constructor tests' => sub { |
| 12 |
plan tests => 2; |
| 13 |
$logger = t::lib::Mocks::Logger->new(); |
| 14 |
isa_ok( $logger, 't::lib::Mocks::Logger', 'Constructor returns expected object' ); |
| 15 |
can_ok( |
| 16 |
$logger, qw(new diag debug_is error_is fatal_is info_is trace_is warn_is |
| 17 |
debug_like error_like fatal_like info_like trace_like warn_like |
| 18 |
count clear) |
| 19 |
); |
| 20 |
}; |
| 21 |
|
| 22 |
# Get a reference to the mocked logger |
| 23 |
my $mocked_logger = Koha::Logger->get(); |
| 24 |
|
| 25 |
# Test logging at various log levels and verify that the messages are captured |
| 26 |
subtest 'Basic logging tests' => sub { |
| 27 |
plan tests => 1; |
| 28 |
|
| 29 |
$logger->clear(); |
| 30 |
|
| 31 |
$mocked_logger->debug('Debug message'); |
| 32 |
$mocked_logger->info('Info message'); |
| 33 |
$mocked_logger->warn('Warning message'); |
| 34 |
$mocked_logger->error('Error message'); |
| 35 |
$mocked_logger->fatal('Fatal message'); |
| 36 |
$mocked_logger->trace('Trace message'); |
| 37 |
|
| 38 |
is( $logger->count, 6, 'All 6 log messages were captured' ); |
| 39 |
}; |
| 40 |
|
| 41 |
# Test exact message matching methods |
| 42 |
subtest 'Exact message matching' => sub { |
| 43 |
plan tests => 6; |
| 44 |
|
| 45 |
$logger->clear(); |
| 46 |
|
| 47 |
$mocked_logger->debug('Debug message'); |
| 48 |
$mocked_logger->info('Info message'); |
| 49 |
$mocked_logger->warn('Warning message'); |
| 50 |
$mocked_logger->error('Error message'); |
| 51 |
$mocked_logger->fatal('Fatal message'); |
| 52 |
$mocked_logger->trace('Trace message'); |
| 53 |
|
| 54 |
$logger->debug_is( 'Debug message', 'Debug message matched exactly' ); |
| 55 |
$logger->info_is( 'Info message', 'Info message matched exactly' ); |
| 56 |
$logger->warn_is( 'Warning message', 'Warning message matched exactly' ); |
| 57 |
$logger->error_is( 'Error message', 'Error message matched exactly' ); |
| 58 |
$logger->fatal_is( 'Fatal message', 'Fatal message matched exactly' ); |
| 59 |
$logger->trace_is( 'Trace message', 'Trace message matched exactly' ); |
| 60 |
}; |
| 61 |
|
| 62 |
# Test regex message matching methods |
| 63 |
subtest 'Regex message matching' => sub { |
| 64 |
plan tests => 6; |
| 65 |
|
| 66 |
$logger->clear(); |
| 67 |
|
| 68 |
$mocked_logger->debug('Debug message 123'); |
| 69 |
$mocked_logger->info('Info message 456'); |
| 70 |
$mocked_logger->warn('Warning message 789'); |
| 71 |
$mocked_logger->error('Error message abc'); |
| 72 |
$mocked_logger->fatal('Fatal message def'); |
| 73 |
$mocked_logger->trace('Trace message ghi'); |
| 74 |
|
| 75 |
$logger->debug_like( qr/Debug.*123/, 'Debug message matched regex' ); |
| 76 |
$logger->info_like( qr/Info.*456/, 'Info message matched regex' ); |
| 77 |
$logger->warn_like( qr/Warning.*789/, 'Warning message matched regex' ); |
| 78 |
$logger->error_like( qr/Error.*abc/, 'Error message matched regex' ); |
| 79 |
$logger->fatal_like( qr/Fatal.*def/, 'Fatal message matched regex' ); |
| 80 |
$logger->trace_like( qr/Trace.*ghi/, 'Trace message matched regex' ); |
| 81 |
}; |
| 82 |
|
| 83 |
# Test count method |
| 84 |
subtest 'Count method tests' => sub { |
| 85 |
plan tests => 7; |
| 86 |
|
| 87 |
$logger->clear(); |
| 88 |
|
| 89 |
is( $logger->count, 0, 'Count is 0 after clear' ); |
| 90 |
|
| 91 |
$mocked_logger->debug('Debug message'); |
| 92 |
is( $logger->count, 1, 'Count is 1 after one message' ); |
| 93 |
is( $logger->count('debug'), 1, 'Debug count is 1' ); |
| 94 |
is( $logger->count('error'), 0, 'Error count is 0' ); |
| 95 |
|
| 96 |
$mocked_logger->error('Error message'); |
| 97 |
is( $logger->count, 2, 'Count is 2 after two messages' ); |
| 98 |
is( $logger->count('debug'), 1, 'Debug count is still 1' ); |
| 99 |
is( $logger->count('error'), 1, 'Error count is now 1' ); |
| 100 |
}; |
| 101 |
|
| 102 |
# Test clear method |
| 103 |
subtest 'Clear method tests' => sub { |
| 104 |
plan tests => 3; |
| 105 |
|
| 106 |
$logger->clear(); |
| 107 |
$mocked_logger->debug('Debug message'); |
| 108 |
$mocked_logger->error('Error message'); |
| 109 |
|
| 110 |
is( $logger->count, 2, 'Count is 2 before clear' ); |
| 111 |
|
| 112 |
$logger->clear('debug'); |
| 113 |
is( $logger->count('debug'), 0, 'Debug count is 0 after specific clear' ); |
| 114 |
is( $logger->count('error'), 1, 'Error count is still 1 after specific clear' ); |
| 115 |
}; |
| 116 |
|
| 117 |
# Test method chaining |
| 118 |
subtest 'Method chaining tests' => sub { |
| 119 |
plan tests => 4; |
| 120 |
|
| 121 |
$logger->clear(); |
| 122 |
|
| 123 |
$mocked_logger->debug('Debug message 1'); |
| 124 |
$mocked_logger->debug('Debug message 2'); |
| 125 |
$mocked_logger->error('Error message'); |
| 126 |
|
| 127 |
my $result = |
| 128 |
$logger->debug_is( 'Debug message 1', 'First debug message matched' ) |
| 129 |
->debug_is( 'Debug message 2', 'Second debug message matched' ) |
| 130 |
->error_is( 'Error message', 'Error message matched' ); |
| 131 |
|
| 132 |
isa_ok( $result, 't::lib::Mocks::Logger', 'Method chaining returns the logger object' ); |
| 133 |
}; |
| 134 |
|
| 135 |
# Test diag method (output capture is complex, just verify it runs) |
| 136 |
subtest 'Diag method test' => sub { |
| 137 |
plan tests => 1; |
| 138 |
|
| 139 |
$logger->clear(); |
| 140 |
$mocked_logger->debug('Debug message'); |
| 141 |
|
| 142 |
# Just make sure it doesn't throw an exception |
| 143 |
eval { $logger->diag(); }; |
| 144 |
is( $@, '', 'diag() method executed without errors' ); |
| 145 |
}; |
| 146 |
|
| 147 |
# Test handling of empty log buffers |
| 148 |
subtest 'Empty log buffer handling' => sub { |
| 149 |
plan tests => 6; |
| 150 |
|
| 151 |
$logger->clear(); |
| 152 |
|
| 153 |
$logger->debug_is( '', 'Empty string returned when no debug messages' ); |
| 154 |
$logger->info_is( '', 'Empty string returned when no info messages' ); |
| 155 |
$logger->warn_is( '', 'Empty string returned when no warn messages' ); |
| 156 |
$logger->error_is( '', 'Empty string returned when no error messages' ); |
| 157 |
$logger->fatal_is( '', 'Empty string returned when no fatal messages' ); |
| 158 |
$logger->trace_is( '', 'Empty string returned when no trace messages' ); |
| 159 |
}; |
| 160 |
|
| 161 |
# Test multiple messages at the same level |
| 162 |
subtest 'Multiple messages at same level' => sub { |
| 163 |
plan tests => 5; |
| 164 |
|
| 165 |
$logger->clear(); |
| 166 |
|
| 167 |
$mocked_logger->debug('Debug message 1'); |
| 168 |
$mocked_logger->debug('Debug message 2'); |
| 169 |
$mocked_logger->debug('Debug message 3'); |
| 170 |
|
| 171 |
is( $logger->count('debug'), 3, 'Three debug messages recorded' ); |
| 172 |
$logger->debug_is( 'Debug message 1', 'First debug message matched' ); |
| 173 |
$logger->debug_is( 'Debug message 2', 'Second debug message matched' ); |
| 174 |
$logger->debug_is( 'Debug message 3', 'Third debug message matched' ); |
| 175 |
is( $logger->count('debug'), 0, 'All debug messages consumed' ); |
| 176 |
}; |