<?php
/**
* Test: NetteDIContainerBuilder code generator.
*
* @author David Grudl
* @package NetteDI
* @subpackage UnitTests
*/
use NetteDI;
require __DIR__ . '/../bootstrap.php';
class Service
{
public $args;
public $methods;
static function create
(DIIContainer
$container =
null)
{
return new self($args);
}
function __construct()
{
}
function __call($nm, $args)
{
$this->
methods[] =
array($nm,
$args);
}
}
$builder = new DIContainerBuilder;
$builder->addDefinition('one')
->setClass('Service');
$builder->addDefinition('three')
->
setClass('Service',
array('a',
'b'));
$builder->addDefinition('four')
->
setClass('Service',
array('a',
'b'))
->
addSetup('methodA',
array('a',
'b'))
->
addSetup('@four::methodB',
array(1,
2));
$builder->addDefinition('five', null)
->setFactory('Service::create');
$six = $builder->addDefinition('six')
->
setFactory('Service::create',
array('@container',
'a',
'b'))
->
addSetup(array('@six',
'methodA'),
array('a',
'b'));
$builder->addDefinition('seven')
->
setFactory(array($six,
'create'),
array($builder,
$six))
->
addSetup(array($six,
'methodA'));
$code = (string) $builder->generateClass();
file_put_contents(TEMP_DIR . '/code.php', "<?phpn$code");
require TEMP_DIR . '/code.php';
$container = new Container;
assert::
true( $container->
getService('one') instanceof Service
);
assert::
same( array(),
$container->
getService('one')->
args );
assert::
same( null,
$container->
getService('one')->
methods );
assert::
true( $container->
getService('three') instanceof Service
);
assert::
same( array('a',
'b'),
$container->
getService('three')->
args );
assert::
same( null,
$container->
getService('three')->
methods );
assert::
true( $container->
getService('four') instanceof Service
);
assert::
same( array('a',
'b'),
$container->
getService('four')->
args );
), $container->getService('four')->methods );
assert::
true( $container->
getService('five') instanceof Service
);
assert::
same( null,
$container->
getService('five')->
methods );
assert::
true( $container->
getService('six') instanceof Service
);
assert::
same( array(array(1 =>
'a',
'b')),
$container->
getService('six')->
args );
), $container->getService('six')->methods );
assert::
true( $container->
getService('seven') instanceof Service
);
assert::
same( array(array(1 =>
$container->
getService('six'))),
$container->
getService('seven')->
args );
), $container->getService('six')->methods );