Koha/t/lib/Mocks.pm
Jonathan Druart 428a744843 Bug 13274: Mock new_dbh in t::lib::Mocks
This patch suggests to create a routine to mock C4::Context::_new_dbh.

NOTE: Works the same with and without this secondary patch.
      koha-qa tests fine. Less cutting and pasting in the future.

Signed-off-by: Mark Tompsett <mtompset@hotmail.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
2014-11-19 09:50:32 -03:00

51 lines
1.2 KiB
Perl

package t::lib::Mocks;
use Modern::Perl;
use C4::Context;
use DBD::Mock;
use Test::MockModule;
my %configs;
sub mock_config {
my $context = new Test::MockModule('C4::Context');
my ( $conf, $value ) = @_;
$configs{$conf} = $value;
$context->mock('config', sub {
my ( $self, $conf ) = @_;
if ( exists $configs{$conf} ) {
return $configs{$conf}
} else {
my $method = $context->original('config');
return $method->($self, $conf);
}
});
}
my %preferences;
sub mock_preference {
my $context = new Test::MockModule('C4::Context');
my ( $pref, $value ) = @_;
$preferences{$pref} = $value;
$context->mock('preference', sub {
my ( $self, $pref ) = @_;
if ( exists $preferences{$pref} ) {
return $preferences{$pref}
} else {
my $method = $context->original('preference');
return $method->($self, $pref);
}
});
}
sub mock_dbh {
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;
} );
return $context;
}
1;