View | Details | Raw Unified | Return to bug 18182
Collapse All | Expand All

(-)a/t/db_dependent/TestBuilder.t (-1 / +36 lines)
Lines 19-29 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 11;
22
use Test::More tests => 12;
23
use Test::Warn;
23
use Test::Warn;
24
use Data::Dumper qw(Dumper);
24
use Data::Dumper qw(Dumper);
25
25
26
use Koha::Database;
26
use Koha::Database;
27
use Koha::Patrons;
27
28
28
BEGIN {
29
BEGIN {
29
    use_ok('t::lib::TestBuilder');
30
    use_ok('t::lib::TestBuilder');
Lines 343-346 subtest 'Default values' => sub { Link Here
343
344
344
$schema->storage->txn_rollback;
345
$schema->storage->txn_rollback;
345
346
347
subtest 'build_object() tests' => sub {
348
349
    plan tests => 5;
350
351
    $schema->storage->txn_begin;
352
353
    $builder = t::lib::TestBuilder->new();
354
355
    my $categorycode = $builder->build( { source => 'Category' } )->{categorycode};
356
    my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
357
358
    my $issuing_rule = $builder->build_object(
359
        {   class => 'Koha::IssuingRules',
360
            value => {
361
                categorycode => $categorycode,
362
                itemtype     => $itemtype
363
            }
364
        }
365
    );
366
367
    is( ref($issuing_rule), 'Koha::IssuingRule', 'Type is correct' );
368
    is( $issuing_rule->categorycode,
369
        $categorycode, 'Firstname correctly set' );
370
    is( $issuing_rule->itemtype, $itemtype, 'Firstname correctly set' );
371
372
    warning_is { $issuing_rule = $builder->build_object( {} ); }
373
    { carped => 'Missing class param' },
374
        'The class parameter is mandatory, raises a warning if absent';
375
    is( $issuing_rule, undef,
376
        'If the class parameter is missing, undef is returned' );
377
378
    $schema->storage->txn_rollback;
379
};
380
346
1;
381
1;
(-)a/t/lib/TestBuilder.pm (-1 / +38 lines)
Lines 1-7 Link Here
1
package t::lib::TestBuilder;
1
package t::lib::TestBuilder;
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
4
use Koha::Database;
5
use Koha::Database;
6
7
use Carp;
8
use Module::Load;
5
use String::Random;
9
use String::Random;
6
10
7
sub new {
11
sub new {
Lines 51-56 sub delete { Link Here
51
    return $rv;
55
    return $rv;
52
}
56
}
53
57
58
sub build_object {
59
    my ( $self, $params ) = @_;
60
61
    my $class = $params->{class};
62
    my $value = $params->{value};
63
64
    if ( not defined $class ) {
65
        carp "Missing class param";
66
        return;
67
    }
68
69
    load $class;
70
    my $source = $class->_type;
71
    my @pks = $self->schema->source( $class->_type )->primary_columns;
72
73
    my $hashref = $self->build({ source => $source, value => $value });
74
    my @ids;
75
76
    foreach my $pk ( @pks ) {
77
        push @ids, { $pk => $hashref->{ $pk } };
78
    }
79
80
    my $object = $class->find( @ids );
81
82
    return $object;
83
}
84
54
sub build {
85
sub build {
55
# build returns a hash of column values for a created record, or undef
86
# build returns a hash of column values for a created record, or undef
56
# build does NOT update a record, or pass back values of an existing record
87
# build does NOT update a record, or pass back values of an existing record
Lines 501-506 Note that you should wrap these actions in a transaction yourself. Link Here
501
    Realize that passing primary key values to build may result in undef
532
    Realize that passing primary key values to build may result in undef
502
    if a record with that primary key already exists.
533
    if a record with that primary key already exists.
503
534
535
=head2 build_object
536
537
Given a plural Koha::Object-derived class, it creates a random element, and
538
returns the corresponding Koha::Object.
539
540
    my $patron = $builder->build_object({ class => 'Koha::Patrons' [, value => { ... }] });
541
504
=head1 AUTHOR
542
=head1 AUTHOR
505
543
506
Yohann Dufour <yohann.dufour@biblibre.com>
544
Yohann Dufour <yohann.dufour@biblibre.com>
507
- 

Return to bug 18182