PortabilityTest.php 4.5 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
class PortabilityTest extends FunctionalTestCase
16
{
17
    protected function setUp(): void
18
    {
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
        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);

37 38 39 40 41 42 43 44 45 46 47
            $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,
            ]);
48
        } catch (Throwable $e) {
49 50
        }
    }
51

52 53 54
    public function tearDown(): void
    {
        self::resetSharedConn();
55
    }
56

57
    public function testFullFetchMode(): void
58
    {
59
        $rows = $this->connection->fetchAllAssociative('SELECT * FROM portability_table');
60
        $this->assertFetchResultRows($rows);
61

62
        $result = $this->connection->executeQuery('SELECT * FROM portability_table');
63

64
        while (($row = $result->fetchAssociative())) {
65 66
            $this->assertFetchResultRow($row);
        }
67

68
        $result = $this->connection
69 70
            ->prepare('SELECT * FROM portability_table')
            ->execute();
71

72
        while (($row = $result->fetchAssociative())) {
73 74 75
            $this->assertFetchResultRow($row);
        }
    }
76

77
    public function testConnFetchMode(): void
78
    {
79
        $rows = $this->connection->fetchAllAssociative('SELECT * FROM portability_table');
80 81
        $this->assertFetchResultRows($rows);

82
        $result = $this->connection->executeQuery('SELECT * FROM portability_table');
83
        while (($row = $result->fetchAssociative())) {
Sergei Morozov's avatar
Sergei Morozov committed
84
            $this->assertFetchResultRow($row);
85 86
        }

87
        $result = $this->connection->prepare('SELECT * FROM portability_table')
88 89 90
            ->execute();

        while (($row = $result->fetchAssociative())) {
Sergei Morozov's avatar
Sergei Morozov committed
91
            $this->assertFetchResultRow($row);
92 93 94
        }
    }

95 96 97
    /**
     * @param array<int, array<string, mixed>> $rows
     */
98
    private function assertFetchResultRows(array $rows): void
99
    {
Gabriel Caruso's avatar
Gabriel Caruso committed
100
        self::assertCount(2, $rows);
jeroendedauw's avatar
jeroendedauw committed
101
        foreach ($rows as $row) {
102 103 104
            $this->assertFetchResultRow($row);
        }
    }
105

106 107 108
    /**
     * @param array<string, mixed> $row
     */
109
    public function assertFetchResultRow(array $row): void
110
    {
111 112 113 114 115
        self::assertThat($row['test_int'], self::logicalOr(
            self::equalTo(1),
            self::equalTo(2)
        ));

Sergei Morozov's avatar
Sergei Morozov committed
116
        self::assertArrayHasKey('test_string', $row, 'Case should be lowered.');
117
        self::assertEquals(3, strlen($row['test_string']));
118
        self::assertNull($row['test_null']);
Sergei Morozov's avatar
Sergei Morozov committed
119
        self::assertArrayNotHasKey(0, $row, 'The row should not contain numerical keys.');
120
    }
121

122
    /**
Sergei Morozov's avatar
Sergei Morozov committed
123 124
     * @param mixed[] $expected
     *
125
     * @dataProvider fetchColumnProvider
126
     */
127
    public function testFetchColumn(string $column, array $expected): void
128
    {
129
        $result = $this->connection->executeQuery('SELECT ' . $column . ' FROM portability_table');
130

131
        self::assertEquals($expected, $result->fetchFirstColumn());
132 133
    }

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

151
    public function testFetchAllNullColumn(): void
152
    {
153
        $column = $this->connection->fetchFirstColumn('SELECT Test_Null FROM portability_table');
154

Sergei Morozov's avatar
Sergei Morozov committed
155
        self::assertSame([null, null], $column);
156
    }
157
}