diff --git a/build/phpunit.xml b/build/phpunit.xml
index 66b7ee74..348a0365 100644
--- a/build/phpunit.xml
+++ b/build/phpunit.xml
@@ -44,6 +44,9 @@
../tests/testcases/ValidatorValidTest.php
../tests/testcases/ValidatorInvalidTest.php
+
+ ../tests/testcases/JsonExporterTest.php
+
../tests/testcases/issues/Issue10Test.php
diff --git a/src/ZugferdDocumentJsonExporter.php b/src/ZugferdDocumentJsonExporter.php
index f7e9c6cc..f3e622b7 100644
--- a/src/ZugferdDocumentJsonExporter.php
+++ b/src/ZugferdDocumentJsonExporter.php
@@ -13,10 +13,12 @@
use \GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\XmlSchemaDateHandler;
use \horstoeko\stringmanagement\PathUtils;
use \horstoeko\zugferd\jms\ZugferdTypesHandler;
+use JMS\Serializer\Exception\RuntimeException as ExceptionRuntimeException;
use \JMS\Serializer\Handler\HandlerRegistryInterface;
use \JMS\Serializer\SerializerBuilder;
use \JMS\Serializer\SerializerInterface;
use RuntimeException;
+use stdClass;
/**
* Class representing the export of a zugferd document
@@ -76,18 +78,14 @@ public function toJsonString(): string
/**
* Returns the invoice object as a json object
- *
- * @return \stdClass
- * @throws RuntimeException
+ *
+ * @return null|stdClass
+ * @throws ExceptionRuntimeException
*/
- public function toJsonObject(): \stdClass
+ public function toJsonObject(): ?\stdClass
{
$jsonObject = json_decode($this->toJsonString());
- if ($jsonObject === null) {
- throw new \RuntimeException("Invalid JSON");
- }
-
return $jsonObject;
}
diff --git a/src/ZugferdObjectHelper.php b/src/ZugferdObjectHelper.php
index dc4a7e6a..83e7734b 100644
--- a/src/ZugferdObjectHelper.php
+++ b/src/ZugferdObjectHelper.php
@@ -1548,6 +1548,7 @@ public function tryCallByPathAndReturn($instance, string $methods)
/**
* Call $method if exists, otherwise $method2 is calles with $value
*
+ * @codeCoverageIgnore
* @param object $instance
* @param string $methodToLookFor
* @param string $methodToCall
diff --git a/src/entities/extended/ram/TradeCurrencyExchangeType.php b/src/entities/extended/ram/TradeCurrencyExchangeType.php
index c60b2d57..47579327 100644
--- a/src/entities/extended/ram/TradeCurrencyExchangeType.php
+++ b/src/entities/extended/ram/TradeCurrencyExchangeType.php
@@ -77,7 +77,7 @@ public function setTargetCurrencyCode($targetCurrencyCode)
/**
* Gets as conversionRate
*
- * @return float
+ * @return \horstoeko\zugferd\entities\extended\udt\RateType
*/
public function getConversionRate()
{
@@ -87,7 +87,7 @@ public function getConversionRate()
/**
* Sets a new conversionRate
*
- * @param float $conversionRate
+ * @param \horstoeko\zugferd\entities\extended\udt\RateType $conversionRate
* @return self
*/
public function setConversionRate($conversionRate)
diff --git a/tests/testcases/JsonExporterTest.php b/tests/testcases/JsonExporterTest.php
new file mode 100644
index 00000000..dd5d5a52
--- /dev/null
+++ b/tests/testcases/JsonExporterTest.php
@@ -0,0 +1,56 @@
+toJsonString();
+
+ $this->assertStringStartsWith('{"ExchangedDocumentContext"', $jsonString);
+ $this->assertStringContainsString('{"GuidelineSpecifiedDocumentContextParameter"', $jsonString);
+ }
+
+ /**
+ * @covers \horstoeko\zugferd\ZugferdDocumentJsonExporter::toPrettyJsonString
+ */
+ public function testToPrettyJsonString(): void
+ {
+ $exporter = new ZugferdDocumentJsonExporter(static::$document);
+ $jsonString = $exporter->toPrettyJsonString();
+
+ $this->assertStringStartsWith("{\n \"ExchangedDocumentContext\": {\n \"GuidelineSpecifiedDocumentContextParameter\": {", $jsonString);
+ }
+
+ /**
+ * @covers \horstoeko\zugferd\ZugferdDocumentJsonExporter::toJsonObject
+ */
+ public function testToJsonObject(): void
+ {
+ $exporter = new ZugferdDocumentJsonExporter(static::$document);
+ $jsonObject = $exporter->toJsonObject();
+
+ $this->assertInstanceOf("stdClass", $jsonObject);
+ $this->assertTrue(isset($jsonObject->ExchangedDocumentContext));
+ $this->assertTrue(isset($jsonObject->ExchangedDocumentContext->GuidelineSpecifiedDocumentContextParameter));
+ }
+}
diff --git a/tests/testcases/ObjectHelperEn16931Test.php b/tests/testcases/ObjectHelperEn16931Test.php
index cc959669..482c7f99 100644
--- a/tests/testcases/ObjectHelperEn16931Test.php
+++ b/tests/testcases/ObjectHelperEn16931Test.php
@@ -2,6 +2,7 @@
namespace horstoeko\zugferd\tests\testcases;
+use DateTime;
use \horstoeko\zugferd\tests\TestCase;
use \horstoeko\zugferd\ZugferdProfiles;
use \horstoeko\zugferd\ZugferdObjectHelper;
@@ -147,12 +148,48 @@ public function testGetTextTypeNullValue(): void
public function testGetCodeTypeWithValue(): void
{
/**
- * @var \horstoeko\zugferd\entities\en16931\udt\TextType
+ * @var \horstoeko\zugferd\entities\en16931\udt\CodeType
*/
$texttype = self::$objectHelper->getCodeType("test");
$this->assertEquals("test", $texttype->value());
}
+ /**
+ * @covers \horstoeko\zugferd\ZugferdObjectHelper::getCodeType2
+ */
+ public function testGetCodeType2WithValue(): void
+ {
+ /**
+ * @var \horstoeko\zugferd\entities\en16931\udt\CodeType
+ */
+ $texttype = self::$objectHelper->getCodeType2("test");
+ $this->assertEquals("test", $texttype->value());
+ $this->assertNull($texttype->getListID());
+ $this->assertNull($texttype->getListVersionID());
+
+ /**
+ * @var \horstoeko\zugferd\entities\en16931\udt\CodeType
+ */
+ $texttype = self::$objectHelper->getCodeType2("test", "listid");
+ $this->assertEquals("test", $texttype->value());
+ $this->assertEquals("listid", $texttype->getListID());
+ $this->assertNull($texttype->getListVersionID());
+
+ /**
+ * @var \horstoeko\zugferd\entities\en16931\udt\CodeType
+ */
+ $texttype = self::$objectHelper->getCodeType2("test", "listid", "listversion");
+ $this->assertEquals("test", $texttype->value());
+ $this->assertEquals("listid", $texttype->getListID());
+ $this->assertEquals("listversion", $texttype->getListVersionID());
+
+ /**
+ * @var \horstoeko\zugferd\entities\en16931\udt\CodeType
+ */
+ $texttype = self::$objectHelper->getCodeType2();
+ $this->assertNull($texttype);
+ }
+
/**
* @covers \horstoeko\zugferd\ZugferdObjectHelper::getCodeType
*/
@@ -1465,6 +1502,12 @@ public function testGetProductCharacteristicType(): void
$productCharacteristic = self::$objectHelper->getProductCharacteristicType("typecode", "description", 0, "valuemeasureunit", "value");
$this->assertEquals("description", $productCharacteristic->getDescription());
$this->assertEquals("value", $productCharacteristic->getValue());
+
+ /**
+ * @var \horstoeko\zugferd\entities\en16931\ram\ProductCharacteristicType
+ */
+ $productCharacteristic = self::$objectHelper->getProductCharacteristicType();
+ $this->assertNull($productCharacteristic);
}
/**
@@ -1479,6 +1522,12 @@ public function testGetProductClassificationType(): void
$this->assertEquals("classcode", $productClassification->getClassCode()->value());
$this->assertEquals("listid", $productClassification->getClassCode()->getListID());
$this->assertEquals("listversionid", $productClassification->getClassCode()->getListVersionID());
+
+ /**
+ * @var \horstoeko\zugferd\entities\en16931\ram\ProductClassificationType
+ */
+ $productClassification = self::$objectHelper->getProductClassificationType();
+ $this->assertNull($productClassification);
}
/**
@@ -1491,6 +1540,12 @@ public function testGetReferencedProductType(): void
*/
$referencedProduct = self::$objectHelper->getReferencedProductType("globalid", "globalidtype", "sellerid", "buyerid", "name", "description", 10, "C62");
$this->assertNull($referencedProduct, "The referenced product is not available in EN16931 profile");
+
+ /**
+ * @var \horstoeko\zugferd\entities\extended\ram\ReferencedProductType
+ */
+ $referencedProduct = self::$objectHelper->getReferencedProductType(null, null, null, null, null, null, null, null);
+ $this->assertNull($referencedProduct);
}
/**
@@ -1503,6 +1558,12 @@ public function testGetCountryIDType(): void
*/
$countryId = self::$objectHelper->getCountryIDType("DE");
$this->assertEquals("DE", $countryId->value());
+
+ /**
+ * @var \horstoeko\zugferd\entities\en16931\qdt\CountryIDType
+ */
+ $countryId = self::$objectHelper->getCountryIDType();
+ $this->assertNull($countryId);
}
/**
@@ -1515,6 +1576,12 @@ public function testGetTradeCountryType(): void
*/
$tradeCountry = self::$objectHelper->getTradeCountryType("DE");
$this->assertEquals("DE", $tradeCountry->getID());
+
+ /**
+ * @var \horstoeko\zugferd\entities\en16931\ram\TradeCountryType
+ */
+ $tradeCountry = self::$objectHelper->getTradeCountryType();
+ $this->assertNull($tradeCountry);
}
/**
@@ -1639,6 +1706,36 @@ public function testToDateTime204(): void
$this->assertEquals("20200202103145", self::$objectHelper->toDateTime("20200202103145", "204")->format("YmdHis"));
}
+ /**
+ * @covers \horstoeko\zugferd\ZugferdObjectHelper::getRateType
+ */
+ public function testGetRateType(): void
+ {
+ /**
+ * @var \horstoeko\zugferd\entities\extended\udt\RateType
+ */
+ $rateType = self::$objectHelper->getRateType(10);
+ $this->assertNull($rateType);
+ }
+
+ /**
+ * @covers \horstoeko\zugferd\ZugferdObjectHelper::getTaxApplicableTradeCurrencyExchangeType
+ */
+ public function testGetTaxApplicableTradeCurrencyExchangeType(): void
+ {
+ /**
+ * @var \horstoeko\zugferd\entities\extended\ram\TradeCurrencyExchangeType
+ */
+ $currencyExchangeType = self::$objectHelper->getTaxApplicableTradeCurrencyExchangeType("EUR", "USD", 10.0, DateTime::createFromFormat("Ymd", "20180305"));
+ $this->assertNull($currencyExchangeType);
+
+ /**
+ * @var \horstoeko\zugferd\entities\extended\ram\TradeCurrencyExchangeType
+ */
+ $currencyExchangeType = self::$objectHelper->getTaxApplicableTradeCurrencyExchangeType(null, null, null, null);
+ $this->assertNull($currencyExchangeType);
+ }
+
/**
* @covers \horstoeko\zugferd\ZugferdObjectHelper::createClassInstance
*/
diff --git a/tests/testcases/ObjectHelperExtendedTest.php b/tests/testcases/ObjectHelperExtendedTest.php
index cd201893..a00b02ce 100644
--- a/tests/testcases/ObjectHelperExtendedTest.php
+++ b/tests/testcases/ObjectHelperExtendedTest.php
@@ -2,6 +2,7 @@
namespace horstoeko\zugferd\tests\testcases;
+use DateTime;
use \horstoeko\zugferd\tests\TestCase;
use \horstoeko\zugferd\ZugferdProfiles;
use \horstoeko\zugferd\ZugferdObjectHelper;
@@ -153,6 +154,42 @@ public function testGetCodeTypeWithValue(): void
$this->assertEquals("test", $texttype->value());
}
+ /**
+ * @covers \horstoeko\zugferd\ZugferdObjectHelper::getCodeType2
+ */
+ public function testGetCodeType2WithValue(): void
+ {
+ /**
+ * @var \horstoeko\zugferd\entities\extended\udt\CodeType
+ */
+ $texttype = self::$objectHelper->getCodeType2("test");
+ $this->assertEquals("test", $texttype->value());
+ $this->assertNull($texttype->getListID());
+ $this->assertNull($texttype->getListVersionID());
+
+ /**
+ * @var \horstoeko\zugferd\entities\extended\udt\CodeType
+ */
+ $texttype = self::$objectHelper->getCodeType2("test", "listid");
+ $this->assertEquals("test", $texttype->value());
+ $this->assertEquals("listid", $texttype->getListID());
+ $this->assertNull($texttype->getListVersionID());
+
+ /**
+ * @var \horstoeko\zugferd\entities\extended\udt\CodeType
+ */
+ $texttype = self::$objectHelper->getCodeType2("test", "listid", "listversion");
+ $this->assertEquals("test", $texttype->value());
+ $this->assertEquals("listid", $texttype->getListID());
+ $this->assertEquals("listversion", $texttype->getListVersionID());
+
+ /**
+ * @var \horstoeko\zugferd\entities\extended\udt\CodeType
+ */
+ $texttype = self::$objectHelper->getCodeType2();
+ $this->assertNull($texttype);
+ }
+
/**
* @covers \horstoeko\zugferd\ZugferdObjectHelper::getCodeType
*/
@@ -1527,6 +1564,12 @@ public function testGetProductClassificationType(): void
$this->assertEquals("listid", $productClassification->getClassCode()->getListID());
$this->assertEquals("listversionid", $productClassification->getClassCode()->getListVersionID());
$this->assertEquals("classname", $productClassification->getClassName());
+
+ /**
+ * @var \horstoeko\zugferd\entities\extended\ram\ProductCharacteristicType
+ */
+ $productCharacteristic = self::$objectHelper->getProductCharacteristicType();
+ $this->assertNull($productCharacteristic);
}
/**
@@ -1546,6 +1589,12 @@ public function testGetReferencedProductType(): void
$this->assertEquals("description", $referencedProduct->getDescription());
$this->assertEquals(10, $referencedProduct->getUnitQuantity()->value());
$this->assertEquals("C62", $referencedProduct->getUnitQuantity()->getUnitCode());
+
+ /**
+ * @var \horstoeko\zugferd\entities\extended\ram\ReferencedProductType
+ */
+ $referencedProduct = self::$objectHelper->getReferencedProductType(null, null, null, null, null, null, null, null);
+ $this->assertNull($referencedProduct);
}
/**
@@ -1658,6 +1707,39 @@ public function testToDateTime204(): void
$this->assertEquals("20200202103145", self::$objectHelper->toDateTime("20200202103145", "204")->format("YmdHis"));
}
+ /**
+ * @covers \horstoeko\zugferd\ZugferdObjectHelper::getRateType
+ */
+ public function testGetRateType(): void
+ {
+ /**
+ * @var \horstoeko\zugferd\entities\extended\udt\RateType
+ */
+ $rateType = self::$objectHelper->getRateType(10);
+ $this->assertEquals(10, $rateType->value());
+ }
+
+ /**
+ * @covers \horstoeko\zugferd\ZugferdObjectHelper::getTaxApplicableTradeCurrencyExchangeType
+ */
+ public function testGetTaxApplicableTradeCurrencyExchangeType(): void
+ {
+ /**
+ * @var \horstoeko\zugferd\entities\extended\ram\TradeCurrencyExchangeType
+ */
+ $currencyExchangeType = self::$objectHelper->getTaxApplicableTradeCurrencyExchangeType("EUR", "USD", 10.0, DateTime::createFromFormat("Ymd", "20180305"));
+ $this->assertNotNull($currencyExchangeType);
+ $this->assertEquals("EUR", $currencyExchangeType->getSourceCurrencyCode());
+ $this->assertEquals("USD", $currencyExchangeType->getTargetCurrencyCode());
+ $this->assertEquals(10.0, $currencyExchangeType->getConversionRate()->value());
+
+ /**
+ * @var \horstoeko\zugferd\entities\extended\ram\TradeCurrencyExchangeType
+ */
+ $currencyExchangeType = self::$objectHelper->getTaxApplicableTradeCurrencyExchangeType(null, null, null, null);
+ $this->assertNull($currencyExchangeType);
+ }
+
/**
* @covers \horstoeko\zugferd\ZugferdObjectHelper::createClassInstance
*/