Failed Conditions
Pull Request — develop (#3582)
by Jonathan
64:57
created

PortabilityTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 76
dl 0
loc 169
rs 10
c 0
b 0
f 0

10 Methods

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