From a653fa034532e254fe28b53ca650c3a320458f42 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Fri, 10 Feb 2023 00:39:32 -0300 Subject: [PATCH] Add test for `Mysqli\Result` --- phpcs.xml.dist | 1 + tests/Functional/Driver/Mysqli/ResultTest.php | 98 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 tests/Functional/Driver/Mysqli/ResultTest.php diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 5771c4252a7..a260db1df34 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -99,6 +99,7 @@ --> src/Driver/IBMDB2/Connection.php src/Driver/Mysqli/Exception/ConnectionFailed.php + tests/Functional/Driver/Mysqli/ResultTest.php diff --git a/tests/Functional/Driver/Mysqli/ResultTest.php b/tests/Functional/Driver/Mysqli/ResultTest.php new file mode 100644 index 00000000000..cce38acb3e2 --- /dev/null +++ b/tests/Functional/Driver/Mysqli/ResultTest.php @@ -0,0 +1,98 @@ +connection->getNativeConnection(); + + assert($nativeConnection instanceof mysqli); + + $this->nativeConnection = $nativeConnection; + + $this->connection->executeStatement('CREATE TABLE my_table (my_col_1 INT NOT NULL);'); + + return; + } + + self::markTestSkipped('This test requires the mysqli driver.'); + } + + protected function tearDown(): void + { + $this->connection->executeStatement('DROP TABLE IF EXISTS my_table;'); + } + + public function testSuccessfulRowCountFromAffectedRows(): void + { + $mysqliStmt = $this->nativeConnection + ->prepare('INSERT INTO my_table VALUES (7);'); + $mysqliStmt->execute(); + + $result = new Result($mysqliStmt); + + self::assertSame(1, $mysqliStmt->affected_rows); + self::assertSame(1, $result->rowCount()); + } + + public function testFailingRowCountFromAffectedRows(): void + { + $mysqliStmt = $this->nativeConnection + ->prepare('INSERT INTO my_table VALUES (NULL);'); + + $mysqliDriver = new mysqli_driver(); + + // Ensure default configuration present in PHP < 8.1. + $mysqliDriver->report_mode = MYSQLI_REPORT_OFF; + + $mysqliStmt->execute(); + + self::assertSame(-1, $mysqliStmt->affected_rows); + + $result = new Result($mysqliStmt); + + self::assertSame(-1, $result->rowCount()); + + // Ensure default configuration present in PHP >= 8.1. + // @see: https://php.watch/versions/8.1/mysqli-error-mode. + $mysqliDriver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT; + + $error = null; + + try { + $mysqliStmt->execute(); + } catch (mysqli_sql_exception $error) { + // no-op. + } + + self::assertInstanceOf(mysqli_sql_exception::class, $error); + + self::assertSame(-1, $mysqliStmt->affected_rows); + + $this->expectException(mysqli_sql_exception::class); + $this->expectExceptionMessage('Column \'my_col_1\' cannot be null'); + + new Result($mysqliStmt); + } +}