File: ResultMetadataTest.php

package info (click to toggle)
php-doctrine-dbal 4.2.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 4,644 kB
  • sloc: php: 46,471; xml: 460; makefile: 22
file content (68 lines) | stat: -rw-r--r-- 2,290 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
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional;

use Doctrine\DBAL\Exception\InvalidColumnIndex;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\Attributes\TestWith;

use function strtolower;

#[RequiresPhpunit('< 11.5.18')]
class ResultMetadataTest extends FunctionalTestCase
{
    protected function setUp(): void
    {
        $table = new Table('result_metadata_table');
        $table->addColumn('test_int', 'integer');
        $table->setPrimaryKey(['test_int']);

        $this->dropAndCreateTable($table);

        $this->connection->insert('result_metadata_table', ['test_int' => 1]);
    }

    public function testColumnNameWithResults(): void
    {
        $sql = 'SELECT test_int, test_int AS alternate_name FROM result_metadata_table';

        $result = $this->connection->executeQuery($sql);

        self::assertEquals(2, $result->columnCount());
        // Depending on the platform, field names might have different case than in the SQL
        // query (for instance, Oracle turns unquoted identifiers into upper case).
        self::assertEquals('test_int', strtolower($result->getColumnName(0)));
        self::assertEquals('alternate_name', strtolower($result->getColumnName(1)));
    }

    #[TestWith([2])]
    #[TestWith([-1])]
    public function testColumnNameWithInvalidIndex(int $index): void
    {
        $sql = 'SELECT test_int, test_int AS alternate_name FROM result_metadata_table';

        $result = $this->connection->executeQuery($sql);

        // Consume the result set to avoid issues with unprocessed buffer between tests
        $result->fetchAllAssociative();

        $this->expectException(InvalidColumnIndex::class);

        $result->getColumnName($index);
    }

    public function testColumnNameWithoutResults(): void
    {
        $sql = 'SELECT test_int, test_int AS alternate_name FROM result_metadata_table WHERE 1 = 0';

        $result = $this->connection->executeQuery($sql);

        self::assertEquals(2, $result->columnCount());
        self::assertEquals('test_int', strtolower($result->getColumnName(0)));
        self::assertEquals('alternate_name', strtolower($result->getColumnName(1)));
    }
}