-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrmTestCase.php
More file actions
270 lines (241 loc) · 9.09 KB
/
OrmTestCase.php
File metadata and controls
270 lines (241 loc) · 9.09 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
namespace Oro\Component\TestUtils\ORM;
use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Common\Cache\Psr6\CacheAdapter;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Driver\Connection;
use Oro\Component\Testing\TempDirExtension;
use Oro\Component\Testing\Unit\Cache\CacheTrait;
use Oro\Component\TestUtils\ORM\Mocks\DriverMock;
use Oro\Component\TestUtils\ORM\Mocks\EntityManagerMock;
use Oro\Component\TestUtils\ORM\Mocks\FetchIterator;
use Oro\Component\TestUtils\ORM\Mocks\StatementMock;
use Symfony\Component\Cache\Adapter\NullAdapter;
/**
* The base class for ORM related test cases.
*
* This class is a clone of Doctrine\Tests\OrmTestCase that is excluded from doctrine package since v2.4.
*/
abstract class OrmTestCase extends \PHPUnit\Framework\TestCase
{
use TempDirExtension, CacheTrait;
/** @var CacheProvider The metadata cache that is shared between all ORM tests */
private $metadataCacheImpl;
/** @var array|null */
private $queryExpectations;
/**
* @after
*/
protected function resetQueryExpectations()
{
$this->queryExpectations = null;
}
protected function getProxyDir($shouldBeCreated = true)
{
return $this->getTempDir('test_orm_proxies', $shouldBeCreated);
}
/**
* Creates an EntityManager for testing purposes.
*
* NOTE: The created EntityManager will have its dependant DBAL parts completely
* mocked out using a DriverMock, ConnectionMock, etc. These mocks can then
* be configured in the tests to simulate the DBAL behavior that is desired
* for a particular test,
*
* @param mixed $conn
* @param EventManager $eventManager
* @param bool $withSharedMetadata
*
* @return EntityManagerMock
*/
protected function getTestEntityManager($conn = null, $eventManager = null, $withSharedMetadata = true)
{
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataCache($this->getMetadataCacheImpl($withSharedMetadata));
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([], true));
$config->setQueryCacheImpl($this->getQueryCacheImpl());
$config->setProxyDir($this->getProxyDir());
$config->setProxyNamespace('Doctrine\Tests\Proxies');
// The namespace of custom functions is hardcoded in \Oro\ORM\Query\AST\FunctionFactory::create, so we are
// making our mock of 'CAST' available in Oro\ORM\Query\AST\Platform\Functions\Mock\ namespace:
if (!\class_exists('Oro\ORM\Query\AST\Platform\Functions\Mock\Cast', false)) {
\class_alias(
\Oro\Component\TestUtils\ORM\Mocks\Cast::class,
'Oro\ORM\Query\AST\Platform\Functions\Mock\Cast'
);
}
$config->setCustomStringFunctions(['cast' => 'Oro\ORM\Query\AST\Functions\Cast']);
if ($conn === null) {
$conn = [
'driverClass' => 'Oro\Component\TestUtils\ORM\Mocks\DriverMock',
'wrapperClass' => 'Oro\Component\TestUtils\ORM\Mocks\ConnectionMock',
'user' => 'john',
'password' => 'wayne'
];
}
if (is_array($conn)) {
$conn = \Doctrine\DBAL\DriverManager::getConnection($conn, $config, $eventManager);
}
return EntityManagerMock::create($conn, $config, $eventManager);
}
/**
* Changes a connection object for the given entity manager
*/
protected function setDriverConnection(Connection $connection, EntityManagerMock $em)
{
/** @var DriverMock $driver */
$driver = $em->getConnection()->getDriver();
$driver->setDriverConnection($connection);
}
/**
* Creates a mock for a statement which handles fetching the given records
*
* @param array $records
* @param array $params
* @param array $types
*
* @return \PHPUnit\Framework\MockObject\MockObject
*/
protected function createFetchStatementMock(array $records, array $params = [], array $types = [])
{
$statement = $this->createMock(StatementMock::class);
$statement->expects($this->exactly(count($records) + 1))
->method('fetch')
->will(
new \PHPUnit\Framework\MockObject\Stub\ConsecutiveCalls(
array_merge($records, [false])
)
);
$statement->expects($this->any())
->method('getIterator')
->willReturn(new FetchIterator($statement));
if ($params) {
if ($types) {
$withConsecutive = [];
foreach ($params as $key => $val) {
$withConsecutive[] = [$key, $val, $types[$key]];
}
$statement->expects($this->exactly(count($params)))
->method('bindValue')
->withConsecutive(...$withConsecutive);
$statement->expects($this->once())
->method('execute');
} else {
$statement->expects($this->once())
->method('execute')
->with($params);
}
}
return $statement;
}
/**
* Creates a mock for 'Doctrine\DBAL\Driver\Connection' and sets it to the given entity manager
*
* @param EntityManagerMock $em
*
* @return Connection|\PHPUnit\Framework\MockObject\MockObject
*/
protected function getDriverConnectionMock(EntityManagerMock $em)
{
$conn = $this->createMock(Connection::class);
$this->setDriverConnection($conn, $em);
return $conn;
}
/**
* Creates a mock for a statement which handles counting a number of records
*
* @param int $numberOfRecords
*
* @return \PHPUnit\Framework\MockObject\MockObject
*/
protected function createCountStatementMock($numberOfRecords)
{
$countStatement = $this->createMock(StatementMock::class);
$countStatement->expects($this->once())
->method('fetchColumn')
->willReturn($numberOfRecords);
return $countStatement;
}
/**
* @param Connection|\PHPUnit\Framework\MockObject\MockObject $conn
* @param string|\PHPUnit\Framework\Constraint\Constraint $sql
* @param array $result
* @param array $params
* @param array $types
*/
protected function setQueryExpectation(
\PHPUnit\Framework\MockObject\MockObject $conn,
$sql,
$result,
$params = [],
$types = []
) {
$stmt = $this->createFetchStatementMock($result, $params, $types);
if ($params) {
$conn->expects($this->once())
->method('prepare')
->with($sql)
->willReturn($stmt);
} else {
$conn->expects($this->once())
->method('query')
->with($sql)
->willReturn($stmt);
}
}
/**
* @param string|\PHPUnit\Framework\Constraint\Constraint $sql
* @param array $result
* @param array $params
* @param array $types
*/
protected function addQueryExpectation(
$sql,
$result,
$params = [],
$types = []
) {
$stmt = $this->createFetchStatementMock($result, $params, $types);
if ($params) {
$this->queryExpectations['prepare'][] = [$sql, $stmt];
} else {
$this->queryExpectations['query'][] = [$sql, $stmt];
}
}
protected function applyQueryExpectations(\PHPUnit\Framework\MockObject\MockObject $conn)
{
if (!$this->queryExpectations) {
throw new \LogicException('The addQueryExpectation() should be called before.');
}
$queryExpectations = $this->queryExpectations;
$this->queryExpectations = null;
foreach ($queryExpectations as $method => $queries) {
$with = [];
$will = [];
foreach ($queries as [$sql, $stmt]) {
$with[] = [$sql];
$will[] = $stmt;
}
$conn->expects($this->exactly(count($queries)))
->method($method)
->withConsecutive(...$with)
->willReturnOnConsecutiveCalls(...$will);
}
}
private function getMetadataCacheImpl($withSharedMetadata)
{
if (!$withSharedMetadata) {
// do not cache anything to avoid influence between tests
return $this->getChainCache();
}
if ($this->metadataCacheImpl === null) {
$this->metadataCacheImpl = $this->getArrayCache();
}
return CacheAdapter::wrap($this->metadataCacheImpl);
}
protected function getQueryCacheImpl()
{
// do not cache anything to avoid influence between tests
return $this->getChainCache([new NullAdapter()]);
}
}