Skip to content

Commit

Permalink
some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 15, 2025
1 parent 4ca285d commit 33234b8
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 32 deletions.
12 changes: 8 additions & 4 deletions files/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

const PHPFHIR_NAMESPACE_SEPARATOR = '\\';

// some defaults

const PHPFHIR_DEFAULT_LIBXML_OPT_MASK = 'LIBXML_NONET | LIBXML_BIGLINES | LIBXML_PARSEHUGE | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL';
define('PHPFHIR_DEFAULT_LIBXML_OPTS', eval('return ' . PHPFHIR_DEFAULT_LIBXML_OPT_MASK . ';'));

// namespace segments
const PHPFHIR_NAMESPACE_VERSION_TYPES = 'Types';
const PHPFHIR_NAMESPACE_TESTS = 'Tests';
Expand Down Expand Up @@ -78,9 +83,6 @@

// Core class names
const PHPFHIR_CLASSNAME_AUTOLOADER = 'Autoloader';
const PHPFHIR_CLASSNAME_FACTORY_CONFIG = 'FactoryConfig';
const PHPFHIR_CLASSNAME_FACTORY_VERSION_CONFIG = 'FactoryVersionConfig';
const PHPFHIR_CLASSNAME_FACTORY = 'Factory';
const PHPFHIR_CLASSNAME_VERSION_CONFIG = 'VersionConfig';
const PHPFHIR_CLASSNAME_RESPONSE_PARSER = 'ResponseParser';
const PHPFHIR_CLASSNAME_CONSTANTS = 'Constants';
Expand Down Expand Up @@ -155,7 +157,9 @@
const PHPFHIR_TEST_CLASSNAME_CONSTANTS = PHPFHIR_CLASSNAME_CONSTANTS . 'Test';
const PHPFHIR_TEST_CLASSNAME_AUTOLOADER = PHPFHIR_CLASSNAME_AUTOLOADER . 'Test';
const PHPFHIR_TEST_CLASSNAME_TYPE_MAP = PHPFHIR_VERSION_CLASSNAME_VERSION_TYPE_MAP . 'Test';
const PHPFHIR_TEST_CLASSNAME_FACTORY_CONFIG = PHPFHIR_CLASSNAME_FACTORY_CONFIG . 'Test';
const PHPFHIR_TEST_CLASSNAME_VERSION_CONFIG = PHPFHIR_CLASSNAME_VERSION_CONFIG . 'Test';
const PHPFHIR_TEST_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG = PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG . 'Test';
const PHPFHIR_TEST_ENCODING_CLASSNAME_SERIALIZE_CONFIG = PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG . 'Test';

// Test constant names
const PHPFHIR_TEST_CONSTANT_INTEGRATION_ENDPOINT = 'PHPFHIR_TEST_INTEGRATION_ENDPOINT';
Expand Down
26 changes: 6 additions & 20 deletions template/core/class_version_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,6 @@
class <?php echo PHPFHIR_CLASSNAME_VERSION_CONFIG; ?> implements <?php echo PHPFHIR_INTERFACE_VERSION_CONFIG; ?>

