SettingsTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Migration\Reader;
  7. /**
  8. * Class SettingsTest
  9. */
  10. class SettingsTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var Settings
  14. */
  15. protected $settings;
  16. /**
  17. * @return void
  18. */
  19. public function setUp()
  20. {
  21. $config = $this->getConfigFile('tests/unit/testsuite/Migration/_files/settings.xml');
  22. $validationState = $this->getMockBuilder(\Magento\Framework\App\Arguments\ValidationState::class)
  23. ->disableOriginalConstructor()
  24. ->setMethods(['isValidationRequired'])
  25. ->getMock();
  26. $validationState->expects($this->any())->method('isValidationRequired')->willReturn(true);
  27. $this->settings = new Settings($config, $validationState);
  28. }
  29. /**
  30. * @param string $configPath
  31. * @return \Migration\Config|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected function getConfigFile($configPath)
  34. {
  35. /** @var \Migration\Config|\PHPUnit_Framework_MockObject_MockObject $config */
  36. $config = $this->getMockBuilder(\Migration\Config::class)->disableOriginalConstructor()
  37. ->setMethods(['getOption'])->getMock();
  38. $config->expects($this->once())->method('getOption')->with('settings_map_file')->will(
  39. $this->returnValue($configPath)
  40. );
  41. return $config;
  42. }
  43. /**
  44. * @return array
  45. */
  46. public function dataProviderNodesIgnore()
  47. {
  48. return [
  49. ['node' => '', 'isIgnored' => false],
  50. ['node' => 'path/to/ignore/is/here', 'isIgnored' => true],
  51. ['node' => 'exact/path/to/ignore', 'isIgnored' => true],
  52. ['node' => 'exact/path/to/ignore/dummy', 'isIgnored' => false],
  53. ['node' => 'dummy/path', 'isIgnored' => false]
  54. ];
  55. }
  56. /**
  57. * @param string $node
  58. * @param bool $isIgnored
  59. * @dataProvider dataProviderNodesIgnore
  60. * @return void
  61. */
  62. public function testIsNodeIgnored($node, $isIgnored)
  63. {
  64. $result = $this->settings->isNodeIgnored($node);
  65. $this->assertEquals($isIgnored, $result);
  66. $result = $this->settings->isNodeIgnored($node);
  67. $this->assertEquals($isIgnored, $result);
  68. }
  69. /**
  70. * @return array
  71. */
  72. public function dataProviderNodeIsMapped()
  73. {
  74. return [
  75. ['node' => '', 'isMapped' => false],
  76. ['node' => 'path/to/rename', 'isMapped' => true],
  77. ['node' => 'renamed/path/to/ignore', 'isMapped' => false],
  78. ['node' => 'some/dummy/path', 'isMapped' => false]
  79. ];
  80. }
  81. /**
  82. * @param string $node
  83. * @param bool $isMapped
  84. * @dataProvider dataProviderNodeIsMapped
  85. * @return void
  86. */
  87. public function testIsNodeMapped($node, $isMapped)
  88. {
  89. $result = $this->settings->isNodeMapped($node);
  90. $this->assertEquals($isMapped, $result);
  91. $result = $this->settings->isNodeMapped($node);
  92. $this->assertEquals($isMapped, $result);
  93. }
  94. /**
  95. * @return array
  96. */
  97. public function dataProviderNodesMap()
  98. {
  99. return [
  100. ['node' => '', 'nodeMap' => ''],
  101. ['node' => 'path/to/rename', 'nodeMap' => 'new/path/renamed'],
  102. ['node' => 'renamed/path/to/ignore', 'nodeMap' => 'renamed/path/to/ignore'],
  103. ['node' => 'some/dummy/path', 'nodeMap' => 'some/dummy/path']
  104. ];
  105. }
  106. /**
  107. * @param string $node
  108. * @param string $nodeMap
  109. * @dataProvider dataProviderNodesMap
  110. * @return void
  111. */
  112. public function testGetNodeMap($node, $nodeMap)
  113. {
  114. $result = $this->settings->getNodeMap($node);
  115. $this->assertEquals($nodeMap, $result);
  116. $result = $this->settings->getNodeMap($node);
  117. $this->assertEquals($nodeMap, $result);
  118. }
  119. /**
  120. * @return array
  121. */
  122. public function dataProviderValueHandler()
  123. {
  124. return [
  125. ['node' => '', 'valueHandler' => false],
  126. ['node' => 'some/key/to/change', 'valueHandler' => ['class' => 'Some\Handler\Class', 'params' => []]],
  127. ['node' => 'handled/path/to/ignore', 'valueHandler' => false],
  128. ['node' => 'some/dummy/path', 'valueHandler' => false]
  129. ];
  130. }
  131. /**
  132. * @param string $node
  133. * @param string $valueHandler
  134. * @dataProvider dataProviderValueHandler
  135. * @return void
  136. */
  137. public function testGetValueHandler($node, $valueHandler)
  138. {
  139. $result = $this->settings->getValueHandler($node);
  140. $this->assertEquals($valueHandler, $result);
  141. $result = $this->settings->getValueHandler($node);
  142. $this->assertEquals($valueHandler, $result);
  143. }
  144. /**
  145. * @return void
  146. */
  147. public function testNoConfigFile()
  148. {
  149. $config = $this->getConfigFile('invalid_file_name');
  150. $this->expectException(\Migration\Exception::class);
  151. $this->expectExceptionMessage('Invalid map filename:');
  152. $validationState = $this->getMockBuilder(\Magento\Framework\App\Arguments\ValidationState::class)
  153. ->disableOriginalConstructor()
  154. ->setMethods(['isValidationRequired'])
  155. ->getMock();
  156. $validationState->expects($this->any())->method('isValidationRequired')->willReturn(true);
  157. new Settings($config, $validationState);
  158. }
  159. /**
  160. * @return void
  161. */
  162. public function testInvalidConfigFile()
  163. {
  164. $config = $this->getConfigFile('tests/unit/testsuite/Migration/_files/settings-invalid.xml');
  165. $this->expectException(\Migration\Exception::class);
  166. $this->expectExceptionMessage('XML file is invalid.');
  167. $validationState = $this->getMockBuilder(\Magento\Framework\App\Arguments\ValidationState::class)
  168. ->disableOriginalConstructor()
  169. ->setMethods(['isValidationRequired'])
  170. ->getMock();
  171. $validationState->expects($this->any())->method('isValidationRequired')->willReturn(true);
  172. new Settings($config, $validationState);
  173. }
  174. }