ConverterTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Config;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. use Magento\Framework\Config\FileIterator;
  9. use Magento\Framework\Filesystem\DriverPool;
  10. use Magento\Framework\Filesystem\File\ReadFactory;
  11. class ConverterTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var Converter
  15. */
  16. private $converter;
  17. /**
  18. * @var string
  19. */
  20. private $fixturePath;
  21. public function setUp()
  22. {
  23. $objectManager = Bootstrap::getObjectManager();
  24. $this->converter = $objectManager->create(Converter::class);
  25. $this->fixturePath = realpath(__DIR__ . '/../_files/view/ui_component');
  26. }
  27. /**
  28. * @param string $componentName
  29. * @return void
  30. * @dataProvider getComponentNameDataProvider
  31. */
  32. public function testConvert($componentName)
  33. {
  34. $expectedResult = $this->getExpectedResult($componentName);
  35. $fixtureFiles = $this->getFixtureFiles($componentName);
  36. foreach ($fixtureFiles as $filePath => $fileContent) {
  37. $dom = new \DOMDocument();
  38. $dom->loadXML($fileContent);
  39. $actualResult = $this->converter->convert($dom);
  40. if (isset($actualResult[Converter::DATA_ATTRIBUTES_KEY])) {
  41. unset($actualResult[Converter::DATA_ATTRIBUTES_KEY]);
  42. }
  43. $this->assertEquals(
  44. $expectedResult,
  45. $actualResult,
  46. "Wrong '{$this->getTypeByPath($filePath)}' configuration for '{$componentName}' Ui Component" . PHP_EOL
  47. );
  48. }
  49. }
  50. public function getComponentNameDataProvider()
  51. {
  52. return [
  53. ['action'],
  54. ['actionDelete'],
  55. ['actions'],
  56. ['actionsColumn'],
  57. ['bookmark'],
  58. ['boolean'],
  59. ['button'],
  60. ['checkbox'],
  61. ['checkboxset'],
  62. ['column'],
  63. ['columns'],
  64. ['columnsControls'],
  65. ['component'],
  66. ['dataSource'],
  67. ['date'],
  68. ['dynamicRows'],
  69. ['email'],
  70. ['exportButton'],
  71. ['field'],
  72. ['fieldset'],
  73. ['file'],
  74. ['fileUploader'],
  75. ['filterDate'],
  76. ['filterInput'],
  77. ['filterRange'],
  78. ['filters'],
  79. ['form'],
  80. ['hidden'],
  81. ['htmlContent'],
  82. ['imageUploader'],
  83. ['input'],
  84. ['insertForm'],
  85. ['insertListing'],
  86. ['listing'],
  87. ['listingToolbar'],
  88. ['massaction'],
  89. ['modal'],
  90. ['multiline'],
  91. ['multiselect'],
  92. ['paging'],
  93. ['radioset'],
  94. ['range'],
  95. ['select'],
  96. ['selectionsColumn'],
  97. ['tab'],
  98. ['text'],
  99. ['textarea'],
  100. ['wysiwyg'],
  101. ];
  102. }
  103. /**
  104. * Retrieve fixture files by $componentName
  105. *
  106. * @param string $componentName
  107. * @return FileIterator
  108. */
  109. private function getFixtureFiles($componentName)
  110. {
  111. $realPaths = [];
  112. foreach (['semantic', 'mixed', 'arbitrary'] as $filePath) {
  113. $realPaths[] = $this->fixturePath . '/' . $filePath . '/' . $componentName . '.xml';
  114. }
  115. return new FileIterator(new ReadFactory(new DriverPool), $realPaths);
  116. }
  117. /**
  118. * Retrieve expected result by $componentName
  119. *
  120. * @param string $componentName
  121. * @return array
  122. */
  123. private function getExpectedResult($componentName)
  124. {
  125. $filename = $this->fixturePath . '/expected/' . $componentName . '.php';
  126. if (is_file($filename)) {
  127. return include($filename);
  128. }
  129. return [];
  130. }
  131. /**
  132. * Retrieve fixture type by file path
  133. *
  134. * @param string $path
  135. * @return string
  136. */
  137. private function getTypeByPath($path)
  138. {
  139. $result = '';
  140. $pos = strpos($path, $this->fixturePath);
  141. if ($pos !== false) {
  142. $restParts = explode('/', substr($path, strlen($this->fixturePath) + 1));
  143. $result = array_shift($restParts);
  144. }
  145. return $result;
  146. }
  147. }