{
// These are used when no default configuration was provided during version code genreation

protected const _DEFAULT_UNSERIALIZE_CONFIG = [
'libxmlOpts' => LIBXML_NONET | LIBXML_BIGLINES | LIBXML_PARSEHUGE | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL,
'jsonDecodeMaxDepth' => 512,
];
protected const _DEFAULT_SERIALIZE_CONFIG = [
'overrideRootXMLNS' => false,
'rootXMLNS' => '<?php echo PHPFHIR_FHIR_XMLNS; ?>',
'xhtmlLibxmlOpts' => LIBXML_NONET | LIBXML_BIGLINES | LIBXML_PARSEHUGE | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL,
];

/** @var <?php echo $unserializeConfigClass->getFullyQualifiedName(true); ?> */
private <?php echo PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG; ?> $_unserializeConfig;

Expand All @@ -71,16 +59,14 @@ class <?php echo PHPFHIR_CLASSNAME_VERSION_CONFIG; ?> implements <?php echo PHPF
public function __construct(null|array|<?php echo $unserializeConfigClass->getEntityName(); ?> $unserializeConfig = null,
null|array|<?php echo $serializeConfigClass->getEntityName(); ?> $serializeConfig = null)
{
if (null !== $unserializeConfig) {
$this->setUnserializeConfig($unserializeConfig);
} else {
$this->setUnserializeConfig(self::_DEFAULT_UNSERIALIZE_CONFIG);
if (null === $unserializeConfig) {
$unserializeConfig = new <?php echo $unserializeConfigClass->getEntityName(); ?>();
}
if (null !== $serializeConfig) {
$this->setSerializeConfig($serializeConfig);
} else {
$this->setSerializeConfig(self::_DEFAULT_SERIALIZE_CONFIG);
$this->setUnserializeConfig($unserializeConfig);
if (null === $serializeConfig) {
$serializeConfig = new <?php echo $serializeConfigClass->getEntityName(); ?>();
}
$this->setSerializeConfig($serializeConfig);
}

/**
Expand Down
12 changes: 8 additions & 4 deletions template/core/encoding/class_serialize_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class <?php echo PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG; ?>

{
/** @var bool */
private bool $_overrideSourceXMLNS;
private bool $_overrideSourceXMLNS = false;
/** @var string */
private string $_rootXMLNS;
/** @var int */
private int $_xhtmlLibxmlOpts;
private int $_xhtmlLibxmlOpts = <?php echo PHPFHIR_DEFAULT_LIBXML_OPT_MASK; ?>;

public function __construct(null|bool $overrideSourceXMLNS = null,
null|string $rootXMLNS = null,
Expand All @@ -53,11 +53,15 @@ public function __construct(null|bool $overrideSourceXMLNS = null,
}

/**
* @param string $rootXMLNS
* @param null|string $rootXMLNS
* @return self
*/
public function setRootXMLNS(string $rootXMLNS): self
public function setRootXMLNS(null|string $rootXMLNS): self
{
if (null === $rootXMLNS) {
unset($this->_rootXMLNS);
return $this;
}
$this->_rootXMLNS = $rootXMLNS;
return $this;
}
Expand Down
8 changes: 4 additions & 4 deletions template/core/encoding/class_unserialize_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class <?php echo PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG; ?>

{
/** @var int */
private int $_libxmlOpts;
private int $_libxmlOpts = <?php echo PHPFHIR_DEFAULT_LIBXML_OPT_MASK; ?>;
/** @var int */
private int $_jsonDecodeMaxDepth;
private int $_jsonDecodeMaxDepth = 512;

public function __construct(null|int $libxmlOpts = null,
null|int $jsonDecodeMaxDepth = null)
Expand Down Expand Up @@ -61,7 +61,7 @@ public function setLibxmlOpts(int $libxmlOpts): self
*/
public function getLibxmlOpts(): int
{
return $this->_libxmlOpts ?? 0;
return $this->_libxmlOpts;
}

/**
Expand All @@ -79,7 +79,7 @@ public function setJSONDecodeMaxDepth(int $jsonDecodeMaxDepth): self
*/
public function getJSONDecodeMaxDepth(): int
{
return $this->_jsonDecodeMaxDepth ?? 0;
return $this->_jsonDecodeMaxDepth;
}
}
<?php return ob_get_clean();
62 changes: 62 additions & 0 deletions template/core/tests/encoding/test_class_serialize_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types=1);

/*
* Copyright 2025 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


use DCarbone\PHPFHIR\Utilities\ImportUtils;

/** @var \DCarbone\PHPFHIR\Config $config */
/** @var \DCarbone\PHPFHIR\CoreFile $coreFile */

$imports = $coreFile->getImports();
$imports->addCoreFileImportsByName(
PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG,
);

$coreFiles = $config->getCoreFiles();

$serializeConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG);

ob_start();

echo "<?php\n\n";?>
namespace <?php echo $coreFile->getFullyQualifiedNamespace(false); ?>;

<?php echo $config->getBasePHPFHIRCopyrightComment(true); ?>

<?php echo ImportUtils::compileImportStatements($imports); ?>
use PHPUnit\Framework\TestCase;

class <?php echo PHPFHIR_TEST_ENCODING_CLASSNAME_SERIALIZE_CONFIG; ?> extends TestCase
{
public function testEmptyConstruct()
{
$sc = new <?php echo $serializeConfigClass->getEntityName(); ?>();
$this->assertFalse($sc->getOverrideSourceXMLNS());
$this->assertNull($sc->getRootXMLNS());
$this->assertEquals(<?php echo PHPFHIR_DEFAULT_LIBXML_OPTS ?>, $sc->getXHTMLLibxmlOpts());
}

public function testValuedConstruct()
{
$sc = new <?php echo $serializeConfigClass->getEntityName(); ?>(overrideSourceXMLNS: true, rootXMLNS: 'urn:foo:bar', xhtmlLibxmlOpts: 123);
$this->assertTrue($sc->getOverrideSourceXMLNS());
$this->assertEquals('urn:foo:bar', $sc->getRootXMLNS());
$this->assertEquals(123, $sc->getXHTMLLibxmlOpts());
}
}
<?php return ob_get_clean();
60 changes: 60 additions & 0 deletions template/core/tests/encoding/test_class_unserialize_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types=1);

/*
* Copyright 2025 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


use DCarbone\PHPFHIR\Utilities\ImportUtils;

/** @var \DCarbone\PHPFHIR\Config $config */
/** @var \DCarbone\PHPFHIR\CoreFile $coreFile */

$imports = $coreFile->getImports();
$imports->addCoreFileImportsByName(
PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG,
);

$coreFiles = $config->getCoreFiles();

$unserializeConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG);

ob_start();
echo "<?php\n\n";?>
namespace <?php echo $coreFile->getFullyQualifiedNamespace(false); ?>;

<?php echo $config->getBasePHPFHIRCopyrightComment(false); ?>

<?php echo ImportUtils::compileImportStatements($imports); ?>
use PHPUnit\Framework\TestCase;

