ProgressTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Migration\App;
  7. /**
  8. * Class ProgressTest
  9. */
  10. class ProgressTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var Progress\File|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $file;
  16. /**
  17. * @var \Migration\App\Progress
  18. */
  19. protected $progress;
  20. /**
  21. * @return void
  22. */
  23. public function setUp()
  24. {
  25. $this->file = $this->getMockBuilder(\Migration\App\Progress\File::class)
  26. ->setMethods(['getData', 'saveData', 'clearLockFile', 'isExists'])
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. $this->progress = new Progress($this->file);
  30. }
  31. /**
  32. * @return array
  33. */
  34. public function isCompletedDataProvider()
  35. {
  36. $step = $this->createMock(\Migration\Step\Map\Data::class);
  37. return [
  38. 'complete' => [
  39. 'data' => [get_class($step) => ['integrity' => ['result' => true]]],
  40. 'step' => $step,
  41. 'stage' => 'integrity',
  42. 'result' => true
  43. ],
  44. 'incomplete' => [
  45. 'data' => [],
  46. 'step' => $step,
  47. 'stage' => 'integrity',
  48. 'result' => false
  49. ]
  50. ];
  51. }
  52. /**
  53. * @param array $data
  54. * @param mixed $step
  55. * @param string $stage
  56. * @param bool $result
  57. * @dataProvider isCompletedDataProvider
  58. * @return void
  59. */
  60. public function testIsCompleted($data, $step, $stage, $result)
  61. {
  62. $this->file->expects($this->once())->method('getData')->will($this->returnValue($data));
  63. $isCompleted = $this->progress->isCompleted($step, $stage);
  64. $this->assertEquals($result, $isCompleted);
  65. }
  66. /**
  67. * @return void
  68. */
  69. public function testAddProcessedEntitySuccess()
  70. {
  71. $step = $this->createMock(\Migration\Step\Map\Data::class);
  72. $stage = 'run';
  73. $result = $this->progress->addProcessedEntity($step, $stage, 'document_name1');
  74. $this->assertTrue($result);
  75. }
  76. /**
  77. * @return void
  78. */
  79. public function testAddProcessedEntityAlreadyExist()
  80. {
  81. $step = $this->createMock(\Migration\Step\Map\Data::class);
  82. $stage = 'run';
  83. $documentName = 'document_name1';
  84. $data = [get_class($step) => [$stage => ['process' => [$documentName]]]];
  85. $this->file->expects($this->once())->method('getData')->will($this->returnValue($data));
  86. $result = $this->progress->addProcessedEntity($step, $stage, $documentName);
  87. $this->assertFalse($result);
  88. }
  89. /**
  90. * @return void
  91. */
  92. public function testResetProcessedEntities()
  93. {
  94. $step = $this->helper = $this->getMockBuilder(\Migration\Step\Map\Migrate::class)
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $stage = 'run';
  98. $this->assertNull($this->progress->resetProcessedEntities($step, $stage));
  99. }
  100. /**
  101. * @return void
  102. */
  103. public function testGetProcessedEntities()
  104. {
  105. $step = $this->getMockBuilder(\Migration\Step\Map\Migrate::class)
  106. ->disableOriginalConstructor()
  107. ->getMock();
  108. $stage = 'run';
  109. $document = ['some_document'];
  110. $progress = [get_class($step) => [$stage => ['process' => $document]]];
  111. $this->file->expects($this->once())->method('getData')->will($this->returnValue($progress));
  112. $result = $this->progress->getProcessedEntities($step, $stage);
  113. $this->assertEquals($document, $result);
  114. }
  115. /**
  116. * @return void
  117. */
  118. public function testSaveResult()
  119. {
  120. $this->file->expects($this->once())->method('saveData')->will($this->returnValue(1));
  121. $step = $this->getMockBuilder(\Migration\Step\Map\Migrate::class)
  122. ->disableOriginalConstructor()
  123. ->getMock();
  124. $this->progress->saveResult($step, 'integrity', 'true');
  125. }
  126. /**
  127. * @return void
  128. */
  129. public function testReset()
  130. {
  131. $this->file->expects($this->once())->method('clearLockFile');
  132. $this->progress->reset();
  133. }
  134. /**
  135. * @return void
  136. */
  137. public function testResetObject()
  138. {
  139. $step = $this->getMockBuilder(\Migration\Step\Map::class)
  140. ->disableOriginalConstructor()
  141. ->getMock();
  142. $data = [get_class($step) => ['dummy_array']];
  143. $this->file->expects($this->once())->method('getData')->will($this->returnValue($data));
  144. $this->file->expects($this->once())->method('saveData')->with([]);
  145. $this->progress->reset($step);
  146. }
  147. /**
  148. * @return void
  149. */
  150. public function testSaveDataNoFile()
  151. {
  152. $this->file->expects($this->any())->method('isExists')->will($this->returnValue(false));
  153. $this->file->expects($this->once())->method('saveData');
  154. $step = $this->getMockBuilder(\Migration\Step\Map::class)
  155. ->disableOriginalConstructor()
  156. ->getMock();
  157. $this->progress->saveResult($step, 'integrity', 'true');
  158. }
  159. /**
  160. * @param array $data
  161. * @param string $step
  162. * @param string $stage
  163. * @param array $processed
  164. * @param array $saveData
  165. *
  166. * @return void
  167. * @dataProvider saveProcessedEntitiesDataProvider
  168. */
  169. public function testSaveProcessedEntities(array $data, $step, $stage, array $processed, array $saveData)
  170. {
  171. $this->file->expects($this->once())->method('getData')->willReturn($data);
  172. $this->file->expects($this->once())->method('saveData')->with($saveData);
  173. $this->progress->saveProcessedEntities($step, $stage, $processed);
  174. }
  175. /**
  176. * @return array
  177. */
  178. public function saveProcessedEntitiesDataProvider()
  179. {
  180. $step = $this->createMock(\Migration\Step\Map\Data::class);
  181. $data = ['test_step' => ['dummy_array']];
  182. return [
  183. 'class' => [
  184. 'data' => $data,
  185. 'step' => $step,
  186. 'stage' => 'run1',
  187. 'processed' => ['table1'],
  188. 'saveData' => [
  189. 'test_step' => ['dummy_array'],
  190. get_class($step) => ['run1' => [\Migration\App\Progress::PROCESS_KEY => ['table1']]],
  191. ],
  192. ],
  193. 'string_name' => [
  194. 'data' => $data,
  195. 'step' => 'step',
  196. 'stage' => 'run2',
  197. 'processed' => ['table2'],
  198. 'saveData' => [
  199. 'test_step' => ['dummy_array'],
  200. 'step' => ['run2' => [\Migration\App\Progress::PROCESS_KEY => ['table2']]],
  201. ],
  202. ]
  203. ];
  204. }
  205. }