-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
110 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,109 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Tests\Functional\Driver\Mysqli; | ||
|
||
use Doctrine\DBAL\Driver\Mysqli\Result; | ||
use Doctrine\DBAL\Schema\Table; | ||
use Doctrine\DBAL\Tests\FunctionalTestCase; | ||
use Doctrine\DBAL\Tests\TestUtil; | ||
use mysqli; | ||
use mysqli_driver; | ||
use mysqli_sql_exception; | ||
|
||
use function sprintf; | ||
|
||
use const MYSQLI_REPORT_ERROR; | ||
use const MYSQLI_REPORT_OFF; | ||
use const MYSQLI_REPORT_STRICT; | ||
|
||
/** @requires extension mysqli */ | ||
final class ResultTest extends FunctionalTestCase | ||
{ | ||
private const TABLE_NAME = 'result_test_table'; | ||
|
||
private mysqli $nativeConnection; | ||
|
||
protected function setUp(): void | ||
{ | ||
if (! TestUtil::isDriverOneOf('mysqli')) { | ||
self::markTestSkipped('This test requires the mysqli driver.'); | ||
|
||
return; | ||
} | ||
|
||
$nativeConnection = $this->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; | ||
|
||
$mysqliExceptionCount = 0; | ||
|
||
try { | ||
$mysqliStmt->execute(); | ||
} catch (mysqli_sql_exception $mysqliException) { | ||
++$mysqliExceptionCount; | ||
|
||
$this->expectException(mysqli_sql_exception::class); | ||
$this->expectExceptionMessage('Column \'my_col_1\' cannot be null'); | ||
|
||
new Result($mysqliStmt); | ||
} catch (mysqli_sql_exception $mysqliException) { | ||
++$mysqliExceptionCount; | ||
} finally { | ||
// Restore default configuration. | ||
$mysqliDriver->report_mode = $mysqliReportMode; | ||
} | ||
|
||
self::assertSame(2, $mysqliExceptionCount); | ||
} | ||
} |