Koha/t/db_dependent/Borrowers.t
Kyle M Hall 6f0316a8d2 Bug 13019 - Add base classes on which to build Koha objects
The idea behind this is to have a pair of base classes on which to build
our new generation of Koha objects. Koha::Object is a base class, which
in it's most basic form, is to represent a row in a table. For example,
Koha::Borrower inherits from Koha::Object. So too could Koha::Biblio
and Koha::Item for example.

Koha::Objects is to represent a way to fetch and manipulate sets of
objects. For example, Koha::Borrowers has a method to get a
Koha::Borrower object by id and a method to search for an get a list
of Koha::Borrower objects. Right now Koha::Objects has only the
essentials but can easily be extended and those enhancements will be
passed down to all the child classes based on it.

By using these classes as a base, we will add consistency to our
code, allow us to keep our code DRY, reduce bugs, and encapsulate our
database access among other benefits.

Test Plan:
1) Apply this patch
2) prove t/Object.t t/db_dependent/Object.t t/db_dependent/Objects.t

Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
2015-02-12 15:21:01 -03:00

93 lines
2.6 KiB
Perl
Executable file

#!/usr/bin/perl
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 12;
use Test::Warn;
use C4::Context;
use Koha::Database;
BEGIN {
use_ok('Koha::Objects');
use_ok('Koha::Borrowers');
}
# Start transaction
my $dbh = C4::Context->dbh;
$dbh->{AutoCommit} = 0;
$dbh->{RaiseError} = 1;
$dbh->do("DELETE FROM issues");
$dbh->do("DELETE FROM borrowers");
my $categorycode =
Koha::Database->new()->schema()->resultset('Category')->first()
->categorycode();
my $branchcode =
Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
my $b1 = Koha::Borrower->new(
{
surname => 'Test 1',
branchcode => $branchcode,
categorycode => $categorycode
}
);
$b1->store();
my $b2 = Koha::Borrower->new(
{
surname => 'Test 2',
branchcode => $branchcode,
categorycode => $categorycode
}
);
$b2->store();
my $b3 = Koha::Borrower->new(
{
surname => 'Test 3',
branchcode => $branchcode,
categorycode => $categorycode
}
);
$b3->store();
my $b1_new = Koha::Borrowers->new()->find( $b1->borrowernumber() );
is( $b1->surname(), $b1_new->surname(), "Found matching borrower" );
my @borrowers = Koha::Borrowers->new()->search( { branchcode => $branchcode } );
is( @borrowers, 3, "Found 3 borrowers with Search" );
my $borrowers = Koha::Borrowers->new()->search( { branchcode => $branchcode } );
is( $borrowers->count( { branchcode => $branchcode } ), 3, "Counted 3 borrowers with Count" );
my $b = $borrowers->next();
is( $b->surname(), 'Test 1', "Next returns first borrower" );
$b = $borrowers->next();
is( $b->surname(), 'Test 2', "Next returns second borrower" );
$b = $borrowers->next();
is( $b->surname(), 'Test 3', "Next returns third borrower" );
$b = $borrowers->next();
is( $b, undef, "Next returns undef" );
# Test Reset and iteration in concert
$borrowers->reset();
foreach my $b ( $borrowers->as_list() ) {
is( $b->categorycode(), $categorycode, "Iteration returns a borrower object" );
}
1;