Failed Conditions
Pull Request — develop (#3582)
by Jonathan
69:13 queued 04:11
created

PortabilityTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Functional;
6
7
use Doctrine\DBAL\ColumnCase;
8
use Doctrine\DBAL\Connection;
9
use Doctrine\DBAL\DriverManager;
10
use Doctrine\DBAL\FetchMode;
11
use Doctrine\DBAL\Portability\Connection as ConnectionPortability;
12
use Doctrine\DBAL\Schema\Table;
13
use Doctrine\Tests\DbalFunctionalTestCase;
14
use Throwable;
15
use function strlen;
16
17
/**
18
 * @group DBAL-56
19
 */
20
class PortabilityTest extends DbalFunctionalTestCase
21
{
22
    /** @var Connection */
23
    private $portableConnection;
24
25
    protected function setUp() : void
26
    {
27
        parent::setUp();
28
29
        $this->portableConnection = $this->getPortableConnection();
30
    }
31
32
    protected function tearDown() : void
33
    {
34
        $this->portableConnection->close();
35
36
        parent::tearDown();
37
    }
38
39
    private function getPortableConnection(
40
        int $portabilityMode = ConnectionPortability::PORTABILITY_ALL,
41
        int $case = ColumnCase::LOWER
42
    ) : Connection {
43
        $params = $this->connection->getParams();
44
45
        $params['wrapperClass'] = ConnectionPortability::class;
46
        $params['portability']  = $portabilityMode;
47
        $params['fetch_case']   = $case;
48
49
        $portableConnection = DriverManager::getConnection($params, $this->connection->getConfiguration(), $this->connection->getEventManager());
50
51
        try {
52
            $table = new Table('portability_table');
53
            $table->addColumn('Test_Int', 'integer');
54
            $table->addColumn('Test_String', 'string', ['fixed' => true, 'length' => 32]);
55
            $table->addColumn('Test_Null', 'string', ['notnull' => false]);
56
            $table->setPrimaryKey(['Test_Int']);
57
58
            $sm = $portableConnection->getSchemaManager();
59
            $sm->dropAndCreateTable($table);
60
61
            $portableConnection->insert('portability_table', ['Test_Int' => 1, 'Test_String' => 'foo', 'Test_Null' => '']);
62
            $portableConnection->insert('portability_table', ['Test_Int' => 2, 'Test_String' => 'foo  ', 'Test_Null' => null]);
63
        } catch (Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
64
        }
65
66
        return $portableConnection;
67
    }
68
69
    public function testFullFetchMode() : void
70
    {
71
        $rows = $this->portableConnection->fetchAll('SELECT * FROM portability_table');
72
        $this->assertFetchResultRows($rows);
73
74
        $stmt = $this->portableConnection->query('SELECT * FROM portability_table');
75
        $stmt->setFetchMode(FetchMode::ASSOCIATIVE);
76
77
        foreach ($stmt as $row) {
78
            $this->assertFetchResultRow($row);
79
        }
80
81
        $stmt = $this->portableConnection->query('SELECT * FROM portability_table');
82
83
        while (($row = $stmt->fetch(FetchMode::ASSOCIATIVE))) {
84
            $this->assertFetchResultRow($row);
85
        }
86
87
        $stmt = $this->portableConnection->prepare('SELECT * FROM portability_table');
88
        $stmt->execute();
89
90
        while (($row = $stmt->fetch(FetchMode::ASSOCIATIVE))) {
91
            $this->assertFetchResultRow($row);
92
        }
93
    }
94
95
    public function testConnFetchMode() : void
96
    {
97
        $this->portableConnection->setFetchMode(FetchMode::ASSOCIATIVE);
98
99
        $rows = $this->portableConnection->fetchAll('SELECT * FROM portability_table');
100
        $this->assertFetchResultRows($rows);
101
102
        $stmt = $this->portableConnection->query('SELECT * FROM portability_table');
103
        foreach ($stmt as $row) {
104
            $this->assertFetchResultRow($row);
105
        }
106
107
        $stmt = $this->portableConnection->query('SELECT * FROM portability_table');
108
        while (($row = $stmt->fetch())) {
109
            $this->assertFetchResultRow($row);
110
        }
111
112
        $stmt = $this->portableConnection->prepare('SELECT * FROM portability_table');
113
        $stmt->execute();
114
        while (($row = $stmt->fetch())) {
115
            $this->assertFetchResultRow($row);
116
        }
117
    }
118
119
    /**
120
     * @param array<int, array<string, mixed>> $rows
121
     */
122
    private function assertFetchResultRows(array $rows) : void
123
    {
124
        self::assertCount(2, $rows);
125
        foreach ($rows as $row) {
126
            $this->assertFetchResultRow($row);
127
        }
128
    }
129
130
    /**
131
     * @param array<string, mixed> $row
132
     */
133
    public function assertFetchResultRow(array $row) : void
134
    {
135
        self::assertContains($row['test_int'], [1, 2], 'Primary key test_int should either be 1 or 2.');
136
        self::assertArrayHasKey('test_string', $row, 'Case should be lowered.');
137
        self::assertEquals(3, strlen($row['test_string']), 'test_string should be rtrimed to length of three for CHAR(32) column.');
138
        self::assertNull($row['test_null']);
139
        self::assertArrayNotHasKey(0, $row, 'The row should not contain numerical keys.');
140
    }
141
142
    /**
143
     * @param mixed[] $expected
144
     *
145
     * @dataProvider fetchAllColumnProvider
146
     */
147
    public function testFetchAllColumn(string $field, array $expected) : void
148
    {
149
        $stmt = $this->portableConnection->query('SELECT ' . $field . ' FROM portability_table');
150
151
        $column = $stmt->fetchAll(FetchMode::COLUMN);
152
        self::assertEquals($expected, $column);
153
    }
154
155
    /**
156
     * @return iterable<string, array<int, mixed>>
157
     */
158
    public static function fetchAllColumnProvider() : iterable
159
    {
160
        return [
161
            'int' => [
162
                'Test_Int',
163
                [1, 2],
164
            ],
165
            'string' => [
166
                'Test_String',
167
                ['foo', 'foo'],
168
            ],
169
        ];
170
    }
171
172
    public function testFetchAllNullColumn() : void
173
    {
174
        $stmt = $this->portableConnection->query('SELECT Test_Null FROM portability_table');
175
176
        $column = $stmt->fetchAll(FetchMode::COLUMN);
177
        self::assertSame([null, null], $column);
178
    }
179
}
180