Completed
Pull Request — 3.0.x (#3070)
by Sergei
63:18
created

StatementTest::testCloseCursor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\DBAL\Tests\Portability;
4
5
use Doctrine\DBAL\Driver\Statement as DriverStatement;
6
use Doctrine\DBAL\ParameterType;
7
use Doctrine\DBAL\Portability\Connection;
8
use Doctrine\DBAL\Portability\Statement;
9
use PHPUnit\Framework\MockObject\MockObject;
10
use PHPUnit\Framework\TestCase;
11
use function iterator_to_array;
12
13
class StatementTest extends TestCase
14
{
15
    /** @var Connection|MockObject */
16
    protected $conn;
17
18
    /** @var Statement */
19
    protected $stmt;
20
21
    /** @var DriverStatement|MockObject */
22
    protected $wrappedStmt;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function setUp() : void
28
    {
29
        $this->wrappedStmt = $this->createMock(DriverStatement::class);
30
        $this->conn        = $this->createConnection();
31
        $this->stmt        = $this->createStatement($this->wrappedStmt, $this->conn);
32
    }
33
34
    /**
35
     * @group DBAL-726
36
     */
37
    public function testBindParam() : void
38
    {
39
        $column   = 'mycolumn';
40
        $variable = 'myvalue';
41
        $type     = ParameterType::STRING;
42
        $length   = 666;
43
44
        $this->wrappedStmt->expects(self::once())
45
            ->method('bindParam')
46
            ->with($column, $variable, $type, $length)
47
            ->will(self::returnValue(true));
48
49
        self::assertTrue($this->stmt->bindParam($column, $variable, $type, $length));
50
    }
51
52
    public function testBindValue() : void
53
    {
54
        $param = 'myparam';
55
        $value = 'myvalue';
56
        $type  = ParameterType::STRING;
57
58
        $this->wrappedStmt->expects(self::once())
59
            ->method('bindValue')
60
            ->with($param, $value, $type)
61
            ->will(self::returnValue(true));
62
63
        self::assertTrue($this->stmt->bindValue($param, $value, $type));
64
    }
65
66
    public function testCloseCursor() : void
67
    {
68
        $this->wrappedStmt->expects(self::once())
69
            ->method('closeCursor')
70
            ->will(self::returnValue(true));
71
72
        self::assertTrue($this->stmt->closeCursor());
73
    }
74
75
    public function testColumnCount() : void
76
    {
77
        $columnCount = 666;
78
79
        $this->wrappedStmt->expects(self::once())
80
            ->method('columnCount')
81
            ->will(self::returnValue($columnCount));
82
83
        self::assertSame($columnCount, $this->stmt->columnCount());
84
    }
85
86
    public function testErrorCode() : void
87
    {
88
        $errorCode = '666';
89
90
        $this->wrappedStmt->expects(self::once())
91
            ->method('errorCode')
92
            ->will(self::returnValue($errorCode));
93
94
        self::assertSame($errorCode, $this->stmt->errorCode());
95
    }
96
97
    public function testErrorInfo() : void
98
    {
99
        $errorInfo = ['666', 'Evil error.'];
100
101
        $this->wrappedStmt->expects(self::once())
102
            ->method('errorInfo')
103
            ->will(self::returnValue($errorInfo));
104
105
        self::assertSame($errorInfo, $this->stmt->errorInfo());
106
    }
107
108
    public function testExecute() : void
109
    {
110
        $params = [
111
            'foo',
112
            'bar',
113
        ];
114
115
        $this->wrappedStmt->expects(self::once())
116
            ->method('execute')
117
            ->with($params)
118
            ->will(self::returnValue(true));
119
120
        self::assertTrue($this->stmt->execute($params));
121
    }
122
123
    public function testGetIterator() : void
124
    {
125
        $this->wrappedStmt->expects(self::exactly(3))
126
            ->method('fetch')
127
            ->willReturnOnConsecutiveCalls('foo', 'bar', false);
128
129
        self::assertSame(['foo', 'bar'], iterator_to_array($this->stmt->getIterator()));
130
    }
131
132
    public function testRowCount() : void
133
    {
134
        $rowCount = 666;
135
136
        $this->wrappedStmt->expects(self::once())
137
            ->method('rowCount')
138
            ->will(self::returnValue($rowCount));
139
140
        self::assertSame($rowCount, $this->stmt->rowCount());
141
    }
142
143
    /**
144
     * @return Connection|MockObject
145
     */
146
    protected function createConnection()
147
    {
148
        return $this->getMockBuilder(Connection::class)
149
            ->disableOriginalConstructor()
150
            ->getMock();
151
    }
152
153
    protected function createStatement(DriverStatement $wrappedStatement, Connection $connection) : Statement
154
    {
155
        return new Statement($wrappedStatement, $connection);
156
    }
157
}
158