Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#104 Use LibXML Internal errors in ProfileResolver #106

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}