1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7use Test::Moose;
8
9use Bread::Board;
10
11{
12    package Foo::Role;
13    use Moose::Role;
14
15    package My::Foo;
16    use Moose;
17    with 'Foo::Role';
18}
19
20# give infer() enough information to create
21# the service all by itself ...
22{
23    my $c = container 'MyTestContainer' => as {
24        typemap 'Foo::Role' => infer( class => 'My::Foo' );
25    };
26
27    ok($c->has_type_mapping_for('Foo::Role'), '... have a type mapping for Foo::Role');
28    does_ok(
29        $c->get_type_mapping_for('Foo::Role'),
30        'Bread::Board::Service'
31    );
32
33    {
34        my $foo = $c->resolve( type => 'Foo::Role' );
35        isa_ok($foo, 'My::Foo');
36    }
37}
38
39# don't give infer enough information
40# and make it figure it out for itself
41{
42    my $c = container 'MyTestContainer' => as {
43        typemap 'My::Foo' => infer;
44    };
45
46    ok($c->has_type_mapping_for('My::Foo'), '... have a type mapping for My::Foo');
47    does_ok(
48        $c->get_type_mapping_for('My::Foo'),
49        'Bread::Board::Service'
50    );
51
52    {
53        my $foo = $c->resolve( type => 'My::Foo' );
54        isa_ok($foo, 'My::Foo');
55    }
56}
57
58{
59    my $c = container 'MyTestContainer' => as {
60        typemap 'My::Foo' => infer(
61            dependencies => { thing => service('thing' => 'THING') }
62        );
63    };
64
65    ok($c->has_type_mapping_for('My::Foo'), '... have a type mapping for My::Foo');
66    my $s = $c->get_type_mapping_for('My::Foo');
67    does_ok($s, 'Bread::Board::Service');
68    ok($s->has_dependency('thing'), "service_args were passed along");
69
70    {
71        my $foo = $c->resolve( type => 'My::Foo' );
72        isa_ok($foo, 'My::Foo');
73    }
74}
75
76{
77    package My::ConstructorInjection;
78    use Moose;
79    extends 'Bread::Board::ConstructorInjection';
80}
81
82{
83    my $c = container 'MyTestContainer' => as {
84        typemap 'My::Foo' => infer(
85            My::ConstructorInjection->new(
86                name  => 'foo',
87                class => 'My::Foo',
88            )
89        );
90    };
91
92    ok($c->has_type_mapping_for('My::Foo'), '... have a type mapping for My::Foo');
93    my $s = $c->get_type_mapping_for('My::Foo');
94    does_ok($s, 'Bread::Board::Service');
95    isa_ok($s, 'My::ConstructorInjection');
96
97    {
98        my $foo = $c->resolve( type => 'My::Foo' );
99        isa_ok($foo, 'My::Foo');
100    }
101}
102
103done_testing;
104