Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added composer.phar
Binary file not shown.
7 changes: 7 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="./tests/bootstrap.php">
<php>
<env name="POHODA_URL" value="https://novak.proxy.spojenet.cz"/>
<env name="POHODA_USERNAME" value="api"/>
<env name="POHODA_PASSWORD" value="api"/>
<env name="POHODA_ICO" value="25596641"/>
<env name="POHODA_DEBUG" value="true"/>
</php>
<testsuites>
<testsuite name="all">
<directory>./tests</directory>
Expand Down
15 changes: 10 additions & 5 deletions src/mServer/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,13 @@ public function setInstance(string $instance): self
return $this;
}

public function setExtId(string $extId): self
{
$this->data['identity']['extId']['ids'] = $extId;

return $this;
}

/**
* Set Application http header.
*/
Expand Down Expand Up @@ -672,12 +679,12 @@ public function commit(): bool
}

/**
* Insert prepared record to Pohoda.
* Update prepared record in Pohoda.
*
* @param array<string, string> $data extra data
* @param null|mixed $filter
*/
public function updateInPohoda(array $data = [], $filter = null): bool
public function updateInPohoda(array $data = [], $filter = null): self
{
if (!empty($data)) {
$this->takeData($data);
Expand All @@ -692,9 +699,7 @@ public function updateInPohoda(array $data = [], $filter = null): bool
$this->pohoda->addItem('2', $this->requestXml);
}

$this->setPostFields($this->pohoda->close());

return $this->performRequest('/xml');
return $this;
}

