View | Details | Raw Unified | Return to bug 26555
Collapse All | Expand All

(-)a/Koha/Object/Message.pm (+77 lines)
Line 0 Link Here
1
package Koha::Object::Message;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use base qw(Class::Accessor);
21
22
use Koha::Exceptions;
23
24
__PACKAGE__->mk_ro_accessors(qw( message type ));
25
26
=head1 NAME
27
28
Koha::Object::Message - Class encapsulating errors in Koha::Object-derived classes
29
30
=head1 SYNOPSIS
31
32
  my ($self, $params) = @_;
33
  push @{$self->{_errors}} = Koha::Object::Message->new($params);
34
35
=head1 API
36
37
=head2 Class methods
38
39
=head3 new
40
41
    my $message = Koha::Object::Message->new(
42
        {
43
            message => $some_message,
44
          [ type    => 'error' ]
45
        }
46
    );
47
48
Create a new Koha::Object::Message object.
49
50
=cut
51
52
sub new {
53
    my ( $class, $params ) = @_;
54
55
    my $message = $params->{message};
56
    my $type    = $params->{type} // 'error';
57
58
    Koha::Exceptions::MissingParameter->throw( "Mandatory parameter missing: 'message'" )
59
        unless $message;
60
61
    my $self = $class->SUPER::new(
62
        {
63
            message => $message,
64
            type    => $type
65
        }
66
    );
67
68
    return $self;
69
}
70
71
=head1 AUTHOR
72
73
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
74
75
=cut
76
77
1;
(-)a/t/Koha/Object/Message.t (-1 / +51 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2020 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 2;
23
use Test::Exception;
24
25
BEGIN {
26
    use_ok('Koha::Object::Message');
27
}
28
29
subtest 'new() tests' => sub {
30
31
    plan tests => 8;
32
33
    my $some_error = 'Some error';
34
35
    my $message = Koha::Object::Message->new({ message => $some_error });
36
    is( ref($message), 'Koha::Object::Message', 'Type is correct' );
37
    is( $message->message, $some_error, 'The message attribute has the right value' );
38
    is( $message->type, 'error', 'If omitted, the type is error' );
39
40
    $message = Koha::Object::Message->new({ message => $some_error, type => 'callback' });
41
    is( ref($message), 'Koha::Object::Message', 'Type is correct' );
42
    is( $message->message, $some_error, 'The message attribute has the right value' );
43
    is( $message->type, 'callback', 'type is correct' );
44
45
    throws_ok
46
        { Koha::Object::Message->new({ blah => 'ohh' }); }
47
        'Koha::Exceptions::MissingParameter',
48
        'Exception thrown if required parameter missing';
49
50
    is( "$@", "Mandatory parameter missing: 'message'", 'Expected exception message' );
51
};

Return to bug 26555