Skip to content

Commit 70e0ece

Browse files
authored
Merge pull request #99 from castillo-n/fix/empty-results-parse-error
Fix undefined array key 0 in RW::parseError() when results is empty
2 parents 9fbe0ad + 85cb5d4 commit 70e0ece

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

src/AbraFlexi/RW.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function parseError(array $responseDecoded)
166166
$this->errors = $responseDecoded['errors'];
167167
}
168168

169-
if (\array_key_exists('results', $responseDecoded) && \is_array($responseDecoded['results'])) {
169+
if (\array_key_exists('results', $responseDecoded) && \is_array($responseDecoded['results']) && \count($responseDecoded['results']) > 0) {
170170
if (\array_key_exists(0, $responseDecoded['results'])) {
171171
foreach ($responseDecoded['results'] as $result) {
172172
if (\array_key_exists('request-id', $result)) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Test\AbraFlexi;
6+
7+
use AbraFlexi\RW;
8+
9+
/**
10+
* Test for issue #98: parseError() crashes when results is an empty array.
11+
*/
12+
class ParseErrorEmptyResultsTest extends \PHPUnit\Framework\TestCase
13+
{
14+
/**
15+
* @covers \AbraFlexi\RW::parseError
16+
*/
17+
public function testParseErrorWithEmptyResultsArray(): void
18+
{
19+
$rw = new RW(null, ['offline' => true]);
20+
21+
// Simulate the API response that causes the crash:
22+
// success is true but results is an empty array
23+
$responseDecoded = [
24+
'success' => 'true',
25+
'stats' => [
26+
'created' => '0',
27+
'updated' => '0',
28+
'deleted' => '0',
29+
'skipped' => '0',
30+
'failed' => '0',
31+
],
32+
'results' => [],
33+
];
34+
35+
// This should not throw "Undefined array key 0"
36+
$errorCount = $rw->parseError($responseDecoded);
37+
38+
$this->assertSame(0, $errorCount);
39+
}
40+
41+
/**
42+
* @covers \AbraFlexi\RW::parseError
43+
*/
44+
public function testParseErrorWithNonEmptyResultsStillWorks(): void
45+
{
46+
$rw = new RW(null, ['offline' => true]);
47+
48+
$responseDecoded = [
49+
'results' => [
50+
[
51+
'errors' => [
52+
['message' => 'Some error', 'for' => 'field'],
53+
],
54+
],
55+
],
56+
];
57+
58+
$errorCount = $rw->parseError($responseDecoded);
59+
60+
$this->assertGreaterThan(0, $errorCount);
61+
}
62+
}

0 commit comments

Comments
 (0)