File: ResultTest.php

package info (click to toggle)
php-doctrine-dbal 4.3.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,092 kB
  • sloc: php: 60,293; xml: 618; makefile: 23
file content (111 lines) | stat: -rw-r--r-- 3,393 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional\Driver\Mysqli;

use Doctrine\DBAL\Driver\Mysqli\Result;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use Doctrine\DBAL\Types\Types;
use mysqli;
use mysqli_driver;
use mysqli_sql_exception;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;

use function sprintf;

use const MYSQLI_REPORT_ERROR;
use const MYSQLI_REPORT_OFF;
use const MYSQLI_REPORT_STRICT;

#[RequiresPhpExtension('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.');
        }

        $nativeConnection = $this->connection->getNativeConnection();

        self::assertInstanceOf(mysqli::class, $nativeConnection);

        $this->nativeConnection = $nativeConnection;

        $table = Table::editor()
            ->setUnquotedName(self::TABLE_NAME)
            ->setColumns(
                Column::editor()
                    ->setUnquotedName('my_col_1')
                    ->setTypeName(Types::INTEGER)
                    ->create(),
            )
            ->create();

        $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));
        self::assertNotFalse($mysqliStmt);

        $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));
        self::assertNotFalse($mysqliStmt);

        $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;

        try {
            $mysqliStmt->execute();
        } catch (mysqli_sql_exception) {
            $this->expectException(mysqli_sql_exception::class);
            $this->expectExceptionMessage('Column \'my_col_1\' cannot be null');

            new Result($mysqliStmt);
        } finally {
            // Restore default configuration.
            $mysqliDriver->report_mode = $mysqliReportMode;
        }
    }
}