From 23dcbda0ec062f39a19cd157f87ec7634ca58c3d Mon Sep 17 00:00:00 2001 From: HorstOeko Date: Sun, 7 Jan 2024 11:21:15 +0100 Subject: [PATCH] Centralisation of profile recognition --- build/phpunit.xml | 3 + src/OrderDocument.php | 5 +- src/OrderDocumentBuilder.php | 19 +- src/OrderDocumentPdfMerger.php | 22 +- src/OrderDocumentReader.php | 23 +- src/OrderObjectHelper.php | 3 +- src/OrderProfileResolver.php | 111 +++++++++ tests/testcases/OrderDocumentBaseTest.php | 12 +- tests/testcases/OrderProfileResolverTest.php | 238 +++++++++++++++++++ 9 files changed, 373 insertions(+), 63 deletions(-) create mode 100644 src/OrderProfileResolver.php create mode 100644 tests/testcases/OrderProfileResolverTest.php diff --git a/build/phpunit.xml b/build/phpunit.xml index 76f9124..e73aea9 100644 --- a/build/phpunit.xml +++ b/build/phpunit.xml @@ -12,6 +12,9 @@ ../tests/testcases/OrderPackageVersionTest.php + + ../tests/testcases/OrderProfileResolverTest.php + ../tests/testcases/OrderDocumentBaseTest.php diff --git a/src/OrderDocument.php b/src/OrderDocument.php index a21b734..f8b487e 100644 --- a/src/OrderDocument.php +++ b/src/OrderDocument.php @@ -13,6 +13,7 @@ use GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\XmlSchemaDateHandler; use horstoeko\orderx\jms\OrderTypesHandler; use horstoeko\orderx\OrderObjectHelper; +use horstoeko\orderx\OrderProfileResolver; use horstoeko\stringmanagement\PathUtils; use JMS\Serializer\Handler\HandlerRegistryInterface; use JMS\Serializer\SerializerBuilder; @@ -79,7 +80,7 @@ class OrderDocument * * @codeCoverageIgnore */ - public function __construct(int $profile) + protected function __construct(int $profile) { $this->initProfile($profile); $this->initObjectHelper(); @@ -132,7 +133,7 @@ public function getProfileDefinition(): array private function initProfile(int $profile): OrderDocument { $this->profileId = $profile; - $this->profileDefinition = OrderProfiles::PROFILEDEF[$profile]; + $this->profileDefinition = OrderProfileResolver::resolveProfileDefById($profile); return $this; } diff --git a/src/OrderDocumentBuilder.php b/src/OrderDocumentBuilder.php index d1c9d46..f525f65 100644 --- a/src/OrderDocumentBuilder.php +++ b/src/OrderDocumentBuilder.php @@ -66,30 +66,17 @@ class OrderDocumentBuilder extends OrderDocument */ protected $currentPosition = null; - /** - * Constructor - * - * @codeCoverageIgnore - * @param int $profile - */ - public function __construct(int $profile) - { - parent::__construct($profile); - - $this->initNewDocument(); - } - /** * Creates a new OrderDocumentBuilder with profile $profile * * @codeCoverageIgnore * - * @param integer $profile + * @param integer $profileId * @return OrderDocumentBuilder */ - public static function createNew(int $profile): OrderDocumentBuilder + public static function createNew(int $profileId): OrderDocumentBuilder { - return (new self($profile)); + return (new static($profileId))->initNewDocument(); } /** diff --git a/src/OrderDocumentPdfMerger.php b/src/OrderDocumentPdfMerger.php index 23800a7..d136452 100644 --- a/src/OrderDocumentPdfMerger.php +++ b/src/OrderDocumentPdfMerger.php @@ -10,11 +10,8 @@ namespace horstoeko\orderx; use Exception; -use horstoeko\orderx\exception\OrderCannotFindProfileString; -use horstoeko\orderx\exception\OrderUnknownProfileException; +use horstoeko\orderx\OrderProfileResolver; use horstoeko\orderx\OrderDocumentPdfBuilderAbstract; -use horstoeko\orderx\OrderProfiles; -use SimpleXMLElement; /** * Class representing the facillity adding existing XML data (file or data-string) @@ -123,21 +120,6 @@ private function xmlDataIsFile(): bool */ private function getProfileDefinition(): array { - $xmlContent = $this->getXmlContent(); - - $xmldocument = new SimpleXMLElement($xmlContent); - $typeelement = $xmldocument->xpath('/rsm:SCRDMCCBDACIOMessageStructure/rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID'); - - if (!is_array($typeelement) || !isset($typeelement[0])) { - throw new OrderCannotFindProfileString(); - } - - foreach (OrderProfiles::PROFILEDEF as $profile => $profiledef) { - if ($typeelement[0] == $profiledef["contextparameter"]) { - return $profiledef; - } - } - - throw new OrderUnknownProfileException((string)$typeelement[0]); + return OrderProfileResolver::resolveProfileDef($this->getXmlContent()); } } diff --git a/src/OrderDocumentReader.php b/src/OrderDocumentReader.php index 775d550..8baf407 100644 --- a/src/OrderDocumentReader.php +++ b/src/OrderDocumentReader.php @@ -11,14 +11,12 @@ use Closure; use DateTime; -use SimpleXMLElement; -use OutOfRangeException; +use horstoeko\orderx\exception\OrderFileNotFoundException; +use horstoeko\orderx\OrderProfileResolver; use horstoeko\stringmanagement\FileUtils; use horstoeko\stringmanagement\PathUtils; use horstoeko\stringmanagement\StringUtils; -use horstoeko\orderx\exception\OrderFileNotFoundException; -use horstoeko\orderx\exception\OrderCannotFindProfileString; -use horstoeko\orderx\exception\OrderUnknownProfileException; +use OutOfRangeException; /** * Class representing the document reader for incoming XML-Documents with @@ -232,20 +230,9 @@ public static function readAndGuessFromFile(string $xmlfilename): OrderDocumentR */ public static function readAndGuessFromContent(string $xmlcontent): OrderDocumentReader { - $xmldocument = new SimpleXMLElement($xmlcontent); - $typeelement = $xmldocument->xpath('/rsm:SCRDMCCBDACIOMessageStructure/rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID'); - - if (!is_array($typeelement) || !isset($typeelement[0])) { - throw new OrderCannotFindProfileString(); - } - - foreach (OrderProfiles::PROFILEDEF as $profile => $profiledef) { - if ($typeelement[0] == $profiledef["contextparameter"]) { - return (new self($profile))->readContent($xmlcontent); - } - } + $profileId = OrderProfileResolver::resolveProfileId($xmlcontent); - throw new OrderUnknownProfileException((string)$typeelement[0]); + return (new static($profileId))->readContent($xmlcontent); } /** diff --git a/src/OrderObjectHelper.php b/src/OrderObjectHelper.php index 1722f83..1468139 100644 --- a/src/OrderObjectHelper.php +++ b/src/OrderObjectHelper.php @@ -12,6 +12,7 @@ use DateTime; use horstoeko\orderx\exception\OrderMimeTypeNotSupportedException; use horstoeko\orderx\exception\OrderUnknownDateFormatException; +use horstoeko\orderx\OrderProfileResolver; use horstoeko\stringmanagement\FileUtils; use horstoeko\stringmanagement\StringUtils; use MimeTyper\Repository\MimeDbRepository; @@ -63,7 +64,7 @@ class OrderObjectHelper public function __construct(int $profile) { $this->profile = $profile; - $this->profiledef = OrderProfiles::PROFILEDEF[$profile]; + $this->profiledef = OrderProfileResolver::resolveProfileDefById($profile); } /** diff --git a/src/OrderProfileResolver.php b/src/OrderProfileResolver.php new file mode 100644 index 0000000..d4e3ea4 --- /dev/null +++ b/src/OrderProfileResolver.php @@ -0,0 +1,111 @@ + + * @license https://opensource.org/licenses/MIT MIT + * @link https://github.com/horstoeko/orderx + */ +class OrderProfileResolver +{ + /** + * Resolve profile id and profile definition by the content of $xmlContent + * + * @param string $xmlContent + * @return array + * @throws Exception + */ + public static function resolve(string $xmlContent): array + { + $xmldocument = new SimpleXMLElement($xmlContent); + $typeelement = $xmldocument->xpath('/rsm:SCRDMCCBDACIOMessageStructure/rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID'); + + if (!is_array($typeelement) || !isset($typeelement[0])) { + throw new OrderCannotFindProfileString(); + } + + /** + * @var int $profile + * @var array $profiledef + */ + foreach (OrderProfiles::PROFILEDEF as $profile => $profiledef) { + if ($typeelement[0] == $profiledef["contextparameter"]) { + return [$profile, $profiledef]; + } + } + + throw new OrderUnknownProfileException((string)$typeelement[0]); + } + + /** + * 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]; + } + + /** + * 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(OrderProfiles::PROFILEDEF[$profileId])) { + throw new Exception('Could not determine the profile...'); + } + + return [$profileId, OrderProfiles::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]; + } +} diff --git a/tests/testcases/OrderDocumentBaseTest.php b/tests/testcases/OrderDocumentBaseTest.php index 0afa317..ed06aab 100644 --- a/tests/testcases/OrderDocumentBaseTest.php +++ b/tests/testcases/OrderDocumentBaseTest.php @@ -3,6 +3,7 @@ namespace horstoeko\orderx\tests\testcases; use horstoeko\orderx\OrderDocument; +use horstoeko\orderx\OrderDocumentBuilder; use horstoeko\orderx\OrderProfiles; use horstoeko\orderx\tests\TestCase; @@ -13,7 +14,7 @@ class OrderDocumentBaseTest extends TestCase */ public function testDocumentCreationBasic(): void { - $doc = new OrderDocument(OrderProfiles::PROFILE_BASIC); + $doc = OrderDocumentBuilder::createNew(OrderProfiles::PROFILE_BASIC); $this->assertNotNull($doc); $this->assertEquals(OrderProfiles::PROFILE_BASIC, $doc->getProfileId()); $this->assertArrayHasKey("contextparameter", $doc->getProfileDefinition()); @@ -27,7 +28,7 @@ public function testDocumentCreationBasic(): void */ public function testDocumentCreationComfort(): void { - $doc = new OrderDocument(OrderProfiles::PROFILE_COMFORT); + $doc = OrderDocumentBuilder::createNew(OrderProfiles::PROFILE_COMFORT); $this->assertNotNull($doc); $this->assertEquals(OrderProfiles::PROFILE_COMFORT, $doc->getProfileId()); $this->assertArrayHasKey("contextparameter", $doc->getProfileDefinition()); @@ -41,7 +42,7 @@ public function testDocumentCreationComfort(): void */ public function testDocumentCreationExtended(): void { - $doc = new OrderDocument(OrderProfiles::PROFILE_EXTENDED); + $doc = OrderDocumentBuilder::createNew(OrderProfiles::PROFILE_EXTENDED); $this->assertNotNull($doc); $this->assertEquals(OrderProfiles::PROFILE_EXTENDED, $doc->getProfileId()); $this->assertArrayHasKey("contextparameter", $doc->getProfileDefinition()); @@ -55,15 +56,14 @@ public function testDocumentCreationExtended(): void */ public function testDocumentInternals(): void { - $doc = new OrderDocument(OrderProfiles::PROFILE_EXTENDED); + $doc = OrderDocumentBuilder::createNew(OrderProfiles::PROFILE_EXTENDED); $property = $this->getPrivatePropertyFromClassname('horstoeko\orderx\OrderDocument', 'serializerBuilder'); $this->assertNotNull($property->getValue($doc)); $property = $this->getPrivatePropertyFromClassname('horstoeko\orderx\OrderDocument', 'serializer'); $this->assertNotNull($property->getValue($doc)); $property = $this->getPrivatePropertyFromClassname('horstoeko\orderx\OrderDocument', 'orderObject'); - $this->assertNull($property->getValue($doc)); + $this->assertNotNull($property->getValue($doc)); $property = $this->getPrivatePropertyFromClassname('horstoeko\orderx\OrderDocument', 'objectHelper'); $this->assertNotNull($property->getValue($doc)); - $this->assertNull($doc->getOrderObject()); } } diff --git a/tests/testcases/OrderProfileResolverTest.php b/tests/testcases/OrderProfileResolverTest.php new file mode 100644 index 0000000..81a8005 --- /dev/null +++ b/tests/testcases/OrderProfileResolverTest.php @@ -0,0 +1,238 @@ + + + + +false + + +A1 + + +urn:order-x.eu:1p0:comfort + + + +HDR; + } + + /** + * Internal helper - returns unknown profile + * + * @return string + */ + private function deliverUnknownProfile(): string + { + return << + + + +false + + +A1 + + +unknown + + + +HDR; + } + + /** + * Internal helper - returns unknown profile + * + * @return string + */ + private function deliverInvalidXml(): string + { + return << + + +HDR; + } + + /** + * @covers \horstoeko\orderx\OrderProfileResolver::resolve + */ + public function testResolveComfort() + { + $resolved = OrderProfileResolver::resolve($this->deliverComfortHeader()); + + $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("attachmentfilename", $resolved[1]); + $this->assertArrayHasKey("xmpname", $resolved[1]); + $this->assertArrayHasKey("xsdfilename", $resolved[1]); + $this->assertArrayHasKey("schematronfilename", $resolved[1]); + + $this->assertEquals(OrderProfiles::PROFILE_COMFORT, $resolved[0]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['name'], $resolved[1]["name"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['altname'], $resolved[1]["altname"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['description'], $resolved[1]["description"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['contextparameter'], $resolved[1]["contextparameter"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['attachmentfilename'], $resolved[1]["attachmentfilename"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['xmpname'], $resolved[1]["xmpname"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['xsdfilename'], $resolved[1]["xsdfilename"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['schematronfilename'], $resolved[1]["schematronfilename"]); + } + + /** + * @covers \horstoeko\orderx\OrderProfileResolver::resolveProfileId + */ + public function testResolveProfileIdComfort() + { + $resolved = OrderProfileResolver::resolveProfileId($this->deliverComfortHeader()); + + $this->assertIsInt($resolved); + $this->assertEquals(OrderProfiles::PROFILE_COMFORT, $resolved); + } + + /** + * @covers \horstoeko\orderx\OrderProfileResolver::resolveProfileDef + */ + public function testResolveProfileDefComfort() + { + $resolved = OrderProfileResolver::resolveProfileDef($this->deliverComfortHeader()); + + $this->assertIsArray($resolved); + $this->assertArrayHasKey("name", $resolved); + $this->assertArrayHasKey("altname", $resolved); + $this->assertArrayHasKey("description", $resolved); + $this->assertArrayHasKey("contextparameter", $resolved); + $this->assertArrayHasKey("attachmentfilename", $resolved); + $this->assertArrayHasKey("xmpname", $resolved); + $this->assertArrayHasKey("xsdfilename", $resolved); + $this->assertArrayHasKey("schematronfilename", $resolved); + + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['name'], $resolved["name"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['altname'], $resolved["altname"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['description'], $resolved["description"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['contextparameter'], $resolved["contextparameter"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['attachmentfilename'], $resolved["attachmentfilename"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['xmpname'], $resolved["xmpname"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['xsdfilename'], $resolved["xsdfilename"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['schematronfilename'], $resolved["schematronfilename"]); + } + + /** + * @covers \horstoeko\orderx\OrderProfileResolver::resolve + */ + public function testResolveUnknownProfile() + { + $this->expectException(OrderUnknownProfileException::class); + $this->expectExceptionMessage('Cannot determain the profile by'); + + OrderProfileResolver::resolveProfileId($this->deliverUnknownProfile()); + } + + /** + * @covers \horstoeko\orderx\OrderProfileResolver::resolve + */ + public function testResolveInvalidXml() + { + $this->expectException(OrderCannotFindProfileString::class); + $this->expectExceptionMessage('The string containing the profile was not found'); + + OrderProfileResolver::resolveProfileId($this->deliverInvalidXml()); + } + + /** + * @covers \horstoeko\orderx\OrderProfileResolver::resolveById + */ + public function testResolveProfileByIdComfort() + { + $resolved = OrderProfileResolver::resolveById(OrderProfiles::PROFILE_COMFORT); + + $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("attachmentfilename", $resolved[1]); + $this->assertArrayHasKey("xmpname", $resolved[1]); + $this->assertArrayHasKey("xsdfilename", $resolved[1]); + $this->assertArrayHasKey("schematronfilename", $resolved[1]); + + $this->assertEquals(OrderProfiles::PROFILE_COMFORT, $resolved[0]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['name'], $resolved[1]["name"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['altname'], $resolved[1]["altname"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['description'], $resolved[1]["description"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['contextparameter'], $resolved[1]["contextparameter"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['attachmentfilename'], $resolved[1]["attachmentfilename"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['xmpname'], $resolved[1]["xmpname"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['xsdfilename'], $resolved[1]["xsdfilename"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['schematronfilename'], $resolved[1]["schematronfilename"]); + } + + /** + * @covers \horstoeko\orderx\OrderProfileResolver::resolveProfileDefById + */ + public function testResolveProfileDefByIdComfort() + { + $resolved = OrderProfileResolver::resolveProfileDefById(OrderProfiles::PROFILE_COMFORT); + + $this->assertIsArray($resolved); + $this->assertArrayHasKey("name", $resolved); + $this->assertArrayHasKey("altname", $resolved); + $this->assertArrayHasKey("description", $resolved); + $this->assertArrayHasKey("contextparameter", $resolved); + $this->assertArrayHasKey("attachmentfilename", $resolved); + $this->assertArrayHasKey("xmpname", $resolved); + $this->assertArrayHasKey("xsdfilename", $resolved); + $this->assertArrayHasKey("schematronfilename", $resolved); + + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['name'], $resolved["name"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['altname'], $resolved["altname"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['description'], $resolved["description"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['contextparameter'], $resolved["contextparameter"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['attachmentfilename'], $resolved["attachmentfilename"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['xmpname'], $resolved["xmpname"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['xsdfilename'], $resolved["xsdfilename"]); + $this->assertEquals(OrderProfiles::PROFILEDEF[OrderProfiles::PROFILE_COMFORT]['schematronfilename'], $resolved["schematronfilename"]); + } + + /** + * @covers \horstoeko\orderx\OrderProfileResolver::resolveProfileDefById + */ + public function testResolveProfileDefByIdUnknown() + { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Could not determine the profile...'); + + OrderProfileResolver::resolveProfileDefById(-1); + } +}