From 32f25f6d8f4d14e908e8a7bdfd903060382bc544 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 28 Sep 2020 10:23:50 -0300 Subject: [PATCH] Bug 26555: Add Koha::Object::Message This patch introduces a tiny class for encapsulating execution messages in Koha::Object derived classes. To test: 1. Apply this patch 2. Run; $ kshell k$ prove t/Koha/Object/Message.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: David Nind Signed-off-by: Martin Renvoize --- Koha/Object/Message.pm | 77 +++++++++++++++++++++++++++++++++++++++++ t/Koha/Object/Message.t | 51 +++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 Koha/Object/Message.pm create mode 100755 t/Koha/Object/Message.t diff --git a/Koha/Object/Message.pm b/Koha/Object/Message.pm new file mode 100644 index 0000000000..42349c9864 --- /dev/null +++ b/Koha/Object/Message.pm @@ -0,0 +1,77 @@ +package Koha::Object::Message; + +# 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 base qw(Class::Accessor); + +use Koha::Exceptions; + +__PACKAGE__->mk_ro_accessors(qw( message type )); + +=head1 NAME + +Koha::Object::Message - Class encapsulating action feedback messages in Koha::Object-derived classes + +=head1 SYNOPSIS + + my ($self, $params) = @_; + push @{$self->{_messages}} = Koha::Object::Message->new($params); + +=head1 API + +=head2 Class methods + +=head3 new + + my $message = Koha::Object::Message->new( + { + message => $some_message, + [ type => 'error' ] + } + ); + +Create a new Koha::Object::Message object. + +=cut + +sub new { + my ( $class, $params ) = @_; + + my $message = $params->{message}; + my $type = $params->{type} // 'error'; + + Koha::Exceptions::MissingParameter->throw( "Mandatory parameter missing: 'message'" ) + unless $message; + + my $self = $class->SUPER::new( + { + message => $message, + type => $type + } + ); + + return $self; +} + +=head1 AUTHOR + +Tomas Cohen Arazi, Etomascohen@theke.ioE + +=cut + +1; diff --git a/t/Koha/Object/Message.t b/t/Koha/Object/Message.t new file mode 100755 index 0000000000..04bbe5e796 --- /dev/null +++ b/t/Koha/Object/Message.t @@ -0,0 +1,51 @@ +#!/usr/bin/perl + +# Copyright 2020 Koha Development team +# +# 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 Test::More tests => 2; +use Test::Exception; + +BEGIN { + use_ok('Koha::Object::Message'); +} + +subtest 'new() tests' => sub { + + plan tests => 8; + + my $some_error = 'Some error'; + + my $message = Koha::Object::Message->new({ message => $some_error }); + is( ref($message), 'Koha::Object::Message', 'Type is correct' ); + is( $message->message, $some_error, 'The message attribute has the right value' ); + is( $message->type, 'error', 'If omitted, the type is error' ); + + $message = Koha::Object::Message->new({ message => $some_error, type => 'callback' }); + is( ref($message), 'Koha::Object::Message', 'Type is correct' ); + is( $message->message, $some_error, 'The message attribute has the right value' ); + is( $message->type, 'callback', 'type is correct' ); + + throws_ok + { Koha::Object::Message->new({ blah => 'ohh' }); } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown if required parameter missing'; + + is( "$@", "Mandatory parameter missing: 'message'", 'Expected exception message' ); +}; -- 2.20.1