File Coverage

lib/Test/Mojo/Role/Log.pm
Criterion Covered Total %
statement 102 102 100.0
branch 6 6 100.0
condition 4 6 66.6
subroutine 16 16 100.0
pod 7 14 50.0
total 135 144 93.7


line stmt bran cond sub pod time code
1             package Test::Mojo::Role::Log;
2 1     1   809381 use Mojo::Base -role, -signatures;
  1         2  
  1         9  
3             our $VERSION = '0.2.3';
4              
5             around 'new' => sub {
6             my $orig = shift;
7             my $self = $orig->(@_);
8             my $log = $self->app->log;
9             if ( $log->level eq 'fatal' ){
10             $log->unsubscribe(
11             message => $log->subscribers('message')->[0]
12             );
13             if ($log->can('trace')){
14             $log->level('trace');
15             }
16             else {
17             $log->level('debug');
18             }
19             }
20             $log->on(message => sub {
21             my $log = shift;
22             push @{$self->logCache}, \@_;
23             });
24             return $self;
25             };
26              
27              
28             around '_build_ok' => sub {
29             my $orig = shift;
30             my $self = $_[0];
31             $self->logCache([]);
32             return $orig->(@_);
33             };
34              
35             has logCache => sub {
36             [];
37             };
38              
39 18     18   34 sub _log_test ($self,$rx,$level,$like,$desc=undef) {
  18         69  
  18         26  
  18         36  
  18         33  
  18         30  
  18         30  
40 18 100 33     172 $desc //= "log ".
    100          
41             (defined $level ? "level=$level ":"").
42             ($like eq 'like' ? "" : "un")."like $rx";
43 18         34 my $logs = '';
44 18         27 for my $entry (@{$self->logCache}){
  18         55  
45 180         517 my ($l,@msg) = @$entry;
46 180 100 100     657 if (not defined $level or $l eq $level){
47 66         198 $logs .= join("\n",@msg)."\n";
48             }
49             }
50 18         85 return $self->test($like,$logs,$rx,Test::Mojo::_desc($desc));
51             }
52              
53 3     3 1 5208 sub log_like ($self,$rx,$desc=undef) {
  3         8  
  3         5  
  3         19  
  3         6  
54 3         12 return $self->_log_test($rx,undef,'like',$desc);
55             }
56              
57 1     1 0 374 sub log_unlike ($self,$rx,$desc=undef) {
  1         3  
  1         3  
  1         2  
  1         3  
58 1         3 return $self->_log_test($rx,undef,'unlike',$desc);
59             }
60              
61 2     2 1 775 sub log_trace_like ($self,$rx,$desc=undef) {
  2         5  
  2         4  
  2         5  
  2         3  
62 2         8 return $self->_log_test($rx,'trace','like',$desc);
63             }
64              
65 1     1 1 377 sub log_debug_like ($self,$rx,$desc=undef) {
  1         4  
  1         3  
  1         2  
  1         2  
66 1         5 return $self->_log_test($rx,'debug','like',$desc);
67             }
68              
69 1     1 1 396 sub log_info_like ($self,$rx,$desc=undef) {
  1         4  
  1         2  
  1         2  
  1         2  
70 1         4 return $self->_log_test($rx,'info','like',$desc);
71             }
72              
73 1     1 1 397 sub log_warn_like ($self,$rx,$desc=undef) {
  1         3  
  1         2  
  1         3  
  1         9  
74 1         7 return $self->_log_test($rx,'warn','like',$desc);
75             }
76              
77 1     1 1 418 sub log_error_like ($self,$rx,$desc=undef) {
  1         2  
  1         3  
  1         3  
  1         3  
78 1         5 return $self->_log_test($rx,'error','like',$desc);
79             }
80              
81 1     1 1 422 sub log_fatal_like ($self,$rx,$desc=undef) {
  1         2  
  1         3  
  1         3  
  1         3  
82 1         4 return $self->_log_test($rx,'fatal','like',$desc);
83             }
84              
85 1     1 0 407 sub log_trace_unlike ($self,$rx,$desc=undef) {
  1         3  
  1         2  
  1         3  
  1         3  
86 1         4 return $self->_log_test($rx,'trace','unlike',$desc);
87             }
88              
89 2     2 0 775 sub log_debug_unlike ($self,$rx,$desc=undef) {
  2         6  
  2         5  
  2         5  
  2         4  
90 2         6 return $self->_log_test($rx,'debug','unlike',$desc);
91             }
92              
93 1     1 0 403 sub log_info_unlike ($self,$rx,$desc=undef) {
  1         3  
  1         19  
  1         3  
  1         4  
94 1         5 return $self->_log_test($rx,'info','unlike',$desc);
95             }
96              
97 1     1 0 422 sub log_warn_unlike ($self,$rx,$desc=undef) {
  1         3  
  1         2  
  1         3  
  1         2  
98 1         5 return $self->_log_test($rx,'warn','unlike',$desc);
99             }
100              
101 1     1 0 370 sub log_error_unlike ($self,$rx,$desc=undef) {
  1         4  
  1         2  
  1         3  
  1         2  
102 1         4 return $self->_log_test($rx,'error','unlike',$desc);
103             }
104              
105 1     1 0 367 sub log_fatal_unlike ($self,$rx,$desc=undef) {
  1         3  
  1         3  
  1         2  
  1         2  
106 1         4 return $self->_log_test($rx,'fatal','unlike',$desc);
107             }
108              
109              
110             1;
111              
112             =encoding utf8
113              
114             =head1 NAME
115              
116             Test::Mojo::Role::Log - test mojo log messages
117              
118             =head1 SYNOPSIS
119              
120             use Test::Mojo;
121              
122             my $t = Test::Mojo->with_roles('+Log')->new('MyApp');
123              
124             $t->get_ok('/gugus')
125             ->log_like(qr{GET "/gugus"})
126             ->log_debug_like(qr{GET "/gugus"})
127             ->log_info_unlike(qr{GET "/gugus"})
128             ->log_debug_like(qr{200 OK.+s.+/s})
129              
130             done_testing();
131              
132             =head1 DESCRIPTION
133              
134             The L role enhances L with additional methods to check log output.
135              
136             =head1 ATTRIBUTES
137              
138             =head2 logCache
139              
140             Points to an array with all the log messages issued since the last request.
141              
142             =head1 METHODS
143              
144             The role L adds the following new methods to L ones.
145              
146             =head2 log_like($rx,$desc)
147              
148             $t->get_ok('/hello')
149             ->log_like(undef,qr{/hello not found},"Request got logged")
150              
151             Check if the given log message has been issued. All the log messages issued since the start of the current request will get checked.
152             If $logLevel is set to undef the logLevel does not get checked.
153              
154             =head2 log_trace_like($rx,$desc) (if mojo supports it)
155              
156             Find a trace level log message matching the given $rx.
157              
158             =head2 log_debug_like($rx,$desc)
159              
160             Find a debug level log message matching the given $rx.
161              
162             =head2 log_info_like($rx,$desc)
163              
164             Find a info level log message matching the given $rx.
165              
166             =head2 log_warn_like($rx,$desc)
167              
168             Find a warn level log message matching the given $rx.
169              
170             =head2 log_error_like($rx,$desc)
171              
172             Find a error level log message matching the given $rx.
173              
174             =head2 log_fatal_like($rx,$desc)
175              
176             Find a fatal level log message matching the given $rx.
177              
178             =head2 *_unlike
179              
180             For each of the methods above there is ac coresponding
181              
182             =head1 AUTHOR
183              
184             Tobias Oetiker Etobi@oetiker.chE
185              
186             =head1 COPYRIGHT
187              
188             Copyright 2020, OETIKER+PARTNER AG
189              
190             =head1 LICENSE
191              
192             Perl Artistic License