Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Driver\Mysqli; | ||
|
||
use Doctrine\DBAL\Driver\Mysqli\Result; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Tests\TestUtil; | ||
use mysqli; | ||
use mysqli_driver; | ||
use mysqli_sql_exception; | ||
|
||
use function assert; | ||
|
||
use const MYSQLI_REPORT_ERROR; | ||
use const MYSQLI_REPORT_OFF; | ||
use const MYSQLI_REPORT_STRICT; | ||
|
||
/** @requires extension mysqli */ | ||
final class ResultTest extends FunctionalTestCase | ||
{ | ||
private mysqli $nativeConnection; | ||
|
||
protected function setUp(): void | ||
{ | ||
if (TestUtil::isDriverOneOf('mysqli')) { | ||
$nativeConnection = $this->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); | ||
|
||
$this->expectException(mysqli_sql_exception::class); | ||
$this->expectExceptionMessage('Column \'my_col_1\' cannot be null'); | ||
|
||
new Result($mysqliStmt); | ||
} | ||
} |