Bug 25423: Add Koha::Exceptions::Object::NotInstantiated
authorTomas Cohen Arazi <tomascohen@theke.io>
Fri, 8 May 2020 11:39:10 +0000 (08:39 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 8 May 2020 13:42:37 +0000 (15:42 +0200)
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Koha/Exceptions/Object.pm
t/Koha/Exceptions.t

index 53bd44c..688a97c 100644 (file)
@@ -55,6 +55,11 @@ use Exception::Class (
         description => 'Invalid data passed',
         fields      => ['type', 'property', 'value'],
     },
+    'Koha::Exceptions::Object::NotInstantiated' => {
+        isa         => 'Koha::Exceptions::Object',
+        description => 'Tried to access a method on an uninstantiated object',
+        fields      => ['class','method']
+    },
     'Koha::Exceptions::Object::NotInStorage' => {
         isa         => 'Koha::Exceptions::Object',
         description => 'The object is not in storage yet',
@@ -76,6 +81,9 @@ sub full_message {
         elsif ( $self->isa('Koha::Exceptions::Object::ReadOnlyProperty') ) {
             $msg = sprintf("Invalid attempt to change readonly property: %s", $self->property );
         }
+        elsif ( $self->isa('Koha::Exceptions::Object::NotInstantiated') ) {
+            $msg = sprintf("Tried to access the '%s' method, but %s is not instantiated", $self->method, $self->class );
+        }
     }
 
     return $msg;
index bb36df3..a63de79 100644 (file)
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 5;
+use Test::More tests => 6;
 use Test::MockObject;
 use Test::Exception;
 
@@ -176,3 +176,25 @@ subtest 'Koha::Exceptions::Patron::Relationship tests' => sub {
         'Exception is thrown :-D';
     is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
 };
+
+subtest 'Koha::Exceptions::Object::NotInstantiated tests' => sub {
+
+    plan tests => 5;
+
+    use_ok('Koha::Exceptions::Object::NotInstantiated');
+
+    throws_ok
+        { Koha::Exceptions::Object::NotInstantiated->throw(
+            method => 'brain_explode', class => 'Koha::JD' ); }
+        'Koha::Exceptions::Object::NotInstantiated',
+        'Exception is thrown :-D';
+
+    # stringify the exception
+    is( "$@", 'Tried to access the \'brain_explode\' method, but Koha::JD is not instantiated', 'Exception stringified correctly' );
+
+    throws_ok
+        { Koha::Exceptions::Object::NotInstantiated->throw( "Manual message exception" ) }
+        'Koha::Exceptions::Object::NotInstantiated',
+        'Exception is thrown :-D';
+    is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' );
+};