public function testGetWithId()
 {
     $interface = 'zibo\\core\\di\\TestInterface';
     $id = 'id';
     $dependency = new Dependency('zibo\\core\\di\\TestObject');
     $dependency->setId($id);
     $container = new DependencyContainer();
     $container->addDependency($interface, $dependency);
     $container->addDependency($interface, new Dependency('zibo\\core\\di\\Dummy'));
     $this->di->setContainer($container);
     $instance = $this->di->get($interface, $id);
     $this->assertNotNull($instance);
     $this->assertTrue($instance instanceof TestObject);
 }
 /**
  * Adds a dependency for the provided class to this container
  * @param string $for A full class name
  * @param Dependency $dependency
  * @return null
  */
 public function addDependency($for, Dependency $dependency)
 {
     if (!String::isString($for, String::NOT_EMPTY)) {
         throw new ZiboException('Invalid for class name provided');
     }
     $id = $dependency->getId();
     if (!isset($this->dependencies[$for])) {
         $this->dependencies[$for] = array();
     }
     if ($id) {
         $this->dependencies[$for][$id] = $dependency;
     } else {
         $this->dependencies[$for][] = $dependency;
         $ids = array_keys($this->dependencies[$for]);
         $dependency->setId(array_pop($ids));
     }
 }
Ejemplo n.º 3
0
 /**
  * @dataProvider providerSetIdThrowsExceptionWhenInvalidValuePassed
  * @expectedException zibo\ZiboException
  */
 public function testSetIdThrowsExceptionWhenInvalidValuePassed($value)
 {
     $dependency = new Dependency('className');
     $dependency->setId($value);
 }