diff --git a/src/ResultHelper.php b/src/ResultHelper.php new file mode 100644 index 0000000..9014dfb --- /dev/null +++ b/src/ResultHelper.php @@ -0,0 +1,44 @@ +, + * "data": array>, + * } $responseData + * @return array{ + * "columns": array, + * "data": array>, + * } + */ + public static function mapColumnNamesIntoData(array $responseData): array + { + $data = $responseData['data']; + $columnNames = array_column($responseData['columns'], 'name'); + + $transformedData = []; + foreach ($data as $row) { + assert(is_array($row)); + $transformedRow = []; + foreach ($row as $index => $value) { + if (isset($columnNames[$index])) { + $transformedRow[$columnNames[$index]] = $value; + } + } + $transformedData[] = $transformedRow; + } + $responseData['data'] = $transformedData; + return $responseData; + } +} diff --git a/tests/Functional/QueryServiceFunctionalTest.php b/tests/Functional/QueryServiceFunctionalTest.php index 365e461..9405f53 100644 --- a/tests/Functional/QueryServiceFunctionalTest.php +++ b/tests/Functional/QueryServiceFunctionalTest.php @@ -202,10 +202,9 @@ public function testQueryJobWithInvalidSQL(): void assert(is_array($statements)); $this->assertCount(1, $statements); - // The statement remains in 'waiting' status because the job failed before execution $statement = $statements[0]; assert(is_array($statement)); - $this->assertEquals('completed', $statement['status']); + $this->assertEquals('failed', $statement['status']); assert(is_string($statement['query'])); $this->assertEquals('SELECT * FROM non_existent_table_12345', $statement['query']); } diff --git a/tests/Phpunit/ResultHelperTest.php b/tests/Phpunit/ResultHelperTest.php new file mode 100644 index 0000000..5c800ff --- /dev/null +++ b/tests/Phpunit/ResultHelperTest.php @@ -0,0 +1,47 @@ + [ + ['name' => 'id', 'type' => 'text'], + ['name' => 'name', 'type' => 'text'], + ['name' => 'city', 'type' => 'text'], + ], + 'data' => [ + ['1', 'Alice', 'Prague'], + ['2', 'Bob', 'Liberec'], + ['3', 'Charlie', 'Brno', 'EXTRA'], + ], + ]; + + $expected = [ + 'columns' => [ + ['name' => 'id', 'type' => 'text'], + ['name' => 'name', 'type' => 'text'], + ['name' => 'city', 'type' => 'text'], + ], + 'data' => [ + ['id' => '1', 'name' => 'Alice', 'city' => 'Prague'], + ['id' => '2', 'name' => 'Bob', 'city' => 'Liberec'], + ['id' => '3', 'name' => 'Charlie', 'city' => 'Brno'], + ], + ]; + + $actual = ResultHelper::mapColumnNamesIntoData($input); + + // Columns should be preserved unchanged + $this->assertSame($expected['columns'], $actual['columns']); + // Data rows should be mapped by column names + $this->assertSame($expected['data'], $actual['data']); + } +}