WriteTest.php 11.3 KB
Newer Older
1 2 3
<?php

namespace Doctrine\Tests\DBAL\Functional;
4 5

use Doctrine\DBAL\ParameterType;
6
use Doctrine\DBAL\Types\Type;
7 8
use function array_filter;
use function strtolower;
9 10 11

class WriteTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
12
    protected function setUp()
13 14 15 16 17 18
    {
        parent::setUp();

        try {
            /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
            $table = new \Doctrine\DBAL\Schema\Table("write_table");
19 20
            $table->addColumn('id', 'integer', array('autoincrement' => true));
            $table->addColumn('test_int', 'integer');
21
            $table->addColumn('test_string', 'string', array('notnull' => false));
22
            $table->setPrimaryKey(array('id'));
23

24
            $this->_conn->getSchemaManager()->createTable($table);
25 26 27 28 29 30
        } catch(\Exception $e) {

        }
        $this->_conn->executeUpdate('DELETE FROM write_table');
    }

31 32 33 34 35 36
    /**
     * @group DBAL-80
     */
    public function testExecuteUpdateFirstTypeIsNull()
    {
        $sql = "INSERT INTO write_table (test_string, test_int) VALUES (?, ?)";
Sergei Morozov's avatar
Sergei Morozov committed
37
        $this->_conn->executeUpdate($sql, ['text', 1111], [null, ParameterType::INTEGER]);
38 39

        $sql = "SELECT * FROM write_table WHERE test_string = ? AND test_int = ?";
Sergei Morozov's avatar
Sergei Morozov committed
40
        self::assertTrue((bool) $this->_conn->fetchColumn($sql, ['text', 1111]));
41 42
    }

43 44
    public function testExecuteUpdate()
    {
45
        $sql = "INSERT INTO write_table (test_int) VALUES ( " . $this->_conn->quote(1) . ")";
46 47
        $affected = $this->_conn->executeUpdate($sql);

48
        self::assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
49 50 51 52 53
    }

    public function testExecuteUpdateWithTypes()
    {
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
54 55
        $affected = $this->_conn->executeUpdate(
            $sql,
Sergei Morozov's avatar
Sergei Morozov committed
56 57
            [1, 'foo'],
            [ParameterType::INTEGER, ParameterType::STRING]
58
        );
59

60
        self::assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
61 62 63 64 65 66 67 68 69 70 71
    }

    public function testPrepareRowCountReturnsAffectedRows()
    {
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
        $stmt = $this->_conn->prepare($sql);

        $stmt->bindValue(1, 1);
        $stmt->bindValue(2, "foo");
        $stmt->execute();

72
        self::assertEquals(1, $stmt->rowCount());
73 74 75 76 77 78 79
    }

    public function testPrepareWithPdoTypes()
    {
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
        $stmt = $this->_conn->prepare($sql);

80
        $stmt->bindValue(1, 1, ParameterType::INTEGER);
Sergei Morozov's avatar
Sergei Morozov committed
81
        $stmt->bindValue(2, 'foo', ParameterType::STRING);
82 83
        $stmt->execute();

84
        self::assertEquals(1, $stmt->rowCount());
85 86 87 88 89 90 91 92 93 94 95
    }

    public function testPrepareWithDbalTypes()
    {
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
        $stmt = $this->_conn->prepare($sql);

        $stmt->bindValue(1, 1, Type::getType('integer'));
        $stmt->bindValue(2, "foo", Type::getType('string'));
        $stmt->execute();

96
        self::assertEquals(1, $stmt->rowCount());
97 98 99 100 101 102 103 104 105 106 107
    }

    public function testPrepareWithDbalTypeNames()
    {
        $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
        $stmt = $this->_conn->prepare($sql);

        $stmt->bindValue(1, 1, 'integer');
        $stmt->bindValue(2, "foo", 'string');
        $stmt->execute();

108
        self::assertEquals(1, $stmt->rowCount());
109 110 111 112
    }

    public function insertRows()
    {
113 114
        self::assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 1, 'test_string' => 'foo')));
        self::assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar')));
