Bug 18226 - Remove "use Test::DBIx::Class" instantiations' dangerous code duplication...
authorOlli-Antti Kivilahti <olli-antti.kivilahti@jns.fi>
Wed, 8 Mar 2017 15:34:28 +0000 (17:34 +0200)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 5 Jun 2017 20:52:47 +0000 (17:52 -0300)
Couldn't make the tests pass using Test::DBIx::Class, so reverted to the "usual way" since these tests are
in db_dependent anyway.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

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

t/db_dependent/Circulation/CalcDateDue.t
t/lib/Mocks.pm

index d1f39de..e533806 100644 (file)
@@ -7,31 +7,29 @@ use Test::MockModule;
 use DBI;
 use DateTime;
 use t::lib::Mocks;
-
-BEGIN {
-    t::lib::Mocks::mock_dbh;
-}
+use t::lib::TestBuilder;
 
 use_ok('C4::Circulation');
 
-my $dbh = C4::Context->dbh();
+my $schema = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+my $builder = t::lib::TestBuilder->new;
 
+my $categorycode = 'B';
+my $itemtype = 'MX';
+my $branchcode = 'FPL';
 my $issuelength = 10;
 my $renewalperiod = 5;
 my $lengthunit = 'days';
 
-my $mock_undef = [
-    []
-];
-
-my $mock_loan_length = [
-    ['issuelength', 'renewalperiod', 'lengthunit'],
-    [$issuelength, $renewalperiod, $lengthunit]
-];
-
-my $categorycode = 'B';
-my $itemtype = 'MX';
-my $branchcode = 'FPL';
+Koha::Database->schema->resultset('Issuingrule')->create({
+  categorycode => $categorycode,
+  itemtype => $itemtype,
+  branchcode => $branchcode,
+  issuelength => $issuelength,
+  renewalperiod => $renewalperiod,
+  lengthunit => $lengthunit,
+});
 
 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode = 'Days'
 t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 1);
@@ -41,10 +39,8 @@ my $dateexpiry = '2013-01-01';
 
 my $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
 my $start_date = DateTime->new({year => 2013, month => 2, day => 9});
-$dbh->{mock_add_resultset} = $mock_loan_length;
 my $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
 is($date, $dateexpiry . 'T23:59:00', 'date expiry');
-$dbh->{mock_add_resultset} = $mock_loan_length;
 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
 
 
@@ -54,11 +50,9 @@ t::lib::Mocks::mock_preference('useDaysMode', 'noDays');
 
 $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
 $start_date = DateTime->new({year => 2013, month => 2, day => 9});
-$dbh->{mock_add_resultset} = $mock_loan_length;
 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
 is($date, $dateexpiry . 'T23:59:00', 'date expiry');
 
-$dbh->{mock_add_resultset} = $mock_loan_length;
 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
 
 
@@ -68,10 +62,10 @@ t::lib::Mocks::mock_preference('useDaysMode', 'Days');
 
 $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
 $start_date = DateTime->new({year => 2013, month => 2, day => 9});
-$dbh->{mock_add_resultset} = $mock_loan_length;
 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
 is($date, '2013-02-' . (9 + $issuelength) . 'T23:59:00', "date expiry ( 9 + $issuelength )");
 
-$dbh->{mock_add_resultset} = $mock_loan_length;
 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
 is($date, '2013-02-' . (9 + $renewalperiod) . 'T23:59:00', "date expiry ( 9 + $renewalperiod )");
+
+$schema->storage->txn_rollback;
index 46a87cb..da20739 100644 (file)
@@ -41,13 +41,4 @@ sub mock_preference {
     });
 }
 
-sub mock_dbh {
-    our $context = new Test::MockModule('Koha::Database');
-    $context->mock( '_new_schema', sub {
-        my $dbh = Koha::Schema->connect( 'DBI:Mock:', '', '' );
-        return $dbh;
-    } );
-    return $context;
-}
-
 1;