--- /dev/null
+package Koha::Exceptions::Object;
+
+use Modern::Perl;
+
+use Exception::Class (
+
+ 'Koha::Exceptions::Object' => {
+ description => 'Something went wrong!',
+ },
+ 'Koha::Exceptions::Object::MethodNotFound' => {
+ isa => 'Koha::Exceptions::Object',
+ description => "Invalid method",
+ },
+ 'Koha::Exceptions::Object::PropertyNotFound' => {
+ isa => 'Koha::Exceptions::Object',
+ description => "Invalid property",
+ }
+);
+
+1;
use Carp;
use Koha::Database;
+use Koha::Exceptions::Object;
=head1 NAME
foreach my $p ( keys %$properties ) {
unless ( grep {/^$p$/} @columns ) {
- carp("No property $p!");
- return 0;
+ Koha::Exceptions::Object::PropertyNotFound->throw( "No property $p for " . ref($self) );
}
}
my $r = eval { $self->_result->$method(@_) };
if ( $@ ) {
- carp "No method $method found for " . ref($self) . " " . $@;
- return
+ Koha::Exceptions::Object::MethodNotFound->throw( "No method $method for " . ref($self) );
}
return $r;
}
use t::lib::TestBuilder;
+use Try::Tiny;
+
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
+my $builder = t::lib::TestBuilder->new;
is( ref(Koha::Authority::Types->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
subtest 'update' => sub {
plan tests => 2;
- my $builder = t::lib::TestBuilder->new;
+
$builder->build( { source => 'City', value => { city_country => 'UK' } } );
$builder->build( { source => 'City', value => { city_country => 'UK' } } );
$builder->build( { source => 'City', value => { city_country => 'UK' } } );
subtest 'reset' => sub {
plan tests => 1;
- my $builder = t::lib::TestBuilder->new;
+
my $patrons = Koha::Patrons->search;
my $first_borrowernumber = $patrons->next->borrowernumber;
my $second_borrowernumber = $patrons->next->borrowernumber;
subtest 'delete' => sub {
plan tests => 2;
- my $builder = t::lib::TestBuilder->new;
+
my $patron_1 = $builder->build({source => 'Borrower'});
my $patron_2 = $builder->build({source => 'Borrower'});
is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->delete, 2, '');
is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
};
+subtest 'Exceptions' => sub {
+ plan tests => 2;
+
+ my $patron_borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
+ my $patron = Koha::Patrons->find( $patron_borrowernumber );
+
+ try {
+ $patron->blah('blah');
+ } catch {
+ ok( $_->isa('Koha::Exceptions::Object::MethodNotFound'),
+ 'Calling a non-existent method should raise a Koha::Exceptions::Object::MethodNotFound exception' );
+ };
+
+ try {
+ $patron->set({ blah => 'blah' });
+ } catch {
+ ok( $_->isa('Koha::Exceptions::Object::PropertyNotFound'),
+ 'Setting a non-existent property should raise a Koha::Exceptions::Object::PropertyNotFound exception' );
+ };
+};
+
$schema->storage->txn_rollback;
1;