115 116 117 118 119 120 121 122 123 124 125
    }

    public function testInsert()
    {
        $this->insertRows();
    }

    public function testDelete()
    {
        $this->insertRows();

126
        self::assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 2)));
Gabriel Caruso's avatar
Gabriel Caruso committed
127
        self::assertCount(1, $this->_conn->fetchAll('SELECT * FROM write_table'));
128

129
        self::assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 1)));
Gabriel Caruso's avatar
Gabriel Caruso committed
130
        self::assertCount(0, $this->_conn->fetchAll('SELECT * FROM write_table'));
131 132 133 134 135 136
    }

    public function testUpdate()
    {
        $this->insertRows();

137 138 139
        self::assertEquals(1, $this->_conn->update('write_table', array('test_string' => 'bar'), array('test_string' => 'foo')));
        self::assertEquals(2, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
        self::assertEquals(0, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
140
    }
141 142 143 144 145 146 147

    public function testLastInsertId()
    {
        if ( ! $this->_conn->getDatabasePlatform()->prefersIdentityColumns()) {
            $this->markTestSkipped('Test only works on platforms with identity columns.');
        }

148
        self::assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar')));
149 150
        $num = $this->_conn->lastInsertId();

151
        self::assertNotNull($num, "LastInsertId() should not be null.");
Gabriel Caruso's avatar
Gabriel Caruso committed
152
        self::assertGreaterThan(0, $num, "LastInsertId() should be non-negative number.");
153
    }
154 155 156 157 158 159 160

    public function testLastInsertIdSequence()
    {
        if ( ! $this->_conn->getDatabasePlatform()->supportsSequences()) {
            $this->markTestSkipped('Test only works on platforms with sequences.');
        }

161
        $sequence = new \Doctrine\DBAL\Schema\Sequence('write_table_id_seq');
162
        try {
163 164 165 166 167
            $this->_conn->getSchemaManager()->createSequence($sequence);
        } catch(\Exception $e) {
        }

        $sequences = $this->_conn->getSchemaManager()->listSequences();
Gabriel Caruso's avatar
Gabriel Caruso committed
168
        self::assertCount(1, array_filter($sequences, function($sequence) {
169
            return strtolower($sequence->getName()) === 'write_table_id_seq';
Gabriel Caruso's avatar
Gabriel Caruso committed
170
        }));
171

172
        $stmt = $this->_conn->query($this->_conn->getDatabasePlatform()->getSequenceNextValSQL('write_table_id_seq'));
173 174
        $nextSequenceVal = $stmt->fetchColumn();

175
        $lastInsertId = $this->_conn->lastInsertId('write_table_id_seq');
176

Gabriel Caruso's avatar
Gabriel Caruso committed
177
        self::assertGreaterThan(0, $lastInsertId);
178
        self::assertEquals($nextSequenceVal, $lastInsertId);
179 180 181 182
    }

    public function testLastInsertIdNoSequenceGiven()
    {
183 184
        if ( ! $this->_conn->getDatabasePlatform()->supportsSequences() || $this->_conn->getDatabasePlatform()->supportsIdentityColumns()) {
            $this->markTestSkipped("Test only works consistently on platforms that support sequences and don't support identity columns.");
185 186
        }

187
        self::assertFalse($this->_conn->lastInsertId( null ));
188 189

    }
190 191 192 193 194 195

    /**
     * @group DBAL-445
     */
    public function testInsertWithKeyValueTypes()
    {
196 197
        $testString = new \DateTime('2013-04-14 10:10:10');

198 199
        $this->_conn->insert(
            'write_table',
200
            array('test_int' => '30', 'test_string' => $testString),
201 202 203 204 205
            array('test_string' => 'datetime', 'test_int' => 'integer')
        );

        $data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');

206
        self::assertEquals($testString->format($this->_conn->getDatabasePlatform()->getDateTimeFormatString()), $data);
207 208 209 210 211 212 213
    }

    /**
     * @group DBAL-445
     */
    public function testUpdateWithKeyValueTypes()
    {
214 215
        $testString = new \DateTime('2013-04-14 10:10:10');

216 217
        $this->_conn->insert(
            'write_table',
218
            array('test_int' => '30', 'test_string' => $testString),
219 220 221
            array('test_string' => 'datetime', 'test_int' => 'integer')
        );

222 223
        $testString = new \DateTime('2013-04-15 10:10:10');

224 225
        $this->_conn->update(
            'write_table',
226
            array('test_string' => $testString),
227 228 229 230 231 232
            array('test_int' => '30'),
            array('test_string' => 'datetime', 'test_int' => 'integer')
        );

        $data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');

233
        self::assertEquals($testString->format($this->_conn->getDatabasePlatform()->getDateTimeFormatString()), $data);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    }

    /**
     * @group DBAL-445
     */
    public function testDeleteWithKeyValueTypes()
    {
        $val = new \DateTime('2013-04-14 10:10:10');
        $this->_conn->insert(
            'write_table',
            array('test_int' => '30', 'test_string' => $val),
            array('test_string' => 'datetime', 'test_int' => 'integer')
        );

        $this->_conn->delete('write_table', array('test_int' => 30, 'test_string' => $val), array('test_string' => 'datetime', 'test_int' => 'integer'));

        $data = $this->_conn->fetchColumn('SELECT test_string FROM write_table WHERE test_int = 30');

252
        self::assertFalse($data);
253
    }
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

    public function testEmptyIdentityInsert()
    {
        $platform = $this->_conn->getDatabasePlatform();

        if ( ! ($platform->supportsIdentityColumns() || $platform->usesSequenceEmulatedIdentityColumns()) ) {
            $this->markTestSkipped(
                'Test only works on platforms with identity columns or sequence emulated identity columns.'
            );
        }

        $table = new \Doctrine\DBAL\Schema\Table('test_empty_identity');
        $table->addColumn('id', 'integer', array('autoincrement' => true));
        $table->setPrimaryKey(array('id'));

        try {
Thomas Müller's avatar
Thomas Müller committed
270
            $this->_conn->getSchemaManager()->dropTable($table->getQuotedName($platform));
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        } catch(\Exception $e) { }

        foreach ($platform->getCreateTableSQL($table) as $sql) {
            $this->_conn->exec($sql);
        }

        $seqName = $platform->usesSequenceEmulatedIdentityColumns()
            ? $platform->getIdentitySequenceName('test_empty_identity', 'id')
            : null;

        $sql = $platform->getEmptyIdentityInsertSQL('test_empty_identity', 'id');

        $this->_conn->exec($sql);

        $firstId = $this->_conn->lastInsertId($seqName);

        $this->_conn->exec($sql);

        $secondId = $this->_conn->lastInsertId($seqName);

Gabriel Caruso's avatar
Gabriel Caruso committed
291
        self::assertGreaterThan($firstId, $secondId);
292 293 294

    }

295 296 297 298 299 300 301 302 303 304 305 306 307
    /**
     * @group DBAL-2688
     */
    public function testUpdateWhereIsNull()
    {
        $this->_conn->insert(
            'write_table',
            ['test_int' => '30', 'test_string' => null],
            ['test_string' => 'string', 'test_int' => 'integer']
        );

        $data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30');

308
        self::assertCount(1, $data);
309 310 311 312 313

        $this->_conn->update('write_table', ['test_int' => 10], ['test_string' => null], ['test_string' => 'string', 'test_int' => 'integer']);

        $data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30');

314
        self::assertCount(0, $data);
315 316 317 318 319 320 321 322 323 324 325 326
    }

    public function testDeleteWhereIsNull()
    {
        $this->_conn->insert(
            'write_table',
            ['test_int' => '30', 'test_string' => null],
            ['test_string' => 'string', 'test_int' => 'integer']
        );

        $data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30');

327
        self::assertCount(1, $data);
328 329 330 331 332

        $this->_conn->delete('write_table', ['test_string' => null], ['test_string' => 'string']);

        $data = $this->_conn->fetchAll('SELECT * FROM write_table WHERE test_int = 30');

333
        self::assertCount(0, $data);
334
    }
335
}