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