Skip to content

Commit

Permalink
Adding response parser tests to integration class (#120)
Browse files Browse the repository at this point in the history
* Adding response parser tests to integration class

* fixing xml unserialization issue in parser

* name cleanup
  • Loading branch information
dcarbone committed Jun 8, 2024
1 parent ca3968e commit 4be8325
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 13 deletions.
26 changes: 13 additions & 13 deletions template/core/classes/class_response_parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ class <?php echo PHPFHIR_CLASSNAME_RESPONSE_PARSER; ?>
private const XML_START = ['<'];
private const JSON_START = ['{', '['];

/** @var \<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_CLASSNAME_CONFIG; ?> $config */
/** @var <?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?> $config */
private <?php echo PHPFHIR_CLASSNAME_CONFIG; ?> $config;

/**
* <?php echo PHPFHIR_CLASSNAME_RESPONSE_PARSER; ?> Constructor
* @param null|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_CLASSNAME_CONFIG; ?> $config
* @param null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?> $config
*/
public function __construct(null|<?php echo PHPFHIR_CLASSNAME_CONFIG; ?> $config = null)
{
Expand All @@ -64,7 +64,7 @@ public function __construct(null|<?php echo PHPFHIR_CLASSNAME_CONFIG; ?> $config
}

/**
* @return \<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_CLASSNAME_CONFIG; ?>
* @return <?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?>

*/
public function getConfig(): <?php echo PHPFHIR_CLASSNAME_CONFIG; ?>
Expand All @@ -77,7 +77,7 @@ public function getConfig(): <?php echo PHPFHIR_CLASSNAME_CONFIG; ?>
* Attempts to parse the provided input into FHIR objects.
*
* @param null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument $input
* @return null|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_TYPE; ?>
* @return null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?>

* @throws \Exception
*/
Expand All @@ -97,7 +97,7 @@ public function parse(null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument

/**
* @param array $input
* @return null|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_TYPE; ?>
* @return null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?>

*/
public function parseArray(array $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
Expand Down Expand Up @@ -127,7 +127,7 @@ public function parseArray(array $input): null|<?php echo PHPFHIR_INTERFACE_TYPE

/**
* @param \stdClass $input
* @return null|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_TYPE; ?>
* @return null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?>

*/
public function parseStdClass(\stdClass $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
Expand All @@ -138,7 +138,7 @@ public function parseStdClass(\stdClass $input): null|<?php echo PHPFHIR_INTERFA

/**
* @param \SimpleXMLElement $input
* @return null|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_TYPE; ?>
* @return null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?>

*/
public function parseSimpleXMLElement(\SimpleXMLElement $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
Expand All @@ -154,12 +154,12 @@ public function parseSimpleXMLElement(\SimpleXMLElement $input): null|<?php echo
$this->getPrintableStringInput($input->saveXML())
));
}
return $fhirType::xmlUnserialize($input, $this->config);
return $fhirType::xmlUnserialize($input, null, $this->config);
}

/**
* @param \DOMDocument $input
* @return null|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_TYPE; ?>
* @return null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?>

*/
public function parseDOMDocument(\DOMDocument $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
Expand All @@ -170,7 +170,7 @@ public function parseDOMDocument(\DOMDocument $input): null|<?php echo PHPFHIR_I

/**
* @param \stdClass|\SimpleXMLElement|\DOMDocument $input
* @return null|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_TYPE; ?>
* @return null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?>

*/
public function parseObject(\stdClass|\SimpleXMLElement|\DOMDocument $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
Expand All @@ -187,7 +187,7 @@ public function parseObject(\stdClass|\SimpleXMLElement|\DOMDocument $input): nu

/**
* @param string $input
* @return null|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_TYPE; ?>
* @return null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?>

* @throws \Exception
*/
Expand All @@ -199,7 +199,7 @@ public function parseXml(string $input): null|<?php echo PHPFHIR_INTERFACE_TYPE;

/**
* @param string $input
* @return null|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_TYPE; ?>
* @return null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?>

*/
public function parseJson(string $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
Expand All @@ -220,7 +220,7 @@ public function parseJson(string $input): null|<?php echo PHPFHIR_INTERFACE_TYPE

/**
* @param string $input
* @return null|\<?php echo ('' === $namespace ? '' : "{$namespace}\\") . PHPFHIR_INTERFACE_TYPE; ?>
* @return null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?>

* @throws \Exception
*/
Expand Down
104 changes: 104 additions & 0 deletions template/types/tests/integration/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
use <?php echo $type->getFullyQualifiedClassName(false); ?>;
use <?php echo $config->getFullyQualifiedName(false, PHPFHIR_CLASSNAME_DEBUG_CLIENT); ?>;
use <?php echo $config->getFullyQualifiedName(false, PHPFHIR_ENUM_TYPE); ?>;
use <?php echo $config->getFullyQualifiedName(false, PHPFHIR_CLASSNAME_RESPONSE_PARSER); ?>;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -308,6 +309,109 @@ public function testValidationJSON(): void
$this->markTestSkipped(sprintf('Validation errors seen: %s', json_encode($errs, JSON_PRETTY_PRINT)));
}
}

public function testResponseParserXML(): void
{
$sourceXML = $this->fetchResource('xml');
$parser = new PHPFHIRResponseParser();
try {
$bundle = $parser->parse($sourceXML);
} catch(\Exception $e) {
throw new AssertionFailedError(
sprintf(
'Error building type "<?php echo $bundleType->getFHIRName(); ?>" from XML: %s; Returned XML: %s',
$e->getMessage(),
$sourceXML
),
$e->getCode(),
$e
);
}
$this->assertInstanceOf(<?php echo $bundleType->getClassName(); ?>::class, $bundle);
$entry = $bundle->getEntry();
<?php if ($bundleEntryProperty->isCollection()) : ?>
if (0 === count($entry)) {
<?php else : ?>
if (null === $entry) {
<?php endif; ?>
$this->markTestSkipped(sprintf(
'Provided test endpoint "<?php echo $config->getTestEndpoint(); ?>" does not have any "<?php echo $type->getFHIRName(); ?>" entries to test against (returned xml: %s)',
$sourceXML
));
}
<?php if ($bundleEntryProperty->isCollection()) : ?>
$this->assertCount(1, $entry);
$resource = $entry[0]->getResource();
<?php else: ?>
$resource = $entry->getResource();
<?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
);
}
$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');
$parser = new PHPFHIRResponseParser();
try {
$bundle = $parser->parse($sourceJSON);
} catch(\Exception $e) {
throw new AssertionFailedError(
sprintf(
'Error building type "<?php echo $bundleType->getFHIRName(); ?>" from JSON: %s; Returned JSON: %s',
$e->getMessage(),
$sourceJSON
),
$e->getCode(),
$e
);
}
$entry = $bundle->getEntry();
<?php if ($bundleEntryProperty->isCollection()) : ?>
if (0 === count($entry)) {
<?php else : ?>
if (null === $entry) {
<?php endif; ?>
$this->markTestSkipped(sprintf(
'Provided test endpoint "<?php echo $config->getTestEndpoint(); ?>" does not have any <?php echo $type->getFHIRName(); ?>" entries to test against (returned json: %s)',
$sourceJSON
));
}

$reEncoded = json_encode($bundle);
try {
$this->assertJsonStringEqualsJsonString($sourceJSON, $reEncoded);
} catch (\Exception $e) {
throw new AssertionFailedError(
sprintf(
"json_encode output of \"<?php echo $type->getClassName(); ?>\" does not match input: %s\nSource:\n%s\nRe-encoded:\n%s\n",
$e->getMessage(),
$sourceJSON,
$reEncoded
),
$e->getCode(),
$e
);
}
}
}
<?php
return ob_get_clean();

0 comments on commit 4be8325

Please sign in to comment.