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 MyApplication; 17 use Moose; 18 has 'logger' => (is => 'ro', isa => 'FileLogger', required => 1); 19} 20 21{ 22 package My::App; 23 use Moose; 24 use Bread::Board; 25 26 extends 'Bread::Board::Container'; 27 28 has 'log_file_name' => ( 29 is => 'ro', 30 isa => 'Str', 31 default => 'logfile.log', 32 ); 33 34 sub BUILD { 35 my $self = shift; 36 37 container $self => as { 38 39 service 'log_file' => $self->log_file_name; 40 41 service 'logger' => ( 42 class => 'FileLogger', 43 lifecycle => 'Singleton', 44 dependencies => { 45 log_file => depends_on('log_file'), 46 } 47 ); 48 49 service 'application' => ( 50 class => 'MyApplication', 51 dependencies => { 52 logger => depends_on('logger'), 53 } 54 ); 55 56 }; 57 } 58} 59 60my $c1 = My::App->new( name => 'MyApp1' ); 61isa_ok($c1, 'My::App'); 62isa_ok($c1, 'Bread::Board::Container'); 63 64my $c2 = My::App->new( name => 'MyApp2', log_file_name => 'another_logfile.log' ); 65isa_ok($c2, 'My::App'); 66isa_ok($c2, 'Bread::Board::Container'); 67 68# test the first one 69 70my $logger1 = $c1->resolve( service => 'logger' ); 71isa_ok($logger1, 'FileLogger'); 72 73is($logger1->log_file, 'logfile.log', '... got the right logfile dep'); 74 75is($c1->fetch('logger/log_file')->service, $c1->fetch('log_file'), '... got the right value'); 76is($c1->fetch('logger/log_file')->get, 'logfile.log', '... got the right value'); 77 78my $app1 = $c1->resolve( service => 'application' ); 79isa_ok($app1, 'MyApplication'); 80 81isa_ok($app1->logger, 'FileLogger'); 82is($app1->logger, $logger1, '... got the right logger (singleton)'); 83 84# test the second one 85 86my $logger2 = $c2->resolve( service => 'logger' ); 87isa_ok($logger2, 'FileLogger'); 88 89is($logger2->log_file, 'another_logfile.log', '... got the right logfile dep'); 90 91is($c2->fetch('logger/log_file')->service, $c2->fetch('log_file'), '... got the right value'); 92is($c2->fetch('logger/log_file')->get, 'another_logfile.log', '... got the right value'); 93 94my $app2 = $c2->resolve( service => 'application' ); 95isa_ok($app2, 'MyApplication'); 96 97isa_ok($app2->logger, 'FileLogger'); 98is($app2->logger, $logger2, '... got the right logger (singleton)'); 99 100# make sure they share nothing 101 102isnt( $logger1, $logger2, '... these are not the same' ); 103isnt( $app1, $app2, '... these are not the same' ); 104 105done_testing; 106