Bug 25423: Add Koha::Exceptions::Object::NotInstantiated
[koha.git] / t / Koha / Exceptions.t
1 #!/usr/bin/perl
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 Test::More tests => 6;
21 use Test::MockObject;
22 use Test::Exception;
23
24 subtest 'Koha::Exceptions::Hold tests' => sub {
25
26     plan tests => 5;
27
28     use_ok('Koha::Exceptions::Hold');
29
30     throws_ok
31         { Koha::Exceptions::Hold::CannotSuspendFound->throw( status => 'W' ); }
32         'Koha::Exceptions::Hold::CannotSuspendFound',
33         'Exception is thrown :-D';
34
35     # stringify the exception
36     is( "$@", 'Found hold cannot be suspended. Status=W', 'Exception stringified correctly' );
37
38     throws_ok
39         { Koha::Exceptions::Hold::CannotSuspendFound->throw( "Manual message exception" ) }
40         'Koha::Exceptions::Hold::CannotSuspendFound',
41         'Exception is thrown :-D';
42     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
43 };
44
45 subtest 'Koha::Exceptions::Object::FKConstraint tests' => sub {
46
47     plan tests => 9;
48
49     use_ok('Koha::Exceptions::Object');
50
51     throws_ok
52         { Koha::Exceptions::Object::FKConstraint->throw( broken_fk => 'nasty', value => 'fk' ); }
53         'Koha::Exceptions::Object::FKConstraint',
54         'Exception is thrown :-D';
55
56     # stringify the exception
57     is( "$@", 'Invalid parameter passed, nasty=fk does not exist', 'Exception stringified correctly' );
58
59     throws_ok
60         { Koha::Exceptions::Object::FKConstraint->throw( "Manual message exception" ) }
61         'Koha::Exceptions::Object::FKConstraint',
62         'Exception is thrown :-D';
63     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
64
65     throws_ok {
66         Koha::Exceptions::Object::BadValue->throw(
67             type     => 'datetime',
68             property => 'a_property',
69             value    => 'a_value'
70         );
71     }
72     'Koha::Exceptions::Object::BadValue',
73         'Koha::Exceptions::Object::BadValue exception is thrown :-D';
74
75     # stringify the exception
76     is( "$@", 'Invalid value passed, a_property=a_value expected type is datetime', 'Koha::Exceptions::Object::BadValue stringified correctly' );
77
78     throws_ok
79         { Koha::Exceptions::Object::BadValue->throw( "Manual message exception" ) }
80         'Koha::Exceptions::Object::BadValue',
81         'Koha::Exceptions::Object::BadValue is thrown :-D';
82     is( "$@", 'Manual message exception', 'Koha::Exceptions::Object::BadValue not stringified if manually passed' );
83 };
84
85 subtest 'Koha::Exceptions::Password tests' => sub {
86
87     plan tests => 5;
88
89     use_ok('Koha::Exceptions::Password');
90
91     throws_ok
92         { Koha::Exceptions::Password::TooShort->throw( length => 4, min_length => 5 ); }
93         'Koha::Exceptions::Password::TooShort',
94         'Exception is thrown :-D';
95
96     # stringify the exception
97     is( "$@", 'Password length (4) is shorter than required (5)', 'Exception stringified correctly' );
98
99     throws_ok
100         { Koha::Exceptions::Password::TooShort->throw( "Manual message exception" ) }
101         'Koha::Exceptions::Password::TooShort',
102         'Exception is thrown :-D';
103     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
104 };
105
106 subtest 'Koha::Exceptions::Metadata tests' => sub {
107
108     plan tests => 5;
109
110     use_ok('Koha::Exceptions::Metadata');
111
112     my $object = Test::MockObject->new;
113     $object->mock( 'id', 'an_id' );
114     $object->mock( 'format', 'a_format' );
115     $object->mock( 'schema', 'a_schema' );
116
117     throws_ok
118         { Koha::Exceptions::Metadata::Invalid->throw(
119             id => 'an_id', format => 'a_format',
120             schema => 'a_schema', decoding_error => 'a_nasty_error' ); }
121         'Koha::Exceptions::Metadata::Invalid',
122         'Exception is thrown :-D';
123
124     # stringify the exception
125     is( "$@", 'Invalid data, cannot decode object (id=an_id, format=a_format, schema=a_schema, decoding_error=\'a_nasty_error\')', 'Exception stringified correctly' );
126
127     throws_ok
128         { Koha::Exceptions::Metadata::Invalid->throw( "Manual message exception" ) }
129         'Koha::Exceptions::Metadata::Invalid',
130         'Exception is thrown :-D';
131     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
132 };
133
134 subtest 'Koha::Exceptions::Patron::Relationship tests' => sub {
135
136     plan tests => 9;
137
138     use_ok('Koha::Exceptions::Patron::Relationship');
139
140     throws_ok
141         { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( no_relationship => 1 ); }
142         'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
143         'Exception is thrown :-D';
144
145     # stringify the exception
146     is( "$@", 'No relationship passed.', 'Exception stringified correctly' );
147
148     throws_ok
149         { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( relationship => 'some' ); }
150         'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
151         'Exception is thrown :-D';
152
153     # stringify the exception
154     is( "$@", "Invalid relationship passed, 'some' is not defined.", 'Exception stringified correctly' );
155
156     my $guarantor_id = 1;
157     my $guarantee_id = 2;
158
159     throws_ok {
160         Koha::Exceptions::Patron::Relationship::DuplicateRelationship->throw(
161             guarantor_id => $guarantor_id,
162             guarantee_id => $guarantee_id
163         );
164     }
165     'Koha::Exceptions::Patron::Relationship::DuplicateRelationship', 'Exception is thrown :-D';
166
167     # stringify the exception
168     is( "$@",
169         "There already exists a relationship for the same guarantor ($guarantor_id) and guarantee ($guarantee_id) combination",
170         'Exception stringified correctly'
171     );
172
173     throws_ok
174         { Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw( "Manual message exception" ) }
175         'Koha::Exceptions::Patron::Relationship::InvalidRelationship',
176         'Exception is thrown :-D';
177     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
178 };
179
180 subtest 'Koha::Exceptions::Object::NotInstantiated tests' => sub {
181
182     plan tests => 5;
183
184     use_ok('Koha::Exceptions::Object::NotInstantiated');
185
186     throws_ok
187         { Koha::Exceptions::Object::NotInstantiated->throw(
188             method => 'brain_explode', class => 'Koha::JD' ); }
189         'Koha::Exceptions::Object::NotInstantiated',
190         'Exception is thrown :-D';
191
192     # stringify the exception
193     is( "$@", 'Tried to access the \'brain_explode\' method, but Koha::JD is not instantiated', 'Exception stringified correctly' );
194
195     throws_ok
196         { Koha::Exceptions::Object::NotInstantiated->throw( "Manual message exception" ) }
197         'Koha::Exceptions::Object::NotInstantiated',
198         'Exception is thrown :-D';
199     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
200 };