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