File: ResultTest.php

package info (click to toggle)
php-doctrine-dbal 3.6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,500 kB
  • sloc: php: 54,704; xml: 485; makefile: 42; sh: 24
file content (77 lines) | stat: -rw-r--r-- 2,284 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
<?php

namespace Doctrine\DBAL\Tests\Functional;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;

class ResultTest extends FunctionalTestCase
{
    /**
     * @param mixed $expected
     *
     * @dataProvider methodProvider
     */
    public function testExceptionHandling(callable $method, $expected): void
    {
        $result = $this->connection->executeQuery(
            $this->connection->getDatabasePlatform()
                ->getDummySelectSQL(),
        );
        $result->free();

        try {
            // some drivers will trigger a PHP error here which, if not suppressed,
            // would be converted to a PHPUnit exception prior to DBAL throwing its own one
            $value = @$method($result);
        } catch (Exception $e) {
            // The drivers that enforce the command sequencing internally will throw an exception
            $this->expectNotToPerformAssertions();

            return;
        }

        self::assertFalse(
            TestUtil::isDriverOneOf('mysqli', 'ibm_db2'),
            'We expect mysqli and ibm_db2 drivers to throw an exception.',
        );
        // Other drivers will silently return an empty result
        self::assertSame($expected, $value);
    }

    /** @return iterable<string, array{callable(Result):mixed, mixed}> */
    public static function methodProvider(): iterable
    {
        yield 'fetchNumeric' => [
            static fn (Result $result) => $result->fetchNumeric(),
            false,
        ];

        yield 'fetchAssociative' => [
            static fn (Result $result) => $result->fetchAssociative(),
            false,
        ];

        yield 'fetchOne' => [
            static fn (Result $result) => $result->fetchOne(),
            false,
        ];

        yield 'fetchAllNumeric' => [
            static fn (Result $result): array => $result->fetchAllNumeric(),
            [],
        ];

        yield 'fetchAllAssociative' => [
            static fn (Result $result): array => $result->fetchAllAssociative(),
            [],
        ];

        yield 'fetchFirstColumn' => [
            static fn (Result $result): array => $result->fetchFirstColumn(),
            [],
        ];
    }
}