IOC

 view release on metacpan or  search on metacpan

t/030_IOC_Service_test.t  view on Meta::CPAN

#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 22;
use Test::Exception;

BEGIN {    
    use_ok('IOC::Service');   
    use_ok('IOC::Container');    
}

{ # create a package for a dummy service
    package Logger;
    sub new {
        my $class = shift;
        return bless {} => $class;
    }
}

# this function will test that
# we got a container in our 
# construction block
sub create_logger {
    my ($container) = @_;
    isa_ok($container, 'IOC::Container');
    return Logger->new();
}

can_ok("IOC::Service", 'new');

# check errors first

throws_ok {
    IOC::Service->new()
} "IOC::InsufficientArguments", '... a service must be created with a name';

throws_ok {
    IOC::Service->new("Fail")
} "IOC::InsufficientArguments", '... a service must be created with a CODE block';

throws_ok {
    IOC::Service->new("Fail", "Fail")
} "IOC::InsufficientArguments", '... a service must be created with a CODE block';

throws_ok {
    IOC::Service->new("Fail", [])
} "IOC::InsufficientArguments", '... a service must be created with a CODE block';

# create the service

my $service = IOC::Service->new('logger' => \&create_logger );
isa_ok($service, 'IOC::Service');

# check the interface

can_ok($service, 'setContainer');
can_ok($service, 'instance');

# check instance errors

throws_ok {
    $service->instance()
} "IOC::IllegalOperation", '... cannot create an instance without a container';

# check set Container errors

throws_ok {
    $service->setContainer()
} "IOC::InsufficientArguments", '... must have a container object to set the container';

throws_ok {
    $service->setContainer("Fail")
} "IOC::InsufficientArguments", '... must have a container object to set the container';

throws_ok {
    $service->setContainer([])



( run in 1.664 second using v1.01-cache-2.11-cpan-5735350b133 )