1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7use Test::Moose;
8use Test::Fatal;
9
10use Bread::Board::BlockInjection;
11use Bread::Board::SetterInjection;
12use Bread::Board::Literal;
13
14{
15    package Needle;
16    use Moose;
17
18    package Mexican::Black::Tar;
19    use Moose;
20
21    package Addict;
22    use Moose;
23
24    has 'needle' => (is => 'rw');
25    has 'spoon'  => (is => 'rw');
26    has 'stash'  => (is => 'rw');
27}
28
29my $s = Bread::Board::BlockInjection->new(
30    name  => 'William',
31    class => 'Addict',
32    block => sub {
33        my $s = shift;
34        $s->class->new(%{ $s->params });
35    },
36    dependencies => {
37        needle => Bread::Board::SetterInjection->new(name => 'spike', class => 'Needle'),
38        spoon  => Bread::Board::Literal->new(name => 'works', value => 'Spoon!'),
39    },
40    parameters => {
41        stash => { isa => 'Mexican::Black::Tar' }
42    }
43);
44isa_ok($s, 'Bread::Board::BlockInjection');
45does_ok($s, 'Bread::Board::Service::WithDependencies');
46does_ok($s, 'Bread::Board::Service::WithParameters');
47does_ok($s, 'Bread::Board::Service');
48
49{
50    my $i = $s->get(stash => Mexican::Black::Tar->new);
51
52    isa_ok($i, 'Addict');
53    isa_ok($i->needle, 'Needle');
54    is($i->spoon, 'Spoon!', '... got our literal service');
55    isa_ok($i->stash, 'Mexican::Black::Tar');
56
57    {
58        my $i2 = $s->get(stash => Mexican::Black::Tar->new);
59        isnt($i, $i2, '... calling it again returns an new object');
60    }
61}
62
63is($s->name, 'William', '... got the right name');
64is($s->class, 'Addict', '... got the right class');
65
66my $deps = $s->dependencies;
67is_deeply([ sort keys %$deps ], [qw/needle spoon/], '... got the right dependency keys');
68
69my $needle = $s->get_dependency('needle');
70isa_ok($needle, 'Bread::Board::Dependency');
71isa_ok($needle->service, 'Bread::Board::SetterInjection');
72
73is($needle->service->name, 'spike', '... got the right name');
74is($needle->service->class, 'Needle', '... got the right class');
75
76my $spoon = $s->get_dependency('spoon');
77isa_ok($spoon, 'Bread::Board::Dependency');
78isa_ok($spoon->service, 'Bread::Board::Literal');
79
80is($spoon->service->name, 'works', '... got the right name');
81is($spoon->service->value, 'Spoon!', '... got the right literal value');
82
83my $params = $s->parameters;
84is_deeply([ sort keys %$params ], [qw/stash/], '... got the right paramter keys');
85is_deeply($params->{stash}, { isa => 'Mexican::Black::Tar' }, '... got the right parameter spec');
86
87## check some errors
88
89isnt(exception {
90    $s->get;
91}, undef, '... you must supply the required parameters');
92
93isnt(exception {
94    $s->get(stash => []);
95}, undef, '... you must supply the required parameters as correct types');
96
97isnt(exception {
98    $s->get(stash => Mexican::Black::Tar->new, foo => 10);
99}, undef, '... you must supply the required parameters (and no more)');
100
101done_testing;
102