public function testCreateFromDataModelNoFieldIdentifier() { $valueCollection = new ValueCollection(); $value = new Value(15, BaseType::INTEGER); $value->setPartOfRecord(true); $valueCollection[] = $value; $this->setExpectedException('\\InvalidArgumentException'); $record = RecordContainer::createFromDataModel($valueCollection); }
public function testMarshallBaseType() { $fieldIdentifier = 'goodIdentifier'; $baseType = BaseType::INTEGER; $value = 666; $component = new Value($value, $baseType, $fieldIdentifier); $component->setPartOfRecord(true); // to get the baseType written in output. $marshaller = $this->getMarshallerFactory('2.1.0')->createMarshaller($component); $element = $marshaller->marshall($component); $this->assertInstanceOf('\\DOMElement', $element); $this->assertEquals('value', $element->nodeName); $this->assertEquals($fieldIdentifier, $element->getAttribute('fieldIdentifier')); $this->assertEquals('integer', $element->getAttribute('baseType')); $this->assertEquals($value . '', $element->nodeValue); }
/** * Unmarshall a DOMElement object corresponding to a QTI Value element. * * @param \DOMElement $element A DOMElement object. * @return \qtism\data\QtiComponent A Value object. * @throws \qtism\data\storage\xml\marshalling\UnmarshallingException If the 'baseType' attribute is not a valid QTI baseType. */ protected function unmarshall(DOMElement $element) { $object = null; if (($baseType = static::getDOMElementAttributeAs($element, 'baseType', 'string')) !== null) { // baseType attribute is set -> part of a record. $baseTypeCst = BaseType::getConstantByName($baseType); if ($baseTypeCst !== false) { $object = new Value(Utils::stringToDatatype(trim($element->nodeValue), $baseTypeCst), $baseTypeCst); $object->setPartOfRecord(true); } else { $msg = "The 'baseType' attribute value ('{$value}') is not a valid QTI baseType in element '" . $element->localName . "'."; throw new UnmarshallingException($msg, $element); } } else { // baseType attribute not set -> not part of a record. $nodeValue = trim($element->nodeValue); if ($nodeValue !== '') { // Try to use the marshaller as parametric to know how to unserialize the value. if ($this->getBaseType() != -1) { $object = new Value(Utils::stringToDatatype($nodeValue, $this->getBaseType()), $this->getBaseType()); } else { // value used as plain string (at your own risks). $object = new Value($nodeValue); } } else { $msg = "The element '" . $element->localName . "' has no value."; throw new UnmarshallingException($msg, $element); } } if (($value = static::getDOMElementAttributeAs($element, 'fieldIdentifier', 'string')) !== null) { $object->setFieldIdentifier($value); } return $object; }