Bugzilla – Attachment 122306 Details for
Bug 28615
Add a simple way to mock Koha::Logger
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 28615: Add a simple way to mock Koha::Logger
Bug-28615-Add-a-simple-way-to-mock-KohaLogger.patch (text/plain), 3.61 KB, created by
Tomás Cohen Arazi (tcohen)
on 2021-06-22 20:58:10 UTC
(
hide
)
Description:
Bug 28615: Add a simple way to mock Koha::Logger
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2021-06-22 20:58:10 UTC
Size:
3.61 KB
patch
obsolete
>From ef85ea5ff894bffd20eebcf7ebe6dfdc330072e9 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Tue, 22 Jun 2021 17:32:20 -0300 >Subject: [PATCH] Bug 28615: Add a simple way to mock Koha::Logger > >This patch introduces a new method for mocking the Koha::Logger. > >As the POD says, it is used by calling > > t::lib::Mocks::mock_logger({ warn => 1 }); > >The warn parameter is optional, and controls wether any use of the >logger will warn. The idea is that we can use that to write tests (that >catch those warnings and thus the codepaths that trigger the call are >properly tested, the same for the parameters to the call). > >I did a > > $ git grep 'Koha::Logger->get->debug' > $ git grep 'Koha::Logger->get->warn' > >In order to find places in which we use the logger, and then find a test >that would trigger it... to provide a useful test case. But I didn't... > >I tried with Overdues.t and Circulation.t and Export/Record.t but none >worked [1]. So this is my test plan: > >1. Apply this patch >2. Add the following line to t/db_dependent/Search.t just below the > imports: > >t::lib::Mocks::mock_logger({ warn => 1 }); > >3. Run: > $ kshell > k$ prove t/db_dependent/Search.t >=> SUCCESS: Suddenly every call to the logger is printing the parameter >it got passed. >4. Sign off :-D > >[1] This is actually frustrating, because it means those codepaths are >not traversed by the tests... > >Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> >--- > t/lib/Mocks.pm | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 55 insertions(+) > >diff --git a/t/lib/Mocks.pm b/t/lib/Mocks.pm >index 17d54f1a53..4f3779795f 100644 >--- a/t/lib/Mocks.pm >+++ b/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 <http://www.gnu.org/licenses>. >+ > 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<warn> 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; >-- >2.32.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 28615
:
122306
|
122307
|
122480
|
122481
|
122484
|
122485
|
122488
|
122489
|
122496
|
122497