Passed
Pull Request — master (#3065)
by Laurence
13:36
created

DBAL2369Test::testSelectOnParameterWithOtherType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

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