Passed
Pull Request — main (#109)
by Andreas
02:00
created

WriteTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 15
eloc 95
c 4
b 0
f 1
dl 0
loc 174
rs 10
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
23
namespace Crate\Test\DBAL\Functional;
24
25
use Crate\DBAL\Types\MapType;
26
use Crate\Test\DBAL\DBALFunctionalTestCase;
27
use Doctrine\DBAL\Schema\Column;
28
use Doctrine\DBAL\Types\Type;
29
use PDO;
30
31
class WriteTest extends DBALFunctionalTestCase
32
{
33
    static private $generated = false;
34
35
    public function setUp() : void
36
    {
37
        parent::setUp();
38
39
        if (self::$generated === false) {
40
            self::$generated = true;
41
            /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
42
            $table = new \Doctrine\DBAL\Schema\Table("write_table");
43
            $table->addColumn('test_int', Type::INTEGER);
44
            $table->addColumn('test_string', Type::STRING);
45
            $table->addColumn('test_float', Type::FLOAT);
46
            $table->addColumn('test_array', Type::TARRAY, array('columnDefinition'=>'ARRAY(STRING)'));
47
            $table->addColumn("test_map", MapType::NAME);
48
            $table->addColumn("test_bool", Type::BOOLEAN);
49
50
            $platformOptions = array(
51
                'type'   => MapType::STRICT,
52
                'fields' => array(
53
                    new Column('id',    Type::getType(Type::INTEGER), array()),
54
                    new Column('name',  Type::getType(Type::STRING), array()),
55
                    new Column('value', Type::getType(Type::FLOAT), array()),
56
                ),
57
            );
58
            $table->addColumn('test_obj', MapType::NAME, array('platformOptions'=>$platformOptions));
59
60
            $sm = $this->_conn->getSchemaManager();
61
            $sm->createTable($table);
62
        }
63
    }
64
65
    public function tearDown() : void
66
    {
67
        if (self::$generated === true) {
68
            $this->execute('drop table write_table');
69
            self::$generated = false;
70
        }
71
    }
72
73
74
    /**
75
     * @group DBAL-80
76
     */
77
    public function testExecuteUpdateFirstTypeIsNull()
78
    {
79
        $sql = "INSERT INTO write_table (test_string, test_int) VALUES (?, ?)";
80
        $this->_conn->executeUpdate($sql, array("text", 1111), array(null, PDO::PARAM_INT));
81
        $this->refresh('write_table');
82
83
        $sql = "SELECT test_obj, test_string, test_int FROM write_table WHERE test_string = ? AND test_int = ?";
84
        $this->assertEquals($this->_conn->fetchColumn($sql, array("text", 1111)), null);
85
        $this->assertEquals($this->_conn->fetchColumn($sql, array("text", 1111), 1), "text");
86
        $this->assertEquals($this->_conn->fetchColumn($sql, array("text", 1111), 2), 1111);
87
    }
88
89
    public function testExecuteUpdate()
90
    {
91
        $sql = "INSERT INTO write_table (test_int) VALUES ( " . $this->_conn->quote(1, PDO::PARAM_INT) . ")";
92
        $affected = $this->_conn->executeUpdate($sql);
93
94
        $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
95
    }
96
97
    public function testExecuteUpdateWithTypes()
98
    {
99
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
100
        $affected = $this->_conn->executeUpdate($sql, array(1, 'foo'), array(\PDO::PARAM_INT, \PDO::PARAM_STR));
101
102
        $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
103
    }
104
105
    public function testPrepareRowCountReturnsAffectedRows()
106
    {
107
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
108
        $stmt = $this->_conn->prepare($sql);
109
110
        $stmt->bindValue(1, 1);
111
        $stmt->bindValue(2, "foo");
112
        $stmt->execute();
113
114
        $this->assertEquals(1, $stmt->rowCount());
115
    }
116
117
    public function testPrepareWithPdoTypes()
118
    {
119
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
120
        $stmt = $this->_conn->prepare($sql);
121
122
        $stmt->bindValue(1, 1, \PDO::PARAM_INT);
123
        $stmt->bindValue(2, "foo", \PDO::PARAM_STR);
124
        $stmt->execute();
125
126
        $this->assertEquals(1, $stmt->rowCount());
127
    }
128
129
    public function testPrepareWithDbalTypes()
130
    {
131
        $sql = "INSERT INTO write_table (test_int, test_string, test_float, test_obj) VALUES (?, ?, ?, ?)";
132
        $stmt = $this->_conn->prepare($sql);
133
134
        $stmt->bindValue(1, 1, Type::getType('integer'));
135
        $stmt->bindValue(2, "foo", Type::getType('string'));
136
        $stmt->bindValue(3, 3.141592, Type::getType('float'));
137
        $stmt->bindValue(4, array('id'=>1, 'name'=>'christian', 'value'=>1.234), Type::getType('map'));
138
        $stmt->execute();
139
140
        $this->assertEquals(1, $stmt->rowCount());
141
    }
142
143
    public function testPrepareWithDbalTypeNames()
144
    {
145
        $sql = "INSERT INTO write_table (test_int, test_string, test_float, test_map, test_bool) VALUES (?, ?, ?, ?, ?)";
146
        $stmt = $this->_conn->prepare($sql);
147
148
        $stmt->bindValue(1, 1, 'integer');
149
        $stmt->bindValue(2, "foo", 'string');
150
        $stmt->bindValue(3, 3.141592, 'float');
151
        $stmt->bindValue(4, array('id'=>1, 'name'=>'christian', 'value'=>1.234), 'map');
152
        $stmt->bindValue(5, true, 'boolean');
153
        $stmt->execute();
154
155
        $this->assertEquals(1, $stmt->rowCount());
156
    }
157
158
    public function insertRows()
159
    {
160
        $this->assertEquals(1, $this->_conn->insert('write_table', array(
161
            'test_int' => 1,
162
            'test_string' => 'foo',
163
            'test_float' => 1.234,
164
            'test_array' => array('foo','bar'),
165
            'test_obj' => array('id'=>1, 'name'=>'foo', 'value'=>1.234),
166
        ), array('integer','string','float','array','map')));
167
        $this->assertEquals(1, $this->_conn->insert('write_table', array(
168
            'test_int' => 2,
169
            'test_string' => 'bar',
170
            'test_float' => 2.345,
171
            'test_array' => array('bar','foo'),
172
            'test_obj' => array('id'=>2, 'name'=>'bar', 'value'=>2.345),
173
        ), array('integer','string','float','array','map')));
174
175
        $this->refresh('write_table');
176
    }
177
178
    public function testInsert()
179
    {
180
        $this->insertRows();
181
    }
182
183
    public function testDelete()
184
    {
185
        $this->insertRows();
186
187
        $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 2)));
188
        $this->refresh('write_table');
189
        $this->assertEquals(1, count($this->_conn->fetchAll('SELECT * FROM write_table')));
190
191
        $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 1)));
192
        $this->refresh('write_table');
193
        $this->assertEquals(0, count($this->_conn->fetchAll('SELECT * FROM write_table')));
194
    }
195
196
    public function testUpdate()
197
    {
198
        $this->insertRows();
199
200
        $this->assertEquals(1, $this->_conn->update('write_table', array('test_string' => 'bar'), array('test_string' => 'foo')));
201
        $this->refresh('write_table');
202
        $this->assertEquals(2, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
203
        $this->refresh('write_table');
204
        $this->assertEquals(0, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
205
    }
206
207
}
208