Failed Conditions
Pull Request — master (#3074)
by Sergei
21:38
created

WriteTest::testEmptyIdentityInsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional;
4
5
use Doctrine\DBAL\ParameterType;
6
use Doctrine\DBAL\Types\Type;
7
use Doctrine\Tests\DbalFunctionalTestCase;
8
9
class WriteTest extends DbalFunctionalTestCase
10
{
11
    protected function setUp()
12
    {
13
        parent::setUp();
14
15
        $this->createTable('write_table');
16
    }
17
18
    private function createTable($tableName)
19
    {
20
        $table = new \Doctrine\DBAL\Schema\Table($tableName);
21
        $table->addColumn('id', 'integer', array('autoincrement' => true));
22
        $table->addColumn('test_int', 'integer');
23
        $table->addColumn('test_string', 'string', array('notnull' => false));
24
        $table->setPrimaryKey(array('id'));
25
26
        $this->_conn->getSchemaManager()->createTable($table);
27
    }
28
29
    protected function tearDown()
30
    {
31
        $this->_conn->getSchemaManager()->dropTable('write_table');
32
33
        parent::tearDown();
34
    }
35
36
    /**
37
     * @group DBAL-80
38
     */
39
    public function testExecuteUpdateFirstTypeIsNull()
40
    {
41
        $sql = "INSERT INTO write_table (test_string, test_int) VALUES (?, ?)";
42
        $this->_conn->executeUpdate($sql, ['text', 1111], [null, ParameterType::INTEGER]);
43
44
        $sql = "SELECT * FROM write_table WHERE test_string = ? AND test_int = ?";
45
        self::assertTrue((bool) $this->_conn->fetchColumn($sql, ['text', 1111]));
46
    }
47
48
    public function testExecuteUpdate()
49
    {
50
        $sql = "INSERT INTO write_table (test_int) VALUES ( " . $this->_conn->quote(1) . ")";
51
        $affected = $this->_conn->executeUpdate($sql);
52
53
        self::assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
54
    }
55
56
    public function testExecuteUpdateWithTypes()
57
    {
58
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
59
        $affected = $this->_conn->executeUpdate(
60
            $sql,
61
            [1, 'foo'],
62
            [ParameterType::INTEGER, ParameterType::STRING]
63
        );
64
65
        self::assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
66
    }
67
68
    public function testPrepareRowCountReturnsAffectedRows()
69
    {
70
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
71
        $stmt = $this->_conn->prepare($sql);
72
73
        $stmt->bindValue(1, 1);
74
        $stmt->bindValue(2, "foo");
75
        $stmt->execute();
76
77
        self::assertEquals(1, $stmt->rowCount());
78
    }
79
80
    public function testPrepareWithPdoTypes()
81
    {
82
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
83
        $stmt = $this->_conn->prepare($sql);
84
85
        $stmt->bindValue(1, 1, ParameterType::INTEGER);
86
        $stmt->bindValue(2, 'foo', ParameterType::STRING);
87
        $stmt->execute();
88
89
        self::assertEquals(1, $stmt->rowCount());
90
    }
91
92
    public function testPrepareWithDbalTypes()
93
    {
94
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
95
        $stmt = $this->_conn->prepare($sql);
96
97
        $stmt->bindValue(1, 1, Type::getType('integer'));
98
        $stmt->bindValue(2, "foo", Type::getType('string'));
99
        $stmt->execute();
100
101
        self::assertEquals(1, $stmt->rowCount());
102
    }
103
104
    public function testPrepareWithDbalTypeNames()
105
    {
106
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
107
        $stmt = $this->_conn->prepare($sql);
108
109
        $stmt->bindValue(1, 1, 'integer');
110
        $stmt->bindValue(2, "foo", 'string');
111
        $stmt->execute();
112
113
        self::assertEquals(1, $stmt->rowCount());
114
    }
115
116
    public function insertRows()
117
    {
118
        self::assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 1, 'test_string' => 'foo')));
119
        self::assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar')));
120
    }
121
122
    public function testInsert()
123
    {
124
        $this->insertRows();
125
    }
126
127
    public function testDelete()
128
    {
129
        $this->insertRows();
130
131
        self::assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 2)));
132
        self::assertCount(1, $this->_conn->fetchAll('SELECT * FROM write_table'));
