public function testClassBuilderCanSpecifyClassToUseWhenCreatingInjectionMethods() { $builder = new BuilderDefinition(); $class = $builder->createClass('Foo'); $this->assertEquals('Zend\\Di\\Definition\\Builder\\InjectionMethod', $class->getMethodBuilder()); $class->setMethodBuilder('Foo'); $this->assertEquals('Foo', $class->getMethodBuilder()); }
<?php namespace MovieApp { class Lister { public $finder; public function __construct(Finder $finder) { $this->finder = $finder; } } class Finder { } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); use Zend\Di\Di, Zend\Di\DefinitionList, Zend\Di\Definition\BuilderDefinition; $builderDefinition = new BuilderDefinition(); $builderDefinition->createClassesFromArray(array('MovieApp\\Lister' => array('methods' => array('__construct' => array('repo' => 'MovieApp\\Finder'))), 'MovieApp\\Finder' => array())); $di = new Di(new DefinitionList(array($builderDefinition))); $lister = $di->get('MovieApp\\Lister'); // expression to test $works = $lister->finder instanceof MovieApp\Finder; // display result echo $works ? 'It works!' : 'It DOES NOT work!'; }
/** * @group nullargs */ public function testNullArgumentsResultInEmptyMethodParameterList() { $this->markTestIncomplete('Null arguments are currently unsupported'); $opt = new Builder\PhpClass(); $opt->setName('ZendTest\Di\TestAsset\OptionalArg'); $optCtor = new Builder\InjectionMethod(); $optCtor->setName('__construct') ->addParameter('param', null); $optInject = new Builder\InjectionMethod(); $optInject->setName('inject') ->addParameter('param1', null) ->addParameter('param2', null); $opt->addInjectionMethod($optCtor) ->addInjectionMethod($optInject); $def = new Definition(); $def->addClass($opt); $this->di->setDefinition($def); $cfg = new Configuration(array( 'instance' => array( 'alias' => array('optional' => 'ZendTest\Di\TestAsset\OptionalArg'), ), 'properties' => array( 'ZendTest\Di\TestAsset\OptionalArg' => array( 'param' => null, 'param1' => null, 'param2' => null, ), ), )); $cfg->configure($this->di); $builder = new ContainerGenerator($this->di); $builder->setContainerClass('Container'); $codeGen = $builder->getCodeGenerator(); $classBody = $codeGen->generate(); $this->assertNotContains('NULL)', $classBody, $classBody); }
public function testBuilderCanBuildFromArray() { $ini = new \Zend\Config\Ini(__DIR__ . '/../_files/sample.ini', 'section-b'); $iniAsArray = $ini->toArray(); $definitionArray = $iniAsArray['di']['definitions'][1]; unset($definitionArray['class']); $definition = new BuilderDefinition(); $definition->createClassesFromArray($definitionArray); $this->assertTrue($definition->hasClass('My\DbAdapter')); $this->assertEquals('__construct', $definition->getInstantiator('My\DbAdapter')); $this->assertEquals( array('username' => null, 'password' => null), $definition->getInjectionMethodParameters('My\DbAdapter', '__construct') ); $this->assertTrue($definition->hasClass('My\Mapper')); $this->assertEquals('__construct', $definition->getInstantiator('My\Mapper')); $this->assertEquals( array('dbAdapter' => 'My\DbAdapter'), $definition->getInjectionMethodParameters('My\Mapper', '__construct') ); $this->assertTrue($definition->hasClass('My\Repository')); $this->assertEquals('__construct', $definition->getInstantiator('My\Repository')); $this->assertEquals( array('mapper' => 'My\Mapper'), $definition->getInjectionMethodParameters('My\Repository', '__construct') ); }