Passed
Pull Request — master (#2920)
by Luís
09:30
created

testBooleanConversionBoolParamEmulatedPrepares()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 11

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
dl 23
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional\Ticket;
4
5
use Doctrine\DBAL\DBALException;
6
use PDO;
7
8
/**
9
 * @group DBAL-630
10
 */
11
class DBAL630Test extends \Doctrine\Tests\DbalFunctionalTestCase
12
{
13
    private $running = false;
14
15
    protected function setUp()
16
    {
17
        parent::setUp();
18
19
        $platform = $this->_conn->getDatabasePlatform()->getName();
20
21
        if (!in_array($platform, array('postgresql'))) {
22
            $this->markTestSkipped('Currently restricted to PostgreSQL');
23
        }
24
25
        try {
26
            $this->_conn->exec('CREATE TABLE dbal630 (id SERIAL, bool_col BOOLEAN NOT NULL);');
27
            $this->_conn->exec('CREATE TABLE dbal630_allow_nulls (id SERIAL, bool_col BOOLEAN);');
28
        } catch (DBALException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
29
        }
30
        $this->running = true;
31
    }
32
33
    protected function tearDown()
34
    {
35
        if ($this->running) {
36
            $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
0 ignored issues
show
Bug introduced by
The method setAttribute() does not exist on Doctrine\DBAL\Driver\Connection. It seems like you code against a sub-type of Doctrine\DBAL\Driver\Connection such as Doctrine\DBAL\Driver\PDOSqlsrv\Connection or Doctrine\DBAL\Driver\PDOConnection. ( Ignorable by Annotation )

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

36
            $this->_conn->getWrappedConnection()->/** @scrutinizer ignore-call */ setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
Loading history...
37
38
            // PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT is deprecated in php 5.6. PDO::ATTR_EMULATE_PREPARES should
39
            // be used instead. so should only it be set when it is supported.
40
            if (PHP_VERSION_ID < 50600) {
41
                $this->_conn->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, false);
42
            }
43
        }
44
45
        parent::tearDown();
46
    }
47
48
    public function testBooleanConversionSqlLiteral()
49
    {
50
        $this->_conn->executeUpdate('INSERT INTO dbal630 (bool_col) VALUES(false)');
51
        $id = $this->_conn->lastInsertId('dbal630_id_seq');
52
        self::assertNotEmpty($id);
53
54
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', array($id));
55
56
        self::assertFalse($row['bool_col']);
57
    }
58
59
    public function testBooleanConversionBoolParamRealPrepares()
60
    {
61
        $this->_conn->executeUpdate('INSERT INTO dbal630 (bool_col) VALUES(?)', array('false'), array(PDO::PARAM_BOOL));
62
        $id = $this->_conn->lastInsertId('dbal630_id_seq');
63
        self::assertNotEmpty($id);
64
65
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', array($id));
66
67
        self::assertFalse($row['bool_col']);
68
    }
69
70 View Code Duplication
    public function testBooleanConversionBoolParamEmulatedPrepares()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
73
74
        // PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT is deprecated in php 5.6. PDO::ATTR_EMULATE_PREPARES should
75
        // be used instead. so should only it be set when it is supported.
76
        if (PHP_VERSION_ID < 50600) {
77
            $this->_conn->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, true);
78
        }
79
80
        $platform = $this->_conn->getDatabasePlatform();
81
82
        $stmt = $this->_conn->prepare('INSERT INTO dbal630 (bool_col) VALUES(?)');
83
        $stmt->bindValue(1, $platform->convertBooleansToDatabaseValue('false'), PDO::PARAM_BOOL);
84
        $stmt->execute();
85
86
        $id = $this->_conn->lastInsertId('dbal630_id_seq');
87
88
        self::assertNotEmpty($id);
89
90
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', array($id));
91
92
        self::assertFalse($row['bool_col']);
93
    }
94
95
    /**
96
     * @dataProvider booleanTypeConversionWithoutPdoTypeProvider
97
     */
98 View Code Duplication
    public function testBooleanConversionNullParamEmulatedPrepares(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
        $statementValue,
100
        $databaseConvertedValue
101
    ) {
102
        $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
103
104
        // PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT is deprecated in php 5.6. PDO::ATTR_EMULATE_PREPARES should
105
        // be used instead. so should only it be set when it is supported.
106
        if (PHP_VERSION_ID < 50600) {
107
            $this->_conn->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, true);
108
        }
109
110
        $platform = $this->_conn->getDatabasePlatform();
111
112
        $stmt = $this->_conn->prepare('INSERT INTO dbal630_allow_nulls (bool_col) VALUES(?)');
113
        $stmt->bindValue(1, $platform->convertBooleansToDatabaseValue($statementValue));
114
        $stmt->execute();
115
116
        $id = $this->_conn->lastInsertId('dbal630_allow_nulls_id_seq');
117
118
        self::assertNotEmpty($id);
119
120
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', array($id));
121
122
        self::assertSame($databaseConvertedValue, $row['bool_col']);
123
    }
124
125
    /**
126
     * @dataProvider booleanTypeConversionUsingBooleanTypeProvider
127
     */
128 View Code Duplication
    public function testBooleanConversionNullParamEmulatedPreparesWithBooleanTypeInBindValue(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
        $statementValue,
130
        $databaseConvertedValue
131
    ) {
132
        $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
133
134
        // PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT is deprecated in php 5.6. PDO::ATTR_EMULATE_PREPARES should
135
        // be used instead. so should only it be set when it is supported.
136
        if (PHP_VERSION_ID < 50600) {
137
            $this->_conn->getWrappedConnection()->setAttribute(PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, true);
138
        }
139
140
        $platform = $this->_conn->getDatabasePlatform();
141
142
        $stmt = $this->_conn->prepare('INSERT INTO dbal630_allow_nulls (bool_col) VALUES(?)');
143
        $stmt->bindValue(1, $platform->convertBooleansToDatabaseValue($statementValue), PDO::PARAM_BOOL);
144
        $stmt->execute();
145
146
        $id = $this->_conn->lastInsertId('dbal630_allow_nulls_id_seq');
147
148
        self::assertNotEmpty($id);
149
150
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', array($id));
151
152
        self::assertSame($databaseConvertedValue, $row['bool_col']);
153
    }
154
155
    /**
156
     * Boolean conversion mapping provider
157
     * @return array
158
     */
159 View Code Duplication
    public function booleanTypeConversionUsingBooleanTypeProvider()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161
        return array(
162
            // statement value, database converted value result
163
            array(true, true),
164
            array(false, false),
165
            array(null, false)
166
        );
167
    }
168
169
    /**
170
     * Boolean conversion mapping provider
171
     * @return array
172
     */
173 View Code Duplication
    public function booleanTypeConversionWithoutPdoTypeProvider()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
    {
175
        return array(
176
            // statement value, database converted value result
177
            array(true, true),
178
            array(false, false),
179
            array(null, null)
180
        );
181
    }
182
}
183