Skip to content

Commit

Permalink
recursively import nodes during xml serialization, integration test c…
Browse files Browse the repository at this point in the history
…lass cleanup.
  • Loading branch information
dcarbone committed Apr 25, 2024
1 parent 0b72b94 commit f152d20
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 155 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ jobs:
- name: 'Install Composer deps'
# language=sh
run: |
rm composer.lock
composer install --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
- name: 'Generate code'
Expand Down
2 changes: 1 addition & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function writeFhirTestFiles(): void
foreach ($testTypes as $testType) {
// skip domain resources
// TODO(@dcarbone): why did you do this.
if (PHPFHIR_TEST_TYPE_INTEGRATION === $testType && !$type->isDomainResource()) {
if (PHPFHIR_TEST_TYPE_INTEGRATION === $testType && !$type->isResourceType()) {
continue;
}

Expand Down
6 changes: 2 additions & 4 deletions src/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,11 @@ public function hasPrimitiveContainerParent(): bool
}

/**
* TODO: super hacky.
*
* @return bool
*/
public function isDomainResource(): bool
public function isResourceType(): bool
{
return str_contains($this->getFullyQualifiedNamespace(false), 'DomainResource');
return str_contains($this->getFullyQualifiedNamespace(false), '\\FHIRResource\\');
}

/**
Expand Down
30 changes: 26 additions & 4 deletions src/Definition/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class Types implements Countable
*/
private Type $containerType;

/**
* This will eventually be the "Bundle" type seen
* @var \DCarbone\PHPFHIR\Definition\Type
*/
private Type $bundleType;

/**
* FHIRTypes constructor.
* @param \DCarbone\PHPFHIR\Config\VersionConfig $config
Expand Down Expand Up @@ -71,7 +77,7 @@ public function __debugInfo()
* @param string $name
* @return \DCarbone\PHPFHIR\Definition\Type|null
*/
public function getTypeByName(string $name): ?Type
public function getTypeByName(string $name): null|Type
{
foreach ($this->types as $type) {
if ($type->getFHIRName() === $name) {
Expand All @@ -85,7 +91,7 @@ public function getTypeByName(string $name): ?Type
* @param string $name
* @return \DCarbone\PHPFHIR\Definition\Type|null
*/
public function getTypeByClassName(string $name): ?Type
public function getTypeByClassName(string $name): null|Type
{
foreach ($this->types as $type) {
if ($type->getClassName() === $name) {
Expand All @@ -100,7 +106,7 @@ public function getTypeByClassName(string $name): ?Type
* @param bool $leadingSlash
* @return \DCarbone\PHPFHIR\Definition\Type|null
*/
public function getTypeByFQN(string $fqn, bool $leadingSlash): ?Type
public function getTypeByFQN(string $fqn, bool $leadingSlash): null|Type
{
foreach ($this->types as $type) {
if ($type->getFullyQualifiedClassName($leadingSlash) === $fqn) {
Expand Down Expand Up @@ -192,7 +198,7 @@ function (Type $t1, Type $t2) {
* @param string $version
* @return \DCarbone\PHPFHIR\Definition\Type|null
*/
public function getContainerType(string $version): ?Type
public function getContainerType(string $version): null|Type
{
if (!isset($this->containerType)) {
foreach ($this->types as $type) {
Expand Down Expand Up @@ -228,6 +234,22 @@ public function isContainedType(string $version, Type $type): bool
return false;
}

/**
* @return \DCarbone\PHPFHIR\Definition\Type|null
*/
public function getBundleType(): null|Type
{
if (!isset($this->bundleType)) {
foreach($this->types as $type) {
if ($type->getFHIRName() === 'Bundle') {
$this->bundleType = $type;
break;
}
}
}
return $this->bundleType ?? null;
}

/**
* @return int
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Render/Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ public static function renderFhirTypeClass(VersionConfig $config, Types $types,
*/
public static function renderFhirTypeClassTest(VersionConfig $config, Types $types, Type $type, string $testType): string
{
return require PHPFHIR_TEMPLATE_TYPE_TESTS_DIR . DIRECTORY_SEPARATOR . $testType . DIRECTORY_SEPARATOR .'class_default.php';
return require PHPFHIR_TEMPLATE_TYPE_TESTS_DIR . DIRECTORY_SEPARATOR . $testType . DIRECTORY_SEPARATOR .'class.php';
}
}
7 changes: 2 additions & 5 deletions template/types/class_xhtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function xmlSerialize(\DOMNode $element = null, null|int|<?php echo PHPFH
return $element;
}
for ($i = 0; $i < $node->childNodes->length; $i++) {
$element->appendChild($element->ownerDocument->importNode($node->childNodes->item($i)));
$element->appendChild($element->ownerDocument->importNode($node->childNodes->item($i), true));
}
return $element;
}
Expand All @@ -167,10 +167,7 @@ public function xmlSerialize(\DOMNode $element = null, null|int|<?php echo PHPFH
public function jsonSerialize(): mixed
{
$node = $this->getNode();
if (null === $node) {
return null;
}
return $node->ownerDocument->saveXML($node);
return $node?->ownerDocument->saveXML($node);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,67 @@
* limitations under the License.
*/


use DCarbone\PHPFHIR\Utilities\CopyrightUtils;
use DCarbone\PHPFHIR\Utilities\ExceptionUtils;

/** @var \DCarbone\PHPFHIR\Config\VersionConfig $config */
/** @var \DCarbone\PHPFHIR\Definition\Types $types */
/** @var \DCarbone\PHPFHIR\Definition\Type $type */
/** @var \DCarbone\PHPFHIR\Definition\Type $bundleType */
/** @var \DCarbone\PHPFHIR\Definition\Property $bundleEntryProperty */
/** @var string $testType */

$typeKind = $type->getKind();

$bundleType = $types->getBundleType();
$bundleEntryProperty = null;

// we can only perform integration tests on "Resource" types.
if (!$type->isResourceType()) {
return;
}

// TODO(@dcarbone): find a more efficient way to do this...
if (null === $bundleType) {
throw ExceptionUtils::createBundleTypeNotFoundException($type);
}

foreach($bundleType->getAllPropertiesIterator() as $prop) {
if ($prop->getName() === 'entry') {
$bundleEntryProperty = $prop;
break;
}
}

if (null === $bundleEntryProperty) {
throw ExceptionUtils::createBundleEntryPropertyNotFoundException($type);
}

// TODO: precompile list of ID's to test with?
$testNS = $type->getFullyQualifiedTestNamespace(PHPFHIR_TEST_TYPE_INTEGRATION, false);
$testClassname = $type->getTestClassName();
$typeNS = $type->getFullyQualifiedClassName(false);
$typeClassname = $type->getClassName();

ob_start(); ?>
ob_start();

echo "<?php\n\n";

echo "namespace {$testNS};\n\n";

echo CopyrightUtils::getFullPHPFHIRCopyrightComment();
?>

use <?php echo $bundleType->getFullyQualifiedClassName(false); ?>;
use <?php echo $type->getFullyQualifiedClassName(false); ?>;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;

/**
* Class <?php echo $testClassname; ?>

* @package \<?php echo $testNS; ?>

*/
class <?php echo $testClassname; ?> extends TestCase
{

/** @var array */
private array $_fetchedResources = [];
Expand Down Expand Up @@ -99,7 +148,7 @@ protected function fetchResource(string $format): string
$this->assertEmpty($err, sprintf('curl error seen: %s', $err));
$this->assertIsString($res);
$this->_fetchedResources[$format] = $res;
$fname = sprintf('%s/<?php echo $type->getFHIRName(); ?>-<?php echo CopyrightUtils::getFHIRVersion(false); ?>-source.%s', PHPFHIR_OUTPUT_TMP_DIR, $format);
$fname = sprintf('%s%s<?php echo $type->getFHIRName(); ?>-<?php echo CopyrightUtils::getFHIRVersion(false); ?>-source.%s', PHPFHIR_OUTPUT_TMP_DIR, DIRECTORY_SEPARATOR, $format);
file_put_contents($fname, $res);
return $res;
}
Expand Down Expand Up @@ -328,7 +377,7 @@ public function testFHIRValidationXML(): void
<?php else: ?>
$resource = $entry->getResource();
<?php endif; ?>
$fname = PHPFHIR_OUTPUT_TMP_DIR . '/' . $resource->_getFHIRTypeName() . '-<?php echo CopyrightUtils::getFHIRVersion(false); ?>.xml';
$fname = PHPFHIR_OUTPUT_TMP_DIR . DIRECTORY_SEPARATOR . $resource->_getFHIRTypeName() . '-<?php echo CopyrightUtils::getFHIRVersion(false); ?>.xml';
file_put_contents($fname, $bundle->xmlSerialize()->ownerDocument->saveXML());
$this->assertFileExists($fname);

Expand Down Expand Up @@ -385,7 +434,7 @@ public function testFHIRValidationJSON()
<?php else: ?>
$resource = $entry->getResource();
<?php endif; ?>
$fname = PHPFHIR_OUTPUT_TMP_DIR . '/' . $resource->_getFHIRTypeName() . '-<?php echo CopyrightUtils::getFHIRVersion(false); ?>.json';
$fname = PHPFHIR_OUTPUT_TMP_DIR . DIRECTORY_SEPARATOR . $resource->_getFHIRTypeName() . '-<?php echo CopyrightUtils::getFHIRVersion(false); ?>.json';
file_put_contents($fname, json_encode($bundle));
$this->assertFileExists($fname);

Expand All @@ -408,5 +457,6 @@ public function testFHIRValidationJSON()
);
}
}
}
<?php
return ob_get_clean();
79 changes: 0 additions & 79 deletions template/types/tests/integration/class_default.php

This file was deleted.

53 changes: 0 additions & 53 deletions template/types/tests/integration/header_domain_resource.php

This file was deleted.

File renamed without changes.

0 comments on commit f152d20

Please sign in to comment.