Exemplo n.º 1
0
 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')
         );
     
 }
Exemplo n.º 2
0
 public function testBuilderCanBuildFromArray()
 {
     $ini = ConfigFactory::fromFile(__DIR__ . '/../_files/sample.ini');
     $iniAsArray = $ini['section-b'];
     $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('My\\DbAdapter::__construct:0' => array('username', null, true, null), 'My\\DbAdapter::__construct:1' => array('password', null, true, null)), $definition->getMethodParameters('My\\DbAdapter', '__construct'));
     $this->assertTrue($definition->hasClass('My\\Mapper'));
     $this->assertEquals('__construct', $definition->getInstantiator('My\\Mapper'));
     $this->assertEquals(array('My\\Mapper::__construct:0' => array('dbAdapter', 'My\\DbAdapter', true, null)), $definition->getMethodParameters('My\\Mapper', '__construct'));
     $this->assertTrue($definition->hasClass('My\\Repository'));
     $this->assertEquals('__construct', $definition->getInstantiator('My\\Repository'));
     $this->assertEquals(array('My\\Repository::__construct:0' => array('mapper', 'My\\Mapper', true, null)), $definition->getMethodParameters('My\\Repository', '__construct'));
 }
Exemplo n.º 3
0
<?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!';
}