PortabilityTest.php 4.48 KB
Newer Older
1 2
<?php

3
namespace Doctrine\DBAL\Tests\Functional;
4

5
use Doctrine\DBAL\ColumnCase;
6
use Doctrine\DBAL\DriverManager;
7 8
use Doctrine\DBAL\Portability\Connection;
use Doctrine\DBAL\Portability\Middleware;
Sergei Morozov's avatar
Sergei Morozov committed
9
use Doctrine\DBAL\Schema\Table;
10
use Doctrine\DBAL\Tests\FunctionalTestCase;
Sergei Morozov's avatar
Sergei Morozov committed
11
use Throwable;
12

13
use function strlen;
14 15 16 17

/**
 * @group DBAL-56
 */
18
class PortabilityTest extends FunctionalTestCase
19
{
20
    protected function setUp(): void
21
    {
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
        parent::setUp();

        $this->connection = DriverManager::getConnection(
            $this->connection->getParams(),
            $this->connection->getConfiguration()
                ->setMiddlewares([new Middleware(Connection::PORTABILITY_ALL, ColumnCase::LOWER)])
        );

        try {
            $table = new Table('portability_table');
            $table->addColumn('Test_Int', 'integer');
            $table->addColumn('Test_String', 'string', ['fixed' => true, 'length' => 32]);
            $table->addColumn('Test_Null', 'string', ['notnull' => false]);
            $table->setPrimaryKey(['Test_Int']);

            $sm = $this->connection->getSchemaManager();
            $sm->createTable($table);

            $this->connection->insert('portability_table', ['Test_Int' => 1, 'Test_String' => 'foo', 'Test_Null' => '']);
            $this->connection->insert('portability_table', ['Test_Int' => 2, 'Test_String' => 'foo  ', 'Test_Null' => null]);
        } catch (Throwable $e) {
43 44
        }
    }
45

46 47 48
    public function tearDown(): void
    {
        self::resetSharedConn();
49
    }
50

51
    public function testFullFetchMode(): void
52
    {
53
        $rows = $this->connection->fetchAllAssociative('SELECT * FROM portability_table');
54
        $this->assertFetchResultRows($rows);
55

56
        $result = $this->connection->query('SELECT * FROM portability_table');
57

58
        while (($row = $result->fetchAssociative())) {
59 60
            $this->assertFetchResultRow($row);
        }
61

62
        $result = $this->connection
63 64
            ->prepare('SELECT * FROM portability_table')
            ->execute();
65

66
        while (($row = $result->fetchAssociative())) {
67 68 69
            $this->assertFetchResultRow($row);
        }
    }
70

71
    public function testConnFetchMode(): void
72
    {
73
        $rows = $this->connection->fetchAllAssociative('SELECT * FROM portability_table');
74 75
        $this->assertFetchResultRows($rows);

76
        $result = $this->connection->query('SELECT * FROM portability_table');
77
        while (($row = $result->fetchAssociative())) {
Sergei Morozov's avatar
Sergei Morozov committed
78
            $this->assertFetchResultRow($row);
79 80
        }

81
        $result = $this->connection->prepare('SELECT * FROM portability_table')
82 83 84
            ->execute();

        while (($row = $result->fetchAssociative())) {
Sergei Morozov's avatar
Sergei Morozov committed
85
            $this->assertFetchResultRow($row);
86 87 88
        }
    }

89 90 91
    /**
     * @param array<int, array<string, mixed>> $rows
     */
92
    private function assertFetchResultRows(array $rows): void
93
    {
Gabriel Caruso's avatar
Gabriel Caruso committed
94
        self::assertCount(2, $rows);
jeroendedauw's avatar
jeroendedauw committed
95
        foreach ($rows as $row) {
96 97 98
            $this->assertFetchResultRow($row);
        }
    }
99

100 101 102
    /**
     * @param array<string, mixed> $row
     */
103
    public function assertFetchResultRow(array $row): void
104
    {
105 106 107 108 109
        self::assertThat($row['test_int'], self::logicalOr(
            self::equalTo(1),
            self::equalTo(2)
        ));

Sergei Morozov's avatar
Sergei Morozov committed
110 111
        self::assertArrayHasKey('test_string', $row, 'Case should be lowered.');
        self::assertEquals(3, strlen($row['test_string']), 'test_string should be rtrimed to length of three for CHAR(32) column.');
112
        self::assertNull($row['test_null']);
Sergei Morozov's avatar
Sergei Morozov committed
113
        self::assertArrayNotHasKey(0, $row, 'The row should not contain numerical keys.');
114
    }
115

116
    /**
Sergei Morozov's avatar
Sergei Morozov committed
117 118
     * @param mixed[] $expected
     *
119
     * @dataProvider fetchColumnProvider
120
     */
121
    public function testFetchColumn(string $field, array $expected): void
122
    {
123
        $result = $this->connection->query('SELECT ' . $field . ' FROM portability_table');
124

125
        $column = $result->fetchFirstColumn();
126
        self::assertEquals($expected, $column);
127 128
    }

129 130 131
    /**
     * @return iterable<string, array<int, mixed>>
     */
132
    public static function fetchColumnProvider(): iterable
133
    {
Sergei Morozov's avatar
Sergei Morozov committed
134 135
        return [
            'int' => [
136
                'Test_Int',
Sergei Morozov's avatar
Sergei Morozov committed
137 138 139
                [1, 2],
            ],
            'string' => [
140
                'Test_String',
Sergei Morozov's avatar
Sergei Morozov committed
141 142 143
                ['foo', 'foo'],
            ],
        ];
144 145
    }

146
    public function testFetchAllNullColumn(): void
147
    {
148
        $column = $this->connection->fetchFirstColumn('SELECT Test_Null FROM portability_table');
149

Sergei Morozov's avatar
Sergei Morozov committed
150
        self::assertSame([null, null], $column);
151
    }
152
}