File: 100_clone_w_constructor_injection.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 (71 lines) | stat: -rw-r--r-- 2,139 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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Scalar::Util qw(refaddr);

use Bread::Board;

{
    package Test::Class;
    use Moose;
    has 'dep' => ( is => 'ro', isa => 'Int' );
}

my $board = Bread::Board::Container->new( name => 'app' );
isa_ok($board, 'Bread::Board::Container');

$board->add_service(
    Bread::Board::ConstructorInjection->new(
        name  => 'test',
        class => 'Test::Class',
        dependencies => {
            dep => Bread::Board::Dependency->new(service_path => '/dep'),
        },
    )
);
ok($board->has_service('test'), '... got the test service');
isa_ok($board->get_service('test'), 'Bread::Board::ConstructorInjection');

# clone ...

my $board2 = $board->clone;
isa_ok($board2, 'Bread::Board::Container');
isnt($board, $board2, '... they are not the same instance');

ok($board2->has_service('test'), '... got the test service');
isa_ok($board2->get_service('test'), 'Bread::Board::ConstructorInjection');

isnt($board->get_service('test'), $board2->get_service('test'), '... not the same test services');

# add dep services ...

$board->add_service(
    Bread::Board::Literal->new(name => 'dep', value => 1)
);
ok($board->has_service('dep'), '... got the dep service');
isa_ok($board->get_service('dep'), 'Bread::Board::Literal');

ok(!$board2->has_service('dep'), '... board2 does not have the dep service');

$board2->add_service(
    Bread::Board::Literal->new(name => 'dep', value => 2)
);
ok($board2->has_service('dep'), '... got the dep service');
isa_ok($board2->get_service('dep'), 'Bread::Board::Literal');

isnt($board->get_service('dep'), $board2->get_service('dep'), '... not the same dep services');

# test them ...

is($board->fetch('/dep')->get(), 1, '... got correct dep');
is($board->fetch('/test')->get()->dep, 1, '... test uses dep');
is(refaddr $board->fetch('/test')->parent, refaddr $board, '... got the right board');

is($board2->fetch('/dep')->get(), 2, '... got correct dep');
is($board2->fetch('/test')->get()->dep, 2, '... test uses dep');
is(refaddr $board2->fetch('/test')->parent, refaddr $board2, '... got the right board');

done_testing;