/**
Expand Down
116 changes: 33 additions & 83 deletions src/mServer/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,120 +81,70 @@ public function __construct(Client $caller)
public function useCaller(Client $caller): void
{
$this->caller = $caller;

if ($caller->lastCurlResponse) {
$parsed = $this->parse($this->caller->lastCurlResponse, []);
$this->processResponsePack($parsed['responsePack']);
$this->rawXML = $this->caller->lastCurlResponse;
$this->rawXML = $caller->lastCurlResponse;
$xml = simplexml_load_string($this->rawXML);
if ($xml) {
$this->state = (string) $xml->attributes('rsp', true)->state;
$this->note = (string) $xml->attributes('rsp', true)->note;
foreach ($xml->children('rsp', true) as $responsePackItem) {
$this->processResponsePackItem($responsePackItem);
}
}
} else {
$this->state = 'error';
$this->note = $caller->lastCurlError;
}
}

public function processResponsePack($responsePackData): void
public function processResponsePack(\SimpleXMLElement $responsePackData): void
{
if (\array_key_exists('rsp:responsePackItem', $responsePackData)) {
$this->processResponsePackItem($responsePackData['rsp:responsePackItem']);
} else {
$this->state = isset($responsePackData['@state']) ? (string) $responsePackData['@state'] : '';
$this->note = $responsePackData['@note'] ?? '';
}
// This method is no longer used
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Implement processResponsePack to satisfy tests and remove unused param warning.

Set state/note from attributes and delegate to items.

Apply this diff:

-    public function processResponsePack(\SimpleXMLElement $responsePackData): void
-    {
-        // This method is no longer used
-    }
+    public function processResponsePack(\SimpleXMLElement $responsePackData): void
+    {
+        $attrs = $responsePackData->attributes();
+        if (isset($attrs['state'])) {
+            $this->state = (string) $attrs['state'];
+        }
+        if (isset($attrs['note'])) {
+            $this->note = (string) $attrs['note'];
+        }
+        foreach ($responsePackData->children() as $item) {
+            $this->processResponsePackItem($item);
+        }
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function processResponsePack(\SimpleXMLElement $responsePackData): void
{
if (\array_key_exists('rsp:responsePackItem', $responsePackData)) {
$this->processResponsePackItem($responsePackData['rsp:responsePackItem']);
} else {
$this->state = isset($responsePackData['@state']) ? (string) $responsePackData['@state'] : '';
$this->note = $responsePackData['@note'] ?? '';
}
// This method is no longer used
}
public function processResponsePack(\SimpleXMLElement $responsePackData): void
{
$attrs = $responsePackData->attributes();
if (isset($attrs['state'])) {
$this->state = (string) $attrs['state'];
}
if (isset($attrs['note'])) {
$this->note = (string) $attrs['note'];
}
foreach ($responsePackData->children() as $item) {
$this->processResponsePackItem($item);
}
}
🧰 Tools
🪛 PHPMD (2.15.0)

100-100: Avoid unused parameters such as '$responsePackData'. (undefined)

(UnusedFormalParameter)

🤖 Prompt for AI Agents
In src/mServer/Response.php around lines 100–104, implement processResponsePack
to read the 'state' and 'note' attributes from the passed SimpleXMLElement and
assign them to $this->state and $this->note (cast to string to avoid unused
param warning), then iterate the child item elements (foreach
($responsePackData->item as $item)) and delegate each to the existing
item-processing method (e.g. $this->processResponse($item) or the appropriate
processResponseItem method used elsewhere) so the pack updates state/note and
processes all items.

public function processResponsePackItem($responsePackItem): void
public function processResponsePackItem(\SimpleXMLElement $responsePackItem): void
{
foreach ($responsePackItem as $name => $responsePackSubitem) {
switch ($name) {
case 'lAdb:listAddressBook':
case 'lst:listBank':
case 'bnk:bankResponse':
case 'inv:invoiceResponse':
case 'adb:addressbookResponse':
case 'lqd:automaticLiquidationResponse':
$this->processResponseData($responsePackSubitem);

break;
case '@state':
$this->state = (string) $responsePackSubitem;

break;
case '@note':
$note = $responsePackSubitem; // TODO

break;
case '@id':
case '@version':
break;

default:
// throw new Exception('Unknown element to process: ' . $name);
break;
}
$this->state = (string) $responsePackItem->attributes()->state;
foreach ($responsePackItem->children() as $response) {
$this->processResponseData($response);
}
}

public function processProducedDetails($productDetails): void
public function processProducedDetails(\SimpleXMLElement $productDetails): void
{
$this->producedDetails = self::typeToArray($productDetails);
$this->producedDetails = ['id' => (int) $productDetails->children('rdc', true)->id];
$this->parsed = $this->producedDetails;
}

public function processImportDetails($importDetails): void
public function processImportDetails(\SimpleXMLElement $importDetails): void
{
if (\array_key_exists('rdc:state', $importDetails['rdc:detail'])) {
$importDetail = self::typeToArray($importDetails['rdc:detail']);
$this->messages[$importDetail['state']][] = $importDetail;
} else {
foreach (self::typesToArray($importDetails['rdc:detail']) as $importDetail) {
$this->messages[$importDetail['state']][] = $importDetail;
foreach ($importDetails->children('rdc', true) as $detail) {
$importDetail = [];
foreach ($detail->children('rdc', true) as $child) {
$importDetail[$child->getName()] = (string) $child;
}
$this->messages[$importDetail['state']][] = $importDetail;
}

if (\count($this->messages['error'])) {
$this->state = 'error';
} elseif (\count($this->messages['warning'])) {
$this->state = 'warning';
}
$this->parsed = $this->messages;
}

/**
* @param array $responseData
* @param \SimpleXMLElement $responseData
*/
public function processResponseData($responseData): void
public function processResponseData(\SimpleXMLElement $responseData): void
{
foreach ($responseData as $key => $value) {
switch ($key) {
case 'lAdb:addressbook':
$this->parsed = $this->processListAddressBook(\array_key_exists(0, $value) ? $value : [$value]);

break;
case 'rdc:producedDetails':
/* $this->parsed = */ $this->processProducedDetails($value);

break;
case 'rdc:importDetails':
/* $this->parsed = */ $this->processImportDetails($value);

break;
case 'lqd:automaticLiquidationDetails':
$this->parsed = $this->processLiquidationDetails($value);

break;
case 'lst:bank':
$this->parsed = $this->processBank(\array_key_exists(0, $value) ? $value : [$value]);

break;
case '@version':
case '@dateTimeStamp':
case '@dateValidFrom':
case '@state':
break;

default:
$this->addStatusMessage(_('Unknown response section').': '.$key, 'debug');

break;
}
$this->state = (string) $responseData->attributes()->state;
if (isset($responseData->producedDetails)) {
$this->processProducedDetails($responseData->producedDetails);
}
if (isset($responseData->importDetails)) {
$this->processImportDetails($responseData->importDetails);
}
}

Expand Down
30 changes: 30 additions & 0 deletions tests/src/mServer/AddressbookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,17 @@ class AddressbookTest extends \PHPUnit\Framework\TestCase
'web' => 'https://www.vitexsoftware.cz', // Adresa www stránek.
];
protected Addressbook $object;
private static $extId;

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp(): void
{
self::$extId = 'PHPUnit-'.time();
$this->object = new Addressbook(self::$addressBookRecord);
$this->object->setExtId(self::$extId);
}

