ResultTest.php 3.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Portability;

use Doctrine\DBAL\Driver\Result as DriverResult;
use Doctrine\DBAL\Portability\Converter;
use Doctrine\DBAL\Portability\Result;
use PHPUnit\Framework\TestCase;

class ResultTest extends TestCase
{
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
    /**
     * @param mixed $return
     *
     * @dataProvider fetchProvider
     */
    public function testFetch(string $source, callable $fetch, $return): void
    {
        $driverResult = $this->createMock(DriverResult::class);
        $driverResult->expects(self::once())
            ->method($source)
            ->willReturn($return);

        $result = $this->newResult($driverResult);

        self::assertSame($return, $fetch($result));
    }

    /**
     * @return iterable<string,array<int,mixed>>
     */
    public static function fetchProvider(): iterable
    {
        yield 'numeric' => [
            'fetchNumeric',
            static function (Result $result): array {
                return $result->fetchNumeric();
            },
            ['bar'],
        ];

        yield 'associative' => [
            'fetchAssociative',
            static function (Result $result): array {
                return $result->fetchAssociative();
            },
            ['foo' => 'bar'],
        ];

        yield 'one' => [
            'fetchOne',
            static function (Result $result) {
                return $result->fetchOne();
            },
            'bar',
        ];

        yield 'all-numeric' => [
            'fetchAllNumeric',
            static function (Result $result): array {
                return $result->fetchAllNumeric();
            },
            [
                ['bar'],
                ['baz'],
            ],
        ];

        yield 'all-associative' => [
            'fetchAllAssociative',
            static function (Result $result): array {
                return $result->fetchAllAssociative();
            },
            [
                ['foo' => 'bar'],
                ['foo' => 'baz'],
            ],
        ];

        yield 'first-column' => [
            'fetchFirstColumn',
            static function (Result $result): array {
                return $result->fetchFirstColumn();
            },
            [
                'bar',
                'baz',
            ],
        ];
    }

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    public function testRowCount(): void
    {
        $driverResult = $this->createMock(DriverResult::class);
        $driverResult->expects(self::once())
            ->method('rowCount')
            ->willReturn(666);

        $result = $this->newResult($driverResult);

        self::assertSame(666, $result->rowCount());
    }

    public function testColumnCount(): void
    {
        $driverResult = $this->createMock(DriverResult::class);
        $driverResult->expects(self::once())
            ->method('columnCount')
            ->willReturn(666);

        $result = $this->newResult($driverResult);

        self::assertSame(666, $result->columnCount());
    }

    public function testFree(): void
    {
        $driverResult = $this->createMock(DriverResult::class);
        $driverResult->expects(self::once())
            ->method('free');

        $this->newResult($driverResult)->free();
    }

    private function newResult(DriverResult $driverResult): Result
    {
        return new Result(
            $driverResult,
            new Converter(false, false, null)
        );
    }
}