File: 200_example_code.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 (103 lines) | stat: -rw-r--r-- 2,507 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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Fatal;

use Bread::Board;

use Bread::Board::Types;

# roles
use Bread::Board::Service;
use Bread::Board::Service::WithClass;
use Bread::Board::Service::WithDependencies;
use Bread::Board::Service::WithParameters;

# services
use Bread::Board::ConstructorInjection;
use Bread::Board::SetterInjection;
use Bread::Board::BlockInjection;
use Bread::Board::Literal;

use Bread::Board::Container;
use Bread::Board::Dependency;

use Bread::Board::Traversable;

use Bread::Board::LifeCycle::Singleton;

{
    package MyLogger;
    use Moose;

    package MyDBI;
    use Moose;

    has 'dsn'      => (is => 'ro', isa => 'Str');
    has 'username' => (is => 'ro', isa => 'Str');
    has 'password' => (is => 'ro', isa => 'Str');

    sub connect {
        my ($class, $dsn, $username, $password) = @_;
        $class->new(dsn => $dsn, username => $username, password => $password);
    }

    package MyAuthenticator;
    use Moose;
    has 'dbh'    => (is => 'ro', isa => 'MyDBI',        required => 1);
    has 'logger' => (is => 'ro', isa => 'MyLogger', required => 1);

}

my $c;
is(exception {
    $c = Bread::Board::Container->new( name => 'Application' );

    $c->add_service(
        Bread::Board::BlockInjection->new(
            name  => 'logger',
            block => sub { MyLogger->new() }
        )
    );

    $c->add_service(
        Bread::Board::BlockInjection->new(
            name  => 'db_conn',
            block => sub { MyDBI->connect('dbi:mysql:test', '', '') }
        )
    );

    $c->add_service(
        Bread::Board::BlockInjection->new(
            name  => 'authenticator',
            block => sub {
                  my $service = shift;
                  MyAuthenticator->new(
                      dbh    => $service->param('db_conn'),
                      logger => $service->param('logger')
                  );
            },
            dependencies => {
                db_conn => Bread::Board::Dependency->new(service_path => 'db_conn'),
                logger  => Bread::Board::Dependency->new(service_path => 'logger'),
            }
        )
    );

}, undef, '... container compiled successfully');

my $authenticator;
is(exception {
    $authenticator = $c->resolve( service => 'authenticator' )
}, undef, '... and the container compiled correctly');


isa_ok($authenticator, 'MyAuthenticator');
isa_ok($authenticator->dbh, 'MyDBI');
isa_ok($authenticator->logger, 'MyLogger');


done_testing;