Passed
Branch main (249e74)
by Daniel
02:10
created

WriteTest::testGetLocalJsonInvoiceIntoXml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 15
rs 9.8666
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
use PHPUnit\Framework\Attributes\CoversClass;
6
use PHPUnit\Framework\TestCase;
7
8
#[CoversClass(\danielgp\efactura\ElectronicInvoiceWrite::class)]
9
final class WriteTest extends TestCase
10
{
11
    const LOCAL_UBL_EXAMPLES_PATH = __DIR__ . '/UBL_examples/';
12
13
    public function testGetLocalJsonInvoiceIntoXml()
14
    {
15
        $url           = self::LOCAL_UBL_EXAMPLES_PATH . 'Romanian/Invoice.json';
16
        $fileHandle    = fopen($url, 'r');
17
        $jsonData      = fread($fileHandle, ((int) filesize($url)));
18
        fclose($fileHandle);
19
        $arrayData     = json_decode($jsonData, true);
20
        $classWrite    = new \danielgp\efactura\ElectronicInvoiceWrite();
21
        $strTargetFile = __DIR__ . '/' . pathinfo($url)['filename'] . '.xml';
22
        $classWrite->writeElectronicInvoice($strTargetFile, $arrayData, [
23
            'Comments'       => false,
24
            'SchemaLocation' => false,
25
        ]);
26
        $this->assertXmlFileEqualsXmlFile(self::LOCAL_UBL_EXAMPLES_PATH
27
            . 'Romanian/eInvoice_ex.xml', $strTargetFile);
28
    }
29
30
    public function testGetLocalJsonInvoiceIntoXmlWithDefaults()
31
    {
32
        // given file does not contain namespaces, as these elements are Optional and taken from default if not provided
33
        $url           = self::LOCAL_UBL_EXAMPLES_PATH . 'Romanian/Invoice_Data.json';
34
        $fileHandle    = fopen($url, 'r');
35
        $jsonData      = fread($fileHandle, ((int) filesize($url)));
36
        fclose($fileHandle);
37
        $arrayData     = json_decode($jsonData, true);
38
        $classWrite    = new \danielgp\efactura\ElectronicInvoiceWrite();
39
        $strTargetFile = __DIR__ . '/' . pathinfo($url)['filename'] . '_raw.xml';
40
        $classWrite->writeElectronicInvoice($strTargetFile, $arrayData, [
41
            'Comments'       => false,
42
            'SchemaLocation' => false,
43
        ]);
44
        $this->assertXmlFileNotEqualsXmlFile(self::LOCAL_UBL_EXAMPLES_PATH
45
            . 'Romanian/eInvoice_ex.xml', $strTargetFile);
46
    }
47
48
    public function tearDown(): void
49
    {
50
        $arrayFiles = new RecursiveDirectoryIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
51
        foreach ($arrayFiles as $strFile) {
52
            if ($strFile->isFile() && ($strFile->getExtension() === 'xml')) {
53
                unlink($strFile->getRealPath());
54
            }
55
        }
56
    }
57
}
58