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 = [ $files = [
// 'tests/Integration/Einvoice/samples/fact1.xml', '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 $xml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
// $xmlWithoutNamespacesAndPrefixes = $this->removeNamespacesAndPrefixes($xml); $json = json_encode($xml);
$array = json_decode($json, true);
// // Output the result $i = Invoice::from($array);
// nlog($xmlWithoutNamespacesAndPrefixes);
$rules = Invoice::getValidationRules($array);
$this->assertIsArray($rules);
$validation_array = Invoice::validate($array);
// } $this->assertIsArray($validation_array);
}
}
public function removeNamespacesFromArray($data) public function removeNamespacesFromArray($data)
@ -65,10 +73,7 @@ class FACT1Test extends TestCase
} }
if (isset($item['attributes'])) { if (isset($item['attributes'])) {
unset($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) // function convertToKeyValue($data)
{ // {
$result = []; // $result = [];
foreach ($data as $item) { // foreach ($data as $item) {
// Remove namespace prefix if present // // Remove namespace prefix if present
$name = preg_replace('/^\{\}(.+)/', '$1', $item['name']); // $name = preg_replace('/^\{\}(.+)/', '$1', $item['name']);
$result[$name] = $item['value']; // $result[$name] = $item['value'];
} // }
return $result; // return $result;
} // }
public function keyValueDeserializer(Reader $reader) // public function keyValueDeserializer(Reader $reader)
{ // {
$values = []; // $values = [];
$reader->read(); // $reader->read();
$reader->next(); // $reader->next();
foreach ($reader->parseGetElements() as $element) { // foreach ($reader->parseGetElements() as $element) {
// Strip the namespace prefix // // Strip the namespace prefix
echo "merp".PHP_EOL; // echo "merp".PHP_EOL;
$name = preg_replace('/^\{\}.*/', '', $element['name']); // $name = preg_replace('/^\{\}.*/', '', $element['name']);
$values[$name] = $element['value']; // $values[$name] = $element['value'];
} // }
return $values; // return $values;
} // }
public function testFactToArray() // public function testFactToArray()
{ // {
$xml = file_get_contents('tests/Integration/Einvoice/samples/fact1_no_prefixes.xml'); // $xml = file_get_contents('tests/Integration/Einvoice/samples/fact1_no_prefixes.xml');
$service = new Service(); // $service = new Service();
// $service->elementMap = [ // // $service->elementMap = [
// '{}' => 'Sabre\Xml\Deserializer\keyValue', // // '{}' => 'Sabre\Xml\Deserializer\keyValue',
// ]; // // ];
// $service->elementMap = [ // // $service->elementMap = [
// '{}*' => function (Reader $reader) use ($service) { // // '{}*' => function (Reader $reader) use ($service) {
// return $this->keyValueDeserializer($reader); // // return $this->keyValueDeserializer($reader);
// } // // }
// ]; // // ];
$result = $this->removeNamespacesFromArray($service->parse($xml)); // $result = $this->removeNamespacesFromArray($service->parse($xml));
// Convert parsed XML to key-value array // // Convert parsed XML to key-value array
if (isset($result['value']) && is_array($result['value'])) { // if (isset($result['value']) && is_array($result['value'])) {
$keyValueArray = $this->convertToKeyValue($result['value']); // $keyValueArray = $this->convertToKeyValue($result['value']);
} else { // } else {
$keyValueArray = []; // $keyValueArray = [];
} // }
// Output the result // // Output the result
nlog($keyValueArray); // nlog($keyValueArray);
// nlog($cleanedArray); // // nlog($cleanedArray);
nlog($service->parse($xml)); // nlog($service->parse($xml));
} // }
// Output the result // Output the result
// ($xmlWithoutNamespaces); // ($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)) { // if (preg_match($pattern, $name, $matches)) {
$extracted = $matches[1]; // $extracted = $matches[1];
return $extracted; // return $extracted;
} // }
return $name; // return $name;
} // }
} }

View File

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