1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Tests; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Configuration; |
8
|
|
|
use Doctrine\DBAL\Connection; |
9
|
|
|
use Doctrine\DBAL\DBALException; |
10
|
|
|
use Doctrine\DBAL\Driver; |
11
|
|
|
use Doctrine\DBAL\Driver\Connection as DriverConnection; |
12
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
13
|
|
|
use Doctrine\DBAL\FetchMode; |
14
|
|
|
use Doctrine\DBAL\Logging\SQLLogger; |
15
|
|
|
use Doctrine\DBAL\ParameterType; |
16
|
|
|
use Doctrine\DBAL\Statement; |
17
|
|
|
use Exception; |
18
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
|
21
|
|
|
class StatementTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** @var Connection|MockObject */ |
24
|
|
|
private $conn; |
25
|
|
|
|
26
|
|
|
/** @var Configuration|MockObject */ |
27
|
|
|
private $configuration; |
28
|
|
|
|
29
|
|
|
/** @var DriverStatement|MockObject */ |
30
|
|
|
private $driverStatement; |
31
|
|
|
|
32
|
|
|
protected function setUp() : void |
33
|
|
|
{ |
34
|
|
|
$this->driverStatement = $this->createMock(DriverStatement::class); |
35
|
|
|
|
36
|
|
|
$driverConnection = $this->createConfiguredMock(DriverConnection::class, [ |
37
|
|
|
'prepare' => $this->driverStatement, |
38
|
|
|
]); |
39
|
|
|
|
40
|
|
|
$driver = $this->createMock(Driver::class); |
41
|
|
|
|
42
|
|
|
$this->conn = $this->getMockBuilder(Connection::class) |
43
|
|
|
->setConstructorArgs([[], $driver]) |
44
|
|
|
->getMock(); |
45
|
|
|
$this->conn->expects($this->atLeastOnce()) |
46
|
|
|
->method('getWrappedConnection') |
47
|
|
|
->will($this->returnValue($driverConnection)); |
48
|
|
|
|
49
|
|
|
$this->configuration = $this->createMock(Configuration::class); |
50
|
|
|
$this->conn->expects($this->any()) |
51
|
|
|
->method('getConfiguration') |
52
|
|
|
->will($this->returnValue($this->configuration)); |
53
|
|
|
|
54
|
|
|
$this->conn->expects($this->any()) |
55
|
|
|
->method('getDriver') |
56
|
|
|
->will($this->returnValue($driver)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testExecuteCallsLoggerStartQueryWithParametersWhenValuesBound() : void |
60
|
|
|
{ |
61
|
|
|
$name = 'foo'; |
62
|
|
|
$var = 'bar'; |
63
|
|
|
$type = ParameterType::STRING; |
64
|
|
|
$values = [$name => $var]; |
65
|
|
|
$types = [$name => $type]; |
66
|
|
|
$sql = ''; |
67
|
|
|
|
68
|
|
|
$logger = $this->createMock(SQLLogger::class); |
69
|
|
|
$logger->expects($this->once()) |
70
|
|
|
->method('startQuery') |
71
|
|
|
->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types)); |
72
|
|
|
|
73
|
|
|
$this->configuration->expects($this->once()) |
74
|
|
|
->method('getSQLLogger') |
75
|
|
|
->will($this->returnValue($logger)); |
76
|
|
|
|
77
|
|
|
$statement = new Statement($sql, $this->conn); |
78
|
|
|
$statement->bindValue($name, $var, $type); |
79
|
|
|
$statement->execute(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testExecuteCallsLoggerStartQueryWithParametersWhenParamsPassedToExecute() : void |
83
|
|
|
{ |
84
|
|
|
$name = 'foo'; |
85
|
|
|
$var = 'bar'; |
86
|
|
|
$values = [$name => $var]; |
87
|
|
|
$types = []; |
88
|
|
|
$sql = ''; |
89
|
|
|
|
90
|
|
|
$logger = $this->createMock(SQLLogger::class); |
91
|
|
|
$logger->expects($this->once()) |
92
|
|
|
->method('startQuery') |
93
|
|
|
->with($this->equalTo($sql), $this->equalTo($values), $this->equalTo($types)); |
94
|
|
|
|
95
|
|
|
$this->configuration->expects($this->once()) |
96
|
|
|
->method('getSQLLogger') |
97
|
|
|
->will($this->returnValue($logger)); |
98
|
|
|
|
99
|
|
|
$statement = new Statement($sql, $this->conn); |
100
|
|
|
$statement->execute($values); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testExecuteCallsStartQueryWithTheParametersBoundViaBindParam() : void |
104
|
|
|
{ |
105
|
|
|
$name = 'foo'; |
106
|
|
|
$var = 'bar'; |
107
|
|
|
$values = [$name => $var]; |
108
|
|
|
$types = [$name => ParameterType::STRING]; |
109
|
|
|
$sql = ''; |
110
|
|
|
|
111
|
|
|
$logger = $this->createMock(SQLLogger::class); |
112
|
|
|
$logger->expects(self::once()) |
113
|
|
|
->method('startQuery') |
114
|
|
|
->with(self::equalTo($sql), self::equalTo($values), self::equalTo($types)); |
115
|
|
|
|
116
|
|
|
$this->configuration->expects(self::once()) |
117
|
|
|
->method('getSQLLogger') |
118
|
|
|
->willReturn($logger); |
119
|
|
|
|
120
|
|
|
$statement = new Statement($sql, $this->conn); |
121
|
|
|
$statement->bindParam($name, $var); |
122
|
|
|
$statement->execute(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testExecuteCallsLoggerStopQueryOnException() : void |
126
|
|
|
{ |
127
|
|
|
$logger = $this->createMock(SQLLogger::class); |
128
|
|
|
|
129
|
|
|
$this->configuration->expects($this->once()) |
130
|
|
|
->method('getSQLLogger') |
131
|
|
|
->will($this->returnValue($logger)); |
132
|
|
|
|
133
|
|
|
// Needed to satisfy construction of DBALException |
134
|
|
|
$this->conn->expects($this->any()) |
135
|
|
|
->method('resolveParams') |
136
|
|
|
->will($this->returnValue([])); |
137
|
|
|
|
138
|
|
|
$logger->expects($this->once()) |
139
|
|
|
->method('startQuery'); |
140
|
|
|
|
141
|
|
|
$logger->expects($this->once()) |
142
|
|
|
->method('stopQuery'); |
143
|
|
|
|
144
|
|
|
$this->driverStatement->expects($this->once()) |
145
|
|
|
->method('execute') |
146
|
|
|
->will($this->throwException(new Exception('Mock test exception'))); |
147
|
|
|
|
148
|
|
|
$statement = new Statement('', $this->conn); |
149
|
|
|
|
150
|
|
|
$this->expectException(DBALException::class); |
151
|
|
|
|
152
|
|
|
$statement->execute(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testPDOCustomClassConstructorArgs() : void |
156
|
|
|
{ |
157
|
|
|
$statement = new Statement('', $this->conn); |
158
|
|
|
|
159
|
|
|
$this->driverStatement->expects($this->once()) |
160
|
|
|
->method('fetchAll') |
161
|
|
|
->with(self::equalTo(FetchMode::CUSTOM_OBJECT), self::equalTo('Example'), self::equalTo(['arg1'])); |
162
|
|
|
|
163
|
|
|
$statement->fetchAll(FetchMode::CUSTOM_OBJECT, 'Example', ['arg1']); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|