-
Notifications
You must be signed in to change notification settings - Fork 24
/
151_sugar_no_container.t
76 lines (52 loc) · 1.81 KB
/
151_sugar_no_container.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use Test::Moose;
use Bread::Board;
{
package FileLogger;
use Moose;
has 'log_file' => (is => 'ro', required => 1);
package MyApplication;
use Moose;
has 'logger' => (is => 'ro', isa => 'FileLogger', required => 1);
}
my $file_service = service 'log_file' => "logfile.log";
does_ok($file_service, 'Bread::Board::Service');
my $logger_service = service 'logger' => (
class => 'FileLogger',
lifecycle => 'Singleton',
dependencies => {
log_file => depends_on('log_file'),
}
);
does_ok($logger_service, 'Bread::Board::Service');
my $app_service = service 'application' => (
class => 'MyApplication',
dependencies => {
logger => depends_on('logger'),
}
);
does_ok($app_service, 'Bread::Board::Service');
my $bunyan_service = alias 'paul_bunyan' => 'logger';
does_ok($bunyan_service, 'Bread::Board::Service');
isa_ok($bunyan_service, 'Bread::Board::Service::Alias');
my $c = container 'MyApp';
isa_ok($c, 'Bread::Board::Container');
foreach ( $file_service, $logger_service, $app_service, $bunyan_service) {
$c->add_service($_);
}
my $logger = $c->resolve( service => 'logger' );
isa_ok($logger, 'FileLogger');
is($logger->log_file, 'logfile.log', '... got the right logfile dep');
is($c->fetch('logger/log_file')->service, $c->fetch('log_file'), '... got the right value');
is($c->fetch('logger/log_file')->get, 'logfile.log', '... got the right value');
my $app = $c->resolve( service => 'application' );
isa_ok($app, 'MyApplication');
isa_ok($app->logger, 'FileLogger');
is($app->logger, $logger, '... got the right logger (singleton)');
my $bunyan = $c->resolve( service => 'paul_bunyan' );
isa_ok($bunyan, 'FileLogger');
is($bunyan, $logger, 'standalone alias works.');
done_testing;