Bug 18182: Make TestBuilder capable of returning Koha::Object
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 28 Feb 2017 12:08:38 +0000 (09:08 -0300)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 21 Apr 2017 00:10:49 +0000 (00:10 +0000)
This patch adds a new method to t::lib::TestBuilder so it can return
Koha::Object-derived objects. The new method is called ->build_object
and requires the plural of the target class to be passed.

'class' is a mandatory param, and a warning is raised and undef is
returned if absent.

It accepts 'value' as the original ->build() method, and that is passed as-is
to ->build().

To test:
- Apply the patches
- Run:
  $ sudo koha-shell kohadev
 k$ cd kohaclone
 k$ prove t/db_dependent/TestBuilder.t
=> SUCCESS: Tests pass!
- Sign off :-D

Sponsored-by: ByWater Solutions
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
t/db_dependent/TestBuilder.t
t/lib/TestBuilder.pm

index 1305718..2a1ab17 100644 (file)
 
 use Modern::Perl;
 
-use Test::More tests => 11;
+use Test::More tests => 12;
 use Test::Warn;
 use Data::Dumper qw(Dumper);
 
 use Koha::Database;
+use Koha::Patrons;
 
 BEGIN {
     use_ok('t::lib::TestBuilder');
@@ -345,4 +346,38 @@ subtest 'Default values' => sub {
 
 $schema->storage->txn_rollback;
 
+subtest 'build_object() tests' => sub {
+
+    plan tests => 5;
+
+    $schema->storage->txn_begin;
+
+    $builder = t::lib::TestBuilder->new();
+
+    my $categorycode = $builder->build( { source => 'Category' } )->{categorycode};
+    my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
+
+    my $issuing_rule = $builder->build_object(
+        {   class => 'Koha::IssuingRules',
+            value => {
+                categorycode => $categorycode,
+                itemtype     => $itemtype
+            }
+        }
+    );
+
+    is( ref($issuing_rule), 'Koha::IssuingRule', 'Type is correct' );
+    is( $issuing_rule->categorycode,
+        $categorycode, 'Firstname correctly set' );
+    is( $issuing_rule->itemtype, $itemtype, 'Firstname correctly set' );
+
+    warning_is { $issuing_rule = $builder->build_object( {} ); }
+    { carped => 'Missing class param' },
+        'The class parameter is mandatory, raises a warning if absent';
+    is( $issuing_rule, undef,
+        'If the class parameter is missing, undef is returned' );
+
+    $schema->storage->txn_rollback;
+};
+
 1;
index 1f3ab51..6509aee 100644 (file)
@@ -1,7 +1,11 @@
 package t::lib::TestBuilder;
 
 use Modern::Perl;
+
 use Koha::Database;
+
+use Carp;
+use Module::Load;
 use String::Random;
 
 sub new {
@@ -51,6 +55,33 @@ sub delete {
     return $rv;
 }
 
+sub build_object {
+    my ( $self, $params ) = @_;
+
+    my $class = $params->{class};
+    my $value = $params->{value};
+
+    if ( not defined $class ) {
+        carp "Missing class param";
+        return;
+    }
+
+    load $class;
+    my $source = $class->_type;
+    my @pks = $self->schema->source( $class->_type )->primary_columns;
+
+    my $hashref = $self->build({ source => $source, value => $value });
+    my @ids;
+
+    foreach my $pk ( @pks ) {
+        push @ids, { $pk => $hashref->{ $pk } };
+    }
+
+    my $object = $class->find( @ids );
+
+    return $object;
+}
+
 sub build {
 # build returns a hash of column values for a created record, or undef
 # build does NOT update a record, or pass back values of an existing record
@@ -501,6 +532,13 @@ Note that you should wrap these actions in a transaction yourself.
     Realize that passing primary key values to build may result in undef
     if a record with that primary key already exists.
 
+=head2 build_object
+
+Given a plural Koha::Object-derived class, it creates a random element, and
+returns the corresponding Koha::Object.
+
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' [, value => { ... }] });
+
 =head1 AUTHOR
 
 Yohann Dufour <yohann.dufour@biblibre.com>