FormTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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;
  7. use Magento\Framework\Api\Filter;
  8. use Magento\Framework\Api\FilterBuilder;
  9. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  10. use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;
  11. use Magento\Framework\View\Element\UiComponent\Processor;
  12. use Magento\Ui\Component\Form;
  13. class FormTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /** @var Form */
  16. protected $model;
  17. /** @var ContextInterface|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $contextMock;
  19. /** @var FilterBuilder|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $filterBuilderMock;
  21. protected function setUp()
  22. {
  23. $this->contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\ContextInterface::class)
  24. ->getMockForAbstractClass();
  25. $this->filterBuilderMock = $this->getMockBuilder(\Magento\Framework\Api\FilterBuilder::class)
  26. ->disableOriginalConstructor()
  27. ->getMock();
  28. $this->contextMock->expects($this->never())->method('getProcessor');
  29. $this->model = new Form(
  30. $this->contextMock,
  31. $this->filterBuilderMock
  32. );
  33. }
  34. public function testGetComponentName()
  35. {
  36. $this->assertEquals(Form::NAME, $this->model->getComponentName());
  37. }
  38. public function testGetDataSourceData()
  39. {
  40. $requestFieldName = 'request_id';
  41. $primaryFieldName = 'primary_id';
  42. $fieldId = 44;
  43. $row = ['key' => 'value'];
  44. $data = [
  45. $fieldId => $row,
  46. ];
  47. $dataSource = [
  48. 'data' => $row,
  49. ];
  50. /** @var DataProviderInterface|\PHPUnit_Framework_MockObject_MockObject $dataProviderMock */
  51. $dataProviderMock =
  52. $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class)
  53. ->getMock();
  54. $dataProviderMock->expects($this->once())
  55. ->method('getRequestFieldName')
  56. ->willReturn($requestFieldName);
  57. $dataProviderMock->expects($this->once())
  58. ->method('getPrimaryFieldName')
  59. ->willReturn($primaryFieldName);
  60. $this->contextMock->expects($this->any())
  61. ->method('getDataProvider')
  62. ->willReturn($dataProviderMock);
  63. $this->contextMock->expects($this->once())
  64. ->method('getRequestParam')
  65. ->with($requestFieldName)
  66. ->willReturn($fieldId);
  67. /** @var Filter|\PHPUnit_Framework_MockObject_MockObject $filterMock */
  68. $filterMock = $this->getMockBuilder(\Magento\Framework\Api\Filter::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->filterBuilderMock->expects($this->once())
  72. ->method('setField')
  73. ->with($primaryFieldName)
  74. ->willReturnSelf();
  75. $this->filterBuilderMock->expects($this->once())
  76. ->method('setValue')
  77. ->with($fieldId)
  78. ->willReturnSelf();
  79. $this->filterBuilderMock->expects($this->once())
  80. ->method('create')
  81. ->willReturn($filterMock);
  82. $dataProviderMock->expects($this->once())
  83. ->method('addFilter')
  84. ->with($filterMock);
  85. $dataProviderMock->expects($this->once())
  86. ->method('getData')
  87. ->willReturn($data);
  88. $this->assertEquals($dataSource, $this->model->getDataSourceData());
  89. }
  90. public function testGetDataSourceDataWithoutData()
  91. {
  92. $requestFieldName = 'request_id';
  93. $primaryFieldName = 'primary_id';
  94. $fieldId = 44;
  95. $data = [];
  96. $dataSource = [];
  97. /** @var DataProviderInterface|\PHPUnit_Framework_MockObject_MockObject $dataProviderMock */
  98. $dataProviderMock =
  99. $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class)
  100. ->getMock();
  101. $dataProviderMock->expects($this->once())
  102. ->method('getRequestFieldName')
  103. ->willReturn($requestFieldName);
  104. $dataProviderMock->expects($this->once())
  105. ->method('getPrimaryFieldName')
  106. ->willReturn($primaryFieldName);
  107. $this->contextMock->expects($this->any())
  108. ->method('getDataProvider')
  109. ->willReturn($dataProviderMock);
  110. $this->contextMock->expects($this->once())
  111. ->method('getRequestParam')
  112. ->with($requestFieldName)
  113. ->willReturn($fieldId);
  114. /** @var Filter|\PHPUnit_Framework_MockObject_MockObject $filterMock */
  115. $filterMock = $this->getMockBuilder(\Magento\Framework\Api\Filter::class)
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $this->filterBuilderMock->expects($this->once())
  119. ->method('setField')
  120. ->with($primaryFieldName)
  121. ->willReturnSelf();
  122. $this->filterBuilderMock->expects($this->once())
  123. ->method('setValue')
  124. ->with($fieldId)
  125. ->willReturnSelf();
  126. $this->filterBuilderMock->expects($this->once())
  127. ->method('create')
  128. ->willReturn($filterMock);
  129. $dataProviderMock->expects($this->once())
  130. ->method('addFilter')
  131. ->with($filterMock);
  132. $dataProviderMock->expects($this->once())
  133. ->method('getData')
  134. ->willReturn($data);
  135. $this->assertEquals($dataSource, $this->model->getDataSourceData());
  136. }
  137. public function testGetDataSourceDataWithoutId()
  138. {
  139. $requestFieldName = 'request_id';
  140. $primaryFieldName = 'primary_id';
  141. $fieldId = null;
  142. $row = ['key' => 'value'];
  143. $data = [
  144. $fieldId => $row,
  145. ];
  146. $dataSource = [
  147. 'data' => $row,
  148. ];
  149. /** @var DataProviderInterface|\PHPUnit_Framework_MockObject_MockObject $dataProviderMock */
  150. $dataProviderMock =
  151. $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class)
  152. ->getMock();
  153. $dataProviderMock->expects($this->once())
  154. ->method('getRequestFieldName')
  155. ->willReturn($requestFieldName);
  156. $dataProviderMock->expects($this->once())
  157. ->method('getPrimaryFieldName')
  158. ->willReturn($primaryFieldName);
  159. $this->contextMock->expects($this->any())
  160. ->method('getDataProvider')
  161. ->willReturn($dataProviderMock);
  162. $this->contextMock->expects($this->once())
  163. ->method('getRequestParam')
  164. ->with($requestFieldName)
  165. ->willReturn($fieldId);
  166. /** @var Filter|\PHPUnit_Framework_MockObject_MockObject $filterMock */
  167. $filterMock = $this->getMockBuilder(\Magento\Framework\Api\Filter::class)
  168. ->disableOriginalConstructor()
  169. ->getMock();
  170. $this->filterBuilderMock->expects($this->once())
  171. ->method('setField')
  172. ->with($primaryFieldName)
  173. ->willReturnSelf();
  174. $this->filterBuilderMock->expects($this->once())
  175. ->method('setValue')
  176. ->with($fieldId)
  177. ->willReturnSelf();
  178. $this->filterBuilderMock->expects($this->once())
  179. ->method('create')
  180. ->willReturn($filterMock);
  181. $dataProviderMock->expects($this->once())
  182. ->method('addFilter')
  183. ->with($filterMock);
  184. $dataProviderMock->expects($this->once())
  185. ->method('getData')
  186. ->willReturn($data);
  187. $this->assertEquals($dataSource, $this->model->getDataSourceData());
  188. }
  189. }