ButtonsTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Test\Unit\Config\Converter;
  7. use Magento\Ui\Config\Converter\Buttons;
  8. use Magento\Ui\Config\ConverterInterface;
  9. use Magento\Ui\Config\ConverterUtils;
  10. class ButtonsTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var Buttons
  14. */
  15. private $converter;
  16. /**
  17. * @var ConverterInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $urlConverter;
  20. public function setUp()
  21. {
  22. $this->urlConverter = $this->getMockBuilder(ConverterInterface::class)->getMockForAbstractClass();
  23. $this->converter = new Buttons($this->urlConverter, new ConverterUtils());
  24. }
  25. public function testConvert()
  26. {
  27. $dom = new \DOMDocument('1.0', 'UTF-8');
  28. $dom->load(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test.xml');
  29. $domXpath = new \DOMXPath($dom);
  30. $buttons = $domXpath->query('//listing/settings/buttons')->item(0);
  31. $url = $domXpath->query('//listing/settings/buttons/button[@name="button_2"]/url')->item(0);
  32. $urlResult = [
  33. 'name' => 'url',
  34. 'xsi:type' => 'url',
  35. 'path' => 'url',
  36. 'param' => [
  37. 'first' => [
  38. 'name' => 'first',
  39. 'value' => 'first_value'
  40. ],
  41. 'second' => [
  42. 'name' => 'second',
  43. 'value' => 'second_value'
  44. ],
  45. ],
  46. ];
  47. $this->urlConverter->expects($this->any())
  48. ->method('convert')
  49. ->with($url, ['type' => 'url'])
  50. ->willReturn($urlResult);
  51. $expectedResult = [
  52. 'name' => 'buttons',
  53. 'xsi:type' => 'array',
  54. 'item' => [
  55. 'button_1' => [
  56. 'name' => 'button_1',
  57. 'xsi:type' => 'string',
  58. 'value' => 'Some_Class',
  59. ],
  60. 'button_2' => [
  61. 'name' => 'button_2',
  62. 'xsi:type' => 'array',
  63. 'item' => [
  64. 'class' => [
  65. 'name' => 'class',
  66. 'xsi:type' => 'string',
  67. 'value' => 'css_class',
  68. ],
  69. 'label' => [
  70. 'name' => 'label',
  71. 'xsi:type' => 'string',
  72. 'translate' => 'true',
  73. 'value' => 'Label Button 2',
  74. ],
  75. 'url' => $urlResult,
  76. 'custom_param' => [
  77. 'name' => 'custom_param',
  78. 'xsi:type' => 'string',
  79. 'value' => 'custom_value'
  80. ],
  81. 'name' => [
  82. 'name' => 'name',
  83. 'xsi:type' => 'string',
  84. 'value' => 'button_2',
  85. ],
  86. ],
  87. ],
  88. ],
  89. ];
  90. $this->assertEquals($expectedResult, $this->converter->convert($buttons));
  91. }
  92. public function testConvertEmptyButtons()
  93. {
  94. $dom = new \DOMDocument('1.0', 'UTF-8');
  95. $dom->load(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'testForm.xml');
  96. $domXpath = new \DOMXPath($dom);
  97. $buttons = $domXpath->query('//form/settings/buttons')->item(0);
  98. $expectedResult = [
  99. 'name' => 'buttons',
  100. 'xsi:type' => 'array',
  101. 'item' => []
  102. ];
  103. $this->assertEquals($expectedResult, $this->converter->convert($buttons));
  104. }
  105. }