34 private $patchRegistryFactoryMock;
39 private $dataPatchReaderMock;
44 private $schemaPatchReaderMock;
49 private $resourceConnectionMock;
54 private $moduleResourceMock;
59 private $patchHistoryMock;
64 private $patchFactoryMock;
69 private $schemaSetupMock;
74 private $moduleDataSetupMock;
79 private $objectManagerMock;
84 private $patchApllier;
89 private $connectionMock;
94 private $patchBacwardCompatability;
98 $this->patchRegistryFactoryMock = $this->createMock(PatchRegistryFactory::class);
99 $this->dataPatchReaderMock = $this->createMock(PatchReader::class);
100 $this->schemaPatchReaderMock = $this->createMock(PatchReader::class);
101 $this->resourceConnectionMock = $this->createMock(ResourceConnection::class);
102 $this->moduleResourceMock = $this->createMock(ModuleResource::class);
103 $this->patchHistoryMock = $this->createMock(PatchHistory::class);
104 $this->patchFactoryMock = $this->createMock(PatchFactory::class);
105 $this->schemaSetupMock = $this->createMock(SchemaSetupInterface::class);
106 $this->moduleDataSetupMock = $this->createMock(ModuleDataSetupInterface::class);
107 $this->objectManagerMock = $this->createMock(ObjectManagerInterface::class);
108 $this->connectionMock = $this->createMock(AdapterInterface::class);
109 $this->moduleDataSetupMock->expects($this->any())->method(
'getConnection')->willReturn($this->connectionMock);
113 PatchBackwardCompatability::class,
115 'moduleResource' => $this->moduleResourceMock
121 'patchRegistryFactory' => $this->patchRegistryFactoryMock,
122 'dataPatchReader' => $this->dataPatchReaderMock,
123 'schemaPatchReader' => $this->schemaPatchReaderMock,
124 'resourceConnection' => $this->resourceConnectionMock,
125 'moduleResource' => $this->moduleResourceMock,
126 'patchHistory' => $this->patchHistoryMock,
127 'patchFactory' => $this->patchFactoryMock,
128 'objectManager' => $this->objectManagerMock,
129 'schemaSetup' => $this->schemaSetupMock,
130 'moduleDataSetup' => $this->moduleDataSetupMock,
131 'patchBackwardCompatability' => $this->patchBacwardCompatability
134 require_once
__DIR__ .
'/../_files/data_patch_classes.php';
135 require_once
__DIR__ .
'/../_files/schema_patch_classes.php';
147 $this->dataPatchReaderMock->expects($this->once())
150 ->willReturn($dataPatches);
152 $this->moduleResourceMock->expects($this->any())->method(
'getDataVersion')->willReturnMap(
154 [$moduleName, $moduleVersionInDb]
159 \SomeDataPatch::class,
160 \OtherDataPatch::class
162 $patchRegistryMock = $this->createAggregateIteratorMock(PatchRegistry::class, $patches, [
'registerPatch']);
163 $patchRegistryMock->expects($this->exactly(2))
164 ->method(
'registerPatch');
166 $this->patchRegistryFactoryMock->expects($this->any())
168 ->willReturn($patchRegistryMock);
170 $patch1 = $this->createMock(\SomeDataPatch::class);
171 $patch1->expects($this->once())->method(
'apply');
172 $patch2 = $this->createMock(\OtherDataPatch::class);
173 $patch2->expects($this->once())->method(
'apply');
174 $this->objectManagerMock->expects($this->any())->method(
'create')->willReturnMap(
176 [
'\\' . \SomeDataPatch::class, [
'moduleDataSetup' => $this->moduleDataSetupMock], $patch1],
177 [
'\\' . \OtherDataPatch::class, [
'moduleDataSetup' => $this->moduleDataSetupMock], $patch2],
180 $this->connectionMock->expects($this->exactly(2))->method(
'beginTransaction');
181 $this->connectionMock->expects($this->exactly(2))->method(
'commit');
182 $this->patchHistoryMock->expects($this->any())->method(
'fixPatch')->willReturnMap(
184 [get_class($patch1)],
185 [get_class($patch2)],
188 $this->patchApllier->applyDataPatch($moduleName);
197 'newly installed module' => [
198 'moduleName' =>
'Module1',
200 \SomeDataPatch::class,
201 \OtherDataPatch::class
203 'moduleVersionInDb' =>
null,
217 $this->dataPatchReaderMock->expects($this->once())
220 ->willReturn($dataPatches);
222 $this->moduleResourceMock->expects($this->any())->method(
'getDataVersion')->willReturnMap(
224 [$moduleName, $moduleVersionInDb]
229 \SomeDataPatch::class,
230 \OtherDataPatch::class
232 $patchRegistryMock = $this->createAggregateIteratorMock(
233 PatchRegistry::class,
237 $patchRegistryMock->expects(self::exactly(2))
238 ->method(
'registerPatch');
240 $this->patchRegistryFactoryMock->expects(self::any())
242 ->willReturn($patchRegistryMock);
244 $patch1 = $this->createMock(\SomeDataPatch::class);
245 $patch1->expects(self::never())->method(
'apply');
246 $patch2 = $this->createMock(\OtherDataPatch::class);
247 $patch2->expects(self::once())->method(
'apply');
248 $this->objectManagerMock->expects(self::any())->method(
'create')->willReturnMap(
250 [
'\\' . \SomeDataPatch::class, [
'moduleDataSetup' => $this->moduleDataSetupMock], $patch1],
251 [
'\\' . \OtherDataPatch::class, [
'moduleDataSetup' => $this->moduleDataSetupMock], $patch2],
254 $this->connectionMock->expects(self::exactly(1))->method(
'beginTransaction');
255 $this->connectionMock->expects(self::exactly(1))->method(
'commit');
256 $this->patchHistoryMock->expects(self::exactly(2))->method(
'fixPatch');
257 $this->patchApllier->applyDataPatch($moduleName);
266 'upgrade module iwth only OtherDataPatch' => [
267 'moduleName' =>
'Module1',
269 \SomeDataPatch::class,
270 \OtherDataPatch::class
272 'moduleVersionInDb' =>
'2.0.0',
289 $this->dataPatchReaderMock->expects($this->once())
292 ->willReturn($dataPatches);
294 $this->moduleResourceMock->expects($this->any())->method(
'getDataVersion')->willReturnMap(
296 [$moduleName, $moduleVersionInDb]
301 \SomeDataPatch::class,
302 \OtherDataPatch::class
304 $patchRegistryMock = $this->createAggregateIteratorMock(PatchRegistry::class, $patches, [
'registerPatch']);
305 $patchRegistryMock->expects($this->exactly(2))
306 ->method(
'registerPatch');
308 $this->patchRegistryFactoryMock->expects($this->any())
310 ->willReturn($patchRegistryMock);
312 $patch1 = $this->createMock(\SomeDataPatch::class);
313 $patch1->expects($this->never())->method(
'apply');
314 $patch2 = $this->createMock(\OtherDataPatch::class);
315 $exception = new \Exception(
'Patch Apply Error');
316 $patch2->expects($this->once())->method(
'apply')->willThrowException($exception);
317 $this->objectManagerMock->expects($this->any())->method(
'create')->willReturnMap(
319 [
'\\' . \SomeDataPatch::class, [
'moduleDataSetup' => $this->moduleDataSetupMock], $patch1],
320 [
'\\' . \OtherDataPatch::class, [
'moduleDataSetup' => $this->moduleDataSetupMock], $patch2],
323 $this->connectionMock->expects($this->exactly(1))->method(
'beginTransaction');
324 $this->connectionMock->expects($this->never())->method(
'commit');
325 $this->connectionMock->expects($this->exactly(1))->method(
'rollback');
326 $this->patchHistoryMock->expects($this->exactly(1))->method(
'fixPatch');
327 $this->patchApllier->applyDataPatch($moduleName);
336 $this->dataPatchReaderMock->expects($this->once())
339 ->willReturn([\stdClass::class]);
340 $patchRegistryMock = $this->createAggregateIteratorMock(
341 PatchRegistry::class,
345 $patchRegistryMock->expects($this->exactly(1))
346 ->method(
'registerPatch');
348 $this->patchRegistryFactoryMock->expects($this->any())
350 ->willReturn($patchRegistryMock);
352 $this->objectManagerMock->expects($this->any())->method(
'create')->willReturnMap(
355 '\\' . \stdClass::class,
356 [
'moduleDataSetup' => $this->moduleDataSetupMock],
357 $this->createMock(\stdClass::class)
362 $this->patchApllier->applyDataPatch(
'module1');
367 $patches = [\NonTransactionableDataPatch::class];
368 $this->dataPatchReaderMock->expects($this->once())
371 ->willReturn($patches);
372 $patchRegistryMock = $this->createAggregateIteratorMock(
373 PatchRegistry::class,
377 $patchRegistryMock->expects($this->exactly(1))
378 ->method(
'registerPatch');
380 $this->patchRegistryFactoryMock->expects($this->any())
382 ->willReturn($patchRegistryMock);
384 $patch1 = $this->createMock($patches[0]);
385 $patch1->expects($this->exactly(1))->method(
'apply');
386 $this->connectionMock->expects($this->never())->method(
'beginTransaction');
387 $this->connectionMock->expects($this->never())->method(
'commit');
388 $this->connectionMock->expects($this->never())->method(
'rollback');
389 $this->patchHistoryMock->expects($this->once())->method(
'fixPatch')->with(get_class($patch1));
390 $this->objectManagerMock->expects($this->any())->method(
'create')->willReturnMap(
394 [
'moduleDataSetup' => $this->moduleDataSetupMock],
400 $this->patchApllier->applyDataPatch(
'module1');
412 $this->schemaPatchReaderMock->expects($this->once())
415 ->willReturn($schemaPatches);
417 $this->moduleResourceMock->expects($this->any())->method(
'getDbVersion')->willReturnMap(
419 [$moduleName, $moduleVersionInDb]
424 \SomeSchemaPatch::class,
425 \OtherSchemaPatch::class
427 $patchRegistryMock = $this->createAggregateIteratorMock(PatchRegistry::class, $patches, [
'registerPatch']);
428 $patchRegistryMock->expects($this->exactly(2))
429 ->method(
'registerPatch');
431 $this->patchRegistryFactoryMock->expects($this->any())
433 ->willReturn($patchRegistryMock);
435 $patch1 = $this->createMock(\SomeSchemaPatch::class);
436 $patch1->expects($this->never())->method(
'apply');
437 $patch2 = $this->createMock(\OtherSchemaPatch::class);
438 $patch2->expects($this->once())->method(
'apply');
439 $this->patchFactoryMock->expects($this->any())->method(
'create')->willReturnMap(
441 [\SomeSchemaPatch::class, [
'schemaSetup' => $this->schemaSetupMock], $patch1],
442 [\OtherSchemaPatch::class, [
'schemaSetup' => $this->schemaSetupMock], $patch2],
445 $this->connectionMock->expects($this->never())->method(
'beginTransaction');
446 $this->connectionMock->expects($this->never())->method(
'commit');
447 $this->patchHistoryMock->expects($this->exactly(2))->method(
'fixPatch');
448 $this->patchApllier->applySchemaPatch($moduleName);
453 $patches = [\RevertableDataPatch::class];
454 $this->dataPatchReaderMock->expects($this->once())
457 ->willReturn($patches);
458 $patchRegistryMock = $this->createAggregateIteratorMock(
459 PatchRegistry::class,
461 [
'registerPatch',
'getReverseIterator']
463 $patchRegistryMock->expects($this->exactly(1))
464 ->method(
'registerPatch');
465 $patchRegistryMock->expects($this->once())->method(
'getReverseIterator')
466 ->willReturn(array_reverse($patches));
468 $this->patchRegistryFactoryMock->expects($this->any())
470 ->willReturn($patchRegistryMock);
472 $patch1 = $this->createMock($patches[0]);
473 $patch1->expects($this->exactly(1))->method(
'revert');
474 $this->connectionMock->expects($this->once())->method(
'beginTransaction');
475 $this->connectionMock->expects($this->once())->method(
'commit');
476 $this->connectionMock->expects($this->never())->method(
'rollback');
477 $this->patchHistoryMock->expects($this->once())->method(
'revertPatchFromHistory')->with(get_class($patch1));
478 $this->objectManagerMock->expects($this->any())->method(
'create')->willReturnMap(
482 [
'moduleDataSetup' => $this->moduleDataSetupMock],
488 $this->patchApllier->revertDataPatches(
'module1');
497 'upgrade module iwth only OtherSchemaPatch' => [
498 'moduleName' =>
'Module1',
500 \SomeSchemaPatch::class,
501 \OtherSchemaPatch::class
503 'moduleVersionInDb' =>
'2.0.0',
518 if (!in_array(ltrim(\IteratorAggregate::class,
'\\'), class_implements(
$className))) {
519 throw new \Exception(
'Mock possible only for classes that implement IteratorAggregate interface.');
524 $someIterator = $this->createMock(\ArrayIterator::class);
526 $mockIteratorAggregate = $this->getMockBuilder(
$className)
527 ->disableOriginalConstructor()
528 ->setMethods(array_merge(
$methods, [
'getIterator']))
531 $mockIteratorAggregate->expects($this->any())->method(
'getIterator')->willReturn($someIterator);
535 $someIterator->expects($this->any())
537 ->willReturnCallback(
function () use (
$iterator) {
541 $someIterator->expects($this->any())
543 ->willReturnCallback(
function () use (
$iterator) {
547 $someIterator->expects($this->any())
549 ->willReturnCallback(
function () use (
$iterator) {
553 $someIterator->expects($this->any())
555 ->willReturnCallback(
function () use (
$iterator) {
559 $someIterator->expects($this->any())
561 ->willReturnCallback(
function () use (
$iterator) {
565 return $mockIteratorAggregate;
testApplyDataPatchForNewlyInstalledModule($moduleName, $dataPatches, $moduleVersionInDb)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
testNonTransactionablePatch()
applyDataPatchDataInstalledModuleProvider()
testSchemaPatchAplly($moduleName, $schemaPatches, $moduleVersionInDb)
testApplyDataPatchRollback($moduleName, $dataPatches, $moduleVersionInDb)
testApplyDataPatchForInstalledModule($moduleName, $dataPatches, $moduleVersionInDb)
schemaPatchDataProvider()
applyDataPatchDataNewModuleProvider()