Skip to content

Commit

Permalink
Add test for Mysqli\Result
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Jun 7, 2023
1 parent 01c4ce1 commit dee5100
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions phpcs.xml.dist
Expand Up @@ -106,6 +106,7 @@
-->
<exclude-pattern>src/Driver/IBMDB2/Connection.php</exclude-pattern>
<exclude-pattern>src/Driver/Mysqli/Exception/ConnectionFailed.php</exclude-pattern>
<exclude-pattern>tests/Functional/Driver/Mysqli/ResultTest.php</exclude-pattern>
</rule>

<!-- See https://github.com/squizlabs/PHP_CodeSniffer/issues/2837 -->
Expand Down
110 changes: 110 additions & 0 deletions tests/Functional/Driver/Mysqli/ResultTest.php
@@ -0,0 +1,110 @@
<?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;
}

/** @psalm-suppress TypeDoesNotContainType */
self::assertSame(2, $mysqliExceptionCount);
}
}

0 comments on commit dee5100

Please sign in to comment.