From 6c15193d0ffc6f7108600896f0755796cff1477f Mon Sep 17 00:00:00 2001 From: Koha Date: Mon, 10 Aug 2015 11:08:20 +0200 Subject: [PATCH] Bug 14375 - Use mock on dbh instead of _new_dbh This patch changes in test modules the mock on database connexion. Instead of mocking method _new_dbh (removed by main patch), mock directly method dbh. Always use t::lib::Mocks::mock_dbh() for that. To test, just run prove on modified test modules. --- t/Acquisition/Invoice.t | 11 ++++------- t/Barcodes_ValueBuilder.t | 11 +++++------ t/Biblio.t | 13 +++---------- t/Calendar.t | 16 +++++----------- t/Images.t | 14 +++++--------- t/ItemType.t | 14 ++++---------- t/Koha.t | 13 ++++--------- t/Letters.t | 14 ++++---------- t/Matcher.t | 14 +++++--------- t/Members/cardnumber.t | 14 +++----------- t/Members_AttributeTypes.t | 14 +++++--------- t/SocialData.t | 14 +++++--------- t/db_dependent/Circulation_issuingrules.t | 10 +++------- t/db_dependent/Context.t | 13 ++++--------- t/db_dependent/ReportsGuided.t | 13 +++---------- t/db_dependent/Search.t | 11 +++++------ t/lib/Mocks.pm | 14 +++++++++----- 17 files changed, 76 insertions(+), 147 deletions(-) diff --git a/t/Acquisition/Invoice.t b/t/Acquisition/Invoice.t index aa1172d..38a2da9 100755 --- a/t/Acquisition/Invoice.t +++ b/t/Acquisition/Invoice.t @@ -1,17 +1,14 @@ #!/usr/bin/perl use Modern::Perl; -use C4::Context; use Test::More tests => 50; use Test::MockModule; +use t::lib::Mocks; +use C4::Context; -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 the DB connexion and C4::Context +my $context = t::lib::Mocks::mock_dbh; my $dbh = C4::Context->dbh; diff --git a/t/Barcodes_ValueBuilder.t b/t/Barcodes_ValueBuilder.t index 64d7fae..a0b6bf2 100644 --- a/t/Barcodes_ValueBuilder.t +++ b/t/Barcodes_ValueBuilder.t @@ -1,20 +1,19 @@ #!/usr/bin/perl + use strict; use warnings; -use DBI; + use Test::More tests => 10; use Test::MockModule; +use t::lib::Mocks; BEGIN { use_ok('C4::Barcodes::ValueBuilder'); } -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 the DB connexion and C4::Context +my $context = t::lib::Mocks::mock_dbh; # Mock data my $incrementaldata = [ diff --git a/t/Biblio.t b/t/Biblio.t index 3217e77..1d662d5 100755 --- a/t/Biblio.t +++ b/t/Biblio.t @@ -20,21 +20,14 @@ use Modern::Perl; use Test::More tests => 46; use Test::MockModule; use Test::Warn; -use DBD::Mock; +use t::lib::Mocks; BEGIN { use_ok('C4::Biblio'); } -my $context = new Test::MockModule('C4::Context'); -$context->mock( - '_new_dbh', - sub { - my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) - || die "Cannot create handle: $DBI::errstr\n"; - return $dbh; - } -); +# Mock the DB connexion and C4::Context +my $context = t::lib::Mocks::mock_dbh; my @arr; my $ret; diff --git a/t/Calendar.t b/t/Calendar.t index 8648968..7a5b657 100755 --- a/t/Calendar.t +++ b/t/Calendar.t @@ -2,11 +2,12 @@ use strict; use warnings; + use DateTime; use DateTime::Duration; use Test::More tests => 35; use Test::MockModule; -use DBD::Mock; +use t::lib::Mocks; use Koha::DateUtils; BEGIN { @@ -17,16 +18,9 @@ BEGIN { use_ok('C4::Calendar'); } -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; - } -); - +# Mock the DB connexion and C4::Context +my $module_context = t::lib::Mocks::mock_dbh; + # We need to mock the C4::Context->preference method for # simplicity and re-usability of the session definition. Any # syspref fits for syspref-agnostic tests. diff --git a/t/Images.t b/t/Images.t index c35d5bd..48fbee0 100644 --- a/t/Images.t +++ b/t/Images.t @@ -4,22 +4,18 @@ use strict; use warnings; + use Test::More tests => 7; use Test::MockModule; +use t::lib::Mocks; BEGIN { use_ok('C4::Images'); } -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 the DB connexion and C4::Context +my $context = t::lib::Mocks::mock_dbh; + my $images = [ [ 'imagenumber', 'biblionumber', 'mimetype', 'imagefile', 'thumbnail' ], [ 1, 2, 'gif', 'red', 001, 000 ], diff --git a/t/ItemType.t b/t/ItemType.t index 238fe2c..dbaf126 100755 --- a/t/ItemType.t +++ b/t/ItemType.t @@ -1,23 +1,17 @@ #!/usr/bin/perl use Modern::Perl; -use DBI; + use Test::More tests => 27; use Test::MockModule; +use t::lib::Mocks; BEGIN { use_ok('C4::ItemType'); } -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 the DB connexion and C4::Context +my $context = t::lib::Mocks::mock_dbh; # Mock data my $itemtypes = [ diff --git a/t/Koha.t b/t/Koha.t index 933fd5b..b85fe85 100755 --- a/t/Koha.t +++ b/t/Koha.t @@ -20,19 +20,14 @@ use Modern::Perl; use C4::Context; use Test::More tests => 29; use Test::MockModule; +use t::lib::Mocks; use DBD::Mock; use_ok('C4::Koha'); -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; - } -); +# Mock the DB connexion and C4::Context +my $context = t::lib::Mocks::mock_dbh; + SKIP: { diff --git a/t/Letters.t b/t/Letters.t index 4a088da..bdab8af 100755 --- a/t/Letters.t +++ b/t/Letters.t @@ -17,19 +17,13 @@ use Modern::Perl; -use DBI; use Test::MockModule; use Test::More tests => 5; use t::lib::Mocks; -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 the DB connexion and C4::Context +my $context = t::lib::Mocks::mock_dbh; + my $mock_letters = [ [ 'module', 'code', 'branchcode', 'name', 'is_html', 'title', 'content' ], [ 'blah', 'ISBN', 'NBSI', 'book', 1, 'green', 'blahblah' ], diff --git a/t/Matcher.t b/t/Matcher.t index 484afe0..e816c4c 100755 --- a/t/Matcher.t +++ b/t/Matcher.t @@ -4,22 +4,18 @@ use strict; use warnings; + use Test::More tests => 10; use Test::MockModule; +use t::lib::Mocks; BEGIN { use_ok('C4::Matcher'); } -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 the DB connexion and C4::Context +my $context = t::lib::Mocks::mock_dbh; + my $matcher = [ [ 'matcher_id', 'code', 'description', 'record_type', 'threshold' ], [ 1, 'ISBN', 'ISBN', 'red', 1 ], diff --git a/t/Members/cardnumber.t b/t/Members/cardnumber.t index bfad5ce..b312404 100644 --- a/t/Members/cardnumber.t +++ b/t/Members/cardnumber.t @@ -1,23 +1,15 @@ #!/usr/bin/env perl use Modern::Perl; -use Test::More tests => 23; +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; - } -); +# Mock the DB connexion and C4::Context +my $context = t::lib::Mocks::mock_dbh; my $dbh = C4::Context->dbh; my $rs = []; diff --git a/t/Members_AttributeTypes.t b/t/Members_AttributeTypes.t index 152fc53..c2c7d6a 100755 --- a/t/Members_AttributeTypes.t +++ b/t/Members_AttributeTypes.t @@ -4,22 +4,18 @@ use strict; use warnings; + use Test::MockModule; use Test::More tests => 9; +use t::lib::Mocks; BEGIN { use_ok('C4::Members::AttributeTypes'); } -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 the DB connexion and C4::Context +my $context = t::lib::Mocks::mock_dbh; + my $members_attributetypes = [ [ 'code', 'description', diff --git a/t/SocialData.t b/t/SocialData.t index 06705a5..5351aad 100644 --- a/t/SocialData.t +++ b/t/SocialData.t @@ -4,22 +4,18 @@ use strict; use warnings; + use Test::More tests => 5; use Test::MockModule; +use t::lib::Mocks; BEGIN { use_ok('C4::SocialData'); } -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 the DB connexion and C4::Context +my $context = t::lib::Mocks::mock_dbh; + my $socialdata = [ [ 'isbn', 'num_critics', diff --git a/t/db_dependent/Circulation_issuingrules.t b/t/db_dependent/Circulation_issuingrules.t index f5abbda..20c23c5 100644 --- a/t/db_dependent/Circulation_issuingrules.t +++ b/t/db_dependent/Circulation_issuingrules.t @@ -4,15 +4,11 @@ use Modern::Perl; use Test::More tests => 7; use Test::MockModule; -use DBI; +use t::lib::Mocks; use DateTime; -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 -}); +# Mock the DB connexion and C4::Context +my $contextmodule = t::lib::Mocks::mock_dbh; use_ok('C4::Circulation'); diff --git a/t/db_dependent/Context.t b/t/db_dependent/Context.t index 4541fa4..b3b3ebc 100755 --- a/t/db_dependent/Context.t +++ b/t/db_dependent/Context.t @@ -5,6 +5,7 @@ use warnings; use Test::More; use Test::MockModule; +use t::lib::Mocks; use vars qw($debug $koha $dbh $config $ret); use Koha::Database; @@ -63,15 +64,9 @@ 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; - } -); + +# Mock the DB connexion +t::lib::Mocks::mock_dbh; my $history; diff --git a/t/db_dependent/ReportsGuided.t b/t/db_dependent/ReportsGuided.t index d51ca8d..9c47c7f 100755 --- a/t/db_dependent/ReportsGuided.t +++ b/t/db_dependent/ReportsGuided.t @@ -4,22 +4,15 @@ 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; - } -); - +# Mock the DB connexion and C4::Context +my $context = t::lib::Mocks::mock_dbh; sub MockedIsAuthorisedValueCategory { my $authorised_value = shift; diff --git a/t/db_dependent/Search.t b/t/db_dependent/Search.t index b96cc19..e4ff161 100644 --- a/t/db_dependent/Search.t +++ b/t/db_dependent/Search.t @@ -29,6 +29,7 @@ use open ':std', ':encoding(utf8)'; use Test::More tests => 4; use Test::MockModule; +use t::lib::Mocks; use MARC::Record; use File::Spec; use File::Basename; @@ -36,7 +37,6 @@ use File::Find; use Test::Warn; use File::Temp qw/ tempdir /; use File::Path; -use DBI; our $child; our $datadir; @@ -96,11 +96,10 @@ our $QueryFuzzy = 0; our $QueryRemoveStopwords = 0; our $UseQueryParser = 0; our $marcflavour = 'MARC21'; -our $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 }); + +# Mock the DB connexion and C4::Context +my $contextmodule = t::lib::Mocks::mock_dbh; + $contextmodule->mock('preference', sub { my ($self, $pref) = @_; if ($pref eq 'marcflavour') { diff --git a/t/lib/Mocks.pm b/t/lib/Mocks.pm index 55ce9d5..cc681b5 100644 --- a/t/lib/Mocks.pm +++ b/t/lib/Mocks.pm @@ -38,13 +38,17 @@ sub mock_preference { }); } +# Set mock on database handler sub mock_dbh { + my $mock_dbh = DBI->connect( 'DBI:Mock:', '', '' ) + || die "Cannot create handle: $DBI::errstr\n"; my $context = new Test::MockModule('C4::Context'); - $context->mock( '_new_dbh', sub { - my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) - || die "Cannot create handle: $DBI::errstr\n"; - return $dbh; - } ); + $context->mock( + 'dbh', + sub { + return $mock_dbh; + } + ); return $context; } -- 2.1.0