<?php
/**
* Test: NetteDIContainer dynamic usage.
*
* @author David Grudl
* @package NetteDI
* @subpackage UnitTests
*/
use NetteDIContainer;
require __DIR__ . '/../bootstrap.php';
class Service
{
}
$one = new Service;
$two = new Service;
$container = new Container;
$container->addService('one', $one);
$container->addService('two', $two);
assert::
true( $container->
hasService('one') );
assert::
true( $container->
isCreated('one') );
assert::
true( $container->
hasService('two') );
assert::
false( $container->
hasService('undefined') );
assert::
same( $one,
$container->
getService('one') );
assert::
same( $two,
$container->
getService('two') );
// class name
$builder = $container->addService('three', 'Service');
assert::
true( $container->
hasService('three') );
assert::
true( $container->
getService('three') instanceof Service
);
assert::
same( $container->
getService('three'),
$container->
getService('three') );
// shared
// factory
$container->addService('four', function($container){
assert::
true( $container instanceof Container
);
return new Service;
});
assert::
true( $container->
hasService('four') );
assert::
false( $container->
isCreated('four') );
assert::
true( $container->
getService('four') instanceof Service
);
assert::
true( $container->
isCreated('four') );
assert::
same( $container->
getService('four'),
$container->
getService('four') );
// shared
// bad factory
try {
$container->addService('five', function($container){});
$container->getService('five');
assert::
fail('Expected exception');
} catch (Exception $e) {
assert::
exception('NetteUnexpectedValueException',
"Unable to create service 'five', value returned by factory '%a%' is not object.",
$e );
}