Translation for 3.20.11
[koha.git] / t / lib / Mocks.pm
blob55ce9d5db59add3f458cb2aa9238f4608a912369
1 package t::lib::Mocks;
3 use Modern::Perl;
4 use C4::Context;
6 use DBD::Mock;
7 use Test::MockModule;
9 my %configs;
10 sub mock_config {
11 my $context = new Test::MockModule('C4::Context');
12 my ( $conf, $value ) = @_;
13 $configs{$conf} = $value;
14 $context->mock('config', sub {
15 my ( $self, $conf ) = @_;
16 if ( exists $configs{$conf} ) {
17 return $configs{$conf}
18 } else {
19 my $method = $context->original('config');
20 return $method->($self, $conf);
22 });
25 my %preferences;
26 sub mock_preference {
27 my $context = new Test::MockModule('C4::Context');
28 my ( $pref, $value ) = @_;
29 $preferences{$pref} = $value;
30 $context->mock('preference', sub {
31 my ( $self, $pref ) = @_;
32 if ( exists $preferences{$pref} ) {
33 return $preferences{$pref}
34 } else {
35 my $method = $context->original('preference');
36 return $method->($self, $pref);
38 });
41 sub mock_dbh {
42 my $context = new Test::MockModule('C4::Context');
43 $context->mock( '_new_dbh', sub {
44 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
45 || die "Cannot create handle: $DBI::errstr\n";
46 return $dbh;
47 } );
48 return $context;