From c05d6d43d9779613fa5d7dbc445b7426c3691b98 Mon Sep 17 00:00:00 2001 From: HorstOeko Date: Tue, 3 Sep 2024 17:08:19 +0200 Subject: [PATCH] #104 Use LibXML Internal errors in ProfileResolver --- README.md | 5 +++-- src/ZugferdProfileResolver.php | 10 +++++++++- tests/assets/invalid2.xml | 1 + tests/testcases/issues/Issue104Test.php | 15 ++++++++++++++- 4 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 tests/assets/invalid2.xml diff --git a/README.md b/README.md index 13df2a8..9c22b97 100644 --- a/README.md +++ b/README.md @@ -4,16 +4,17 @@ [![PHP version](https://img.shields.io/packagist/php-v/horstoeko/zugferd.svg?style=plastic)](https://packagist.org/packages/horstoeko/zugferd) [![License](https://img.shields.io/packagist/l/horstoeko/zugferd.svg?style=plastic)](https://packagist.org/packages/horstoeko/zugferd) + - + ## Table of Contents diff --git a/src/ZugferdProfileResolver.php b/src/ZugferdProfileResolver.php index 33b8ea5..d653d89 100644 --- a/src/ZugferdProfileResolver.php +++ b/src/ZugferdProfileResolver.php @@ -9,6 +9,7 @@ namespace horstoeko\zugferd; +use Throwable; use SimpleXMLElement; use horstoeko\zugferd\ZugferdProfiles; use horstoeko\zugferd\exception\ZugferdUnknownProfileException; @@ -34,11 +35,18 @@ class ZugferdProfileResolver */ public static function resolve(string $xmlContent): array { + $prevUseInternalErrors = \libxml_use_internal_errors(true); + try { $xmldocument = new SimpleXMLElement($xmlContent); $typeelement = $xmldocument->xpath('/rsm:CrossIndustryInvoice/rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID'); - } catch (\Throwable $e) { + if (libxml_get_last_error()) { + throw new ZugferdUnknownXmlContentException(); + } + } catch (Throwable $e) { throw new ZugferdUnknownXmlContentException(); + } finally { + libxml_use_internal_errors($prevUseInternalErrors); } if (!is_array($typeelement) || !isset($typeelement[0])) { diff --git a/tests/assets/invalid2.xml b/tests/assets/invalid2.xml new file mode 100644 index 0000000..93e4c61 --- /dev/null +++ b/tests/assets/invalid2.xml @@ -0,0 +1 @@ +Bad XML \ No newline at end of file diff --git a/tests/testcases/issues/Issue104Test.php b/tests/testcases/issues/Issue104Test.php index 21114c1..cd75851 100644 --- a/tests/testcases/issues/Issue104Test.php +++ b/tests/testcases/issues/Issue104Test.php @@ -12,7 +12,7 @@ class Issue104Test extends TestCase * @return void * @issue 104 */ - public function testInvalidException() + public function testInvalidException1() { $this->expectException(ZugferdUnknownXmlContentException::class); @@ -20,4 +20,17 @@ public function testInvalidException() $this->assertNull($document); } + + /** + * @return void + * @issue 104 + */ + public function testInvalidException2() + { + $this->expectException(ZugferdUnknownXmlContentException::class); + + $document = ZugferdDocumentReader::readAndGuessFromFile(dirname(__FILE__) . '/../../assets/invalid2.xml'); + + $this->assertNull($document); + } } \ No newline at end of file