File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )) {
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments