From 86392fbe8eba4c9d093e2d7831a311f78fef7265 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, along with the field to be used as id on searching the object. The plural is because the singular form is not aware of its plural (I always thought we should move plural stuff into the singular class, this is one of the reasons). The id is required because that's the way to find the recently created object. If any of those parameters is ommited, a warning is raised and undef is returned. 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 --- t/db_dependent/TestBuilder.t | 36 +++++++++++++++++++++++++++++++++++- t/lib/TestBuilder.pm | 30 ++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/TestBuilder.t b/t/db_dependent/TestBuilder.t index 512a8b48c5..badf90cf37 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'); @@ -343,4 +344,37 @@ subtest 'Default values' => sub { $schema->storage->txn_rollback; +subtest 'build_object() tests' => sub { + + plan tests => 7; + + $schema->storage->txn_begin; + + $builder = t::lib::TestBuilder->new(); + my $patron = $builder->build_object( + { class => 'Koha::Patrons', + id => 'borrowernumber', + value => { firstname => 'Tomasito', surname => 'None' } + } + ); + + is( ref($patron), 'Koha::Patron', 'Type is correct' ); + is( $patron->firstname, 'Tomasito', 'Firstname correctly set' ); + is( $patron->surname, 'None', 'Firstname correctly set' ); + + warning_is + { $patron = $builder->build_object({ class => 'Koha::Patrons' }); } + { carped => 'Missing id param' }, + 'The id parameter is mandatory, raises a warning if absent'; + is( $patron, undef, 'If the id parameter is missing, undef is returned' ); + + warning_is + { $patron = $builder->build_object({ id => 'borrowernumber' }); } + { carped => 'Missing class param' }, + 'The class parameter is mandatory, raises a warning if absent'; + is( $patron, 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..25a8d7e7c0 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,32 @@ sub delete { return $rv; } +sub build_object { + my ( $self, $params ) = @_; + + my $class = $params->{class}; + my $id = $params->{id}; + my $value = $params->{value}; + + if ( not defined $class ) { + carp "Missing class param"; + return; + } + + if ( not defined $id ) { + carp "Missing id param"; + return; + } + + load $class; + my $source = $class->_type; + + my $hashref = $self->build({ source => $source, value => $value }); + my $object = $class->find( $hashref->{$id} ); + + 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 -- 2.12.0