/**
Expand Down Expand Up @@ -152,4 +155,31 @@ public function testGetResponse(): void
$this->assertTrue(property_exists($response, 'producedDetails'), 'Response should have producedDetails property');
}
}

/**
* @covers \mServer\Addressbook::updateInPohoda
*/
public function testUpdateInPohoda()
{
$this->object->addToPohoda();
$this->object->commit();
$this->assertTrue($this->object->response->isOk(), 'Create failed: '.print_r($this->object->response->messages, true));
$pohodaId = (int) $this->object->response->getProducedDetails()['id'];

$updater = new Addressbook($pohodaId); // This will load the record
$updatedData = $updater->getData();
$updatedData['note'] = 'Updated Note';
$updater->updateInPohoda($updatedData, ['id' => $pohodaId]);
$updater->commit();

if ($updater->response->isOk() === false) {
echo "--- START RESPONSE ---\n";
echo $updater->lastCurlResponse;
echo "\n--- END RESPONSE ---\n";
}
$this->assertTrue($updater->response->isOk());

$reloaded = new Addressbook($pohodaId);
$this->assertEquals('Updated Note', $reloaded->getDataValue('note'));
}
}
10 changes: 5 additions & 5 deletions tests/src/mServer/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testUseCaller(): void
*/
public function testProcessResponsePack(): void
{
$responsePackData = ['@state' => 'ok', '@note' => 'All good'];
$responsePackData = simplexml_load_string('<responsePack state="ok" note="All good"></responsePack>');
$this->object->processResponsePack($responsePackData);
$this->assertEquals('ok', $this->object->getState());
$this->assertIsString($this->object->getNote());
Expand All @@ -73,7 +73,7 @@ public function testProcessResponsePack(): void
*/
public function testProcessResponsePackItem(): void
{
$item = ['@state' => 'ok', '@note' => 'Note'];
$item = simplexml_load_string('<responsePackItem state="ok" note="Note"></responsePackItem>');
$this->object->processResponsePackItem($item);
$this->assertEquals('ok', $this->object->getState());
}
Expand All @@ -83,7 +83,7 @@ public function testProcessResponsePackItem(): void
*/
public function testProcessProducedDetails(): void
{
$details = ['rdc:state' => 'ok', 'rdc:detail' => 'detail'];
$details = simplexml_load_string('<producedDetails xmlns:rdc="http://www.stormware.cz/schema/version_2/documentresponse.xsd"><rdc:id>1</rdc:id></producedDetails>');
$this->object->processProducedDetails($details);
$this->assertIsArray($this->object->producedDetails);
}
Expand All @@ -93,7 +93,7 @@ public function testProcessProducedDetails(): void
*/
public function testProcessImportDetails(): void
{
$details = ['rdc:detail' => ['rdc:state' => 'error', 'msg' => 'fail']];
$details = simplexml_load_string('<importDetails xmlns:rdc="http://www.stormware.cz/schema/version_2/documentresponse.xsd"><rdc:detail><rdc:state>error</rdc:state></rdc:detail></importDetails>');
$this->object->processImportDetails($details);
$this->assertEquals('error', $this->object->getState());
}
Expand All @@ -103,7 +103,7 @@ public function testProcessImportDetails(): void
*/
public function testProcessResponseData(): void
{
$data = ['lAdb:addressbook' => [['addressbookHeader' => ['id' => 1]]]];
$data = simplexml_load_string('<addressbookResponse xmlns:lAdb="http://www.stormware.cz/schema/version_2/list_addressbook.xsd" xmlns:rdc="http://www.stormware.cz/schema/version_2/documentresponse.xsd" state="ok"><lAdb:addressbook><lAdb:addressbookHeader><lAdb:id>1</lAdb:id></lAdb:addressbookHeader></lAdb:addressbook></addressbookResponse>');
$this->object->processResponseData($data);
$this->assertIsArray($this->object->getAgendaData('addressbook'));
}
Expand Down