From a2a35c19de86cbd15a0088fc58f5cc7533e78bae Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Wed, 19 Apr 2023 22:50:12 +0100 Subject: [PATCH] Split up loadFirstRow Signed-off-by: Kamil Tekiela --- libraries/classes/InsertEdit.php | 22 +++++++-------- psalm-baseline.xml | 1 + test/classes/InsertEditTest.php | 47 +++++++++++++++++++------------- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/libraries/classes/InsertEdit.php b/libraries/classes/InsertEdit.php index 8dfdf059c1..8572a97fbe 100644 --- a/libraries/classes/InsertEdit.php +++ b/libraries/classes/InsertEdit.php @@ -244,23 +244,20 @@ class InsertEdit /** * No primary key given, just load first row - * - * @param string $table name of the table - * @param string $db name of the database - * - * @return array - * @phpstan-return array{ResultInterface, false[]} */ - private function loadFirstRow(string $table, string $db): array + private function loadFirstRow(string $table, string $db): ResultInterface { - $result = $this->dbi->query( + return $this->dbi->query( 'SELECT * FROM ' . Util::backquote($db) . '.' . Util::backquote($table) . ' LIMIT 1;', ); - // Can be a string on some old configuration storage settings - $rows = array_fill(0, (int) $GLOBALS['cfg']['InsertRows'], false); + } - return [$result, $rows]; + /** @return false[] */ + private function getInsertRows(): array + { + // Can be a string on some old configuration storage settings + return array_fill(0, (int) $GLOBALS['cfg']['InsertRows'], false); } /** @@ -1770,7 +1767,8 @@ class InsertEdit // we are inserting $insertMode = true; $whereClause = null; - [$result, $rows] = $this->loadFirstRow($table, $db); + $result = $this->loadFirstRow($table, $db); + $rows = $this->getInsertRows(); $whereClauses = null; $whereClauseArray = []; $foundUniqueKey = false; diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 8d73a2f27e..1371fb7330 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -14897,6 +14897,7 @@ $result $result $result + $result diff --git a/test/classes/InsertEditTest.php b/test/classes/InsertEditTest.php index 59f750fafd..d167bae171 100644 --- a/test/classes/InsertEditTest.php +++ b/test/classes/InsertEditTest.php @@ -336,23 +336,8 @@ class InsertEditTest extends AbstractTestCase $this->assertFalse($result); } - /** @return list}> */ - public static function dataProviderConfigValueInsertRows(): array + public function testLoadFirstRow(): void { - return [[2, [false, false]], ['2', [false, false]], [3, [false, false, false]], ['3', [false, false, false]]]; - } - - /** - * Test for loadFirstRow - * - * @param array $rowsValue - * - * @dataProvider dataProviderConfigValueInsertRows - */ - public function testLoadFirstRow(string|int $configValue, array $rowsValue): void - { - $GLOBALS['cfg']['InsertRows'] = $configValue; - $resultStub = $this->createMock(DummyResult::class); $dbi = $this->getMockBuilder(DatabaseInterface::class) @@ -380,10 +365,34 @@ class InsertEditTest extends AbstractTestCase ['table', 'db'], ); - $this->assertEquals( - [$resultStub, $rowsValue], - $result, + $this->assertEquals($resultStub, $result); + } + + /** @return list}> */ + public static function dataProviderConfigValueInsertRows(): array + { + return [[2, [false, false]], ['2', [false, false]], [3, [false, false, false]], ['3', [false, false, false]]]; + } + + /** + * Test for loadFirstRow + * + * @param array $rowsValue + * + * @dataProvider dataProviderConfigValueInsertRows + */ + public function testGetInsertRows(string|int $configValue, array $rowsValue): void + { + $GLOBALS['cfg']['InsertRows'] = $configValue; + + $result = $this->callFunction( + $this->insertEdit, + InsertEdit::class, + 'getInsertRows', + [], ); + + $this->assertEquals($rowsValue, $result); } /**