Skip to content

Commit

Permalink
More code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Oct 3, 2023
1 parent 1d8899c commit 4a91fd8
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 11 deletions.
3 changes: 3 additions & 0 deletions build/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<file>../tests/testcases/ValidatorValidTest.php</file>
<file>../tests/testcases/ValidatorInvalidTest.php</file>
</testsuite>
<testsuite name="Exporter">
<file>../tests/testcases/JsonExporterTest.php</file>
</testsuite>
<testsuite name="issues">
<file>../tests/testcases/issues/Issue10Test.php</file>
</testsuite>
Expand Down
14 changes: 6 additions & 8 deletions src/ZugferdDocumentJsonExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/ZugferdObjectHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/entities/extended/ram/TradeCurrencyExchangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function setTargetCurrencyCode($targetCurrencyCode)
/**
* Gets as conversionRate
*
* @return float
* @return \horstoeko\zugferd\entities\extended\udt\RateType
*/
public function getConversionRate()
{
Expand All @@ -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)
Expand Down
56 changes: 56 additions & 0 deletions tests/testcases/JsonExporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace horstoeko\zugferd\tests\testcases;

use \horstoeko\zugferd\tests\TestCase;
use \horstoeko\zugferd\ZugferdDocumentJsonExporter;
use \horstoeko\zugferd\ZugferdDocumentReader;

class JsonExporterTest extends TestCase
{
/**
* @var ZugferdDocumentReader
*/
protected static $document;

public static function setUpBeforeClass(): void
{
self::$document = ZugferdDocumentReader::readAndGuessFromFile(dirname(__FILE__) . "/../assets/xrechnung_simple_2.xml");
}

/**
* @covers \horstoeko\zugferd\ZugferdDocumentJsonExporter::toJsonString
*/
public function testToJsonString(): void
{
$exporter = new ZugferdDocumentJsonExporter(static::$document);
$jsonString = $exporter->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));
}
}
99 changes: 98 additions & 1 deletion tests/testcases/ObjectHelperEn16931Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace horstoeko\zugferd\tests\testcases;

use DateTime;
use \horstoeko\zugferd\tests\TestCase;
use \horstoeko\zugferd\ZugferdProfiles;
use \horstoeko\zugferd\ZugferdObjectHelper;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down
Loading

0 comments on commit 4a91fd8

Please sign in to comment.