DataTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Migration\Mode;
  7. class DataTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var Data
  11. */
  12. protected $data;
  13. /**
  14. * @var \Migration\App\Mode\StepList|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $stepList;
  17. /**
  18. * @var \Migration\Logger\Logger|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $logger;
  21. /**
  22. * @var \Migration\Config|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $config;
  25. /**
  26. * @var \Migration\App\Progress|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $progress;
  29. /**
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. $this->stepList = $this->getMockBuilder(\Migration\App\Mode\StepList::class)->disableOriginalConstructor()
  35. ->setMethods(['getSteps'])
  36. ->getMock();
  37. /** @var \Migration\App\Mode\StepListFactory|\PHPUnit_Framework_MockObject_MockObject $stepListFactory */
  38. $stepListFactory = $this->getMockBuilder(\Migration\App\Mode\StepListFactory::class)
  39. ->disableOriginalConstructor()
  40. ->setMethods(['create'])
  41. ->getMock();
  42. $stepListFactory->expects($this->any())->method('create')->with(['mode' => 'data'])
  43. ->willReturn($this->stepList);
  44. $this->logger = $this->getMockBuilder(\Migration\Logger\Logger::class)->disableOriginalConstructor()
  45. ->setMethods(['info', 'warning', 'notice'])
  46. ->getMock();
  47. $this->progress = $this->getMockBuilder(\Migration\App\Progress::class)->disableOriginalConstructor()
  48. ->setMethods(['saveResult', 'isCompleted', 'reset'])
  49. ->getMock();
  50. /** @var \Migration\App\SetupDeltaLog|\PHPUnit_Framework_MockObject_MockObject $setupDeltaLog */
  51. $setupDeltaLog = $this->getMockBuilder(\Migration\App\SetupDeltaLog::class)->disableOriginalConstructor()
  52. ->setMethods(['perform'])
  53. ->getMock();
  54. $setupDeltaLog->expects($this->any())->method('perform')->willReturn(true);
  55. $this->config = $this->createPartialMock(
  56. \Migration\Config::class,
  57. ['getStep']
  58. );
  59. $this->data = new Data($this->progress, $this->logger, $stepListFactory, $setupDeltaLog, $this->config);
  60. }
  61. /**
  62. * @return void
  63. */
  64. public function testRunStepsIntegrityFail()
  65. {
  66. $this->expectException(\Migration\Exception::class);
  67. $this->expectExceptionMessage('Integrity Check failed');
  68. $step = $this->getMockBuilder(\Migration\App\Step\StageInterface::class)->getMock();
  69. $step->expects($this->once())->method('perform')->will($this->returnValue(false));
  70. $this->progress->expects($this->any())->method('saveResult')->willReturnSelf();
  71. $this->progress->expects($this->any())->method('isCompleted')->willReturn(false);
  72. $this->stepList->expects($this->once())->method('getSteps')
  73. ->willReturn(['Step1' => ['integrity' => $step]]);
  74. $this->assertSame($this->data, $this->data->run());
  75. }
  76. /**
  77. * @expectedException \Migration\Exception
  78. * @expectedExceptionMessage Volume Check failed
  79. * @return void
  80. */
  81. public function testRunStepsVolumeFail()
  82. {
  83. $stepData = $this->getMockBuilder(\Migration\App\Step\StageInterface::class)->getMock();
  84. $stepData->expects($this->once())->method('perform')->will($this->returnValue(true));
  85. $stepVolume = $this->getMockBuilder(\Migration\App\Step\StageInterface::class)->getMock();
  86. $stepVolume->expects($this->once())->method('perform')->will($this->returnValue(false));
  87. $this->progress->expects($this->any())->method('saveResult')->willReturnSelf();
  88. $this->progress->expects($this->any())->method('isCompleted')->willReturn(false);
  89. $this->progress->expects($this->any())->method('reset')->with($stepVolume);
  90. $this->logger->expects($this->any())->method('info');
  91. $this->stepList->expects($this->any())->method('getSteps')
  92. ->willReturn(['Step1' => ['data' => $stepData, 'volume' => $stepVolume]]);
  93. $this->assertTrue($this->data->run());
  94. }
  95. /**
  96. * @return void
  97. */
  98. public function testRunStepsDataMigrationFail()
  99. {
  100. $this->expectException(\Migration\Exception::class);
  101. $this->expectExceptionMessage('Data Migration failed');
  102. $stageIntegrity = $this->getMockBuilder(\Migration\App\Step\StageInterface::class)->getMock();
  103. $stageIntegrity->expects($this->once())->method('perform')->will($this->returnValue(true));
  104. $stageData = $this->getMockBuilder(\Migration\App\Step\StageInterface::class)->getMock();
  105. $stageData->expects($this->once())->method('perform')->will($this->returnValue(false));
  106. $this->progress->expects($this->any())->method('saveResult')->willReturnSelf();
  107. $this->progress->expects($this->any())->method('isCompleted')->willReturn(false);
  108. $this->progress->expects($this->any())->method('reset')->with($stageData);
  109. $this->logger->expects($this->any())->method('info');
  110. $this->stepList->expects($this->any())->method('getSteps')
  111. ->willReturn(['Step1' => ['integrity' => $stageIntegrity, 'data' => $stageData]]);
  112. $this->assertSame($this->data, $this->data->run());
  113. }
  114. /**
  115. * @return void
  116. */
  117. public function testRunStepsSuccess()
  118. {
  119. $stageIntegrity = $this->getMockBuilder(\Migration\App\Step\StageInterface::class)->getMock();
  120. $stageIntegrity->expects($this->once())->method('perform')->will($this->returnValue(true));
  121. $stageData = $this->getMockBuilder(\Migration\App\Step\StageInterface::class)->getMock();
  122. $stageData->expects($this->once())->method('perform')->will($this->returnValue(true));
  123. $stageVolume = $this->getMockBuilder(\Migration\App\Step\StageInterface::class)->getMock();
  124. $stageVolume->expects($this->once())->method('perform')->will($this->returnValue(true));
  125. $this->progress->expects($this->any())->method('saveResult')->willReturnSelf();
  126. $this->progress->expects($this->any())->method('isCompleted')->willReturn(false);
  127. $this->logger->expects($this->at(0))->method('info')->with("started");
  128. $this->logger->expects($this->at(1))->method('info')->with("started");
  129. $this->logger->expects($this->at(2))->method('info')->with("started");
  130. $this->logger->expects($this->at(3))->method('info')->with("started");
  131. $this->logger->expects($this->at(4))->method('info')->with("Migration completed");
  132. $this->stepList->expects($this->any())->method('getSteps')->willReturn(
  133. [
  134. 'Title' => [
  135. 'integrity' => $stageIntegrity,
  136. 'data' => $stageData,
  137. 'volume' => $stageVolume
  138. ]
  139. ]
  140. );
  141. $this->assertTrue($this->data->run());
  142. }
  143. /**
  144. * @return void
  145. */
  146. public function testRunStepsWithSuccessProgress()
  147. {
  148. $stageIntegrity = $this->getMockBuilder(\Migration\App\Step\StageInterface::class)->getMock();
  149. $stageIntegrity->expects($this->never())->method('perform');
  150. $stageData = $this->getMockBuilder(\Migration\App\Step\StageInterface::class)->getMock();
  151. $stageData->expects($this->never())->method('perform');
  152. $stageVolume = $this->getMockBuilder(\Migration\App\Step\StageInterface::class)->getMock();
  153. $stageVolume->expects($this->never())->method('perform');
  154. $this->progress->expects($this->never())->method('saveResult');
  155. $this->progress->expects($this->any())->method('isCompleted')->willReturn(true);
  156. $this->logger->expects($this->at(0))->method('info')->with("started");
  157. $this->logger->expects($this->at(1))->method('info')->with("started");
  158. $this->logger->expects($this->at(2))->method('info')->with("started");
  159. $this->logger->expects($this->at(3))->method('info')->with("started");
  160. $this->logger->expects($this->at(4))->method('info')->with("Migration completed");
  161. $this->stepList->expects($this->any())->method('getSteps')->willReturn(
  162. [
  163. 'Title' => [
  164. 'integrity' => $stageIntegrity,
  165. 'data' => $stageData,
  166. 'volume' => $stageVolume
  167. ]
  168. ]
  169. );
  170. $this->assertTrue($this->data->run());
  171. }
  172. }