From be245fd77864c69e2c17652c8445e74b58d83248 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 2 Mar 2021 11:04:38 -0300 Subject: [PATCH] Bug 27833: Make Koha::Exceptions::Patron::Attribute::* have parameters This patch adds a 'field' to this exceptions. They now have an 'attribute' parameter referencing the object itself, To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/Koha/Exceptions.t => SUCCESS: Tests pass! 3. Sign off! --- Koha/Exceptions/Patron/Attribute.pm | 61 +++++++++++++++++++++++++++-- t/Koha/Exceptions.t | 31 ++++++++++++++- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/Koha/Exceptions/Patron/Attribute.pm b/Koha/Exceptions/Patron/Attribute.pm index 80d7370894c..93bc34ca470 100644 --- a/Koha/Exceptions/Patron/Attribute.pm +++ b/Koha/Exceptions/Patron/Attribute.pm @@ -8,13 +8,66 @@ use Exception::Class ( description => 'Something went wrong' }, 'Koha::Exceptions::Patron::Attribute::NonRepeatable' => { - isa => 'Koha::Exceptions::Patron::Attribute', - description => "repeatable not set for attribute type and tried to add a new attribute for the same code" + isa => 'Koha::Exceptions::Patron::Attribute', + description => "repeatable not set for attribute type and tried to add a new attribute for the same code", + fields => [ "attribute" ] }, 'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint' => { - isa => 'Koha::Exceptions::Patron::Attribute', - description => "unique_id set for attribute type and tried to add a new with the same code and value" + isa => 'Koha::Exceptions::Patron::Attribute', + description => "unique_id set for attribute type and tried to add a new with the same code and value", + fields => [ "attribute" ] } ); +sub full_message { + my $self = shift; + + my $msg = $self->message; + + unless ( $msg) { + if ( $self->isa('Koha::Exceptions::Patron::Attribute::NonRepeatable') ) { + $msg = sprintf( + "Tried to add more than one non-repeatable attributes. code=%s attribute=%s", + $self->attribute->code, + $self->attribute->attribute + ); + } + elsif ( $self->isa('Koha::Exceptions::Patron::Attribute::UniqueIDConstraint') ) { + $msg = sprintf( + "Your action breaks a unique constraint on the attribute. code=%s attribute=%s", + $self->attribute->code, + $self->attribute->attribute + ); + } + } + + return $msg; +} + +=head1 NAME + +Koha::Exceptions::Patron::Attribute - Base class for patron attribute exceptions + +=head1 Exceptions + +=head2 Koha::Exceptions::Patron::Attribute + +Generic patron attribute exception + +=head2 Koha::Exceptions::Patron::Attribute::NonRepeatable + +Exception to be used trying to add more than one non-repeatable attribute. + +=head2 Koha::Exceptions::Patron::Attribute::UniqueIDConstraint + +Exception to be used when trying to add an attribute that breaks its type unique constraint. + +=head1 Class methods + +=head2 full_message + +Overloaded method for exception stringifying. + +=cut + 1; diff --git a/t/Koha/Exceptions.t b/t/Koha/Exceptions.t index 5cd3f1cfffa..1a019435825 100755 --- a/t/Koha/Exceptions.t +++ b/t/Koha/Exceptions.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 6; +use Test::More tests => 7; use Test::MockObject; use Test::Exception; @@ -197,3 +197,32 @@ subtest 'Koha::Exceptions::Object::NotInstantiated tests' => sub { 'Exception is thrown :-D'; is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' ); }; + +subtest 'Koha::Exceptions::Patron::Attribute::* tests' => sub { + + plan tests => 5; + + use_ok("Koha::Exceptions::Patron::Attribute"); + + my $code = 'CODE'; + my $attribute = 'ATTRIBUTE'; + + my $mocked_attribute = Test::MockObject->new(); + $mocked_attribute->mock('code', sub { return $code } ); + $mocked_attribute->mock('attribute', sub { return $attribute } ); + + throws_ok + { Koha::Exceptions::Patron::Attribute::NonRepeatable->throw( + attribute => $mocked_attribute ); } + 'Koha::Exceptions::Patron::Attribute::NonRepeatable', + 'Exception is thrown :-D'; + + # stringify the exception + is( "$@", "Tried to add more than one non-repeatable attributes. code=$code attribute=$attribute", 'Exception stringified correctly' ); + + throws_ok + { Koha::Exceptions::Patron::Attribute::NonRepeatable->throw( "Manual message exception" ) } + 'Koha::Exceptions::Patron::Attribute::NonRepeatable', + 'Exception is thrown :-D'; + is( "$@", 'Manual message exception', 'Exception not stringified if manually passed' ); +}; -- 2.30.1