1#!/usr/bin/perl 2 3use strict; 4use warnings; 5 6use Test::More; 7use Scalar::Util qw(refaddr); 8 9use Bread::Board; 10 11{ 12 package Test::Class; 13 use Moose; 14 has 'dep' => ( is => 'ro', isa => 'Int' ); 15} 16 17my $board = Bread::Board::Container->new( name => 'app' ); 18isa_ok($board, 'Bread::Board::Container'); 19 20$board->add_service( 21 Bread::Board::ConstructorInjection->new( 22 name => 'test', 23 class => 'Test::Class', 24 dependencies => { 25 dep => Bread::Board::Dependency->new(service_path => '/dep'), 26 }, 27 ) 28); 29ok($board->has_service('test'), '... got the test service'); 30isa_ok($board->get_service('test'), 'Bread::Board::ConstructorInjection'); 31 32# clone ... 33 34my $board2 = $board->clone; 35isa_ok($board2, 'Bread::Board::Container'); 36isnt($board, $board2, '... they are not the same instance'); 37 38ok($board2->has_service('test'), '... got the test service'); 39isa_ok($board2->get_service('test'), 'Bread::Board::ConstructorInjection'); 40 41isnt($board->get_service('test'), $board2->get_service('test'), '... not the same test services'); 42 43# add dep services ... 44 45$board->add_service( 46 Bread::Board::Literal->new(name => 'dep', value => 1) 47); 48ok($board->has_service('dep'), '... got the dep service'); 49isa_ok($board->get_service('dep'), 'Bread::Board::Literal'); 50 51ok(!$board2->has_service('dep'), '... board2 does not have the dep service'); 52 53$board2->add_service( 54 Bread::Board::Literal->new(name => 'dep', value => 2) 55); 56ok($board2->has_service('dep'), '... got the dep service'); 57isa_ok($board2->get_service('dep'), 'Bread::Board::Literal'); 58 59isnt($board->get_service('dep'), $board2->get_service('dep'), '... not the same dep services'); 60 61# test them ... 62 63is($board->fetch('/dep')->get(), 1, '... got correct dep'); 64is($board->fetch('/test')->get()->dep, 1, '... test uses dep'); 65is(refaddr $board->fetch('/test')->parent, refaddr $board, '... got the right board'); 66 67is($board2->fetch('/dep')->get(), 2, '... got correct dep'); 68is($board2->fetch('/test')->get()->dep, 2, '... test uses dep'); 69is(refaddr $board2->fetch('/test')->parent, refaddr $board2, '... got the right board'); 70 71done_testing; 72