Completed
Push — master ( 5e9cdb...0c7452 )
by Marco
48s
created

testBooleanConversionNullParamEmulatedPrepares()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 19
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 19
loc 19
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
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
39
        parent::tearDown();
40
    }
41
42
    public function testBooleanConversionSqlLiteral()
43
    {
44
        $this->_conn->executeUpdate('INSERT INTO dbal630 (bool_col) VALUES(false)');
45
        $id = $this->_conn->lastInsertId('dbal630_id_seq');
46
        self::assertNotEmpty($id);
47
48
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', array($id));
49
50
        self::assertFalse($row['bool_col']);
51
    }
52
53
    public function testBooleanConversionBoolParamRealPrepares()
54
    {
55
        $this->_conn->executeUpdate('INSERT INTO dbal630 (bool_col) VALUES(?)', array('false'), array(PDO::PARAM_BOOL));
56
        $id = $this->_conn->lastInsertId('dbal630_id_seq');
57
        self::assertNotEmpty($id);
58
59
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', array($id));
60
61
        self::assertFalse($row['bool_col']);
62
    }
63
64 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...
65
    {
66
        $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
67
68
        $platform = $this->_conn->getDatabasePlatform();
69
70
        $stmt = $this->_conn->prepare('INSERT INTO dbal630 (bool_col) VALUES(?)');
71
        $stmt->bindValue(1, $platform->convertBooleansToDatabaseValue('false'), PDO::PARAM_BOOL);
72
        $stmt->execute();
73
74
        $id = $this->_conn->lastInsertId('dbal630_id_seq');
75
76
        self::assertNotEmpty($id);
77
78
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630 WHERE id = ?', array($id));
79
80
        self::assertFalse($row['bool_col']);
81
    }
82
83
    /**
84
     * @dataProvider booleanTypeConversionWithoutPdoTypeProvider
85
     */
86 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...
87
        $statementValue,
88
        $databaseConvertedValue
89
    ) {
90
        $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
91
92
        $platform = $this->_conn->getDatabasePlatform();
93
94
        $stmt = $this->_conn->prepare('INSERT INTO dbal630_allow_nulls (bool_col) VALUES(?)');
95
        $stmt->bindValue(1, $platform->convertBooleansToDatabaseValue($statementValue));
96
        $stmt->execute();
97
98
        $id = $this->_conn->lastInsertId('dbal630_allow_nulls_id_seq');
99
100
        self::assertNotEmpty($id);
101
102
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', array($id));
103
104
        self::assertSame($databaseConvertedValue, $row['bool_col']);
105
    }
106
107
    /**
108
     * @dataProvider booleanTypeConversionUsingBooleanTypeProvider
109
     */
110 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...
111
        $statementValue,
112
        $databaseConvertedValue
113
    ) {
114
        $this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
115
116
        $platform = $this->_conn->getDatabasePlatform();
117
118
        $stmt = $this->_conn->prepare('INSERT INTO dbal630_allow_nulls (bool_col) VALUES(?)');
119
        $stmt->bindValue(1, $platform->convertBooleansToDatabaseValue($statementValue), PDO::PARAM_BOOL);
120
        $stmt->execute();
121
122
        $id = $this->_conn->lastInsertId('dbal630_allow_nulls_id_seq');
123
124
        self::assertNotEmpty($id);
125
126
        $row = $this->_conn->fetchAssoc('SELECT bool_col FROM dbal630_allow_nulls WHERE id = ?', array($id));
127
128
        self::assertSame($databaseConvertedValue, $row['bool_col']);
129
    }
130
131
    /**
132
     * Boolean conversion mapping provider
133
     * @return array
134
     */
135 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...
136
    {
137
        return array(
138
            // statement value, database converted value result
139
            array(true, true),
140
            array(false, false),
141
            array(null, false)
142
        );
143
    }
144
145
    /**
146
     * Boolean conversion mapping provider
147
     * @return array
148
     */
149 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...
150
    {
151
        return array(
152
            // statement value, database converted value result
153
            array(true, true),
154
            array(false, false),
155
            array(null, null)
156
        );
157
    }
158
}
159