forked from doctrine/dbal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatementTest.php
181 lines (144 loc) · 4.79 KB
/
StatementTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace Doctrine\Tests\DBAL\Portability;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Portability\Connection;
use Doctrine\DBAL\Portability\Statement;
use Doctrine\Tests\DbalTestCase;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionProperty;
use function iterator_to_array;
class StatementTest extends DbalTestCase
{
/** @var Connection|MockObject */
protected $conn;
/** @var Statement */
protected $stmt;
/** @var DriverStatement|MockObject */
protected $wrappedStmt;
/**
* {@inheritdoc}
*/
protected function setUp() : void
{
$this->wrappedStmt = $this->createMock(DriverStatement::class);
$this->conn = $this->createConnection();
$this->stmt = $this->createStatement($this->wrappedStmt, $this->conn);
}
/**
* @group DBAL-726
*/
public function testBindParam()
{
$column = 'mycolumn';
$variable = 'myvalue';
$type = ParameterType::STRING;
$length = 666;
$this->wrappedStmt->expects($this->once())
->method('bindParam')
->with($column, $variable, $type, $length)
->will($this->returnValue(true));
self::assertTrue($this->stmt->bindParam($column, $variable, $type, $length));
}
public function testBindValue()
{
$param = 'myparam';
$value = 'myvalue';
$type = ParameterType::STRING;
$this->wrappedStmt->expects($this->once())
->method('bindValue')
->with($param, $value, $type)
->will($this->returnValue(true));
self::assertTrue($this->stmt->bindValue($param, $value, $type));
}
public function testCloseCursor()
{
$this->wrappedStmt->expects($this->once())
->method('closeCursor')
->will($this->returnValue(true));
self::assertTrue($this->stmt->closeCursor());
}
public function testColumnCount()
{
$columnCount = 666;
$this->wrappedStmt->expects($this->once())
->method('columnCount')
->will($this->returnValue($columnCount));
self::assertSame($columnCount, $this->stmt->columnCount());
}
public function testErrorCode()
{
$errorCode = '666';
$this->wrappedStmt->expects($this->once())
->method('errorCode')
->will($this->returnValue($errorCode));
self::assertSame($errorCode, $this->stmt->errorCode());
}
public function testErrorInfo()
{
$errorInfo = ['666', 'Evil error.'];
$this->wrappedStmt->expects($this->once())
->method('errorInfo')
->will($this->returnValue($errorInfo));
self::assertSame($errorInfo, $this->stmt->errorInfo());
}
public function testExecute()
{
$params = [
'foo',
'bar',
];
$this->wrappedStmt->expects($this->once())
->method('execute')
->with($params)
->will($this->returnValue(true));
self::assertTrue($this->stmt->execute($params));
}
public function testSetFetchMode()
{
$fetchMode = FetchMode::CUSTOM_OBJECT;
$arg1 = 'MyClass';
$arg2 = [1, 2];
$this->wrappedStmt->expects($this->once())
->method('setFetchMode')
->with($fetchMode, $arg1, $arg2)
->will($this->returnValue(true));
$re = new ReflectionProperty($this->stmt, 'defaultFetchMode');
$re->setAccessible(true);
self::assertSame(FetchMode::MIXED, $re->getValue($this->stmt));
self::assertTrue($this->stmt->setFetchMode($fetchMode, $arg1, $arg2));
self::assertSame($fetchMode, $re->getValue($this->stmt));
}
public function testGetIterator()
{
$this->wrappedStmt->expects($this->exactly(3))
->method('fetch')
->willReturnOnConsecutiveCalls('foo', 'bar', false);
self::assertSame(['foo', 'bar'], iterator_to_array($this->stmt->getIterator()));
}
public function testRowCount()
{
$rowCount = 666;
$this->wrappedStmt->expects($this->once())
->method('rowCount')
->will($this->returnValue($rowCount));
self::assertSame($rowCount, $this->stmt->rowCount());
}
/**
* @return Connection|MockObject
*/
protected function createConnection()
{
return $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->getMock();
}
/**
* @return Statement
*/
protected function createStatement(DriverStatement $wrappedStatement, Connection $connection)
{
return new Statement($wrappedStatement, $connection);
}
}