MssqlCommandTest.php 3.04 KB
Newer Older
resurtm committed
1 2 3 4
<?php

namespace yiiunit\framework\db\mssql;

Alexander Makarov committed
5 6
use yiiunit\framework\db\CommandTest;

7 8 9 10
/**
 * @group db
 * @group mssql
 */
Alexander Makarov committed
11
class MssqlCommandTest extends CommandTest
resurtm committed
12
{
13
    protected $driverName = 'sqlsrv';
resurtm committed
14

15 16 17
    public function testAutoQuoting()
    {
        $db = $this->getConnection(false);
resurtm committed
18

19
        $sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t';
20
        $command = $db->createCommand($sql);
21
        $this->assertEquals("SELECT [id], [t].[name] FROM [customer] t", $command->sql);
22
    }
resurtm committed
23

24 25 26 27
    public function testPrepareCancel()
    {
        $this->markTestSkipped('MSSQL driver does not support this feature.');
    }
resurtm committed
28

29 30 31
    public function testBindParamValue()
    {
        $db = $this->getConnection();
resurtm committed
32

33
        // bindParam
34
        $sql = 'INSERT INTO customer(email, name, address) VALUES (:email, :name, :address)';
35 36 37 38 39 40 41 42
        $command = $db->createCommand($sql);
        $email = 'user4@example.com';
        $name = 'user4';
        $address = 'address4';
        $command->bindParam(':email', $email);
        $command->bindParam(':name', $name);
        $command->bindParam(':address', $address);
        $command->execute();
resurtm committed
43

44
        $sql = 'SELECT name FROM customer WHERE email=:email';
45 46 47
        $command = $db->createCommand($sql);
        $command->bindParam(':email', $email);
        $this->assertEquals($name, $command->queryScalar());
resurtm committed
48

49
        $sql = 'INSERT INTO type (int_col, char_col, float_col, blob_col, numeric_col, bool_col) VALUES (:int_col, :char_col, :float_col, CONVERT([varbinary], :blob_col), :numeric_col, :bool_col)';
50 51 52 53 54 55 56 57 58 59 60 61 62 63
        $command = $db->createCommand($sql);
        $intCol = 123;
        $charCol = 'abc';
        $floatCol = 1.23;
        $blobCol = "\x10\x11\x12";
        $numericCol = '1.23';
        $boolCol = false;
        $command->bindParam(':int_col', $intCol);
        $command->bindParam(':char_col', $charCol);
        $command->bindParam(':float_col', $floatCol);
        $command->bindParam(':blob_col', $blobCol);
        $command->bindParam(':numeric_col', $numericCol);
        $command->bindParam(':bool_col', $boolCol);
        $this->assertEquals(1, $command->execute());
resurtm committed
64

65
        $sql = 'SELECT int_col, char_col, float_col, CONVERT([nvarchar], blob_col) AS blob_col, numeric_col FROM type';
66 67 68 69 70 71
        $row = $db->createCommand($sql)->queryOne();
        $this->assertEquals($intCol, $row['int_col']);
        $this->assertEquals($charCol, trim($row['char_col']));
        $this->assertEquals($floatCol, $row['float_col']);
        $this->assertEquals($blobCol, $row['blob_col']);
        $this->assertEquals($numericCol, $row['numeric_col']);
resurtm committed
72

73
        // bindValue
74
        $sql = 'INSERT INTO customer(email, name, address) VALUES (:email, \'user5\', \'address5\')';
75 76 77
        $command = $db->createCommand($sql);
        $command->bindValue(':email', 'user5@example.com');
        $command->execute();
resurtm committed
78

79
        $sql = 'SELECT email FROM customer WHERE name=:name';
80 81 82 83
        $command = $db->createCommand($sql);
        $command->bindValue(':name', 'user5');
        $this->assertEquals('user5@example.com', $command->queryScalar());
    }
resurtm committed
84
}