Skip to content

Commit

Permalink
#25 Centralisation of profile recognition -> Added methods to Zugferd…
Browse files Browse the repository at this point in the history
…ProfileResolver for resolving profile definition by profile id
  • Loading branch information
HorstOeko committed Oct 12, 2023
1 parent e3c1953 commit e13d6e0
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/ZugferdProfileResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ZugferdProfileResolver
*
* @param string $xmlContent
* @return array
* @throws Exception
*/
public static function resolve(string $xmlContent): array
{
Expand Down Expand Up @@ -75,4 +76,34 @@ public static function resolveProfileDef(string $xmlContent): array
{
return static::resolve($xmlContent)[1];
}

/**
* Resolve profile id and profile definition by it's id
*
* @param integer $profileId
* @return array
* @throws Exception
*/
public static function resolveById(int $profileId): array
{
if (!isset(ZugferdProfiles::PROFILEDEF[$profileId])) {
throw new Exception('Could not determine the profile...');
}

return [$profileId, ZugferdProfiles::PROFILEDEF[$profileId]];
}

/**
* Resolve profile profile definition by it's id
*
* @param int $profileId
* @return array
* @throws Exception
*/
public static function resolveProfileDefById(int $profileId): array
{
$resolved = static::resolveById($profileId);

return $resolved[1];
}
}
74 changes: 74 additions & 0 deletions tests/testcases/ProfileResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,78 @@ public function testResolveInvalidXml()

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

/**
* @covers \horstoeko\zugferd\ZugferdProfileResolver::resolveById
*/
public function testResolveProfileByIdEn16931()
{
$resolved = ZugferdProfileResolver::resolveById(ZugferdProfiles::PROFILE_EN16931);

$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::resolveProfileDefById
*/
public function testResolveProfileDefByIdEn16931()
{
$resolved = ZugferdProfileResolver::resolveProfileDefById(ZugferdProfiles::PROFILE_EN16931);

$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::resolveProfileDefById
*/
public function testResolveProfileDefByIdUnknown()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not determine the profile...');

ZugferdProfileResolver::resolveProfileDefById(-1);
}
}

0 comments on commit e13d6e0

Please sign in to comment.