Skip to content

Commit

Permalink
Add method to handle empty receptionEmails in Response
Browse files Browse the repository at this point in the history
Enhanced the Response model to handle cases where receptionEmails is empty by setting it to a new ReceptionEmails object. Added corresponding XML stub and test case to validate this change.

Fixed a potential issue in TypeArray's count method by providing a default empty array.
  • Loading branch information
firebed committed Nov 19, 2024
1 parent 0c36813 commit c29f372
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Models/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,13 @@ public function isSuccessful(): bool
{
return $this->getStatusCode() === 'Success';
}

public function set($key, $value): static
{
if ($key === 'receptionEmails' && empty($value)) {
return parent::set('receptionEmails', new ReceptionEmails());
}

return parent::set($key, $value);
}
}
2 changes: 1 addition & 1 deletion src/Models/TypeArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function offsetUnset($offset): void

public function count(): int
{
return count($this->attributes[$this->childKey]);
return count($this->attributes[$this->childKey] ?? []);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions stubs/response-doc-with-empty-reception-emails.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<ResponseDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<response>
<index>1</index>
<invoiceUid>6F825B9B74717280D1F9A38252D0B65604C6D162</invoiceUid>
<invoiceMark>480301204040191</invoiceMark>
<qrUrl>https://mydataapidev.aade.gr/TimologioQR/QRInfo?q=sSsxThUx5i3O8ankrp8XUDiL0q12vbzuWZP0nEfMgAyQ2nbm7s6vSz3CYYcmod6i0uCrPCvSLrQ5N1qxEHF8JsfMNz6NmibNhBNXJiR8ecC%3d</qrUrl>
<statusCode>Success</statusCode>
<receptionEmails/>
</response>
</ResponseDoc>
15 changes: 15 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ public function test_it_reads_response_correctly()
$this->assertEquals("[email protected]", $doc[0]->getReceptionEmails()->offsetGet(1));
}

public function test_it_reads_response_with_empty_reception_emails_correctly()
{
$doc = $this->getResponseDocFromXml('response-doc-with-empty-reception-emails');

$this->assertCount(1, $doc);
$this->assertEquals(1, $doc[0]->getIndex());
$this->assertEquals("6F825B9B74717280D1F9A38252D0B65604C6D162", $doc[0]->getInvoiceUid());
$this->assertEquals("480301204040191", $doc[0]->getInvoiceMark());
$this->assertEquals("https://mydataapidev.aade.gr/TimologioQR/QRInfo?q=sSsxThUx5i3O8ankrp8XUDiL0q12vbzuWZP0nEfMgAyQ2nbm7s6vSz3CYYcmod6i0uCrPCvSLrQ5N1qxEHF8JsfMNz6NmibNhBNXJiR8ecC%3d", $doc[0]->getQrUrl());
$this->assertEquals("Success", $doc[0]->getStatusCode());

$this->assertIsArray($doc[0]->getReceptionEmails()->all());
$this->assertCount(0, $doc[0]->getReceptionEmails());
}

public function test_it_reads_errors_correctly()
{
$doc = $this->getResponseDocFromXml('response-doc-with-errors');
Expand Down

0 comments on commit c29f372

Please sign in to comment.