|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\ParameterType; |
|
6
|
|
|
use Doctrine\DBAL\Statement; |
|
7
|
|
|
use Doctrine\DBAL\Logging\SQLLogger; |
|
8
|
|
|
|
|
9
|
|
|
class StatementTest extends \Doctrine\Tests\DbalTestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* |
|
13
|
|
|
* @var \Doctrine\DBAL\Connection |
|
14
|
|
|
*/ |
|
15
|
|
|
private $conn; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* |
|
19
|
|
|
* @var \Doctrine\DBAL\Configuration |
|
20
|
|
|
*/ |
|
21
|
|
|
private $configuration; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \PDOStatement |
|
25
|
|
|
*/ |
|
26
|
|
|
private $pdoStatement; |
|
27
|
|
|
|
|
28
|
|
|
protected function setUp() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->pdoStatement = $this->getMockBuilder('\PDOStatement') |
|
|
|
|
|
|
31
|
|
|
->setMethods(array('execute', 'bindParam', 'bindValue')) |
|
32
|
|
|
->getMock(); |
|
33
|
|
|
$platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform(); |
|
34
|
|
|
$driverConnection = $this->createMock('\Doctrine\DBAL\Driver\Connection'); |
|
35
|
|
|
$driverConnection->expects($this->any()) |
|
36
|
|
|
->method('prepare') |
|
37
|
|
|
->will($this->returnValue($this->pdoStatement)); |
|
38
|
|
|
|
|
39
|
|
|
$driver = $this->createMock('\Doctrine\DBAL\Driver'); |
|
40
|
|
|
$constructorArgs = array( |
|
41
|
|
|
array( |
|
42
|
|
|
'platform' => $platform |
|
43
|
|
|
), |
|
44
|
|
|
$driver |
|
45
|
|
|
); |
|
46
|
|
|
$this->conn = $this->getMockBuilder('\Doctrine\DBAL\Connection') |
|
|
|
|
|
|
47
|
|
|
->setConstructorArgs($constructorArgs) |
|
48
|
|
|
->getMock(); |
|
49
|
|
|
$this->conn->expects($this->atLeastOnce()) |
|
50
|
|
|
->method('getWrappedConnection') |
|
51
|
|
|
->will($this->returnValue($driverConnection)); |
|
52
|
|
|
|
|
53
|
|
|
$this->configuration = $this->createMock('\Doctrine\DBAL\Configuration'); |
|
|
|
|
|
|
54
|
|
|
$this->conn->expects($this->any()) |
|
55
|
|
|
->method('getConfiguration') |
|
56
|
|
|
->will($this->returnValue($this->configuration)); |
|
57
|
|
|
|
|
58
|
|
|
$this->conn->expects($this->any()) |
|
59
|
|
|
->method('getDriver') |
|
60
|
|
|
->will($this->returnValue($driver)); |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testExecuteCallsLoggerStartQueryWithParametersWhenValuesBound() |
|
65
|
|
|
{ |
|
66
|
|
|
$name = 'foo'; |
|
67
|
|
|
$var = 'bar'; |
|
68
|
|
|
$type = ParameterType::STRING; |
|
69
|
|
|
$values = [$name => $var]; |
|
70
|
|
|
$types = [$name => $type]; |
|
71
|
|
|
$sql = ''; |
|
72
|
|
|
|
|
73
|
|
|
$logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLogger'); |
|
74
|
|
|
$logger->expects($this->once()) |
|
75
|
|
|
->method('startQuery') |
|
76
|
|
|
->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types)); |
|
77
|
|
|
|
|
78
|
|
|
$this->configuration->expects($this->once()) |
|
79
|
|
|
->method('getSQLLogger') |
|
80
|
|
|
->will($this->returnValue($logger)); |
|
81
|
|
|
|
|
82
|
|
|
$statement = new Statement($sql, $this->conn); |
|
83
|
|
|
$statement->bindValue($name, $var, $type); |
|
84
|
|
|
$statement->execute(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testExecuteCallsLoggerStartQueryWithParametersWhenParamsPassedToExecute() |
|
88
|
|
|
{ |
|
89
|
|
|
$name = 'foo'; |
|
90
|
|
|
$var = 'bar'; |
|
91
|
|
|
$values = array($name => $var); |
|
92
|
|
|
$types = array(); |
|
93
|
|
|
$sql = ''; |
|
94
|
|
|
|
|
95
|
|
|
$logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLogger'); |
|
96
|
|
|
$logger->expects($this->once()) |
|
97
|
|
|
->method('startQuery') |
|
98
|
|
|
->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types)); |
|
99
|
|
|
|
|
100
|
|
|
$this->configuration->expects($this->once()) |
|
101
|
|
|
->method('getSQLLogger') |
|
102
|
|
|
->will($this->returnValue($logger)); |
|
103
|
|
|
|
|
104
|
|
|
$statement = new Statement($sql, $this->conn); |
|
105
|
|
|
$statement->execute($values); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testExecuteCallsStartQueryWithTheParametersBoundViaBindParam() |
|
109
|
|
|
{ |
|
110
|
|
|
$name = 'foo'; |
|
111
|
|
|
$var = 'bar'; |
|
112
|
|
|
$values = [$name => $var]; |
|
113
|
|
|
$types = [$name => ParameterType::STRING]; |
|
114
|
|
|
$sql = ''; |
|
115
|
|
|
|
|
116
|
|
|
$logger = $this->createMock(SQLLogger::class); |
|
117
|
|
|
$logger->expects(self::once()) |
|
118
|
|
|
->method('startQuery') |
|
119
|
|
|
->with(self::equalTo($sql), self::equalTo($values), self::equalTo($types)); |
|
120
|
|
|
|
|
121
|
|
|
$this->configuration->expects(self::once()) |
|
122
|
|
|
->method('getSQLLogger') |
|
123
|
|
|
->willReturn($logger); |
|
124
|
|
|
|
|
125
|
|
|
$statement = new Statement($sql, $this->conn); |
|
126
|
|
|
$statement->bindParam($name, $var); |
|
127
|
|
|
$statement->execute(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @expectedException \Doctrine\DBAL\DBALException |
|
132
|
|
|
*/ |
|
133
|
|
|
public function testExecuteCallsLoggerStopQueryOnException() |
|
134
|
|
|
{ |
|
135
|
|
|
$logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLogger'); |
|
136
|
|
|
|
|
137
|
|
|
$this->configuration->expects($this->once()) |
|
138
|
|
|
->method('getSQLLogger') |
|
139
|
|
|
->will($this->returnValue($logger)); |
|
140
|
|
|
|
|
141
|
|
|
// Needed to satisfy construction of DBALException |
|
142
|
|
|
$this->conn->expects($this->any()) |
|
143
|
|
|
->method('resolveParams') |
|
144
|
|
|
->will($this->returnValue(array())); |
|
145
|
|
|
|
|
146
|
|
|
$logger->expects($this->once()) |
|
147
|
|
|
->method('startQuery'); |
|
148
|
|
|
|
|
149
|
|
|
$logger->expects($this->once()) |
|
150
|
|
|
->method('stopQuery'); |
|
151
|
|
|
|
|
152
|
|
|
$this->pdoStatement->expects($this->once()) |
|
153
|
|
|
->method('execute') |
|
154
|
|
|
->will($this->throwException(new \Exception("Mock test exception"))); |
|
155
|
|
|
|
|
156
|
|
|
$statement = new Statement("", $this->conn); |
|
157
|
|
|
$statement->execute(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @expectedException \Doctrine\DBAL\DBALException |
|
162
|
|
|
*/ |
|
163
|
|
|
public function testExecuteCallsLoggerExtendedFailsOnException() |
|
164
|
|
|
{ |
|
165
|
|
|
$logger = $this->createMock('\Doctrine\DBAL\Logging\SQLLoggerExtended'); |
|
166
|
|
|
|
|
167
|
|
|
$this->configuration->expects($this->once()) |
|
168
|
|
|
->method('getSQLLogger') |
|
169
|
|
|
->will($this->returnValue($logger)); |
|
170
|
|
|
|
|
171
|
|
|
// Needed to satisfy construction of DBALException |
|
172
|
|
|
$this->conn->expects($this->any()) |
|
173
|
|
|
->method('resolveParams') |
|
174
|
|
|
->will($this->returnValue(array())); |
|
175
|
|
|
|
|
176
|
|
|
$logger->expects($this->once()) |
|
177
|
|
|
->method('startQuery'); |
|
178
|
|
|
|
|
179
|
|
|
$logger->expects($this->once()) |
|
180
|
|
|
->method('fail'); |
|
181
|
|
|
|
|
182
|
|
|
$this->pdoStatement->expects($this->once()) |
|
183
|
|
|
->method('execute') |
|
184
|
|
|
->will($this->throwException(new \Exception("Mock test exception"))); |
|
185
|
|
|
|
|
186
|
|
|
$statement = new Statement("", $this->conn); |
|
187
|
|
|
$statement->execute(); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..