|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\DBAL\Tests\Functional\Driver\Mysqli; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Driver\Mysqli\Result; |
| 8 | +use Doctrine\DBAL\Schema\Table; |
| 9 | +use Doctrine\DBAL\Tests\FunctionalTestCase; |
| 10 | +use Doctrine\DBAL\Tests\TestUtil; |
| 11 | +use mysqli; |
| 12 | +use mysqli_driver; |
| 13 | +use mysqli_sql_exception; |
| 14 | + |
| 15 | +use function sprintf; |
| 16 | + |
| 17 | +use const MYSQLI_REPORT_ERROR; |
| 18 | +use const MYSQLI_REPORT_OFF; |
| 19 | +use const MYSQLI_REPORT_STRICT; |
| 20 | + |
| 21 | +/** @requires extension mysqli */ |
| 22 | +final class ResultTest extends FunctionalTestCase |
| 23 | +{ |
| 24 | + private const TABLE_NAME = 'result_test_table'; |
| 25 | + |
| 26 | + private mysqli $nativeConnection; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + if (! TestUtil::isDriverOneOf('mysqli')) { |
| 31 | + self::markTestSkipped('This test requires the mysqli driver.'); |
| 32 | + |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + $nativeConnection = $this->connection->getNativeConnection(); |
| 37 | + |
| 38 | + self::assertInstanceOf(mysqli::class, $nativeConnection); |
| 39 | + |
| 40 | + $this->nativeConnection = $nativeConnection; |
| 41 | + |
| 42 | + $table = new Table(self::TABLE_NAME); |
| 43 | + $table->addColumn('my_col_1', 'integer', ['notnull' => true]); |
| 44 | + |
| 45 | + $this->dropAndCreateTable($table); |
| 46 | + } |
| 47 | + |
| 48 | + protected function tearDown(): void |
| 49 | + { |
| 50 | + $this->dropTableIfExists(self::TABLE_NAME); |
| 51 | + } |
| 52 | + |
| 53 | + public function testIntegerOnFailingRowCountFromAffectedRows(): void |
| 54 | + { |
| 55 | + $mysqliStmt = $this->nativeConnection |
| 56 | + ->prepare(sprintf('INSERT INTO %s VALUES (NULL);', self::TABLE_NAME)); |
| 57 | + |
| 58 | + $mysqliDriver = new mysqli_driver(); |
| 59 | + |
| 60 | + $mysqliReportMode = $mysqliDriver->report_mode; |
| 61 | + |
| 62 | + // Set MySQL's driver report mode to `MYSQLI_REPORT_OFF` in order to avoid exception on errors. |
| 63 | + $mysqliDriver->report_mode = MYSQLI_REPORT_OFF; |
| 64 | + |
| 65 | + try { |
| 66 | + $mysqliStmt->execute(); |
| 67 | + |
| 68 | + self::assertSame(-1, $mysqliStmt->affected_rows); |
| 69 | + self::assertSame(-1, (new Result($mysqliStmt))->rowCount()); |
| 70 | + } finally { |
| 71 | + // Restore default configuration. |
| 72 | + $mysqliDriver->report_mode = $mysqliReportMode; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public function testExceptionOnFailingRowCountFromAffectedRows(): void |
| 77 | + { |
| 78 | + $mysqliStmt = $this->nativeConnection |
| 79 | + ->prepare(sprintf('INSERT INTO %s VALUES (NULL);', self::TABLE_NAME)); |
| 80 | + |
| 81 | + $mysqliDriver = new mysqli_driver(); |
| 82 | + |
| 83 | + $mysqliReportMode = $mysqliDriver->report_mode; |
| 84 | + |
| 85 | + // Set MySQL's driver report mode to `MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT` in order to throw exception on |
| 86 | + // errors. |
| 87 | + $mysqliDriver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT; |
| 88 | + |
| 89 | + try { |
| 90 | + $mysqliStmt->execute(); |
| 91 | + } catch (mysqli_sql_exception $mysqliException) { |
| 92 | + $this->expectException(mysqli_sql_exception::class); |
| 93 | + $this->expectExceptionMessage('Column \'my_col_1\' cannot be null'); |
| 94 | + |
| 95 | + new Result($mysqliStmt); |
| 96 | + } finally { |
| 97 | + // Restore default configuration. |
| 98 | + $mysqliDriver->report_mode = $mysqliReportMode; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments