File: 046_custom_parameter_service.t

package info (click to toggle)
libbread-board-perl 0.29-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 620 kB
  • sloc: perl: 5,280; makefile: 13
file content (134 lines) | stat: -rw-r--r-- 3,199 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!perl
use warnings;
use strict;
use Test::More;
use Bread::Board;

our $at_underscore;
our $params;

{
    package Foo;
    use Moose;
    has myattr => (
        isa => 'Int',
        is => 'rw',
    );
    has foo => (
        is       => 'rw',
        isa      => 'Str',
        required => 1,
    );
}

{
    package MyCustomWithParametersService;
    use Moose::Role;
    with 'Bread::Board::Service::WithParameters'
        =>  { -excludes => '_build_parameters' };

    sub _build_parameters {
        {
            foo => {
                isa      => 'Str',
                required => 1,
            }
        }
    }

    no Moose::Role;
}

{
    package MyCustomBlockInjection;
    use Moose;
    extends 'Bread::Board::BlockInjection';
    with 'MyCustomWithParametersService',
         'Bread::Board::Service::WithDependencies';

    around get => sub {
        my $orig = shift;
        my $self = shift;

        $at_underscore = \@_;
        $params        = $self->params;

        return $self->$orig(@_);
    };

    __PACKAGE__->meta->make_immutable;

    no Moose;
}

{
    package MyCustomConstructorInjection;
    use Moose;
    extends 'Bread::Board::ConstructorInjection';
    with 'Bread::Board::Service::WithClass',
         'MyCustomWithParametersService',
         'Bread::Board::Service::WithDependencies';

    around get => sub {
        my $orig = shift;
        my $self = shift;

        $at_underscore = \@_;
        $params        = $self->params;

        return $self->$orig(@_);
    };

    __PACKAGE__->meta->make_immutable;

    no Moose;
}

my $c = Bread::Board::Container->new( name => 'TestApp' );
$c->add_service(
    MyCustomConstructorInjection->new(
        class => 'Foo',
        name  => 'foo_ci',
        dependencies => {
            myattr => Bread::Board::Literal->new(
                name  => 'true',
                value => 1
            )
        }
    )
);
$c->add_service(
    MyCustomBlockInjection->new(
        block => sub {
            my $s = shift;
            my $foo = $s->param('foo_ci');
            $foo->myattr(2);
            return $foo;
        },
        name  => 'foo_bi',
        dependencies => {
            foo_ci => Bread::Board::Dependency->new(
                service_path => 'foo_ci',
                service_params => { foo => 'baz' },
            )
        },
    )
);
eval { $c->resolve(service => 'foo_ci') };
like($@, qr/'foo' missing/, q/Can't resolve foo_ci without mandatory attribute/);
ok(my $foo_ci = $c->resolve(service => 'foo_ci', parameters => { foo => 'bar' }), 'got the constructor injection right');
isa_ok($foo_ci, 'Foo');
is_deeply($params, { myattr => 1, foo => 'bar' }, 'params ok');
is_deeply($at_underscore, [ 'foo', 'bar' ], '@_ ok');

$foo_ci->myattr(2);
$foo_ci->foo('baz');

eval { $c->resolve(service => 'foo_bi') };
like($@, qr/'foo' missing/, q/Can't resolve foo_bi without mandatory attribute/);
ok(my $foo_bi = $c->resolve(service => 'foo_bi', parameters => { foo => 'baz' }), 'got the block injection right');
isa_ok($foo_bi, 'Foo');
is_deeply($params, { foo_ci => $foo_ci, foo => 'baz' }, 'params ok');
is_deeply($at_underscore, [ 'foo', 'baz' ], '@_ ok');

done_testing;