From e13d6e0767ca09029d1a018454c4428c8318428b Mon Sep 17 00:00:00 2001 From: HorstOeko Date: Thu, 12 Oct 2023 06:23:28 +0200 Subject: [PATCH] #25 Centralisation of profile recognition -> Added methods to ZugferdProfileResolver for resolving profile definition by profile id --- src/ZugferdProfileResolver.php | 31 +++++++++++ tests/testcases/ProfileResolverTest.php | 74 +++++++++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/src/ZugferdProfileResolver.php b/src/ZugferdProfileResolver.php index f43206c6..ca3d89a2 100644 --- a/src/ZugferdProfileResolver.php +++ b/src/ZugferdProfileResolver.php @@ -29,6 +29,7 @@ class ZugferdProfileResolver * * @param string $xmlContent * @return array + * @throws Exception */ public static function resolve(string $xmlContent): array { @@ -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]; + } } \ No newline at end of file diff --git a/tests/testcases/ProfileResolverTest.php b/tests/testcases/ProfileResolverTest.php index ddd570c8..592cf813 100644 --- a/tests/testcases/ProfileResolverTest.php +++ b/tests/testcases/ProfileResolverTest.php @@ -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); + } }