2024-05-18 15:04:53 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Integration\Einvoice;
|
|
|
|
|
|
|
|
use Tests\TestCase;
|
2024-05-19 23:50:39 +02:00
|
|
|
use Sabre\Xml\Reader;
|
|
|
|
use Sabre\Xml\Service;
|
2024-05-18 15:04:53 +02:00
|
|
|
use Invoiceninja\Einvoice\Models\FACT1\Invoice;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
class FACT1Test extends TestCase
|
|
|
|
{
|
|
|
|
|
2024-05-19 23:50:39 +02:00
|
|
|
public array $set = [];
|
2024-05-18 15:04:53 +02:00
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
public function testValidationFact1()
|
|
|
|
{
|
2024-05-18 15:04:53 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
$files = [
|
|
|
|
'tests/Integration/Einvoice/samples/fact1_no_prefixes.xml',
|
|
|
|
];
|
2024-05-18 15:04:53 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
foreach($files as $f) {
|
2024-05-18 15:04:53 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
$xml = file_get_contents($f);
|
2024-05-18 15:04:53 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
$xml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
|
|
|
|
$json = json_encode($xml);
|
|
|
|
$array = json_decode($json, true);
|
2024-05-18 15:04:53 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
$i = Invoice::from($array);
|
|
|
|
|
|
|
|
$rules = Invoice::getValidationRules($array);
|
2024-05-18 15:04:53 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
$this->assertIsArray($rules);
|
2024-05-18 15:04:53 +02:00
|
|
|
|
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
$validation_array = Invoice::validate($array);
|
|
|
|
|
|
|
|
$this->assertIsArray($validation_array);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2024-05-18 15:04:53 +02:00
|
|
|
|
|
|
|
|
2024-05-19 23:50:39 +02:00
|
|
|
public function removeNamespacesFromArray($data)
|
|
|
|
{
|
|
|
|
if (is_array($data)) {
|
|
|
|
foreach ($data as &$item) {
|
|
|
|
if (isset($item['name'])) {
|
|
|
|
// Remove the namespace from the name
|
|
|
|
$item['name'] = preg_replace('/^\{\}(.+)/', '$1', $item['name']);
|
|
|
|
}
|
|
|
|
if (isset($item['value']) && is_array($item['value'])) {
|
|
|
|
// Recursively process child elements
|
|
|
|
$item['value'] = $this->removeNamespacesFromArray($item['value']);
|
|
|
|
}
|
|
|
|
if (isset($item['attributes'])) {
|
|
|
|
unset($item['attributes']);
|
2024-05-20 05:26:34 +02:00
|
|
|
|
2024-05-19 23:50:39 +02:00
|
|
|
}
|
2024-05-18 15:04:53 +02:00
|
|
|
}
|
2024-05-19 23:50:39 +02:00
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// function convertToKeyValue($data)
|
|
|
|
// {
|
|
|
|
// $result = [];
|
|
|
|
// foreach ($data as $item) {
|
|
|
|
// // Remove namespace prefix if present
|
|
|
|
// $name = preg_replace('/^\{\}(.+)/', '$1', $item['name']);
|
|
|
|
// $result[$name] = $item['value'];
|
|
|
|
// }
|
|
|
|
// return $result;
|
|
|
|
// }
|
2024-05-19 23:50:39 +02:00
|
|
|
|
2024-05-18 15:04:53 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// public function keyValueDeserializer(Reader $reader)
|
|
|
|
// {
|
|
|
|
// $values = [];
|
|
|
|
// $reader->read();
|
|
|
|
// $reader->next();
|
|
|
|
// foreach ($reader->parseGetElements() as $element) {
|
|
|
|
// // Strip the namespace prefix
|
|
|
|
// echo "merp".PHP_EOL;
|
|
|
|
// $name = preg_replace('/^\{\}.*/', '', $element['name']);
|
|
|
|
// $values[$name] = $element['value'];
|
|
|
|
// }
|
|
|
|
// return $values;
|
|
|
|
// }
|
2024-05-19 23:50:39 +02:00
|
|
|
|
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// public function testFactToArray()
|
|
|
|
// {
|
2024-05-19 23:50:39 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// $xml = file_get_contents('tests/Integration/Einvoice/samples/fact1_no_prefixes.xml');
|
|
|
|
// $service = new Service();
|
2024-05-19 23:50:39 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// // $service->elementMap = [
|
|
|
|
// // '{}' => 'Sabre\Xml\Deserializer\keyValue',
|
|
|
|
// // ];
|
2024-05-18 15:04:53 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// // $service->elementMap = [
|
|
|
|
// // '{}*' => function (Reader $reader) use ($service) {
|
|
|
|
// // return $this->keyValueDeserializer($reader);
|
|
|
|
// // }
|
|
|
|
// // ];
|
2024-05-19 23:50:39 +02:00
|
|
|
|
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// $result = $this->removeNamespacesFromArray($service->parse($xml));
|
2024-05-19 23:50:39 +02:00
|
|
|
|
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// // Convert parsed XML to key-value array
|
|
|
|
// if (isset($result['value']) && is_array($result['value'])) {
|
|
|
|
// $keyValueArray = $this->convertToKeyValue($result['value']);
|
|
|
|
// } else {
|
|
|
|
// $keyValueArray = [];
|
|
|
|
// }
|
2024-05-18 15:04:53 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// // Output the result
|
|
|
|
// nlog($keyValueArray);
|
2024-05-19 23:50:39 +02:00
|
|
|
|
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// // nlog($cleanedArray);
|
|
|
|
// nlog($service->parse($xml));
|
2024-05-19 23:50:39 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// }
|
2024-05-19 23:50:39 +02:00
|
|
|
|
|
|
|
// Output the result
|
|
|
|
// ($xmlWithoutNamespaces);
|
|
|
|
|
|
|
|
// $reader = new Reader();
|
|
|
|
// $service = new Service();
|
|
|
|
|
|
|
|
// $service->elementMap = [
|
|
|
|
// '*' => 'Sabre\Xml\Deserializer\keyValue',
|
|
|
|
// ];
|
|
|
|
|
|
|
|
// nlog($service->parse($xmlstring));
|
|
|
|
|
|
|
|
// $payload ='';
|
|
|
|
|
|
|
|
// // $reader->xml($xmlstring);
|
|
|
|
// // $payload = $reader->parse();
|
|
|
|
|
|
|
|
// // nlog($payload);
|
|
|
|
// $validation_array = false;
|
|
|
|
// try {
|
|
|
|
// $rules = Invoice::getValidationRules($payload);
|
|
|
|
// nlog($rules);
|
|
|
|
|
|
|
|
// $this->assertIsArray($rules);
|
|
|
|
|
|
|
|
// $payload = Invoice::from($payload)->toArray();
|
|
|
|
// nlog($payload);
|
|
|
|
// $this->assertIsArray($payload);
|
|
|
|
|
|
|
|
// $validation_array = Invoice::validate($payload);
|
|
|
|
|
|
|
|
// $this->assertIsArray($validation_array);
|
|
|
|
|
|
|
|
// } catch(\Illuminate\Validation\ValidationException $e) {
|
|
|
|
|
|
|
|
// nlog($e->errors());
|
|
|
|
// }
|
|
|
|
|
|
|
|
// $this->assertIsArray($validation_array);
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// private function extractName($name): string
|
|
|
|
// {
|
2024-05-19 23:50:39 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// $pattern = '/\{[^{}]*\}([^{}]*)/';
|
2024-05-19 23:50:39 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// if (preg_match($pattern, $name, $matches)) {
|
|
|
|
// $extracted = $matches[1];
|
|
|
|
// return $extracted;
|
|
|
|
// }
|
2024-05-18 15:04:53 +02:00
|
|
|
|
2024-05-20 05:26:34 +02:00
|
|
|
// return $name;
|
|
|
|
// }
|
2024-05-18 15:04:53 +02:00
|
|
|
}
|