Completed
Pull Request — master (#3065)
by Laurence
16:45
created

DBAL2369Test::setUp()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 30
rs 9.7
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional\Ticket;
4
5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\ParameterType;
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\Tests\DbalFunctionalTestCase;
9
10
/**
11
 * @group DBAL-2369
12
 */
13
class DBAL2369Test extends DbalFunctionalTestCase
14
{
15
    /**
16
     * @throws DBALException
17
     */
18
    protected function setUp() : void
19
    {
20
        parent::setUp();
21
22
        if ($this->_conn->getDatabasePlatform()->getName() !== 'mssql') {
23
            $this->markTestSkipped('Related to SQLSRV only');
24
        }
25
26
        /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
27
        $sm = $this->_conn->getSchemaManager();
28
        if ($sm->tablesExist(['dbal2369'])) {
29
            return;
30
        }
31
32
        $table = new Table('dbal2369');
33
        $table->addColumn('id', 'integer');
34
        $table->addColumn('textfield', 'string');
35
        $table->setPrimaryKey(['id']);
36
37
        $sm->createTable($table);
38
39
        $this->_conn->insert(
40
            'dbal2369',
41
            [
42
                'id'        => 1,
43
                'textfield' => 'test',
44
            ],
45
            [
46
                ParameterType::INTEGER,
47
                ParameterType::STRING,
48
            ]
49
        );
50
    }
51
52
    /**
53
     * @throws DBALException
54
     */
55
    public function testSelectOnId() : void
56
    {
57
        $query = 'SELECT id, textfield FROM dbal2369 WHERE id = ?';
58
        $stmt  = $this->_conn->prepare($query);
59
        $stmt->bindValue(1, 1, ParameterType::INTEGER);
60
        $stmt->execute();
61
62
        $ret = $stmt->fetch();
63
64
        self::assertArrayHasKey('id', $ret);
65
        self::assertEquals($ret['id'], 1);
66
        self::assertArrayHasKey('textfield', $ret);
67
        self::assertEquals($ret['textfield'], 'test');
68
    }
69
70
    /**
71
     * @throws DBALException
72
     */
73
    public function testSelectOnParameterSuccess() : void
74
    {
75
        $query = 'SELECT id, textfield FROM dbal2369 WHERE textfield = ?';
76
        $stmt  = $this->_conn->prepare($query);
77
        $stmt->bindValue(1, 'test', ParameterType::STRING);
78
        $stmt->execute();
79
80
        $ret = $stmt->fetch();
81
82
        self::assertArrayHasKey('id', $ret);
83
        self::assertEquals($ret['id'], 1);
84
        self::assertArrayHasKey('textfield', $ret);
85
        self::assertEquals($ret['textfield'], 'test');
86
    }
87
88
    /**
89
     * This case triggers the exception of string to int conversion error on SQL Server
90
     *
91
     * @throws DBALException
92
     */
93
    public function testSelectOnParameterFails() : void
94
    {
95
        self::expectException(DBALException::class);
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\TestCase::expectException() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
        self::/** @scrutinizer ignore-call */ 
96
              expectException(DBALException::class);
Loading history...
96
97
        $query = 'SELECT id, textfield FROM dbal2369 WHERE textfield = ?';
98
        $stmt  = $this->_conn->prepare($query);
99
        $stmt->bindValue(1, 3, ParameterType::INTEGER);
100
        $stmt->execute();
101
    }
102
103
    /**
104
     * @throws DBALException
105
     */
106
    public function testSelectOnParameterWithOtherType() : void
107
    {
108
        $query = 'SELECT id, textfield FROM dbal2369 WHERE textfield = ?';
109
        $stmt  = $this->_conn->prepare($query);
110
        $stmt->bindValue(1, '3', ParameterType::INTEGER);
111
        $stmt->execute();
112
113
        self::assertFalse($stmt->fetch());
114
    }
115
}
116