PatchApplierTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\Test\Unit\Patch;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\DB\Adapter\AdapterInterface;
  9. use Magento\Framework\Module\ModuleResource;
  10. use Magento\Framework\ObjectManagerInterface;
  11. use Magento\Framework\Setup\ModuleDataSetupInterface;
  12. use Magento\Framework\Setup\Patch\PatchBackwardCompatability;
  13. use Magento\Framework\Setup\SchemaSetupInterface;
  14. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  15. use Magento\Framework\Setup\Patch\PatchApplier;
  16. use Magento\Framework\Setup\Patch\PatchFactory;
  17. use Magento\Framework\Setup\Patch\PatchHistory;
  18. use Magento\Framework\Setup\Patch\PatchReader;
  19. use Magento\Framework\Setup\Patch\PatchRegistry;
  20. use Magento\Framework\Setup\Patch\PatchRegistryFactory;
  21. /**
  22. * Class PatchApplierTest
  23. * @package Magento\Framework\Setup\Test\Unit\Patch
  24. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  25. */
  26. class PatchApplierTest extends \PHPUnit\Framework\TestCase
  27. {
  28. /**
  29. * @var PatchRegistryFactory|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $patchRegistryFactoryMock;
  32. /**
  33. * @var PatchReader|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $dataPatchReaderMock;
  36. /**
  37. * @var PatchReader|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $schemaPatchReaderMock;
  40. /**
  41. * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $resourceConnectionMock;
  44. /**
  45. * @var ModuleResource|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $moduleResourceMock;
  48. /**
  49. * @var PatchHistory|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $patchHistoryMock;
  52. /**
  53. * @var PatchFactory|\PHPUnit_Framework_MockObject_MockObject
  54. */
  55. private $patchFactoryMock;
  56. /**
  57. * @var \Magento\Framework\Setup\SetupInterface|\PHPUnit_Framework_MockObject_MockObject
  58. */
  59. private $schemaSetupMock;
  60. /**
  61. * @var ModuleDataSetupInterface|\PHPUnit_Framework_MockObject_MockObject
  62. */
  63. private $moduleDataSetupMock;
  64. /**
  65. * @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  66. */
  67. private $objectManagerMock;
  68. /**
  69. * @var PatchApplier
  70. */
  71. private $patchApllier;
  72. /**
  73. * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  74. */
  75. private $connectionMock;
  76. /**
  77. * @var PatchBackwardCompatability |\PHPUnit_Framework_MockObject_MockObject
  78. */
  79. private $patchBacwardCompatability;
  80. protected function setUp()
  81. {
  82. $this->patchRegistryFactoryMock = $this->createMock(PatchRegistryFactory::class);
  83. $this->dataPatchReaderMock = $this->createMock(PatchReader::class);
  84. $this->schemaPatchReaderMock = $this->createMock(PatchReader::class);
  85. $this->resourceConnectionMock = $this->createMock(ResourceConnection::class);
  86. $this->moduleResourceMock = $this->createMock(ModuleResource::class);
  87. $this->patchHistoryMock = $this->createMock(PatchHistory::class);
  88. $this->patchFactoryMock = $this->createMock(PatchFactory::class);
  89. $this->schemaSetupMock = $this->createMock(SchemaSetupInterface::class);
  90. $this->moduleDataSetupMock = $this->createMock(ModuleDataSetupInterface::class);
  91. $this->objectManagerMock = $this->createMock(ObjectManagerInterface::class);
  92. $this->connectionMock = $this->createMock(AdapterInterface::class);
  93. $this->moduleDataSetupMock->expects($this->any())->method('getConnection')->willReturn($this->connectionMock);
  94. $objectManager = new ObjectManager($this);
  95. $this->patchBacwardCompatability = $objectManager->getObject(
  96. PatchBackwardCompatability::class,
  97. [
  98. 'moduleResource' => $this->moduleResourceMock
  99. ]
  100. );
  101. $this->patchApllier = $objectManager->getObject(
  102. PatchApplier::class,
  103. [
  104. 'patchRegistryFactory' => $this->patchRegistryFactoryMock,
  105. 'dataPatchReader' => $this->dataPatchReaderMock,
  106. 'schemaPatchReader' => $this->schemaPatchReaderMock,
  107. 'resourceConnection' => $this->resourceConnectionMock,
  108. 'moduleResource' => $this->moduleResourceMock,
  109. 'patchHistory' => $this->patchHistoryMock,
  110. 'patchFactory' => $this->patchFactoryMock,
  111. 'objectManager' => $this->objectManagerMock,
  112. 'schemaSetup' => $this->schemaSetupMock,
  113. 'moduleDataSetup' => $this->moduleDataSetupMock,
  114. 'patchBackwardCompatability' => $this->patchBacwardCompatability
  115. ]
  116. );
  117. require_once __DIR__ . '/../_files/data_patch_classes.php';
  118. require_once __DIR__ . '/../_files/schema_patch_classes.php';
  119. }
  120. /**
  121. * @param $moduleName
  122. * @param $dataPatches
  123. * @param $moduleVersionInDb
  124. *
  125. * @dataProvider applyDataPatchDataNewModuleProvider()
  126. */
  127. public function testApplyDataPatchForNewlyInstalledModule($moduleName, $dataPatches, $moduleVersionInDb)
  128. {
  129. $this->dataPatchReaderMock->expects($this->once())
  130. ->method('read')
  131. ->with($moduleName)
  132. ->willReturn($dataPatches);
  133. $this->moduleResourceMock->expects($this->any())->method('getDataVersion')->willReturnMap(
  134. [
  135. [$moduleName, $moduleVersionInDb]
  136. ]
  137. );
  138. $patches = [
  139. \SomeDataPatch::class,
  140. \OtherDataPatch::class
  141. ];
  142. $patchRegistryMock = $this->createAggregateIteratorMock(PatchRegistry::class, $patches, ['registerPatch']);
  143. $patchRegistryMock->expects($this->exactly(2))
  144. ->method('registerPatch');
  145. $this->patchRegistryFactoryMock->expects($this->any())
  146. ->method('create')
  147. ->willReturn($patchRegistryMock);
  148. $patch1 = $this->createMock(\SomeDataPatch::class);
  149. $patch1->expects($this->once())->method('apply');
  150. $patch2 = $this->createMock(\OtherDataPatch::class);
  151. $patch2->expects($this->once())->method('apply');
  152. $this->objectManagerMock->expects($this->any())->method('create')->willReturnMap(
  153. [
  154. ['\\' . \SomeDataPatch::class, ['moduleDataSetup' => $this->moduleDataSetupMock], $patch1],
  155. ['\\' . \OtherDataPatch::class, ['moduleDataSetup' => $this->moduleDataSetupMock], $patch2],
  156. ]
  157. );
  158. $this->connectionMock->expects($this->exactly(2))->method('beginTransaction');
  159. $this->connectionMock->expects($this->exactly(2))->method('commit');
  160. $this->patchHistoryMock->expects($this->any())->method('fixPatch')->willReturnMap(
  161. [
  162. [get_class($patch1)],
  163. [get_class($patch2)],
  164. ]
  165. );
  166. $this->patchApllier->applyDataPatch($moduleName);
  167. }
  168. /**
  169. * @return array
  170. */
  171. public function applyDataPatchDataNewModuleProvider()
  172. {
  173. return [
  174. 'newly installed module' => [
  175. 'moduleName' => 'Module1',
  176. 'dataPatches' => [
  177. \SomeDataPatch::class,
  178. \OtherDataPatch::class
  179. ],
  180. 'moduleVersionInDb' => null,
  181. ],
  182. ];
  183. }
  184. /**
  185. * @param $moduleName
  186. * @param $dataPatches
  187. * @param $moduleVersionInDb
  188. *
  189. * @dataProvider applyDataPatchDataInstalledModuleProvider()
  190. */
  191. public function testApplyDataPatchForInstalledModule($moduleName, $dataPatches, $moduleVersionInDb)
  192. {
  193. $this->dataPatchReaderMock->expects($this->once())
  194. ->method('read')
  195. ->with($moduleName)
  196. ->willReturn($dataPatches);
  197. $this->moduleResourceMock->expects($this->any())->method('getDataVersion')->willReturnMap(
  198. [
  199. [$moduleName, $moduleVersionInDb]
  200. ]
  201. );
  202. $patches = [
  203. \SomeDataPatch::class,
  204. \OtherDataPatch::class
  205. ];
  206. $patchRegistryMock = $this->createAggregateIteratorMock(
  207. PatchRegistry::class,
  208. $patches,
  209. ['registerPatch']
  210. );
  211. $patchRegistryMock->expects(self::exactly(2))
  212. ->method('registerPatch');
  213. $this->patchRegistryFactoryMock->expects(self::any())
  214. ->method('create')
  215. ->willReturn($patchRegistryMock);
  216. $patch1 = $this->createMock(\SomeDataPatch::class);
  217. $patch1->expects(self::never())->method('apply');
  218. $patch2 = $this->createMock(\OtherDataPatch::class);
  219. $patch2->expects(self::once())->method('apply');
  220. $this->objectManagerMock->expects(self::any())->method('create')->willReturnMap(
  221. [
  222. ['\\' . \SomeDataPatch::class, ['moduleDataSetup' => $this->moduleDataSetupMock], $patch1],
  223. ['\\' . \OtherDataPatch::class, ['moduleDataSetup' => $this->moduleDataSetupMock], $patch2],
  224. ]
  225. );
  226. $this->connectionMock->expects(self::exactly(1))->method('beginTransaction');
  227. $this->connectionMock->expects(self::exactly(1))->method('commit');
  228. $this->patchHistoryMock->expects(self::exactly(2))->method('fixPatch');
  229. $this->patchApllier->applyDataPatch($moduleName);
  230. }
  231. /**
  232. * @return array
  233. */
  234. public function applyDataPatchDataInstalledModuleProvider()
  235. {
  236. return [
  237. 'upgrade module iwth only OtherDataPatch' => [
  238. 'moduleName' => 'Module1',
  239. 'dataPatches' => [
  240. \SomeDataPatch::class,
  241. \OtherDataPatch::class
  242. ],
  243. 'moduleVersionInDb' => '2.0.0',
  244. ]
  245. ];
  246. }
  247. /**
  248. * @param $moduleName
  249. * @param $dataPatches
  250. * @param $moduleVersionInDb
  251. *
  252. * @expectedException \Magento\Framework\Setup\Exception
  253. * @expectedExceptionMessage Patch Apply Error
  254. *
  255. * @dataProvider applyDataPatchDataInstalledModuleProvider()
  256. */
  257. public function testApplyDataPatchRollback($moduleName, $dataPatches, $moduleVersionInDb)
  258. {
  259. $this->dataPatchReaderMock->expects($this->once())
  260. ->method('read')
  261. ->with($moduleName)
  262. ->willReturn($dataPatches);
  263. $this->moduleResourceMock->expects($this->any())->method('getDataVersion')->willReturnMap(
  264. [
  265. [$moduleName, $moduleVersionInDb]
  266. ]
  267. );
  268. $patches = [
  269. \SomeDataPatch::class,
  270. \OtherDataPatch::class
  271. ];
  272. $patchRegistryMock = $this->createAggregateIteratorMock(PatchRegistry::class, $patches, ['registerPatch']);
  273. $patchRegistryMock->expects($this->exactly(2))
  274. ->method('registerPatch');
  275. $this->patchRegistryFactoryMock->expects($this->any())
  276. ->method('create')
  277. ->willReturn($patchRegistryMock);
  278. $patch1 = $this->createMock(\SomeDataPatch::class);
  279. $patch1->expects($this->never())->method('apply');
  280. $patch2 = $this->createMock(\OtherDataPatch::class);
  281. $exception = new \Exception('Patch Apply Error');
  282. $patch2->expects($this->once())->method('apply')->willThrowException($exception);
  283. $this->objectManagerMock->expects($this->any())->method('create')->willReturnMap(
  284. [
  285. ['\\' . \SomeDataPatch::class, ['moduleDataSetup' => $this->moduleDataSetupMock], $patch1],
  286. ['\\' . \OtherDataPatch::class, ['moduleDataSetup' => $this->moduleDataSetupMock], $patch2],
  287. ]
  288. );
  289. $this->connectionMock->expects($this->exactly(1))->method('beginTransaction');
  290. $this->connectionMock->expects($this->never())->method('commit');
  291. $this->connectionMock->expects($this->exactly(1))->method('rollback');
  292. $this->patchHistoryMock->expects($this->exactly(1))->method('fixPatch');
  293. $this->patchApllier->applyDataPatch($moduleName);
  294. }
  295. /**
  296. * @expectedException \Magento\Framework\Setup\Exception
  297. * @expectedExceptionMessageRegExp "Patch [a-zA-Z0-9\_]+ should implement DataPatchInterface"
  298. */
  299. public function testNonDataPatchApply()
  300. {
  301. $this->dataPatchReaderMock->expects($this->once())
  302. ->method('read')
  303. ->with('module1')
  304. ->willReturn([\stdClass::class]);
  305. $patchRegistryMock = $this->createAggregateIteratorMock(
  306. PatchRegistry::class,
  307. [\stdClass::class],
  308. ['registerPatch']
  309. );
  310. $patchRegistryMock->expects($this->exactly(1))
  311. ->method('registerPatch');
  312. $this->patchRegistryFactoryMock->expects($this->any())
  313. ->method('create')
  314. ->willReturn($patchRegistryMock);
  315. $this->objectManagerMock->expects($this->any())->method('create')->willReturnMap(
  316. [
  317. [
  318. '\\' . \stdClass::class,
  319. ['moduleDataSetup' => $this->moduleDataSetupMock],
  320. $this->createMock(\stdClass::class)
  321. ],
  322. ]
  323. );
  324. $this->patchApllier->applyDataPatch('module1');
  325. }
  326. public function testNonTransactionablePatch()
  327. {
  328. $patches = [\NonTransactionableDataPatch::class];
  329. $this->dataPatchReaderMock->expects($this->once())
  330. ->method('read')
  331. ->with('module1')
  332. ->willReturn($patches);
  333. $patchRegistryMock = $this->createAggregateIteratorMock(
  334. PatchRegistry::class,
  335. $patches,
  336. ['registerPatch']
  337. );
  338. $patchRegistryMock->expects($this->exactly(1))
  339. ->method('registerPatch');
  340. $this->patchRegistryFactoryMock->expects($this->any())
  341. ->method('create')
  342. ->willReturn($patchRegistryMock);
  343. $patch1 = $this->createMock($patches[0]);
  344. $patch1->expects($this->exactly(1))->method('apply');
  345. $this->connectionMock->expects($this->never())->method('beginTransaction');
  346. $this->connectionMock->expects($this->never())->method('commit');
  347. $this->connectionMock->expects($this->never())->method('rollback');
  348. $this->patchHistoryMock->expects($this->once())->method('fixPatch')->with(get_class($patch1));
  349. $this->objectManagerMock->expects($this->any())->method('create')->willReturnMap(
  350. [
  351. [
  352. '\\' . $patches[0],
  353. ['moduleDataSetup' => $this->moduleDataSetupMock],
  354. $patch1
  355. ],
  356. ]
  357. );
  358. $this->patchApllier->applyDataPatch('module1');
  359. }
  360. /**
  361. * @param $moduleName
  362. * @param $schemaPatches
  363. * @param $moduleVersionInDb
  364. *
  365. * @dataProvider schemaPatchDataProvider()
  366. */
  367. public function testSchemaPatchAplly($moduleName, $schemaPatches, $moduleVersionInDb)
  368. {
  369. $this->schemaPatchReaderMock->expects($this->once())
  370. ->method('read')
  371. ->with($moduleName)
  372. ->willReturn($schemaPatches);
  373. $this->moduleResourceMock->expects($this->any())->method('getDbVersion')->willReturnMap(
  374. [
  375. [$moduleName, $moduleVersionInDb]
  376. ]
  377. );
  378. $patches = [
  379. \SomeSchemaPatch::class,
  380. \OtherSchemaPatch::class
  381. ];
  382. $patchRegistryMock = $this->createAggregateIteratorMock(PatchRegistry::class, $patches, ['registerPatch']);
  383. $patchRegistryMock->expects($this->exactly(2))
  384. ->method('registerPatch');
  385. $this->patchRegistryFactoryMock->expects($this->any())
  386. ->method('create')
  387. ->willReturn($patchRegistryMock);
  388. $patch1 = $this->createMock(\SomeSchemaPatch::class);
  389. $patch1->expects($this->never())->method('apply');
  390. $patch2 = $this->createMock(\OtherSchemaPatch::class);
  391. $patch2->expects($this->once())->method('apply');
  392. $this->patchFactoryMock->expects($this->any())->method('create')->willReturnMap(
  393. [
  394. [\SomeSchemaPatch::class, ['schemaSetup' => $this->schemaSetupMock], $patch1],
  395. [\OtherSchemaPatch::class, ['schemaSetup' => $this->schemaSetupMock], $patch2],
  396. ]
  397. );
  398. $this->connectionMock->expects($this->never())->method('beginTransaction');
  399. $this->connectionMock->expects($this->never())->method('commit');
  400. $this->patchHistoryMock->expects($this->exactly(2))->method('fixPatch');
  401. $this->patchApllier->applySchemaPatch($moduleName);
  402. }
  403. public function testRevertDataPatches()
  404. {
  405. $patches = [\RevertableDataPatch::class];
  406. $this->dataPatchReaderMock->expects($this->once())
  407. ->method('read')
  408. ->with('module1')
  409. ->willReturn($patches);
  410. $patchRegistryMock = $this->createAggregateIteratorMock(
  411. PatchRegistry::class,
  412. $patches,
  413. ['registerPatch', 'getReverseIterator']
  414. );
  415. $patchRegistryMock->expects($this->exactly(1))
  416. ->method('registerPatch');
  417. $patchRegistryMock->expects($this->once())->method('getReverseIterator')
  418. ->willReturn(array_reverse($patches));
  419. $this->patchRegistryFactoryMock->expects($this->any())
  420. ->method('create')
  421. ->willReturn($patchRegistryMock);
  422. $patch1 = $this->createMock($patches[0]);
  423. $patch1->expects($this->exactly(1))->method('revert');
  424. $this->connectionMock->expects($this->once())->method('beginTransaction');
  425. $this->connectionMock->expects($this->once())->method('commit');
  426. $this->connectionMock->expects($this->never())->method('rollback');
  427. $this->patchHistoryMock->expects($this->once())->method('revertPatchFromHistory')->with(get_class($patch1));
  428. $this->objectManagerMock->expects($this->any())->method('create')->willReturnMap(
  429. [
  430. [
  431. '\\' . $patches[0],
  432. ['moduleDataSetup' => $this->moduleDataSetupMock],
  433. $patch1
  434. ],
  435. ]
  436. );
  437. $this->patchApllier->revertDataPatches('module1');
  438. }
  439. /**
  440. * @return array
  441. */
  442. public function schemaPatchDataProvider()
  443. {
  444. return [
  445. 'upgrade module iwth only OtherSchemaPatch' => [
  446. 'moduleName' => 'Module1',
  447. 'schemaPatches' => [
  448. \SomeSchemaPatch::class,
  449. \OtherSchemaPatch::class
  450. ],
  451. 'moduleVersionInDb' => '2.0.0',
  452. ]
  453. ];
  454. }
  455. /**
  456. * Create mock of class that implements IteratorAggregate
  457. *
  458. * @param string $className
  459. * @param array $items
  460. * @param array $methods
  461. * @return \PHPUnit_Framework_MockObject_MockObject|\IteratorAggregate
  462. * @throws \Exception
  463. */
  464. private function createAggregateIteratorMock($className, array $items = [], array $methods = [])
  465. {
  466. if (!in_array(ltrim(\IteratorAggregate::class, '\\'), class_implements($className))) {
  467. throw new \Exception('Mock possible only for classes that implement IteratorAggregate interface.');
  468. }
  469. /**
  470. * PHPUnit_Framework_MockObject_MockObject
  471. */
  472. $someIterator = $this->createMock(\ArrayIterator::class);
  473. $mockIteratorAggregate = $this->getMockBuilder($className)
  474. ->disableOriginalConstructor()
  475. ->setMethods(array_merge($methods, ['getIterator']))
  476. ->getMock();
  477. $mockIteratorAggregate->expects($this->any())->method('getIterator')->willReturn($someIterator);
  478. $iterator = new \ArrayIterator($items);
  479. $someIterator->expects($this->any())
  480. ->method('rewind')
  481. ->willReturnCallback(function () use ($iterator) {
  482. $iterator->rewind();
  483. });
  484. $someIterator->expects($this->any())
  485. ->method('current')
  486. ->willReturnCallback(function () use ($iterator) {
  487. return $iterator->current();
  488. });
  489. $someIterator->expects($this->any())
  490. ->method('key')
  491. ->willReturnCallback(function () use ($iterator) {
  492. return $iterator->key();
  493. });
  494. $someIterator->expects($this->any())
  495. ->method('next')
  496. ->willReturnCallback(function () use ($iterator) {
  497. $iterator->next();
  498. });
  499. $someIterator->expects($this->any())
  500. ->method('valid')
  501. ->willReturnCallback(function () use ($iterator) {
  502. return $iterator->valid();
  503. });
  504. return $mockIteratorAggregate;
  505. }
  506. }