From e2d14666d9762a6742a326b67f4296f6269c4d4c Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 28 Feb 2017 09:08:38 -0300 Subject: [PATCH] Bug 18182: Make TestBuilder capable of returning Koha::Object 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 Signed-off-by: Tomas Cohen Arazi Signed-off-by: Jonathan Druart Signed-off-by: Kyle M Hall --- t/db_dependent/TestBuilder.t | 37 ++++++++++++++++++++++++++++++++++- t/lib/TestBuilder.pm | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/TestBuilder.t b/t/db_dependent/TestBuilder.t index 1305718d81..2a1ab178a6 100644 --- a/t/db_dependent/TestBuilder.t +++ b/t/db_dependent/TestBuilder.t @@ -19,11 +19,12 @@ 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; diff --git a/t/lib/TestBuilder.pm b/t/lib/TestBuilder.pm index 1f3ab51c6b..6509aee9da 100644 --- a/t/lib/TestBuilder.pm +++ b/t/lib/TestBuilder.pm @@ -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 -- 2.39.5