Bug 14778: Use mock_dbh where it should be used
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 20 Oct 2015 10:48:00 +0000 (11:48 +0100)
committerTomas Cohen Arazi <tomascohen@theke.io>
Fri, 23 Oct 2015 15:01:18 +0000 (12:01 -0300)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
t/Barcodes_ValueBuilder.t
t/Borrower.t
t/Circulation_barcodedecode.t
t/Members/cardnumber.t
t/db_dependent/Circulation_issuingrules.t
t/db_dependent/Context.t
t/db_dependent/ReportsGuided.t

index 64d7fae..da90101 100644 (file)
@@ -1,7 +1,6 @@
 #!/usr/bin/perl
-use strict;
-use warnings;
-use DBI;
+
+use Modern::Perl;
 use Test::More tests => 10;
 use Test::MockModule;
 
@@ -9,21 +8,27 @@ BEGIN {
     use_ok('C4::Barcodes::ValueBuilder');
 }
 
+use Test::DBIx::Class {
+    schema_class => 'Koha::Schema',
+    connect_info => ['dbi:SQLite:dbname=:memory:','',''],
+    connect_opts => { name_sep => '.', quote_char => '`', },
+    fixture_class => '::Populate',
+}, 'Biblio' ;
+
+sub fixtures {
+    my ( $data ) = @_;
+    fixtures_ok [
+        Item => [
+            @$data
+        ],
+    ], 'add fixtures';
+}
 
-my $module = new Test::MockModule('C4::Context');
-$module->mock('_new_dbh', sub {
-                             my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
-                             || die "Cannot create handle: $DBI::errstr\n";
-                             return $dbh });
-
-# Mock data
-my $incrementaldata = [
-    ['max(abs(barcode))'],
-    ['33333074344563'],
-];
-
+my $db = Test::MockModule->new('Koha::Database');
+$db->mock(
+    _new_schema => sub { return Schema(); }
+);
 
