1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7use Test::Moose;
8
9use Bread::Board;
10
11{
12    package FileLogger;
13    use Moose;
14    has 'log_file' => (is => 'ro', required => 1);
15
16    package DBH;
17    use Moose;
18    has 'dsn' => (is => 'ro', isa => 'Str');
19
20    package MyApplication;
21    use Moose;
22    has 'logger' => (is => 'ro', isa => 'FileLogger', required => 1);
23    has 'dbh'    => (is => 'ro', isa => 'DBH',        required => 1);
24}
25
26{
27    package My::App;
28    use Moose;
29    use Bread::Board;
30
31    extends 'Bread::Board::Container';
32
33    has 'log_file_name' => (
34        is      => 'ro',
35        isa     => 'Str',
36        default => 'logfile.log',
37    );
38
39    sub BUILD {
40        my $self = shift;
41
42        container $self => as {
43
44            service 'log_file' => $self->log_file_name;
45
46            service 'logger' => (
47                class        => 'FileLogger',
48                lifecycle    => 'Singleton',
49                dependencies => {
50                    log_file => depends_on('log_file'),
51                }
52            );
53
54            service 'application' => (
55                class        => 'MyApplication',
56                dependencies => {
57                    logger => depends_on('logger'),
58                }
59            );
60
61        };
62    }
63
64    package My::App::Extended;
65    use Moose;
66    use Bread::Board;
67
68    extends 'My::App';
69
70    has 'dsn' => (
71        is       => 'ro',
72        isa      => 'Str',
73        required => 1,
74    );
75
76    sub BUILD {
77        my $self = shift;
78        container $self => as {
79
80            service 'db_conn' => (
81                class        => 'DBH',
82                dependencies => [
83                    (service 'dsn' => $self->dsn)
84                ]
85            );
86
87            service 'application' => (
88                class        => 'MyApplication',
89                dependencies => {
90                    logger => depends_on('logger'),
91                    dbh    => depends_on('db_conn'),
92                }
93            );
94        };
95    }
96
97}
98
99my $c = My::App::Extended->new( name => 'MyApp', dsn => 'dbi:mysql:test' );
100isa_ok($c, 'My::App::Extended');
101isa_ok($c, 'My::App');
102isa_ok($c, 'Bread::Board::Container');
103
104# test the first one
105
106my $logger = $c->resolve( service => 'logger' );
107isa_ok($logger, 'FileLogger');
108
109is($logger->log_file, 'logfile.log', '... got the right logfile dep');
110
111is($c->fetch('logger/log_file')->service, $c->fetch('log_file'), '... got the right value');
112is($c->fetch('logger/log_file')->get, 'logfile.log', '... got the right value');
113
114my $dbh = $c->resolve( service => 'db_conn' );
115isa_ok($dbh, 'DBH');
116
117is($dbh->dsn, 'dbi:mysql:test', '... got the right dsn');
118
119my $app = $c->resolve( service => 'application' );
120isa_ok($app, 'MyApplication');
121
122isa_ok($app->logger, 'FileLogger');
123is($app->logger, $logger, '... got the right logger (singleton)');
124
125isa_ok($app->dbh, 'DBH');
126isnt($app->dbh, $dbh, '... got the right db_conn (not a singleton)');
127
128
129done_testing;
130