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 Feb 13, 2023
1 parent e002157 commit a653fa0
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions phpcs.xml.dist
Expand Up @@ -99,6 +99,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
98 changes: 98 additions & 0 deletions tests/Functional/Driver/Mysqli/ResultTest.php
@@ -0,0 +1,98 @@
<?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);

self::assertSame(-1, $mysqliStmt->affected_rows);

$this->expectException(mysqli_sql_exception::class);
$this->expectExceptionMessage('Column \'my_col_1\' cannot be null');

new Result($mysqliStmt);
}
}

0 comments on commit a653fa0

Please sign in to comment.