1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7
8use Bread::Board;
9
10{
11    package FileLogger;
12    use Moose;
13    has 'log_file' => (is => 'ro', required => 1);
14
15    package MyApplication;
16    use Moose;
17    has 'logger' => (is => 'ro', isa => 'FileLogger', required => 1);
18}
19
20my $c = container 'MyApp' => as {
21
22    service 'log_file' => "logfile.log";
23
24    service 'logger' => (
25        class        => 'FileLogger',
26        lifecycle    => 'Singleton',
27        dependencies => [
28            depends_on('log_file'),
29        ]
30    );
31
32    service 'application' => (
33        class        => 'MyApplication',
34        dependencies => [
35            depends_on('logger'),
36        ]
37    );
38
39};
40
41my $logger = $c->resolve( service => 'logger' );
42isa_ok($logger, 'FileLogger');
43
44is($logger->log_file, 'logfile.log', '... got the right logfile dep');
45
46is($c->fetch('logger/log_file')->service, $c->fetch('log_file'), '... got the right value');
47is($c->fetch('logger/log_file')->get, 'logfile.log', '... got the right value');
48
49my $app = $c->resolve( service => 'application' );
50isa_ok($app, 'MyApplication');
51
52isa_ok($app->logger, 'FileLogger');
53is($app->logger, $logger, '... got the right logger (singleton)');
54
55done_testing;
56