Skip to content

Commit

Permalink
Merge pull request #106 from horstoeko/Issue_104
Browse files Browse the repository at this point in the history
#104 Use LibXML Internal errors in ProfileResolver
  • Loading branch information
horstoeko authored Sep 4, 2024
2 parents 164e57a + c05d6d4 commit d967bfe
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<!--
[![CI](https://github.com/horstoeko/zugferd/actions/workflows/build.ci.yml/badge.svg)](https://github.com/horstoeko/zugferd/actions/workflows/build.ci.yml)
-->

<!--
[![CI (Ant, PHP 7.3)](https://github.com/horstoeko/zugferd/actions/workflows/build.php73.ant.yml/badge.svg)](https://github.com/horstoeko/zugferd/actions/workflows/build.php73.ant.yml)
[![CI (Ant, PHP 7.4)](https://github.com/horstoeko/zugferd/actions/workflows/build.php74.ant.yml/badge.svg)](https://github.com/horstoeko/zugferd/actions/workflows/build.php74.ant.yml)
[![CI (PHP 8.0)](https://github.com/horstoeko/zugferd/actions/workflows/build.php80.ant.yml/badge.svg)](https://github.com/horstoeko/zugferd/actions/workflows/build.php80.ant.yml)
[![CI (PHP 8.1)](https://github.com/horstoeko/zugferd/actions/workflows/build.php81.ant.yml/badge.svg)](https://github.com/horstoeko/zugferd/actions/workflows/build.php81.ant.yml)
[![CI (PHP 8.2)](https://github.com/horstoeko/zugferd/actions/workflows/build.php82.ant.yml/badge.svg)](https://github.com/horstoeko/zugferd/actions/workflows/build.php82.ant.yml)
[![CI (PHP 8.3)](https://github.com/horstoeko/zugferd/actions/workflows/build.php83.ant.yml/badge.svg)](https://github.com/horstoeko/zugferd/actions/workflows/build.php83.ant.yml)
-->


## Table of Contents

Expand Down
10 changes: 9 additions & 1 deletion src/ZugferdProfileResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace horstoeko\zugferd;

use Throwable;
use SimpleXMLElement;
use horstoeko\zugferd\ZugferdProfiles;
use horstoeko\zugferd\exception\ZugferdUnknownProfileException;
Expand All @@ -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])) {
Expand Down
1 change: 1 addition & 0 deletions tests/assets/invalid2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bad XML
15 changes: 14 additions & 1 deletion tests/testcases/issues/Issue104Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@ class Issue104Test extends TestCase
* @return void
* @issue 104
*/
public function testInvalidException()
public function testInvalidException1()
{
$this->expectException(ZugferdUnknownXmlContentException::class);

$document = ZugferdDocumentReader::readAndGuessFromFile(dirname(__FILE__) . '/../../assets/invalid.xml');

$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);
}
}

0 comments on commit d967bfe

Please sign in to comment.