diff --git a/template/core/classes/class_response_parser.php b/template/core/classes/class_response_parser.php index 34db0945..e419bd8f 100644 --- a/template/core/classes/class_response_parser.php +++ b/template/core/classes/class_response_parser.php @@ -48,12 +48,12 @@ class private const XML_START = ['<']; private const JSON_START = ['{', '[']; - /** @var \ $config */ + /** @var getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?> $config */ private $config; /** * Constructor - * @param null|\ $config + * @param null|getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?> $config */ public function __construct(null| $config = null) { @@ -64,7 +64,7 @@ public function __construct(null| $config } /** - * @return \ + * @return getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?> */ public function getConfig(): @@ -77,7 +77,7 @@ public function getConfig(): * Attempts to parse the provided input into FHIR objects. * * @param null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument $input - * @return null|\ + * @return null|getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?> * @throws \Exception */ @@ -97,7 +97,7 @@ public function parse(null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument /** * @param array $input - * @return null|\ + * @return null|getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?> */ public function parseArray(array $input): null| @@ -127,7 +127,7 @@ public function parseArray(array $input): null| + * @return null|getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?> */ public function parseStdClass(\stdClass $input): null| @@ -138,7 +138,7 @@ public function parseStdClass(\stdClass $input): null| + * @return null|getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?> */ public function parseSimpleXMLElement(\SimpleXMLElement $input): null| @@ -154,12 +154,12 @@ public function parseSimpleXMLElement(\SimpleXMLElement $input): null|getPrintableStringInput($input->saveXML()) )); } - return $fhirType::xmlUnserialize($input, $this->config); + return $fhirType::xmlUnserialize($input, null, $this->config); } /** * @param \DOMDocument $input - * @return null|\ + * @return null|getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?> */ public function parseDOMDocument(\DOMDocument $input): null| @@ -170,7 +170,7 @@ public function parseDOMDocument(\DOMDocument $input): null| + * @return null|getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?> */ public function parseObject(\stdClass|\SimpleXMLElement|\DOMDocument $input): null| @@ -187,7 +187,7 @@ public function parseObject(\stdClass|\SimpleXMLElement|\DOMDocument $input): nu /** * @param string $input - * @return null|\ + * @return null|getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?> * @throws \Exception */ @@ -199,7 +199,7 @@ public function parseXml(string $input): null| + * @return null|getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?> */ public function parseJson(string $input): null| @@ -220,7 +220,7 @@ public function parseJson(string $input): null| + * @return null|getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?> * @throws \Exception */ diff --git a/template/types/tests/integration/class.php b/template/types/tests/integration/class.php index 297234a4..d8bfb6e3 100644 --- a/template/types/tests/integration/class.php +++ b/template/types/tests/integration/class.php @@ -70,6 +70,7 @@ use getFullyQualifiedClassName(false); ?>; use getFullyQualifiedName(false, PHPFHIR_CLASSNAME_DEBUG_CLIENT); ?>; use getFullyQualifiedName(false, PHPFHIR_ENUM_TYPE); ?>; +use getFullyQualifiedName(false, PHPFHIR_CLASSNAME_RESPONSE_PARSER); ?>; use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\TestCase; @@ -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 "getFHIRName(); ?>" from XML: %s; Returned XML: %s', + $e->getMessage(), + $sourceXML + ), + $e->getCode(), + $e + ); + } + $this->assertInstanceOf(getClassName(); ?>::class, $bundle); + $entry = $bundle->getEntry(); +isCollection()) : ?> + if (0 === count($entry)) { + + if (null === $entry) { + + $this->markTestSkipped(sprintf( + 'Provided test endpoint "getTestEndpoint(); ?>" does not have any "getFHIRName(); ?>" entries to test against (returned xml: %s)', + $sourceXML + )); + } +isCollection()) : ?> + $this->assertCount(1, $entry); + $resource = $entry[0]->getResource(); + + $resource = $entry->getResource(); + + $resourceXmlWriter = $resource->xmlSerialize(); + $resourceXml = $resourceXmlWriter->outputMemory(); + try { + $type = getClassName(); ?>::xmlUnserialize($resourceXml); + } catch (\Exception $e) { + throw new AssertionFailedError( + sprintf( + 'Error building type "getFHIRName(); ?>" from XML: %s; XML: %s', + $e->getMessage(), + $resourceXml + ), + $e->getCode(), + $e + ); + } + $this->assertInstanceOf(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 "getFHIRName(); ?>" from JSON: %s; Returned JSON: %s', + $e->getMessage(), + $sourceJSON + ), + $e->getCode(), + $e + ); + } + $entry = $bundle->getEntry(); +isCollection()) : ?> + if (0 === count($entry)) { + + if (null === $entry) { + + $this->markTestSkipped(sprintf( + 'Provided test endpoint "getTestEndpoint(); ?>" does not have any 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 \"getClassName(); ?>\" does not match input: %s\nSource:\n%s\nRe-encoded:\n%s\n", + $e->getMessage(), + $sourceJSON, + $reEncoded + ), + $e->getCode(), + $e + ); + } + } }