1#!/usr/bin/perl 2 3use strict; 4use warnings; 5 6use Test::More; 7use Test::Fatal; 8 9use Bread::Board; 10use Bread::Board::LifeCycle::Singleton::WithParameters; 11 12{ 13 package Foo; 14 use Moose; 15 has 'bar' => (is => 'ro', isa => 'Int', required => 1); 16 has 'baz' => (is => 'ro', isa => 'Str', required => 1); 17} 18 19my $c = container 'MyApp' => as { 20 21 service 'foo' => ( 22 lifecycle => 'Singleton::WithParameters', 23 class => 'Foo', 24 parameters => { 25 bar => { isa => 'Int' }, 26 baz => { isa => 'Str' }, 27 } 28 ); 29 30}; 31 32my $foo; 33is(exception { 34 $foo = $c->resolve( service => 'foo', parameters => { bar => 10, baz => 'BAZ' } ); 35}, undef, '... got the service correctly'); 36isa_ok($foo, 'Foo'); 37is($foo->bar, 10, '... got the right parameter value'); 38is($foo->baz, 'BAZ', '... got the right parameter value'); 39 40# this is the same instance ... 41my $foo2; 42is(exception { 43 $foo2 = $c->resolve( service => 'foo', parameters => { bar => 10, baz => 'BAZ' } ); 44}, undef, '... got the service correctly'); 45isa_ok($foo2, 'Foo'); 46is($foo2->bar, 10, '... got the right parameter value'); 47is($foo2->baz, 'BAZ', '... got the right parameter value'); 48 49# this will be different instance ... 50my $foo3; 51is(exception { 52 $foo3 = $c->resolve( service => 'foo', parameters => { bar => 20, baz => 'BAZ' } ); 53}, undef, '... got the service correctly'); 54isa_ok($foo3, 'Foo'); 55is($foo3->bar, 20, '... got the right parameter value'); 56is($foo3->baz, 'BAZ', '... got the right parameter value'); 57 58# this is the same instance ... 59my $foo4; 60is(exception { 61 $foo4 = $c->resolve( service => 'foo', parameters => { bar => 10, baz => 'BAZ' } ); 62}, undef, '... got the service correctly'); 63isa_ok($foo4, 'Foo'); 64is($foo4->bar, 10, '... got the right parameter value'); 65is($foo4->baz, 'BAZ', '... got the right parameter value'); 66 67# this will be different instance ... 68my $foo5; 69is(exception { 70 $foo5 = $c->resolve( service => 'foo', parameters => { bar => 10, baz => 'Baz' }); 71}, undef, '... got the service correctly'); 72isa_ok($foo5, 'Foo'); 73is($foo5->bar, 10, '... got the right parameter value'); 74is($foo5->baz, 'Baz', '... got the right parameter value'); 75 76# confirm our assumptions ... 77 78is($foo, $foo2, '... they are the same instances (same params)'); 79isnt($foo, $foo3, '... they are not the same instances (diff params)'); 80is($foo, $foo4, '... they are the same instances (same params)'); 81isnt($foo, $foo5, '... they are the same instances (same params)'); 82isnt($foo3, $foo5, '... they are the same instances (same params)'); 83 84done_testing; 85