Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correcting primitive xml location logic #127

Merged
merged 2 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions template/types/properties/methods/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public function set<?php echo ucfirst($propertyName); ?>(array $<?php echo $prop
}
foreach($<?php echo $propertyName; ?> as $v) {
if ($v instanceof <?php echo $propertyTypeClassName; ?>) {
$this->add<?php echo ucfirst($propertyName); ?>($v, $xmlLocation);
$this-><?php echo $property->getSetterName(); ?>($v, $xmlLocation);
} else {
$this->add<?php echo ucfirst($propertyName); ?>(new <?php echo $propertyTypeClassName; ?>($v), $xmlLocation);
$this-><?php echo $property->getSetterName(); ?>(new <?php echo $propertyTypeClassName; ?>($v), $xmlLocation);
}
}
return $this;
Expand Down
16 changes: 10 additions & 6 deletions template/types/serialization/xml/serialize/body.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@

// first, marshal attribute values

// this is only used in primitive types. they have no other fields, and I am just going to assume you want it
// as an attribute if marshalled directly.
foreach ($type->getLocalProperties()->localPropertiesOfTypeKinds(includeCollections: false, kinds: null) as $property) : ?>
if (($this->_primitiveXmlLocations[self::FIELD_VALUE] ?? <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::ATTRIBUTE) === <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::ATTRIBUTE) {
$xw->writeAttribute(self::FIELD_VALUE, $this->getFormattedValue());
}
$xw->writeAttribute(self::FIELD_VALUE, $this->getFormattedValue());
<?php endforeach;

foreach ($type->getLocalProperties()->localPropertiesIterator() as $property) :
Expand Down Expand Up @@ -150,12 +150,16 @@
}
<?php endif;

// ... is NOT a typed proprety...
else: ?>
else:
// NOTE: This clause is _only_ applicable to primitive types value's. Since these are always assumed
// to be attributes, this is useless.
//
// Uncomment and implement properly if the need arises in the future.
/*?>
if (($this->_primitiveXmlLocations[self::FIELD_VALUE] ?? <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::ATTRIBUTE) === <?php echo PHPFHIR_ENUM_XML_LOCATION_ENUM; ?>::ELEMENT) {
$xw->writeSimpleElement(self::FIELD_VALUE, $this->getFormattedValue());
}
<?php endif;
<?php */ endif;

endforeach;

