1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Tests for FACT

This commit is contained in:
David Bomba 2024-05-20 13:26:34 +10:00
parent ec1ee4507b
commit 0aaaccd047
2 changed files with 78 additions and 73 deletions

View File

@ -29,26 +29,34 @@ class FACT1Test extends TestCase
}
// public function testValidationFact1()
// {
public function testValidationFact1()
{
// $files = [
// 'tests/Integration/Einvoice/samples/fact1.xml',
// ];
$files = [
'tests/Integration/Einvoice/samples/fact1_no_prefixes.xml',
];
// foreach($files as $f) {
foreach($files as $f) {
// $xml = file_get_contents($f);
$xml = file_get_contents($f);
// // Remove namespaces and prefixes
// $xmlWithoutNamespacesAndPrefixes = $this->removeNamespacesAndPrefixes($xml);
$xml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json, true);
// // Output the result
// nlog($xmlWithoutNamespacesAndPrefixes);
$i = Invoice::from($array);
$rules = Invoice::getValidationRules($array);
$this->assertIsArray($rules);
$validation_array = Invoice::validate($array);
// }
$this->assertIsArray($validation_array);
}
}
public function removeNamespacesFromArray($data)
@ -65,10 +73,7 @@ class FACT1Test extends TestCase
}
if (isset($item['attributes'])) {
unset($item['attributes']);
// Process attributes if needed
// $item['attributes'] = array_map(function ($attr) {
// return preg_replace('/^\{\}(.+)/', '$1', $attr);
// }, $item['attributes']);
}
}
}
@ -77,68 +82,68 @@ class FACT1Test extends TestCase
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;
}
// 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;
// }
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;
}
// 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;
// }
public function testFactToArray()
{
// public function testFactToArray()
// {
$xml = file_get_contents('tests/Integration/Einvoice/samples/fact1_no_prefixes.xml');
$service = new Service();
// $xml = file_get_contents('tests/Integration/Einvoice/samples/fact1_no_prefixes.xml');
// $service = new Service();
// $service->elementMap = [
// '{}' => 'Sabre\Xml\Deserializer\keyValue',
// ];
// // $service->elementMap = [
// // '{}' => 'Sabre\Xml\Deserializer\keyValue',
// // ];
// $service->elementMap = [
// '{}*' => function (Reader $reader) use ($service) {
// return $this->keyValueDeserializer($reader);
// }
// ];
// // $service->elementMap = [
// // '{}*' => function (Reader $reader) use ($service) {
// // return $this->keyValueDeserializer($reader);
// // }
// // ];
$result = $this->removeNamespacesFromArray($service->parse($xml));
// $result = $this->removeNamespacesFromArray($service->parse($xml));
// Convert parsed XML to key-value array
if (isset($result['value']) && is_array($result['value'])) {
$keyValueArray = $this->convertToKeyValue($result['value']);
} else {
$keyValueArray = [];
}
// // Convert parsed XML to key-value array
// if (isset($result['value']) && is_array($result['value'])) {
// $keyValueArray = $this->convertToKeyValue($result['value']);
// } else {
// $keyValueArray = [];
// }
// Output the result
nlog($keyValueArray);
// // Output the result
// nlog($keyValueArray);
// nlog($cleanedArray);
nlog($service->parse($xml));
// // nlog($cleanedArray);
// nlog($service->parse($xml));
}
// }
// Output the result
// ($xmlWithoutNamespaces);
@ -183,16 +188,16 @@ public function keyValueDeserializer(Reader $reader)
// }
private function extractName($name): string
{
// private function extractName($name): string
// {
$pattern = '/\{[^{}]*\}([^{}]*)/';
// $pattern = '/\{[^{}]*\}([^{}]*)/';
if (preg_match($pattern, $name, $matches)) {
$extracted = $matches[1];
return $extracted;
}
// if (preg_match($pattern, $name, $matches)) {
// $extracted = $matches[1];
// return $extracted;
// }
return $name;
}
// return $name;
// }
}

View File

@ -57,7 +57,7 @@
</PartyLegalEntity>
<Contact>
<Name>Someone</Name>
<Telephone />
<Telephone>88282819832</Telephone>
<ElectronicMail>some@email.com</ElectronicMail>
</Contact>
</Party>