/** * {@inheritDoc} */ public function generate(TypeOutlineInterface $typeOutline, ImplementationInterface $implementation) { if (!$this->isAbleToGenerate($typeOutline)) { throw new UnableToGenerateTypeException('Invalid type outline', $typeOutline); } $parentType = null; /** @var ClassOutline $typeOutline */ if ($typeOutline->getParentClass()) { $parentType = $this->schemaLinker->generateType($typeOutline->getParentClass(), $implementation); if (!$parentType instanceof AbstractClassType) { throw new UnableToGenerateTypeException('Parent type of class type must by an instance of class type', $typeOutline); } } try { $classOptions = $implementation->getClassTypeImplementation($typeOutline->getName()); $typeName = $typeOutline->getName(); if (isset($this->loadedClasses[$typeName])) { return $this->loadedClasses[$typeName]; } $classType = new ClassType($typeOutline->getName(), $classOptions->getClassName(), $parentType); $this->loadedClasses[$typeName] = $classType; foreach ($typeOutline->getProperties() as $property) { /* @var PropertyOutline $property */ $classType->addProperty($this->createPropertyDefinition($property, $classOptions, $implementation)); } unset($this->loadedClasses[$typeName]); return $classType; } catch (ClassNotFoundException $e) { $msg = 'Cannot find class name for "' . $typeOutline->getName() . '" type outline'; throw new UnableToGenerateTypeException($msg, $typeOutline, $e); } }
private function registerPredefinedClassTypes(SchemaInterface $expectedSchema) { $classType = new ClassType('Exception', '\\Exception'); $classType->addProperty(new PropertyDefinition('message', new StringType())); $cause = new PropertyDefinition('cause', $classType, true); $cause->setTargetPropertyName('previous'); $classType->addProperty($cause); $expectedSchema->registerType('Exception', $classType); }
protected function setUp() { $this->object = new ChoiceType(); $this->object->addType('int64', new IntegerType()); $classType = new ClassType('User', 'stdClass'); $classType->addProperty(new PropertyDefinition('name', new StringType())); $classType->addProperty(new PropertyDefinition('lastname', new StringType())); $this->object->addType('User', $classType); }
/** * @dataProvider isSubclassOfProvider */ public function testIsSubclassOf(ClassType $classType, $result) { $this->assertSame($result, $classType->isSubclassOf('User')); }
/** * @dataProvider creatingExceptionDataProvider */ public function testCreatingException($array, $exception) { $this->assertEqualsExceptions($exception, $this->object->create($array)); }
public function testGenerateClass() { $parentClass = new ClassOutline('vacuumBadger'); $outline = new ClassOutline('vacuum', array(), $parentClass); $outline->addProperty($this->propertyOutline('zoo')->setIsNullable(true))->addProperty($this->propertyOutline('bridge')->setIsNullable(true))->addProperty($this->propertyOutline('galley'))->addProperty($this->propertyOutline('granddaughter')->setDefaultValue('is young'))->addProperty($this->propertyOutline('everyNumber')); $this->classMap->registerClass('vacuum', 'VacuumZooBridge')->registerClass('vacuumBadger', 'VacuumBadger'); $classImplementation = new ClassTypeImplementation('vacuum'); $classImplementation->addPropertyImplementation($this->propertyImplementation('zoo'))->addPropertyImplementation($this->propertyImplementation('bridge')->setSetter('setBridge')->setGetter('getBridge'))->addPropertyImplementation($this->propertyImplementation('galley')->setSetter('setGalley'))->addPropertyImplementation($this->propertyImplementation('granddaughter')->setGetter('getGranddaughter'))->addPropertyImplementation($this->propertyImplementation('everyNumber')->setTargetPropertyName('notEveryNumber')); $this->implementation->registerClassTypeImplementation($classImplementation); $parentClassExpected = new ClassType('vacuumBadger', 'VacuumBadger'); $expected = new ClassType('vacuum', 'VacuumZooBridge', $parentClassExpected); $expected->addProperty($this->property('zoo')->setIsNullable(true))->addProperty($this->property('bridge')->setGetterName('getBridge')->setSetterName('setBridge')->setIsNullable(true))->addProperty($this->property('galley')->setSetterName('setGalley'))->addProperty($this->property('granddaughter')->setDefaultValue('is young')->setGetterName('getGranddaughter'))->addProperty($this->property('everyNumber')->setTargetPropertyName('notEveryNumber')); $result = $this->object->generate($outline, $this->implementation); $this->assertEquals($expected, $result); }