Expand Down
108 changes: 55 additions & 53 deletions template/types/tests/integration/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ protected function setUp(): void
* @param string $format Either xml or json
* @return string
*/
protected function fetchResource(string $format): string
protected function fetchResourceBundle(string $format): string
{
if (isset($this->_fetchedResources[$format])) {
return $this->_fetchedResources[$format];
}
$rc = $this->client->get(sprintf('/%s', <?php echo PHPFHIR_ENUM_TYPE; ?>::<?php echo $type->getConstName(false); ?>->value), ['_count' => '1', '_format' => $format]);
$rc = $this->client->get(sprintf('/%s', <?php echo PHPFHIR_ENUM_TYPE; ?>::<?php echo $type->getConstName(false); ?>->value), ['_count' => '5', '_format' => $format]);
$this->assertEmpty($rc->err, sprintf('curl error seen: %s', $rc->err));
if (404 === $rc->code) {
$this->markTestSkipped(sprintf('Endpoint "%s" has no resources of type "%s"', $this->client->_getBaseUrl(), <?php echo PHPFHIR_ENUM_TYPE; ?>::<?php echo $type->getConstName(false); ?>->value));
Expand Down Expand Up @@ -139,7 +139,7 @@ function_exists('json_last_error_msg') ? json_last_error_msg() : ('Code: '.json_

public function testXML(): void
{
$sourceXML = $this->fetchResource('xml');
$sourceXML = $this->fetchResourceBundle('xml');
try {
$bundle = <?php echo $bundleType->getClassName(); ?>::xmlUnserialize($sourceXML);
} catch(\Exception $e) {
Expand All @@ -166,36 +166,37 @@ public function testXML(): void
));
}
<?php if ($bundleEntryProperty->isCollection()) : ?>
$this->assertCount(1, $entry);
$resource = $entry[0]->getResource();
foreach ($entry as $ent) {
<?php else: ?>
$resource = $entry->getResource();
foreach ([$entry] as $ent) {
<?php endif; ?>
$resourceXmlWriter = $resource->xmlSerialize();
$resourceXml = $resourceXmlWriter->outputMemory();
try {
$type = <?php echo $type->getClassName(); ?>::xmlUnserialize($resourceXml);
} catch (\Exception $e) {
throw new AssertionFailedError(
sprintf(
'Error building type "<?php echo $type->getFHIRName(); ?>" from XML: %s; XML: %s',
$e->getMessage(),
$resourceXml
),
$e->getCode(),
$e
);
$resource = $ent->getResource();
$resourceXmlWriter = $resource->xmlSerialize();
$resourceXml = $resourceXmlWriter->outputMemory();
try {
$type = <?php echo $type->getClassName(); ?>::xmlUnserialize($resourceXml);
} catch (\Exception $e) {
throw new AssertionFailedError(
sprintf(
'Error building type "<?php echo $type->getFHIRName(); ?>" from XML: %s; XML: %s',
$e->getMessage(),
$resourceXml
),
$e->getCode(),
$e
);
}
$this->assertInstanceOf(<?php echo $type->getClassName(); ?>::class, $type);
$typeXmlWriter = $type->xmlSerialize();
$this->assertEquals($resourceXml, $typeXmlWriter->outputMemory());
$bundleXmlWriter = $bundle->xmlSerialize();
$this->assertXmlStringEqualsXmlString($sourceXML, $bundleXmlWriter->outputMemory());
}
$this->assertInstanceOf(<?php echo $type->getClassName(); ?>::class, $type);
$typeXmlWriter = $type->xmlSerialize();
$this->assertEquals($resourceXml, $typeXmlWriter->outputMemory());
$bundleXmlWriter = $bundle->xmlSerialize();
$this->assertXmlStringEqualsXmlString($sourceXML, $bundleXmlWriter->outputMemory());
}

public function testJSON(): void
{
$sourceJSON = $this->fetchResource('json');
$sourceJSON = $this->fetchResourceBundle('json');
$decoded = $this->decodeJson($sourceJSON, true);
try {
$bundle = new <?php echo $bundleType->getClassName(); ?>($decoded);
Expand Down Expand Up @@ -241,7 +242,7 @@ public function testJSON(): void

public function testValidationXML(): void
{
$sourceXML = $this->fetchResource('xml');
$sourceXML = $this->fetchResourceBundle('xml');
try {
$bundle = <?php echo $bundleType->getClassName(); ?>::xmlUnserialize($sourceXML);
} catch(\Exception $e) {
Expand Down Expand Up @@ -276,7 +277,7 @@ public function testValidationXML(): void

public function testValidationJSON(): void
{
$sourceJSON = $this->fetchResource('json');
$sourceJSON = $this->fetchResourceBundle('json');
$decoded = $this->decodeJson($sourceJSON, true);
try {
$bundle = new <?php echo $bundleType->getClassName(); ?>($decoded);
Expand Down Expand Up @@ -312,7 +313,7 @@ public function testValidationJSON(): void

public function testResponseParserXML(): void
{
$sourceXML = $this->fetchResource('xml');
$sourceXML = $this->fetchResourceBundle('xml');
$parser = new PHPFHIRResponseParser();
try {
$bundle = $parser->parse($sourceXML);
Expand Down Expand Up @@ -340,36 +341,37 @@ public function testResponseParserXML(): void
));
}
<?php if ($bundleEntryProperty->isCollection()) : ?>
$this->assertCount(1, $entry);
$resource = $entry[0]->getResource();
foreach ($entry as $ent) {
<?php else: ?>
$resource = $entry->getResource();
foreach ([$entry] as $ent) {
<?php endif; ?>
$resourceXmlWriter = $resource->xmlSerialize();
$resourceXml = $resourceXmlWriter->outputMemory();
try {
$type = <?php echo $type->getClassName(); ?>::xmlUnserialize($resourceXml);
} catch (\Exception $e) {
throw new AssertionFailedError(
sprintf(
'Error building type "<?php echo $type->getFHIRName(); ?>" from XML: %s; XML: %s',
$e->getMessage(),
$resourceXml
),
$e->getCode(),
$e
);
$resource = $ent->getResource();
$resourceXmlWriter = $resource->xmlSerialize();
$resourceXml = $resourceXmlWriter->outputMemory();
try {
$type = <?php echo $type->getClassName(); ?>::xmlUnserialize($resourceXml);
} catch (\Exception $e) {
throw new AssertionFailedError(
sprintf(
'Error building type "<?php echo $type->getFHIRName(); ?>" from XML: %s; XML: %s',
$e->getMessage(),
$resourceXml
),
$e->getCode(),
$e
);
}
$this->assertInstanceOf(<?php echo $type->getClassName(); ?>::class, $type);
$typeXmlWriter = $type->xmlSerialize();
$this->assertEquals($resourceXml, $typeXmlWriter->outputMemory());
$bundleXmlWriter = $bundle->xmlSerialize();
$this->assertXmlStringEqualsXmlString($sourceXML, $bundleXmlWriter->outputMemory());
}
$this->assertInstanceOf(<?php echo $type->getClassName(); ?>::class, $type);
$typeXmlWriter = $type->xmlSerialize();
$this->assertEquals($resourceXml, $typeXmlWriter->outputMemory());
$bundleXmlWriter = $bundle->xmlSerialize();
$this->assertXmlStringEqualsXmlString($sourceXML, $bundleXmlWriter->outputMemory());
}

public function testResponseParserJSON(): void
{
$sourceJSON = $this->fetchResource('json');
$sourceJSON = $this->fetchResourceBundle('json');
$parser = new PHPFHIRResponseParser();
try {
$bundle = $parser->parse($sourceJSON);
Expand Down Expand Up @@ -414,4 +416,4 @@ public function testResponseParserJSON(): void
}
}
<?php
return ob_get_clean();
return ob_get_clean();
Loading