Skip to content

Commit

Permalink
#25 Centralisation of profile recognition -> Added ZugferdProfileReso…
Browse files Browse the repository at this point in the history
…lver, Added tests for the new ZugferdProfileResolver
  • Loading branch information
HorstOeko committed Oct 12, 2023
1 parent af9845b commit 8db2564
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/ZugferdProfileResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* This file is a part of horstoeko/zugferd.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace horstoeko\zugferd;

use \Exception;
use \SimpleXMLElement;
use \horstoeko\zugferd\ZugferdProfiles;

/**
* Class representing the profile resolver
*
* @category Zugferd
* @package Zugferd
* @author D. Erling <horstoeko@erling.com.de>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/horstoeko/zugferd
*/
class ZugferdProfileResolver
{
/**
* Resolve profile id and profile definition by the content of $xmlContent
*
* @param string $xmlContent
* @return array
*/
public static function resolve(string $xmlContent): array
{
$xmldocument = new SimpleXMLElement($xmlContent);
$typeelement = $xmldocument->xpath('/rsm:CrossIndustryInvoice/rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID');

if (!is_array($typeelement) || !isset($typeelement[0])) {
throw new Exception('Could not determine the profile...');
}

/**
* @var int $profile
* @var array $profiledef
*/
foreach (ZugferdProfiles::PROFILEDEF as $profile => $profiledef) {
if ($typeelement[0] == $profiledef["contextparameter"]) {
return [$profile, $profiledef];
}
}

throw new Exception('Could not determine the profile...');
}

/**
* Resolve profile id by the content of $xmlContent
*
* @param string $xmlContent
* @return int
* @throws Exception
*/
public static function resolveProfileId(string $xmlContent): int
{
return static::resolve($xmlContent)[0];
}

/**
* Resolve profile definition by the content of $xmlContent
*
* @param string $xmlContent
* @return array
* @throws Exception
*/
public static function resolveProfileDef(string $xmlContent): array
{
return static::resolve($xmlContent)[1];
}
}
158 changes: 158 additions & 0 deletions tests/testcases/ProfileResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace horstoeko\zugferd\tests\testcases;

use \horstoeko\zugferd\tests\TestCase;
use horstoeko\zugferd\ZugferdProfileResolver;
use horstoeko\zugferd\ZugferdProfiles;

class ProfileResolverTest extends TestCase
{
/**
* Internal helper - returns the EN16931 Header
*
* @return string
*/
private function deliverEn16931Header(): string
{
return <<<HDR
<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100" xmlns:a="urn:un:unece:uncefact:data:standard:QualifiedDataType:100" xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:10" xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:cen.eu:en16931:2017</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
</rsm:CrossIndustryInvoice>
HDR;
}

/**
* Internal helper - returns unknown profile
*
* @return string
*/
private function deliverUnknownProfile(): string
{
return <<<HDR
<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100" xmlns:a="urn:un:unece:uncefact:data:standard:QualifiedDataType:100" xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:10" xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>unknown</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
</rsm:CrossIndustryInvoice>
HDR;
}

/**
* Internal helper - returns unknown profile
*
* @return string
*/
private function deliverInvalidXml(): string
{
return <<<HDR
<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100" xmlns:a="urn:un:unece:uncefact:data:standard:QualifiedDataType:100" xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:10" xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</rsm:CrossIndustryInvoice>
HDR;
}

/**
* @covers \horstoeko\zugferd\ZugferdProfileResolver::resolve
*/
public function testResolveEn16931()
{
$resolved = ZugferdProfileResolver::resolve($this->deliverEn16931Header());

$this->assertIsArray($resolved);
$this->assertArrayHasKey(0, $resolved);
$this->assertArrayHasKey(1, $resolved);
$this->assertIsInt($resolved[0]);
$this->assertIsArray($resolved[1]);
$this->assertArrayHasKey("name", $resolved[1]);
$this->assertArrayHasKey("altname", $resolved[1]);
$this->assertArrayHasKey("description", $resolved[1]);
$this->assertArrayHasKey("contextparameter", $resolved[1]);
$this->assertArrayHasKey("businessprocess", $resolved[1]);
$this->assertArrayHasKey("attachmentfilename", $resolved[1]);
$this->assertArrayHasKey("xmpname", $resolved[1]);
$this->assertArrayHasKey("xsdfilename", $resolved[1]);
$this->assertArrayHasKey("schematronfilename", $resolved[1]);

$this->assertEquals(ZugferdProfiles::PROFILE_EN16931, $resolved[0]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['name'], $resolved[1]["name"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['altname'], $resolved[1]["altname"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['description'], $resolved[1]["description"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['contextparameter'], $resolved[1]["contextparameter"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['businessprocess'], $resolved[1]["businessprocess"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['attachmentfilename'], $resolved[1]["attachmentfilename"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['xmpname'], $resolved[1]["xmpname"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['xsdfilename'], $resolved[1]["xsdfilename"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['schematronfilename'], $resolved[1]["schematronfilename"]);
}

/**
* @covers \horstoeko\zugferd\ZugferdProfileResolver::resolveProfileId
*/
public function testResolveProfileIdEn16931()
{
$resolved = ZugferdProfileResolver::resolveProfileId($this->deliverEn16931Header());

$this->assertIsInt($resolved);
$this->assertEquals(ZugferdProfiles::PROFILE_EN16931, $resolved);
}

/**
* @covers \horstoeko\zugferd\ZugferdProfileResolver::resolveProfileDef
*/
public function testResolveProfileDefEn16931()
{
$resolved = ZugferdProfileResolver::resolveProfileDef($this->deliverEn16931Header());

$this->assertIsArray($resolved);
$this->assertArrayHasKey("name", $resolved);
$this->assertArrayHasKey("altname", $resolved);
$this->assertArrayHasKey("description", $resolved);
$this->assertArrayHasKey("contextparameter", $resolved);
$this->assertArrayHasKey("businessprocess", $resolved);
$this->assertArrayHasKey("attachmentfilename", $resolved);
$this->assertArrayHasKey("xmpname", $resolved);
$this->assertArrayHasKey("xsdfilename", $resolved);
$this->assertArrayHasKey("schematronfilename", $resolved);

$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['name'], $resolved["name"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['altname'], $resolved["altname"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['description'], $resolved["description"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['contextparameter'], $resolved["contextparameter"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['businessprocess'], $resolved["businessprocess"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['attachmentfilename'], $resolved["attachmentfilename"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['xmpname'], $resolved["xmpname"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['xsdfilename'], $resolved["xsdfilename"]);
$this->assertEquals(ZugferdProfiles::PROFILEDEF[ZugferdProfiles::PROFILE_EN16931]['schematronfilename'], $resolved["schematronfilename"]);
}

/**
* @covers \horstoeko\zugferd\ZugferdProfileResolver::resolve
*/
public function testResolveUnknownProfile()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not determine the profile...');

ZugferdProfileResolver::resolveProfileId($this->deliverUnknownProfile());
}

/**
* @covers \horstoeko\zugferd\ZugferdProfileResolver::resolve
*/
public function testResolveInvalidXml()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not determine the profile...');

ZugferdProfileResolver::resolveProfileId($this->deliverInvalidXml());
}
}

0 comments on commit 8db2564

Please sign in to comment.