Bug 10298: Mock C4::Context->preference
authorJonathan Druart <jonathan.druart@biblibre.com>
Tue, 21 May 2013 15:13:35 +0000 (17:13 +0200)
committerGalen Charlton <gmc@esilibrary.com>
Fri, 9 Aug 2013 16:34:50 +0000 (16:34 +0000)
t::lib::Mocks::Context tried to deal with preferences but did not manage
to.

This patch removes this module and add 2 routines in t::lib::Mocks in
order to mock C4::context->preference and C4::Context->config.

To test:

===START t/test.pl===

use Modern::Perl;
use t::lib::Mocks;
use C4::Context;

say "initial value for version: " . C4::Context->preference('Version');
say "initial value for language: " . C4::Context->preference('language');
t::lib::Mocks::mock_preference('Version', "new version for testing");
say "version is mocked with: " . C4::Context->preference('Version');
say "language is not yet mocked: " . C4::Context->preference('language');
t::lib::Mocks::mock_preference('language', 'new langage for testing');
t::lib::Mocks::mock_preference('Version', 'another version for testing');
say "version is mocked with another value: " . C4::Context->preference('Version');
say "language is finally mocked: " . C4::Context->preference('language');
===END===

Try to execute this file and check that the output is consistent.

Signed-off-by: Julian Maurice <julian.maurice@biblibre.com>
Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
t/lib/Mocks.pm
t/lib/Mocks/Context.pm [deleted file]

index 9e2d831..3f834d2 100644 (file)
@@ -1,23 +1,39 @@
 package t::lib::Mocks;
 
 use Modern::Perl;
+use C4::Context;
 use Test::MockModule;
-use t::lib::Mocks::Context;
 
-our (@ISA,@EXPORT,@EXPORT_OK);
-BEGIN {
-    require Exporter;
-    @ISA = qw(Exporter);
-    push @EXPORT, qw(
-        &set_solr
-        &set_zebra
-    );
+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 $context = new Test::MockModule('C4::Context');
-sub set_solr {
-    $context->mock('preference', sub { &t::lib::Mocks::Context::MockPreference( @_, "Solr", $context ) });
-}
-sub set_zebra {
-    $context->mock('preference', sub { &t::lib::Mocks::Context::MockPreference( @_, "Zebra", $context ) });
+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);
+        }
+    });
 }
+
+1;
diff --git a/t/lib/Mocks/Context.pm b/t/lib/Mocks/Context.pm
deleted file mode 100644 (file)
index 185209a..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-package t::lib::Mocks::Context;
-use t::lib::Mocks::Context;
-use C4::Context;
-
-sub MockPreference {
-    my ( $self, $syspref, $value, $mock_object ) = @_;
-    return $value if $syspref eq 'SearchEngine';
-    $mock_object->unmock("preference");
-    my $r = C4::Context->preference($syspref);
-    $mock_object->mock('preference', sub { &t::lib::Mocks::Context::MockPreference( @_, $value, $mock_object ) });
-    return $r;
-}
-1;