public function generateData(Type $type) { switch ($type->getName()) { case 'String': return $this->generateString(15); case 'Integer': return mt_rand(0, 99999) * (mt_rand(0, 1) === 0 ? -1 : 1); // negativ, positiv, 0-xxxx // negativ, positiv, 0-xxxx case 'Array': // wir füllen den array mit random strings, oder whatever, numerische schlüssel $array = array(); if ($type->isTyped()) { for ($i = 1; $i <= 15; $i++) { $array[] = $this->generateData($type->getType()); } } else { $randomTypes = array(new StringType(), new IntegerType(), new BooleanType()); for ($i = 1; $i <= 15; $i++) { $array[] = $this->generateDataRandomType($randomTypes); } } return $array; case 'Boolean': return mt_rand(0, 1) === 1; } throw new \Psc\Code\NotImplementedException('Kein switch für randomData für: ' . $type->getName()); }
public function getRule(WebforgeType $type) { if (!$type instanceof \Webforge\Types\ValidationType) { throw new TypeExportException($type . ' muss das Interface ValidationType implementieren'); } return $type->getValidatorRule($this); }
/** * @param mixed $value */ public function toString($value, Type $type = NULL) { if (isset($type)) { // wenn wir den typ checken statt der value geht auch (int) $timestamp, new DateTimeType() // das ist cooler if ($type instanceof Types\DateTimeType) { if ($value) { return $value->i18n_format($this->dateTimeFormat); } } if ($type instanceof Types\DateType) { if ($value) { return $value->i18n_format($this->dateFormat); } } if ($type instanceof Types\EntityType && $value != NULL) { return $value->getContextLabel(); } $that = $this; if ($type instanceof Types\CollectionType) { return \Psc\FE\Helper::listObjects($value, ', ', function ($innerValue) use($that, $type) { // ersetzt den getter try { return $that->toString($innerValue, $type->getType()); } catch (NotTypedException $e) { // was tun wenn wir den inner type nicht kennen? return $that->toString($innerValue); } }); } if ($type instanceof Types\I18nType) { return \Psc\FE\Helper::listObjects($value, "<br />", function ($innerValue, $key) use($that, $type) { // ersetzt den getter try { return '[' . $key . '] ' . $that->toString($innerValue, $type->getType()); } catch (NotTypedException $e) { // was tun wenn wir den inner type nicht kennen? return $that->toString($innerValue); } }); } if ($type instanceof Types\BooleanType) { if ($value == TRUE) { return '<span class="ui-icon ui-icon-check"></span>'; } else { return ''; } } if ($type instanceof Types\MarkupTextType) { return TPL::MiniMarkup($value); } } return (string) $value; }
/** * @return Psc\CMS\Component */ public function inferComponent(Type $type) { if (array_key_exists($type->getName(), $this->explicitMappings)) { $component = $this->createComponent($this->explicitMappings[$type->getName()]); } elseif ($type instanceof MappedComponentType) { $component = $type->getMappedComponent($this); } else { throw NoComponentFoundException::build("Zum Type '%s' konnte keine Komponente ermittelt werden. Das einfachste ist im Type \\Webforge\\Types\\MappedComponentType zu implementieren.", $type->getName())->set('type', $type)->end(); } $component->setType($type); $this->manager->dispatchEvent(self::EVENT_COMPONENT_MAPPED, compact('component', 'type'), $this); return $component; }
/** * $this->invalidArgument(1, $param1, 'PositiveInteger', __FUNCTION__); * @param string|Type $expected der Type oder ein String der den Typ beschreibt * @cc-ignore */ protected function invalidArgument($num, $actual, $expected, $function) { $class = Code::getClass($this); $namespace = Code::getNamespace($class); $context = $class . '::' . $function . '()'; $expandType = function ($type) use($class, $namespace) { if (is_string($type)) { $type = Type::create($type); if ($type instanceof ObjectType) { $type->expandNamespace($namespace); } } return $type; }; if (is_array($expected)) { $composite = Type::create('Composite'); foreach ($expected as $type) { $composite->addComponent($expandType($type)); } $expected = $composite; } else { $expected = $expandType($expected); } return Exception::invalidArgument($num, $context, $actual, $expected); }
public function testConstruct() { $objectExporter = new ObjectExporter(); $t = function ($type) { return Type::create($type); }; $set = new Set(); // set implements Walkable $set->set('name', 'oids_pages', $t('String')); $j1 = new Set(); $j1->set('name', 'oid', $t('String')); $j1->set('referencedColumnName', 'oid', $t('String')); $j1->set('onDelete', 'cascade', $t('String')); $j2 = new Set(); $j2->set('name', 'page_id', $t('String')); $j2->set('referencedColumnName', 'id', $t('String')); $j2->set('onDelete', 'cascade', $t('String')); $set->set('joinColumns', array($j1, $j2), new ArrayType(new ObjectType(new GClass('Psc\\Data\\Walkable')))); $set->set('inverseJoinColumns', array($j2, $j1), new ArrayType(new ObjectType(new GClass('Psc\\Data\\Walkable')))); $this->assertInstanceOf('stdClass', $object = $objectExporter->walkWalkable($set)); $this->assertInternalType('array', $object->joinColumns); $this->assertInternalType('array', $object->inverseJoinColumns); $this->assertInstanceOf('stdClass', $object->joinColumns[0]); $this->assertInstanceOf('stdClass', $object->inverseJoinColumns[0]); }
protected function createType($typeDef) { if (mb_strpos($typeDef, '\\') !== FALSE) { $typeDef = sprintf('Object<%s>', $typeDef); } return Type::create($typeDef); }
public static function Column($name, $type, $label = NULL) { if (is_string($type)) { $type = Type::create($type); } return new Column($name, $type, $label); }
public function testRespectsTypeValidationRule() { $c = $this->createComponent(); $typeMock = $this->getMock('Webforge\\Types\\EnumType', array('getValidatorRule'), array(Type::create('String'), array('v1', 'v2'))); $typeMock->expects($this->once())->method('getValidatorRule')->with($this->isInstanceOf('Psc\\Data\\Type\\TypeRuleMapper')); $c->setType($typeMock); $c->init(); }
public function testAddProperty() { $cb = $this->classBuilder; $cb->addProperty('thumb')->setType(Type::create('Image'))->addProperty('image')->setType(new ImageType())->addProperty('text')->setType(MarkupTextType::create())->addProperty('link')->setType(Type::create('Link')); $this->assertCount(4, $cb->getProperties()); $this->assertInstanceof($this->cProperty, $cb->getProperty('image')); return $cb; }
public function getWalkableType($field) { if ($field === 'date') { return Type::create('Integer'); } elseif ($field === 'timezone') { return Type::create('String'); } }
public function __construct($propertyPlural, $propertySingular, $itemType = NULL) { $this->propertyPlural = $propertyPlural; $this->propertySingular = $propertySingular; $this->collectionType = Type::create('Array'); if (isset($itemType)) { $this->setItemType(Type::create($itemType)); } }
public function testImplementationInnerTypeConstruct() { $type = Type::create('Collection', CollectionType::DOCTRINE_ARRAY_COLLECTION, new ObjectType(new GClass('Psc\\Doctrine\\Entity'))); $this->assertEquals('Doctrine\\Common\\Collections\\ArrayCollection', $type->getClass()->getFQN()); $this->assertTrue($type->isTyped()); $this->assertInstanceOf('Webforge\\Types\\ObjectType', $type->getType()); $this->assertEquals('Psc\\Doctrine\\Entity', $type->getType()->getClassFQN()); return $type; }
/** * Wandelt einen Doctrine Type in einen Psc Type um * * @return Webforge\Types\Type */ public function getPscType($dcTypeConstant) { if ($dcTypeConstant === NULL) { throw new TypeConversionException('dcTypeConstant kann nicht NULL sein'); } $flip = array_flip($this->casts); if (!array_key_exists($dcTypeConstant, $flip)) { throw TypeConversionException::typeTarget('Doctrine-Type: ' . $dcTypeConstant, 'Psc-Type'); } return Type::create($flip[$dcTypeConstant]); }
public static function provideToString() { $tests = array(); $test = function ($expectedValue, $value, $type) use(&$tests) { $tests[] = array($expectedValue, $value, Type::create($type)); }; $test('21.11.1984 00:02', new \Psc\DateTime\DateTime('21.11.1984 00:02:02'), 'DateTime'); $test('21.11.1984', new \Psc\DateTime\Date('21.11.1984'), 'Date'); $test('eins, zwei, drei', new \Psc\Data\ArrayCollection(array('eins', 'zwei', 'drei')), 'Collection'); return $tests; }
public static function provideMappings() { $mapper = new TypeRuleMapper(); $test = function ($type, $rule) use(&$tests, $mapper) { $type = WebforgeType::create($type); $tests[] = array($mapper->createRule($rule), $type); }; $test('Id', 'Psc\\Form\\IdValidatorRule'); $test('String', 'Psc\\Form\\NesValidatorRule'); return $tests; }
public static function provideExportException() { $tests = array(); $test = function ($type) use(&$tests) { if (!$type instanceof Type) { $type = Type::create($type); } $tests[] = array($type); }; $test(new \Psc\Data\Type\FailingTestType()); return $tests; }
public static function provideTestMapping() { $tests = array(); $test = function ($type, $expectedComponent) use(&$tests) { $type = Type::create($type); $tests[] = array($expectedComponent, $type); }; $test('String', 'TextField'); $test('Email', 'EmailField'); $test('DateTime', 'DateTimePicker'); $test('Date', 'DatePicker'); // Mapped Component $test('SmallInteger', 'SmallIntegerField'); $test('Birthday', 'BirthdayPicker'); return $tests; }
public static function provideMappings() { $ruleMapper = new ComponentRuleMapper(); $tests = array(); $test = function ($component, $rule) use(&$tests, $ruleMapper) { $tests[] = array($ruleMapper->createRule($rule), $component); }; // delegates to typeRuleMapper $component = new IntegerField(); $component->setType(Type::create('Id')); $test($component, 'Psc\\Form\\IdValidatorRule'); $component = new TextField(); $component->setType(Type::create('String')); $test($component, 'Psc\\Form\\NesValidatorRule'); // eigene Componenten-Rules $component = new DatePicker(); $component->init(); $test($component, 'Psc\\Form\\DateValidatorRule'); return $tests; }
public function createWalkable() { $t = function ($type) { return Type::create($type); }; $set = new Set(); // set implements Walkable $set->set('name', 'oids_pages', $t('String')); $j1 = new Set(); $j1->set('name', 'oid', $t('String')); $j1->set('referencedColumnName', 'oid', $t('String')); $j1->set('onDelete', 'cascade', $t('String')); $j2 = new Set(); $j2->set('name', 'page_id', $t('String')); $j2->set('referencedColumnName', 'id', $t('String')); $j2->set('onDelete', 'cascade', $t('String')); $set->set('joinColumns', array($j1, $j2), new ArrayType(new ObjectType(new GClass('Psc\\Data\\Walkable')))); $set->set('inverseJoinColumns', array($j2, $j1), new ArrayType(new ObjectType(new GClass('Psc\\Data\\Walkable')))); return $set; }
protected static function createData(array $data, $spec) { $set = new Data(); $f = function ($field, $type = 'String') use(&$set) { $set->setFieldType($field, Type::create($type)); }; if ($spec === 'person') { $f('firstName'); $f('name'); $f('telephone'); $f('company.title'); $f('address.street'); $f('address.zip', 'PositiveInteger'); $f('address.countryCode'); $f('address.city'); $f('taxId'); } elseif ($spec === 'recipient') { $f('company.title'); $f('company.department'); $f('company.co'); $f('address.street'); $f('address.zip', 'PositiveInteger'); $f('address.countryCode'); $f('address.city'); } elseif ($spec === 'invoice') { $f('dateTime', 'DateTime'); $f('labelId'); $f('place'); // wenn nicht gesetzt wird person address city genommen $f('performancePeriod'); $f('text', 'MarkupText'); } else { throw new \Psc\Exception('unbekannte spec: ' . $spec); } // validiert ein bissl foreach ($data as $field => $value) { $set->set($field, $value); } return $set; }
public function testAcceptance() { $gClass = new GClass(__NAMESPACE__ . '\\Table' . uniqid('table')); $this->extension->compile($gClass); // quicktest $this->test->gClass($gClass)->hasProperty('columns')->hasMethod('getColumns')->hasMethod('setColumns', array('columns'))->hasMethod('removeColumn', array('column'))->hasMethod('getColumn', array('key'))->hasMethod('addColumn', array('column'))->hasMethod('hasColumn', array('column')); // acceptance $classWriter = new ClassWriter($gClass); $classWriter->write($file = $this->newFile('out.php'), array(), ClassWriter::OVERWRITE); require $file; $table = $gClass->newInstance(); $c1 = new Column('c1', Type::create('String'), 'Spalte 1'); $c2 = new Column('c2', Type::create('Integer'), 'Spalte 2'); $this->assertInternalType('array', $table->getColumns(), 'getColumns gibt keinen Array zurück'); $this->assertCount(0, $table->getColumns()); $table->setColumns(array($c1, $c2)); $this->assertEquals(array($c1, $c2), $table->getColumns(), 'setColumns() funktioniert nicht richtig'); $this->assertTrue($table->hasColumn($c1), 'hasColumn(c1) gibt nicht true zurück'); $this->assertTrue($table->hasColumn($c2), 'hasColumn(c2) gibt nicht true zurück'); $table->removeColumn($c1); $this->assertEquals(array($c2), $table->getColumns(), 'c2 wurde nicht entfernt'); $this->assertFalse($table->hasColumn($c1), 'hasColumn(c1) muss false sein nach removen'); $this->assertTrue($table->hasColumn($c2), 'hasColumn(c2) muss noch true sein nach dem removen von c1'); }
public function addControlColumn() { return $this->createColumn('ctrl', Type::create('String'), self::EMPTY_LABEL, array(), function (Entity $entity) { return \Psc\UI\fHTML::checkbox('egp-ctrl[]', FALSE, $entity->getIdentifier())->addClass('\\Psc\\identifier-checkbox'); }); }
public function setUp() { $this->chainClass = 'Psc\\Data\\Table'; parent::setUp(); $this->table = new Table(array(Table::Column('num', Type::create('Integer')), Table::Column('ident', Type::create('String')), Table::Column('Sound', Type::create('String')))); }
public function createType($typeName) { if ($typeName instanceof Type) { return $typeName; } return Type::create($typeName); }
protected function getRandomData($typeName) { if (!$typeName instanceof Type) { $type = Type::create($typeName); } else { $type = $typeName; } $generator = $this->createRandomGenerator(); return $generator->generateData($type); }
protected function assertDocType($docType, WebforgeType $type) { $msg = 'DokumentationsType des Type ' . $type . ' ist nicht korrekt'; $this->assertEquals($docType, $type->getName(WebforgeType::CONTEXT_DOCBLOCK), $msg); $this->assertEquals($docType, $type->getDocType(), $msg); }
public function testInvalidArgumentException() { $this->assertEquals(Exception::invalidArgument(1, 'Psc\\ASimpleObjectClass::doSomething()', 'wrongValue', Type::create('Composite')->setComponents(Type::create('Integer'), Type::create('Object<Psc\\NiceClass>'))), $this->simpleObject->doSomething('wrongValue')); }
public function testUpdateIndex_WrongUniqueConstraint() { $this->setExpectedException('InvalidArgumentException'); $this->validator->updateIndex(new UniqueConstraint('test', array('episode_id' => Type::create('Integer'))), array()); }
public function walkListValue(array $list, Type $type) { $walkedList = array(); foreach ($list as $value) { $walkedList[] = $this->walkPHPValue($value, $type instanceof EnclosingType && $type->isTyped() ? $type->getType() : NULL, 'list'); } return $this->codeWriter->writeList($walkedList, $type); }