From 6855c48d19028bd112f934c5283d2812f1c8147e Mon Sep 17 00:00:00 2001 From: Markus Thielen Date: Tue, 28 Jan 2025 08:43:08 +0100 Subject: [PATCH] * DtdValidator can be instantiated with custom dtds * DtdValidator can be instantiated for a directory with DTDs `::forDtdDirectory()` --- src/CXml/Validation/DtdValidator.php | 27 ++++++++++--------- tests/CXmlTest/Handling/HandlerTest.php | 2 +- tests/CXmlTest/Model/OrderRequestTest.php | 2 +- .../Model/ProductActivityMessageTest.php | 2 +- .../Model/PunchOutSetupRequestTest.php | 2 +- ...unchoutOrderMessageAdvancedPricingTest.php | 2 +- .../Model/PunchoutOrderMessageTest.php | 2 +- tests/CXmlTest/Model/QuoteMessageTest.php | 2 +- .../CXmlTest/Model/ShipNoticeRequestTest.php | 2 +- .../Model/StatusUpdateRequestTest.php | 2 +- .../Validation/MessageValidatorTest.php | 2 +- 11 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/CXml/Validation/DtdValidator.php b/src/CXml/Validation/DtdValidator.php index 261da47..245a427 100644 --- a/src/CXml/Validation/DtdValidator.php +++ b/src/CXml/Validation/DtdValidator.php @@ -15,12 +15,20 @@ readonly class DtdValidator { - public function __construct(private string $pathToCxmlDtds) + public function __construct( + private array $pathToDtds, + ) { + Assertion::notEmpty($pathToDtds); + } + + public static function forDtdDirectory(string $directory): self { - Assertion::directory($pathToCxmlDtds); - Assertion::file($pathToCxmlDtds . '/cXML.dtd'); - Assertion::file($pathToCxmlDtds . '/Fulfill.dtd'); - Assertion::file($pathToCxmlDtds . '/Quote.dtd'); + Assertion::directory($directory); + + $pathToDtds = glob($directory . '/*.dtd'); + Assertion::notEmpty($pathToDtds); + + return new self($pathToDtds); } /** @@ -38,12 +46,7 @@ public function validateAgainstDtd(string $xml): void $old = new DOMDocument(); $old->loadXML($xml); - $validateFiles = ['cXML.dtd', 'Fulfill.dtd', 'Quote.dtd']; - if (file_exists($this->pathToCxmlDtds . '/Custom.dtd')) { - $validateFiles[] = 'Custom.dtd'; - } - - $this->validateAgainstMultipleDtd($validateFiles, $old); + $this->validateAgainstMultipleDtd($this->pathToDtds, $old); // reset throwing of php errors for libxml libxml_use_internal_errors($internalErrors); @@ -57,7 +60,7 @@ private function injectDtd(DOMDocument $originalDomDocument, string $dtdFilename $creator = new DOMImplementation(); try { - $doctype = $creator->createDocumentType('cXML', '', $this->pathToCxmlDtds . '/' . $dtdFilename); + $doctype = $creator->createDocumentType('cXML', '', $dtdFilename); $new = $creator->createDocument('', '', $doctype); } catch (DOMException $domException) { throw new CXmlInvalidException($domException->getMessage(), (string)$originalDomDocument->saveXML(), $domException); diff --git a/tests/CXmlTest/Handling/HandlerTest.php b/tests/CXmlTest/Handling/HandlerTest.php index 8872d1f..1d4c00c 100644 --- a/tests/CXmlTest/Handling/HandlerTest.php +++ b/tests/CXmlTest/Handling/HandlerTest.php @@ -52,7 +52,7 @@ private static function loadFixture(string $filename): ?string public function testEndpoint(string $requestCxml, string $expectedHandlerCalled): void { $serializer = Serializer::create(); - $messageValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050'); + $messageValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050'); $credentialRepository = new Registry(); $credentialRepository->registerCredential( diff --git a/tests/CXmlTest/Model/OrderRequestTest.php b/tests/CXmlTest/Model/OrderRequestTest.php index 9eb184d..bf91a8c 100644 --- a/tests/CXmlTest/Model/OrderRequestTest.php +++ b/tests/CXmlTest/Model/OrderRequestTest.php @@ -43,7 +43,7 @@ final class OrderRequestTest extends TestCase implements PayloadIdentityFactoryI protected function setUp(): void { - $this->dtdValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); + $this->dtdValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); } public function testMinimumExample(): void diff --git a/tests/CXmlTest/Model/ProductActivityMessageTest.php b/tests/CXmlTest/Model/ProductActivityMessageTest.php index f8100a4..aa85846 100644 --- a/tests/CXmlTest/Model/ProductActivityMessageTest.php +++ b/tests/CXmlTest/Model/ProductActivityMessageTest.php @@ -31,7 +31,7 @@ final class ProductActivityMessageTest extends TestCase implements PayloadIdenti protected function setUp(): void { - $this->dtdValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); + $this->dtdValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); } public function testMinimumExample(): void diff --git a/tests/CXmlTest/Model/PunchOutSetupRequestTest.php b/tests/CXmlTest/Model/PunchOutSetupRequestTest.php index 5cb98fd..2546bd4 100644 --- a/tests/CXmlTest/Model/PunchOutSetupRequestTest.php +++ b/tests/CXmlTest/Model/PunchOutSetupRequestTest.php @@ -41,7 +41,7 @@ final class PunchOutSetupRequestTest extends TestCase implements PayloadIdentity protected function setUp(): void { - $this->dtdValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); + $this->dtdValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); } public function testMinimumExample(): void diff --git a/tests/CXmlTest/Model/PunchoutOrderMessageAdvancedPricingTest.php b/tests/CXmlTest/Model/PunchoutOrderMessageAdvancedPricingTest.php index 557ff6a..a72b647 100644 --- a/tests/CXmlTest/Model/PunchoutOrderMessageAdvancedPricingTest.php +++ b/tests/CXmlTest/Model/PunchoutOrderMessageAdvancedPricingTest.php @@ -32,7 +32,7 @@ final class PunchoutOrderMessageAdvancedPricingTest extends TestCase implements protected function setUp(): void { - $this->dtdValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); + $this->dtdValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); } public function testMinimumExampleAdvPricing(): void diff --git a/tests/CXmlTest/Model/PunchoutOrderMessageTest.php b/tests/CXmlTest/Model/PunchoutOrderMessageTest.php index 67abe60..b0b4dad 100644 --- a/tests/CXmlTest/Model/PunchoutOrderMessageTest.php +++ b/tests/CXmlTest/Model/PunchoutOrderMessageTest.php @@ -32,7 +32,7 @@ final class PunchoutOrderMessageTest extends TestCase implements PayloadIdentity protected function setUp(): void { - $this->dtdValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); + $this->dtdValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); } public function testMinimumExample(): void diff --git a/tests/CXmlTest/Model/QuoteMessageTest.php b/tests/CXmlTest/Model/QuoteMessageTest.php index b5ad51c..9281190 100644 --- a/tests/CXmlTest/Model/QuoteMessageTest.php +++ b/tests/CXmlTest/Model/QuoteMessageTest.php @@ -37,7 +37,7 @@ final class QuoteMessageTest extends TestCase implements PayloadIdentityFactoryI protected function setUp(): void { - $this->dtdValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); + $this->dtdValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); } public function testMinimumExample(): void diff --git a/tests/CXmlTest/Model/ShipNoticeRequestTest.php b/tests/CXmlTest/Model/ShipNoticeRequestTest.php index 8e44a45..e84dfe8 100644 --- a/tests/CXmlTest/Model/ShipNoticeRequestTest.php +++ b/tests/CXmlTest/Model/ShipNoticeRequestTest.php @@ -30,7 +30,7 @@ final class ShipNoticeRequestTest extends TestCase implements PayloadIdentityFac protected function setUp(): void { - $this->dtdValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); + $this->dtdValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); } public function testMinimumExample(): void diff --git a/tests/CXmlTest/Model/StatusUpdateRequestTest.php b/tests/CXmlTest/Model/StatusUpdateRequestTest.php index df2fc92..85cedf2 100644 --- a/tests/CXmlTest/Model/StatusUpdateRequestTest.php +++ b/tests/CXmlTest/Model/StatusUpdateRequestTest.php @@ -26,7 +26,7 @@ final class StatusUpdateRequestTest extends TestCase implements PayloadIdentityF protected function setUp(): void { - $this->dtdValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); + $this->dtdValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.050/'); } public function testMinimumExample(): void diff --git a/tests/CXmlTest/Validation/MessageValidatorTest.php b/tests/CXmlTest/Validation/MessageValidatorTest.php index 4d47e1c..3207818 100644 --- a/tests/CXmlTest/Validation/MessageValidatorTest.php +++ b/tests/CXmlTest/Validation/MessageValidatorTest.php @@ -21,7 +21,7 @@ final class MessageValidatorTest extends TestCase protected function setUp(): void { - $this->dtdValidator = new DtdValidator(__DIR__ . '/../../metadata/cxml/dtd/1.2.063'); + $this->dtdValidator = DtdValidator::forDtdDirectory(__DIR__ . '/../../metadata/cxml/dtd/1.2.063'); } public function testValidateProfileRequestSuccess(): void