Failed Conditions
Push — master ( 30b923...92920e )
by Marco
19s queued 13s
created

PortabilityTest::testPortabilityPdoSqlServer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0

1 Method

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