Failed Conditions
Pull Request — master (#2958)
by Sergei
61:13
created

PortabilityTest::testFullFetchMode()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 8
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Driver\ResultStatement;
7
use Doctrine\DBAL\DriverManager;
8
use Doctrine\DBAL\Portability\Connection as ConnectionPortability;
9
10
/**
11
 * @group DBAL-56
12
 */
13
class PortabilityTest extends \Doctrine\Tests\DbalFunctionalTestCase
14
{
15
    private $portableConnection;
16
17
    protected function tearDown()
18
    {
19
        if ($this->portableConnection) {
20
            $this->portableConnection->close();
21
        }
22
23
        parent::tearDown();
24
    }
25
26
    /**
27
     * @param   integer     $portabilityMode
28
     * @param   integer     $case
29
     * @return  Connection
30
     */
31
    private function getPortableConnection(
32
        $portabilityMode = ConnectionPortability::PORTABILITY_ALL,
33
        $case = ConnectionPortability::CASE_LOWER
34
    ) {
35
        if (!$this->portableConnection) {
36
            $params = $this->_conn->getParams();
37
38
            $params['wrapperClass'] = ConnectionPortability::class;
39
            $params['portability']  = $portabilityMode;
40
            $params['fetch_case']   = $case;
41
42
            $this->portableConnection = DriverManager::getConnection($params, $this->_conn->getConfiguration(), $this->_conn->getEventManager());
43
44
            try {
45
                /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
46
                $table = new \Doctrine\DBAL\Schema\Table("portability_table");
47
                $table->addColumn('Test_Int', 'integer');
48
                $table->addColumn('Test_String', 'string', array('fixed' => true, 'length' => 32));
49
                $table->addColumn('Test_Null', 'string', array('notnull' => false));
50
                $table->setPrimaryKey(array('Test_Int'));
51
52
                $sm = $this->portableConnection->getSchemaManager();
53
                $sm->createTable($table);
54
55
                $this->portableConnection->insert('portability_table', array('Test_Int' => 1, 'Test_String' => 'foo', 'Test_Null' => ''));
56
                $this->portableConnection->insert('portability_table', array('Test_Int' => 2, 'Test_String' => 'foo  ', 'Test_Null' => null));
57
            } catch(\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
58
59
            }
60
        }
61
62
        return $this->portableConnection;
63
    }
64
65
    public function testFullFetchMode()
66
    {
67
        $rows = $this->getPortableConnection()->fetchAll('SELECT * FROM portability_table');
68
        $this->assertFetchResultRows($rows);
69
70
        $stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
71
        $stmt->setFetchMode(ResultStatement::FETCH_ASSOC);
72
73
        foreach ($stmt as $row) {
74
            $this->assertFetchResultRow($row);
75
        }
76
77
        $stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
78
79
        while (($row = $stmt->fetch(ResultStatement::FETCH_ASSOC))) {
80
            $this->assertFetchResultRow($row);
81
        }
82
83
        $stmt = $this->getPortableConnection()->prepare('SELECT * FROM portability_table');
84
        $stmt->execute();
85
86
        while (($row = $stmt->fetch(ResultStatement::FETCH_ASSOC))) {
87
            $this->assertFetchResultRow($row);
88
        }
89
    }
90
91
    public function testConnFetchMode()
92
    {
93
        $conn = $this->getPortableConnection();
94
        $conn->setFetchMode(ResultStatement::FETCH_ASSOC);
95
96
        $rows = $conn->fetchAll('SELECT * FROM portability_table');
97
        $this->assertFetchResultRows($rows);
98
99
        $stmt = $conn->query('SELECT * FROM portability_table');
100
        foreach ($stmt as $row) {
101
          $this->assertFetchResultRow($row);
102
        }
103
104
        $stmt = $conn->query('SELECT * FROM portability_table');
105
        while (($row = $stmt->fetch())) {
106
          $this->assertFetchResultRow($row);
107
        }
108
109
        $stmt = $conn->prepare('SELECT * FROM portability_table');
110
        $stmt->execute();
111
        while (($row = $stmt->fetch())) {
112
          $this->assertFetchResultRow($row);
113
        }
114
    }
115
116
    public function assertFetchResultRows($rows)
117
    {
118
        self::assertCount(2, $rows);
119
        foreach ($rows as $row) {
120
            $this->assertFetchResultRow($row);
121
        }
122
    }
123
124
    public function assertFetchResultRow($row)
125
    {
126
        self::assertContains($row['test_int'], array(1, 2), "Primary key test_int should either be 1 or 2.");
127
        self::assertArrayHasKey('test_string', $row, "Case should be lowered.");
128
        self::assertEquals(3, strlen($row['test_string']), "test_string should be rtrimed to length of three for CHAR(32) column.");
129
        self::assertNull($row['test_null']);
130
        self::assertArrayNotHasKey(0, $row, "The row should not contain numerical keys.");
131
    }
132
133
    /**
134
     * @requires extension pdo
135
     */
136
    public function testPortabilityPdoSqlServer()
137
    {
138
        $portability = ConnectionPortability::PORTABILITY_SQLSRV;
139
        $params = array(
140
            'portability' => $portability
141
        );
142
143
        $driverMock = $this->getMockBuilder('Doctrine\\DBAL\\Driver\\PDOSqlsrv\\Driver')
144
            ->setMethods(array('connect'))
145
            ->getMock();
146
147
        $driverMock->expects($this->once())
148
                   ->method('connect')
149
                   ->will($this->returnValue(null));
150
151
        $connection = new ConnectionPortability($params, $driverMock);
152
153
        $connection->connect($params);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\DBAL\Portability\Connection::connect() has too many arguments starting with $params. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        $connection->/** @scrutinizer ignore-call */ 
154
                     connect($params);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
154
155
        self::assertEquals($portability, $connection->getPortability());
156
    }
157
158
    /**
159
     * @dataProvider fetchAllColumnProvider
160
     */
161
    public function testFetchAllColumn($field, array $expected)
162
    {
163
        $conn = $this->getPortableConnection();
164
        $stmt = $conn->query('SELECT ' . $field . ' FROM portability_table');
165
166
        $column = $stmt->fetchAll(ResultStatement::FETCH_COLUMN);
167
        self::assertEquals($expected, $column);
168
    }
169
170
    public static function fetchAllColumnProvider()
171
    {
172
        return array(
173
            'int' => array(
174
                'Test_Int',
175
                array(1, 2),
176
            ),
177
            'string' => array(
178
                'Test_String',
179
                array('foo', 'foo'),
180
            ),
181
        );
182
    }
183
184
    public function testFetchAllNullColumn()
185
    {
186
        $conn = $this->getPortableConnection();
187
        $stmt = $conn->query('SELECT Test_Null FROM portability_table');
188
189
        $column = $stmt->fetchAll(ResultStatement::FETCH_COLUMN);
190
        self::assertSame(array(null, null), $column);
191
    }
192
}
193