133
134
        self::assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 1)));
135
        self::assertCount(0, $this->_conn->fetchAll('SELECT * FROM write_table'));
136
    }
137
138
    public function testUpdate()
139
    {
140
        $this->insertRows();
141
142
        self::assertEquals(1, $this->_conn->update('write_table', array('test_string' => 'bar'), array('test_string' => 'foo')));
143
        self::assertEquals(2, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
144
        self::assertEquals(0, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
145
    }
146
147
    /**
148
     * @group DBAL-445
149
     */
150
    public function testInsertWithKeyValueTypes()
151
    {
152
        $testString = new \DateTime('2013-04-14 10:10:10');
153
154
        $this->_conn->insert(
155
            'write_table',
156
            array('test_int' => '30', 'test_string' => $testString),
157
            array('test_string' => 'datetime', 'test_int' => 'integer')
158
        );
159
160
        $data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');
161
162
        self::assertEquals($testString->format($this->_conn->getDatabasePlatform()->getDateTimeFormatString()), $data);
163
    }
164
165
    /**
166
     * @group DBAL-445
167
     */
168
    public function testUpdateWithKeyValueTypes()
169
    {
170
        $testString = new \DateTime('2013-04-14 10:10:10');
171
172
        $this->_conn->insert(
173
            'write_table',
174
            array('test_int' => '30', 'test_string' => $testString),
175
            array('test_string' => 'datetime', 'test_int' => 'integer')
176
        );
177
178
        $testString = new \DateTime('2013-04-15 10:10:10');
179
180
        $this->_conn->update(
181
            'write_table',
182
            array('test_string' => $testString),
183
            array('test_int' => '30'),
184
            array('test_string' => 'datetime', 'test_int' => 'integer')
185
        );
186
187
        $data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');
188
189
        self::assertEquals($testString->format($this->_conn->getDatabasePlatform()->getDateTimeFormatString()), $data);
190
    }
191
192
    /**
193
     * @group DBAL-445
194
     */
195
    public function testDeleteWithKeyValueTypes()
196
    {
197
        $val = new \DateTime('2013-04-14 10:10:10');
198
        $this->_conn->insert(
199
            'write_table',
200
            array('test_int' => '30', 'test_string' => $val),
201
            array('test_string' => 'datetime', 'test_int' => 'integer')
202
        );
203
204
        $this->_conn->delete('write_table', array('test_int' => 30, 'test_string' => $val), array('test_string' => 'datetime', 'test_int' => 'integer'));
205
206
        $data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');
207
208
        self::assertFalse($data);
209
    }
210
211
    public function testEmptyIdentityInsert()
212
    {
213
        $table = new \Doctrine\DBAL\Schema\Table('test_empty_identity');
214
        $table->addColumn('id', 'integer', array('autoincrement' => true));
215
        $table->setPrimaryKey(array('id'));
216
217
        $this->_conn->getSchemaManager()->createTable($table);
218
219
        $sql = $this->_conn->getDatabasePlatform()->getEmptyIdentityInsertSQL('test_empty_identity', 'id');
220
221
        self::assertSame(1, $this->_conn->exec($sql));
222
    }
223
224
    /**
225
     * @group DBAL-2688
226
     */
227
    public function testUpdateWhereIsNull()
228
    {
229
        $this->_conn->insert(
230
            'write_table',
231
            ['test_int' => '30', 'test_string' => null],
232
            ['test_string' => 'string', 'test_int' => 'integer']
233
        );
234
235
        $data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
236
237
        self::assertCount(1, $data);
238
239
        $this->_conn->update('write_table', ['test_int' => 10], ['test_string' => null], ['test_string' => 'string', 'test_int' => 'integer']);
240
241
        $data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
242
243
        self::assertCount(0, $data);
244
    }
245
246
    public function testDeleteWhereIsNull()
247
    {
248
        $this->_conn->insert(
249
            'write_table',
250
            ['test_int' => '30', 'test_string' => null],
251
            ['test_string' => 'string', 'test_int' => 'integer']
252
        );
253
254
        $data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
255
256
        self::assertCount(1, $data);
257
258
        $this->_conn->delete('write_table', ['test_string' => null], ['test_string' => 'string']);
259
260
        $data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30');
261
262
        self::assertCount(0, $data);
263
    }
264
}
265