File: 061_extends_w_sugar_and_inheritance.t

package info (click to toggle)
libbread-board-perl 0.29-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 620 kB
  • sloc: perl: 5,280; makefile: 13
file content (129 lines) | stat: -rw-r--r-- 2,977 bytes parent folder | download | duplicates (5)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/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 DBH;
    use Moose;
    has 'dsn' => (is => 'ro', isa => 'Str');

    package MyApplication;
    use Moose;
    has 'logger' => (is => 'ro', isa => 'FileLogger', required => 1);
    has 'dbh'    => (is => 'ro', isa => 'DBH',        required => 1);
}

{
    package My::App;
    use Moose;
    use Bread::Board;

    extends 'Bread::Board::Container';

    has 'log_file_name' => (
        is      => 'ro',
        isa     => 'Str',
        default => 'logfile.log',
    );

    sub BUILD {
        my $self = shift;

        container $self => as {

            service 'log_file' => $self->log_file_name;

            service 'logger' => (
                class        => 'FileLogger',
                lifecycle    => 'Singleton',
                dependencies => {
                    log_file => depends_on('log_file'),
                }
            );

            service 'application' => (
                class        => 'MyApplication',
                dependencies => {
                    logger => depends_on('logger'),
                }
            );

        };
    }

    package My::App::Extended;
    use Moose;
    use Bread::Board;

    extends 'My::App';

    has 'dsn' => (
        is       => 'ro',
        isa      => 'Str',
        required => 1,
    );

    sub BUILD {
        my $self = shift;
        container $self => as {

            service 'db_conn' => (
                class        => 'DBH',
                dependencies => [
                    (service 'dsn' => $self->dsn)
                ]
            );

            service 'application' => (
                class        => 'MyApplication',
                dependencies => {
                    logger => depends_on('logger'),
                    dbh    => depends_on('db_conn'),
                }
            );
        };
    }

}

my $c = My::App::Extended->new( name => 'MyApp', dsn => 'dbi:mysql:test' );
isa_ok($c, 'My::App::Extended');
isa_ok($c, 'My::App');
isa_ok($c, 'Bread::Board::Container');

# test the first one

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 $dbh = $c->resolve( service => 'db_conn' );
isa_ok($dbh, 'DBH');

is($dbh->dsn, 'dbi:mysql:test', '... got the right dsn');

my $app = $c->resolve( service => 'application' );
isa_ok($app, 'MyApplication');

isa_ok($app->logger, 'FileLogger');
is($app->logger, $logger, '... got the right logger (singleton)');

isa_ok($app->dbh, 'DBH');
isnt($app->dbh, $dbh, '... got the right db_conn (not a singleton)');


done_testing;