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');
$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;
package t::lib::TestBuilder;
use Modern::Perl;
+
use Koha::Database;
+
+use Carp;
+use Module::Load;
use String::Random;
sub new {
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
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>