class <?php echo PHPFHIR_TEST_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG; ?> extends TestCase
{
public function testEmptyConstruct()
{
$uc = new <?php echo $unserializeConfigClass->getEntityName(); ?>();
$this->assertEquals(<?php echo PHPFHIR_DEFAULT_LIBXML_OPTS; ?>, $uc->getLibxmlOpts());
$this->assertEquals(512, $uc->getJSONDecodeMaxDepth());
}

public function testValuedConstruct()
{
$uc = new <?php echo $unserializeConfigClass->getEntityName(); ?>(libxmlOpts: 123, jsonDecodeMaxDepth: 456);
$this->assertEquals(123, $uc->getLibxmlOpts());
$this->assertEquals(456, $uc->getJSONDecodeMaxDepth());
}
}

<?php return ob_get_clean();
97 changes: 97 additions & 0 deletions template/core/tests/test_class_version_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php declare(strict_types=1);

/*
* Copyright 2025 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


use DCarbone\PHPFHIR\Utilities\ImportUtils;

/** @var \DCarbone\PHPFHIR\Config $config */
/** @var \DCarbone\PHPFHIR\CoreFile $coreFile */

$imports = $coreFile->getImports();
$imports->addCoreFileImportsByName(
PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG,
PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG,
PHPFHIR_CLASSNAME_VERSION_CONFIG,
);

$coreFiles = $config->getCoreFiles();

$versionConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_CLASSNAME_VERSION_CONFIG);
$serializeConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG);
$unserializeConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG);

ob_start();
echo "<?php\n\n";?>
namespace <?php echo $coreFile->getFullyQualifiedNamespace(false); ?>;

<?php echo $config->getBasePHPFHIRCopyrightComment(true); ?>

<?php echo ImportUtils::compileImportStatements($imports); ?>
use PHPUnit\Framework\TestCase;

class <?php echo PHPFHIR_TEST_CLASSNAME_VERSION_CONFIG; ?> extends TestCase
{
public function testEmptyConstruct()
{
$vc = new <?php echo $versionConfigClass->getEntityName(); ?>();
$uc = $vc->getUnserializeConfig();
$this->assertInstanceOf(<?php echo $unserializeConfigClass->getEntityName(); ?>::class, $uc);
$this->assertEquals(<?php echo PHPFHIR_DEFAULT_LIBXML_OPTS; ?>, $uc->getLibxmlOpts());
$this->assertEquals(512, $uc->getJSONDecodeMaxDepth());
$sc = $vc->getSerializeConfig();
$this->assertInstanceOf(<?php echo $serializeConfigClass->getEntityName(); ?>::class, $sc);
$this->assertFalse($sc->getOverrideSourceXMLNS());
$this->assertNull($sc->getRootXMLNS());
$this->assertEquals(<?php echo PHPFHIR_DEFAULT_LIBXML_OPTS; ?>, $sc->getXHTMLLibxmlOpts());
}

public function testArrayConstruct()
{
$vc = new <?php echo $versionConfigClass->getEntityName(); ?>(
unserializeConfig: ['libxmlOpts' => 456, 'jsonDecodeMaxDepth' => 789],
serializeConfig: ['overrideSourceXMLNS' => true, 'rootXMLNS' => 'urn:foo:bar', 'xhtmlLibxmlOpts' => 123],
);
$uc = $vc->getUnserializeConfig();
$this->assertInstanceOf(<?php echo $unserializeConfigClass->getEntityName(); ?>::class, $uc);
$this->assertEquals(456, $uc->getLibxmlOpts());
$this->assertEquals(789, $uc->getJSONDecodeMaxDepth());
$sc = $vc->getSerializeConfig();
$this->assertInstanceOf(<?php echo $serializeConfigClass->getEntityName(); ?>::class, $sc);
$this->assertTrue($sc->getOverrideSourceXMLNS());
$this->assertEquals('urn:foo:bar', $sc->getRootXMLNS());
$this->assertEquals(123, $sc->getXHTMLLibxmlOpts());
}

public function testValuedConstruct()
{
$vc = new <?php echo $versionConfigClass->getEntityName(); ?>(
unserializeConfig: new <?php echo $unserializeConfigClass->getEntityName(); ?>(),
serializeConfig: new <?php echo $serializeConfigClass->getEntityName(); ?>(),
);
$uc = $vc->getUnserializeConfig();
$this->assertInstanceOf(<?php echo $unserializeConfigClass->getEntityName(); ?>::class, $uc);
$this->assertEquals(<?php echo PHPFHIR_DEFAULT_LIBXML_OPTS; ?>, $uc->getLibxmlOpts());
$this->assertEquals(512, $uc->getJSONDecodeMaxDepth());
$sc = $vc->getSerializeConfig();
$this->assertInstanceOf(<?php echo $serializeConfigClass->getEntityName(); ?>::class, $sc);
$this->assertFalse($sc->getOverrideSourceXMLNS());
$this->assertNull($sc->getRootXMLNS());
$this->assertEquals(<?php echo PHPFHIR_DEFAULT_LIBXML_OPTS; ?>, $sc->getXHTMLLibxmlOpts());
}
}
<?php return ob_get_clean();

0 comments on commit 33234b8

Please sign in to comment.