-my $dbh = C4::Context->dbh();
 
 my %args = (
     year        => '2012',
@@ -35,43 +40,29 @@ my %args = (
     locsubfield => 'a'
 );
 
-$dbh->{mock_add_resultset} = $incrementaldata;
-my ($nextnum, $scr, $history);
-
-($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args);
+fixtures([
+    [ qw/ itemnumber barcode / ],
+    [ 1, 33333074344563 ]
+]);
+my ($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args);
 is($nextnum, 33333074344564, 'incremental barcode');
 is($scr, undef, 'incremental javascript');
 
-# This should run exactly one query so we can test
-$history = $dbh->{mock_all_history};
-is(scalar(@{$history}), 1, 'Correct number of statements executed for incremental barcode') ;
-
-my $hbyymmincrdata = [
-    ['number'],
+fixtures([
+    ['barcode'],
     ['890'],
-];
+]);
 
-$dbh->{mock_add_resultset} = $hbyymmincrdata;
-$dbh->{mock_clear_history} = 1;
 ($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
 is($nextnum, '12070891', 'hbyymmincr barcode');
 ok(length($scr) > 0, 'hbyymmincr javascript');
 
-# This should run exactly one query so we can test
-$history = $dbh->{mock_all_history};
-is(scalar(@{$history}), 1, 'Correct number of statements executed for hbyymmincr barcode') ;
-
-my $annualdata = [
-    ['max(cast( substring_index(barcode, \'-\',-1) as signed))'],
+fixtures([
+    ['barcode'],
+    #max(cast( substring_index(barcode, \'-\',-1) as signed))'],
     ['34'],
-];
+]);
 
-$dbh->{mock_add_resultset} = $annualdata;
-$dbh->{mock_clear_history} = 1;
 ($nextnum, $scr) = C4::Barcodes::ValueBuilder::annual::get_barcode(\%args);
 is($nextnum, '2012-0035', 'annual barcode');
 is($scr, undef, 'annual javascript');
-
-# This should run exactly one query so we can test
-$history = $dbh->{mock_all_history};
-is(scalar(@{$history}), 1, 'Correct number of statements executed for hbyymmincr barcode') ;
index 1e13725..4181942 100755 (executable)
@@ -19,9 +19,10 @@ use Modern::Perl;
 
 use Test::More tests => 18;
 use Test::Warn;
-use Koha::Database;
+use t::lib::Mocks;
 
 BEGIN {
+    t::lib::Mocks::mock_dbh;
     use_ok('Koha::Object');
     use_ok('Koha::Borrower');
 }
index f397cc9..5b1e065 100644 (file)
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-
-use Test::More tests => 26;
+use Test::More tests => 25;
 use t::lib::Mocks;
+use C4::Circulation;
+use C4::Context;
+
+BEGIN {
+    t::lib::Mocks::mock_dbh;
+};
 
 C4::Context->_new_userenv(123456);
 C4::Context->set_userenv(1,'kmkale' , 1, 'km', 'kale' , 'IMS', 'IMS Branch DEscription', 0, 'kmkale@anantcorp.com');
 
-BEGIN {
-    # Mock the DB connexion and C4::Context
-    my $context = t::lib::Mocks::mock_dbh;
-    use_ok('C4::Circulation');
-}
-
 our %inputs = (
     cuecat     => ["26002315", '.C3nZC3nZC3nYD3b6ENnZCNnY.fHmc.C3D1Dxr2C3nZE3n7.', ".C3nZC3nZC3nYD3b6ENnZCNnY.fHmc.C3D1Dxr2C3nZE3n7.\r\n",
                     'q.C3nZC3nZC3nWDNzYDxf2CNnY.fHmc.C3DWC3nZCNjXD3nW.', '.C3nZC3nZC3nWCxjWE3D1C3nX.cGf2.ENr7C3v7D3T3ENj3C3zYDNnZ.' ],
@@ -46,7 +45,7 @@ our %outputs = (
     EAN13      => [qw(0892685001928 0000000695152)],
     other      => [qw(26002315 T0031472 T32 Alphanum123), "Alpha Num 345"],
 );
-    
+
 my @filters = sort keys %inputs;
 foreach my $filter (@filters) {
     foreach my $datum (@{$inputs{$filter}}) {
index bfad5ce..b2ec79f 100644 (file)
@@ -3,21 +3,13 @@
 use Modern::Perl;
 use Test::More tests => 23;
 
-use Test::MockModule;
-use DBD::Mock;
 use t::lib::Mocks;
 
 use_ok('C4::Members');
 
-my $module_context = new Test::MockModule('C4::Context');
-$module_context->mock(
-    '_new_dbh',
-    sub {
-        my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
-          || die "Cannot create handle: $DBI::errstr\n";
-        return $dbh;
-    }
-);
+BEGIN {
+    t::lib::Mocks::mock_dbh;
+}
 
 my $dbh = C4::Context->dbh;
 my $rs = [];
index f5abbda..aff1f93 100644 (file)
@@ -6,13 +6,11 @@ use Test::More tests => 7;
 use Test::MockModule;
 use DBI;
 use DateTime;
+use t::lib::Mocks;
 
-my $contextmodule = new Test::MockModule('C4::Context');
-$contextmodule->mock('_new_dbh', sub {
-    my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
-    || die "Cannot create handle: $DBI::errstr\n";
-    return $dbh
-});
+BEGIN {
+    t::lib::Mocks::mock_dbh;
+}
 
 use_ok('C4::Circulation');
 
@@ -60,16 +58,8 @@ is_deeply($loanlength, $default, 'none matches');
 #=== CalcDateDue
 
 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode = 'Days'
-$contextmodule->mock('preference', sub {
-    my ($self, $syspref) = @_;
-    if ( $syspref eq "ReturnBeforeExpiry" ) {
-        return 1;
-    } elsif ( $syspref eq "useDaysMode" ) {
-        return 'Days';
-    } else {
-        return;
-    }
-});
+t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 1);
+t::lib::Mocks::mock_preference('useDaysMode', 'Days');
 
 my $dateexpiry = '2013-01-01';
 
@@ -83,16 +73,8 @@ $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borr
 
 
 #Set syspref ReturnBeforeExpiry = 1 and useDaysMode != 'Days'
-$contextmodule->mock('preference', sub {
-    my ($self, $syspref) = @_;
-    if ( $syspref eq "ReturnBeforeExpiry" ) {
-        return 1;
-    } elsif ( $syspref eq "useDaysMode" ) {
-        return 'noDays';
-    } else {
-        return;
-    }
-});
+t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 1);
+t::lib::Mocks::mock_preference('useDaysMode', 'noDays');
 
 $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
 $start_date = DateTime->new({year => 2013, month => 2, day => 9});
@@ -105,16 +87,8 @@ $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borr
 
 
 #Set syspref ReturnBeforeExpiry = 0 and useDaysMode = 'Days'
-$contextmodule->mock('preference', sub {
-    my ($self, $syspref) = @_;
-    if ( $syspref eq "ReturnBeforeExpiry" ) {
-        return 0;
-    } elsif ( $syspref eq "useDaysMode" ) {
-        return 'Days';
-    } else {
-        return;
-    }
-});
+t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 0);
+t::lib::Mocks::mock_preference('useDaysMode', 'Days');
 
 $borrower = {categorycode => 'B', dateexpiry => $dateexpiry};
 $start_date = DateTime->new({year => 2013, month => 2, day => 9});
index 4541fa4..8c0abfd 100755 (executable)
@@ -6,6 +6,7 @@ use warnings;
 use Test::More;
 use Test::MockModule;
 use vars qw($debug $koha $dbh $config $ret);
+use t::lib::Mocks;
 
 use Koha::Database;
 
@@ -63,15 +64,7 @@ foreach (sort @keys) {
 ok($config = $koha->{config}, 'Getting $koha->{config} ');
 
 # Testing syspref caching
-my $module = new Test::MockModule('C4::Context');
-$module->mock(
-    '_new_dbh',
-    sub {
-        my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
-          || die "Cannot create handle: $DBI::errstr\n";
-        return $dbh;
-    }
-);
+t::lib::Mocks::mock_dbh;
 
 my $history;
 
index d51ca8d..097722a 100755 (executable)
@@ -4,22 +4,17 @@ use Modern::Perl;
 
 use Test::More tests => 13;
 use Test::MockModule;
-use DBD::Mock;
+
+use t::lib::Mocks;
 
 use_ok('C4::Reports::Guided');
 
 my $context = new Test::MockModule('C4::Context');
 my $koha = new Test::MockModule('C4::Koha');
 
-$context->mock(
-    '_new_dbh',
-    sub {
-        my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
-          || die "Cannot create handle: $DBI::errstr\n";
-        return $dbh;
-    }
-);
-
+BEGIN {
+    t::lib::Mocks::mock_dbh;
+}
 
 sub MockedIsAuthorisedValueCategory {
     my $authorised_value = shift;
@@ -53,11 +48,7 @@ $koha->mock(
                 'GetReservedAuthorisedValues returns a fixed list');
 }
 
-SKIP: {
-
-    skip "DBD::Mock is too old", 8
-        unless $DBD::Mock::VERSION >= 1.45;
-
+{
     ok( IsAuthorisedValueValid('LOC'),
         'User defined authorised value category is valid');