public function testBuilderDefinitionHasMethods() { $class = new Builder\PhpClass(); $class->setName('Foo'); $definition = new BuilderDefinition(); $definition->addClass($class); $this->assertFalse($definition->hasMethods('Foo')); $class->createInjectionMethod('injectBar'); $this->assertTrue($definition->hasMethods('Foo')); }
public function testBuilderCanBuildClassWithMethods() { $class = new Builder\PhpClass(); $class->setName('Foo'); $class->addSuperType('Parent'); $injectionMethod = new Builder\InjectionMethod(); $injectionMethod->setName('injectBar'); $injectionMethod->addParameter('bar', 'Bar'); $class->addInjectionMethod($injectionMethod); $definition = new BuilderDefinition(); $definition->addClass($class); $this->assertTrue($definition->hasClass('Foo')); $this->assertEquals('__construct', $definition->getInstantiator('Foo')); $this->assertContains('Parent', $definition->getClassSupertypes('Foo')); $this->assertTrue($definition->hasMethods('Foo')); $this->assertTrue($definition->hasMethod('Foo', 'injectBar')); $this->assertContains('injectBar', $definition->getMethods('Foo')); $this->assertEquals(array('Foo::injectBar:0' => array('bar', 'Bar', true)), $definition->getMethodParameters('Foo', 'injectBar')); }
/** * Create classes from array * * @param array $builderData * @return void */ public function createClassesFromArray(array $builderData) { foreach ($builderData as $className => $classInfo) { $class = new Builder\PhpClass(); $class->setName($className); foreach ($classInfo as $type => $typeData) { switch (strtolower($type)) { case 'supertypes': foreach ($typeData as $superType) { $class->addSuperType($superType); } break; case 'instantiator': $class->setInstantiator($typeData); break; case 'methods': case 'method': foreach ($typeData as $injectionMethodName => $injectionMethodData) { $injectionMethod = new Builder\InjectionMethod(); $injectionMethod->setName($injectionMethodName); foreach ($injectionMethodData as $parameterName => $parameterType) { $parameterType = $parameterType ?: null; // force empty string to null $injectionMethod->addParameter($parameterName, $parameterType); } $class->addInjectionMethod($injectionMethod); } break; } } $this->addClass($class); } }
/** * @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); }