diff --git a/phpcs.xml.dist b/phpcs.xml.dist index fd5936c5ee7..4599df238c9 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -106,6 +106,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..262c7d8660d --- /dev/null +++ b/tests/Functional/Driver/Mysqli/ResultTest.php @@ -0,0 +1,105 @@ +connection->getNativeConnection(); + + self::assertInstanceOf(mysqli::class, $nativeConnection); + + $this->nativeConnection = $nativeConnection; + + $table = new Table(self::TABLE_NAME); + $table->addColumn('my_col_1', 'integer', ['notnull' => true]); + + $this->dropAndCreateTable($table); + } + + protected function tearDown(): void + { + $this->dropTableIfExists(self::TABLE_NAME); + } + + public function testIntegerOnFailingRowCountFromAffectedRows(): void + { + $mysqliStmt = $this->nativeConnection + ->prepare(sprintf('INSERT INTO %s VALUES (NULL);', self::TABLE_NAME)); + + $mysqliDriver = new mysqli_driver(); + + $mysqliReportMode = $mysqliDriver->report_mode; + + // Set MySQL's driver report mode to `MYSQLI_REPORT_OFF` in order to avoid exception on errors. + $mysqliDriver->report_mode = MYSQLI_REPORT_OFF; + + try { + $mysqliStmt->execute(); + + self::assertSame(-1, $mysqliStmt->affected_rows); + self::assertSame(-1, (new Result($mysqliStmt))->rowCount()); + } finally { + // Restore default configuration. + $mysqliDriver->report_mode = $mysqliReportMode; + } + } + + public function testExceptionOnFailingRowCountFromAffectedRows(): void + { + $mysqliStmt = $this->nativeConnection + ->prepare(sprintf('INSERT INTO %s VALUES (NULL);', self::TABLE_NAME)); + + $mysqliDriver = new mysqli_driver(); + + $mysqliReportMode = $mysqliDriver->report_mode; + + // Set MySQL's driver report mode to `MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT` in order to throw exception on + // errors. + $mysqliDriver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT; + + $mysqliException = null; + + try { + $mysqliStmt->execute(); + } catch (mysqli_sql_exception $mysqliException) { + $this->expectException(mysqli_sql_exception::class); + $this->expectExceptionMessage('Column \'my_col_1\' cannot be null'); + + new Result($mysqliStmt); + } finally { + // Restore default configuration. + $mysqliDriver->report_mode = $mysqliReportMode; + } + + self::assertInstanceOf(mysqli_sql_exception::class, $mysqliException); + } +}