FieldTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Test\Unit\Component\Form;
  7. use Magento\Ui\Component\Form\Field;
  8. use Magento\Framework\View\Element\UiComponentFactory;
  9. use Magento\Framework\View\Element\UiComponentInterface;
  10. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  11. /**
  12. * Class FieldTest
  13. *
  14. * Test for class \Magento\Ui\Component\Form\Field
  15. */
  16. class FieldTest extends \PHPUnit\Framework\TestCase
  17. {
  18. const NAME = 'test-name';
  19. const COMPONENT_NAME = 'test-name';
  20. const COMPONENT_NAMESPACE = 'test-name';
  21. /**
  22. * @var Field
  23. */
  24. protected $field;
  25. /**
  26. * @var UiComponentFactory|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $uiComponentFactoryMock;
  29. /**
  30. * @var ContextInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $contextMock;
  33. /**
  34. * @var array
  35. */
  36. protected $testConfigData = [
  37. ['config', null, ['test-key' => 'test-value']],
  38. ['js_config', null, ['test-key' => 'test-value']]
  39. ];
  40. /**
  41. * Set up
  42. *
  43. * @return void
  44. */
  45. protected function setUp()
  46. {
  47. $this->uiComponentFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentFactory::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\ContextInterface::class)
  51. ->getMockForAbstractClass();
  52. $this->field = new Field(
  53. $this->contextMock,
  54. $this->uiComponentFactoryMock
  55. );
  56. }
  57. /**
  58. * Run test for prepare method
  59. *
  60. * @param array $data
  61. * @param array $expectedData
  62. * @return void
  63. *
  64. * @dataProvider prepareSuccessDataProvider
  65. */
  66. public function testPrepareSuccess(array $data, array $expectedData)
  67. {
  68. $processor = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\Processor::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->contextMock->expects($this->atLeastOnce())->method('getProcessor')->willReturn($processor);
  72. $this->uiComponentFactoryMock->expects($this->once())
  73. ->method('create')
  74. ->with(self::NAME, $data['config']['formElement'], $this->arrayHasKey('context'))
  75. ->willReturn($this->getWrappedComponentMock());
  76. $this->contextMock->expects($this->any())
  77. ->method('getNamespace')
  78. ->willReturn(self::COMPONENT_NAMESPACE);
  79. $this->field->setData($data);
  80. $this->field->prepare();
  81. $result = $this->field->getData();
  82. $this->assertEquals($expectedData, $result);
  83. }
  84. /**
  85. * Data provider for testPrepare
  86. *
  87. * @return array
  88. */
  89. public function prepareSuccessDataProvider()
  90. {
  91. return [
  92. [
  93. 'data' => [
  94. 'name' => self::NAME,
  95. 'config' => [
  96. 'formElement' => 'test',
  97. ]
  98. ],
  99. 'expectedData' => [
  100. 'name' => self::NAME,
  101. 'config' => [
  102. 'test-key' => 'test-value',
  103. ],
  104. 'js_config' => [
  105. 'extends' => self::NAME,
  106. 'test-key' => 'test-value',
  107. ]
  108. ]
  109. ],
  110. ];
  111. }
  112. /**
  113. * @return \PHPUnit_Framework_MockObject_MockObject|UiComponentInterface
  114. */
  115. protected function getWrappedComponentMock()
  116. {
  117. $wrappedComponentMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
  118. ->getMockForAbstractClass();
  119. $wrappedComponentMock->expects($this->any())
  120. ->method('getData')
  121. ->willReturnMap($this->testConfigData);
  122. $wrappedComponentMock->expects($this->once())
  123. ->method('setData')
  124. ->with('config', $this->logicalNot($this->isEmpty()));
  125. $wrappedComponentMock->expects($this->once())
  126. ->method('prepare');
  127. $wrappedComponentMock->expects($this->atLeastOnce())
  128. ->method('getChildComponents')
  129. ->willReturn($this->getComponentsMock());
  130. $wrappedComponentMock->expects($this->any())
  131. ->method('getComponentName')
  132. ->willReturn(self::COMPONENT_NAME);
  133. $wrappedComponentMock->expects($this->once())
  134. ->method('getContext')
  135. ->willReturn($this->contextMock);
  136. return $wrappedComponentMock;
  137. }
  138. /**
  139. * @return \PHPUnit_Framework_MockObject_MockObject[]|UiComponentInterface[]
  140. */
  141. protected function getComponentsMock()
  142. {
  143. $componentMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentInterface::class)
  144. ->getMockForAbstractClass();
  145. return [$componentMock];
  146. }
  147. /**
  148. * Run test prepare method (Exception)
  149. *
  150. * @return void
  151. *
  152. * @expectedException \Magento\Framework\Exception\LocalizedException
  153. * @expectedExceptionMessage The "formElement" configuration parameter is required for the "test-name" field.
  154. */
  155. public function testPrepareException()
  156. {
  157. $this->contextMock->expects($this->never())->method('getProcessor');
  158. $this->uiComponentFactoryMock->expects($this->never())
  159. ->method('create');
  160. $this->field->setData(['name' => self::NAME]);
  161. $this->field->prepare();
  162. }
  163. }