@@ -, +, @@ t::lib::Mocks::mock_logger({ warn => 1 }); $ git grep 'Koha::Logger->get->debug' $ git grep 'Koha::Logger->get->warn' imports: $ kshell k$ prove t/db_dependent/Search.t --- t/lib/Mocks.pm | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) --- a/t/lib/Mocks.pm +++ a/t/lib/Mocks.pm @@ -1,9 +1,25 @@ package t::lib::Mocks; +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + use Modern::Perl; use C4::Context; use Test::MockModule; +use Test::MockObject; my %configs; sub mock_config { @@ -74,4 +90,43 @@ sub mock_userenv { ); } +=head3 mock_logger + + my $mocked_logger = t::lib::Mocks::mock_logger({ [ warn => 0|1 ] }); + +Mocks the Koha::Logger for testing purposes. The mocked subs (log levels) +return the passed string, in case we want to test the debugging string contents. + +A I parameter can be passed so the call to the logging method warns instead +of just returning (useful for writing tests). + +=cut + +sub mock_logger { + my ($params) = @_; + + my $warn = $params->{warn}; + + my $mocked_logger_class = Test::MockModule->new("Koha::Logger"); + $mocked_logger_class->mock( 'get', sub + { + my $mocked_logger = Test::MockObject->new(); + $mocked_logger->mock( 'debug', sub { + my ( $self, $string ) = @_; + warn $string + if $warn; + return $string + }); + $mocked_logger->mock( 'warn', sub { + my ( $self, $string ) = @_; + warn $string + if $warn; + return $string; + }); + } + ); + + return $mocked_logger_class; +} + 1; --