Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Oct 13, 2024
1 parent 05932ec commit 64245e9
Show file tree
Hide file tree
Showing 20 changed files with 85 additions and 6,468 deletions.
49 changes: 33 additions & 16 deletions src/OrderProfileResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

namespace horstoeko\orderx;

use Exception;
use Throwable;
use SimpleXMLElement;
use horstoeko\orderx\OrderProfiles;
use horstoeko\orderx\exception\OrderUnknownXmlContentException;
use horstoeko\orderx\exception\OrderCannotFindProfileString;
use horstoeko\orderx\exception\OrderUnknownProfileException;
use horstoeko\orderx\OrderProfiles;
use SimpleXMLElement;

/**
* Class representing the profile resolver
Expand All @@ -29,14 +30,28 @@ class OrderProfileResolver
/**
* Resolve profile id and profile definition by the content of $xmlContent
*
* @param string $xmlContent
* @param string $xmlContent
* @return array
* @throws Exception
* @throws OrderCannotFindProfileString
* @throws OrderUnknownProfileException
*/
public static function resolve(string $xmlContent): array
{
$xmldocument = new SimpleXMLElement($xmlContent);
$typeelement = $xmldocument->xpath('/rsm:SCRDMCCBDACIOMessageStructure/rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID');
$prevUseInternalErrors = \libxml_use_internal_errors(true);

try {
libxml_clear_errors();
$xmldocument = new SimpleXMLElement($xmlContent);
$typeelement = $xmldocument->xpath('/rsm:SCRDMCCBDACIOMessageStructure/rsm:ExchangedDocumentContext/ram:GuidelineSpecifiedDocumentContextParameter/ram:ID');
if (libxml_get_last_error()) {
throw new OrderUnknownXmlContentException();
}
} catch (Throwable $e) {
throw new OrderUnknownXmlContentException();
} finally {
libxml_clear_errors();
libxml_use_internal_errors($prevUseInternalErrors);
}

if (!is_array($typeelement) || !isset($typeelement[0])) {
throw new OrderCannotFindProfileString();
Expand All @@ -58,9 +73,10 @@ public static function resolve(string $xmlContent): array
/**
* Resolve profile id by the content of $xmlContent
*
* @param string $xmlContent
* @param string $xmlContent
* @return int
* @throws Exception
* @throws OrderCannotFindProfileString
* @throws OrderUnknownProfileException
*/
public static function resolveProfileId(string $xmlContent): int
{
Expand All @@ -70,9 +86,10 @@ public static function resolveProfileId(string $xmlContent): int
/**
* Resolve profile definition by the content of $xmlContent
*
* @param string $xmlContent
* @param string $xmlContent
* @return array
* @throws Exception
* @throws OrderCannotFindProfileString
* @throws OrderUnknownProfileException
*/
public static function resolveProfileDef(string $xmlContent): array
{
Expand All @@ -82,14 +99,14 @@ public static function resolveProfileDef(string $xmlContent): array
/**
* Resolve profile id and profile definition by it's id
*
* @param integer $profileId
* @param int $profileId
* @return array
* @throws Exception
* @throws OrderUnknownProfileException
*/
public static function resolveById(int $profileId): array
{
if (!isset(OrderProfiles::PROFILEDEF[$profileId])) {
throw new Exception('Could not determine the profile...');
throw new OrderUnknownProfileException('Could not determine the profile...');
}

return [$profileId, OrderProfiles::PROFILEDEF[$profileId]];
Expand All @@ -98,9 +115,9 @@ public static function resolveById(int $profileId): array
/**
* Resolve profile profile definition by it's id
*
* @param int $profileId
* @param int $profileId
* @return array
* @throws Exception
* @throws OrderUnknownProfileException
*/
public static function resolveProfileDefById(int $profileId): array
{
Expand Down
51 changes: 51 additions & 0 deletions src/exception/OrderUnknownXmlContentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* This file is a part of horstoeko/zugferd.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace horstoeko\orderx\exception;

use Exception;
use Throwable;

/**
* Class representing an exception for unknown profile
*
* @category Zugferd
* @package Zugferd
* @author D. Erling <[email protected]>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/horstoeko/zugferd
*/
class OrderUnknownXmlContentException extends Exception
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct($this->buildMessage());
}

/**
* @inheritDoc
*/
public function __toString()
{
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}

/**
* Build the message
*
* @return string
*/
private function buildMessage(): string
{
return "The XML does not match the requirements for an XML in CII-Syntax";
}
}
12 changes: 0 additions & 12 deletions tests/testcases/OrderDocumentBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

class OrderDocumentBaseTest extends TestCase
{
/**
* @covers \horstoeko\orderx\OrderDocument
*/
public function testDocumentCreationBasic(): void
{
$doc = OrderDocumentBuilder::createNew(OrderProfiles::PROFILE_BASIC);
Expand All @@ -23,9 +20,6 @@ public function testDocumentCreationBasic(): void
$this->assertEquals("basic", $doc->getProfileDefinitionParameter("name"));
}

/**
* @covers \horstoeko\orderx\OrderDocument
*/
public function testDocumentCreationComfort(): void
{
$doc = OrderDocumentBuilder::createNew(OrderProfiles::PROFILE_COMFORT);
Expand All @@ -37,9 +31,6 @@ public function testDocumentCreationComfort(): void
$this->assertEquals("comfort", $doc->getProfileDefinitionParameter("name"));
}

/**
* @covers \horstoeko\orderx\OrderDocument
*/
public function testDocumentCreationExtended(): void
{
$doc = OrderDocumentBuilder::createNew(OrderProfiles::PROFILE_EXTENDED);
Expand All @@ -51,9 +42,6 @@ public function testDocumentCreationExtended(): void
$this->assertEquals("extended", $doc->getProfileDefinitionParameter("name"));
}

/**
* @covers \horstoeko\orderx\OrderDocument
*/
public function testDocumentInternals(): void
{
$doc = OrderDocumentBuilder::createNew(OrderProfiles::PROFILE_EXTENDED);
Expand Down
Loading

0 comments on commit 64245e9

Please sign in to comment.