Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FormTest.php
Go to the documentation of this file.
1 <?php
7 
14 
15 class FormTest extends \PHPUnit\Framework\TestCase
16 {
18  protected $model;
19 
21  protected $contextMock;
22 
24  protected $filterBuilderMock;
25 
26  protected function setUp()
27  {
28  $this->contextMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\ContextInterface::class)
29  ->getMockForAbstractClass();
30  $this->filterBuilderMock = $this->getMockBuilder(\Magento\Framework\Api\FilterBuilder::class)
31  ->disableOriginalConstructor()
32  ->getMock();
33 
34  $this->contextMock->expects($this->never())->method('getProcessor');
35 
36  $this->model = new Form(
37  $this->contextMock,
38  $this->filterBuilderMock
39  );
40  }
41 
42  public function testGetComponentName()
43  {
44  $this->assertEquals(Form::NAME, $this->model->getComponentName());
45  }
46 
47  public function testGetDataSourceData()
48  {
49  $requestFieldName = 'request_id';
50  $primaryFieldName = 'primary_id';
51  $fieldId = 44;
52  $row = ['key' => 'value'];
53  $data = [
54  $fieldId => $row,
55  ];
56  $dataSource = [
57  'data' => $row,
58  ];
59 
61  $dataProviderMock =
62  $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class)
63  ->getMock();
64  $dataProviderMock->expects($this->once())
65  ->method('getRequestFieldName')
66  ->willReturn($requestFieldName);
67  $dataProviderMock->expects($this->once())
68  ->method('getPrimaryFieldName')
69  ->willReturn($primaryFieldName);
70 
71  $this->contextMock->expects($this->any())
72  ->method('getDataProvider')
73  ->willReturn($dataProviderMock);
74  $this->contextMock->expects($this->once())
75  ->method('getRequestParam')
76  ->with($requestFieldName)
77  ->willReturn($fieldId);
78 
80  $filterMock = $this->getMockBuilder(\Magento\Framework\Api\Filter::class)
81  ->disableOriginalConstructor()
82  ->getMock();
83 
84  $this->filterBuilderMock->expects($this->once())
85  ->method('setField')
86  ->with($primaryFieldName)
87  ->willReturnSelf();
88  $this->filterBuilderMock->expects($this->once())
89  ->method('setValue')
90  ->with($fieldId)
91  ->willReturnSelf();
92  $this->filterBuilderMock->expects($this->once())
93  ->method('create')
94  ->willReturn($filterMock);
95 
96  $dataProviderMock->expects($this->once())
97  ->method('addFilter')
98  ->with($filterMock);
99  $dataProviderMock->expects($this->once())
100  ->method('getData')
101  ->willReturn($data);
102 
103  $this->assertEquals($dataSource, $this->model->getDataSourceData());
104  }
105 
106  public function testGetDataSourceDataWithoutData()
107  {
108  $requestFieldName = 'request_id';
109  $primaryFieldName = 'primary_id';
110  $fieldId = 44;
111  $data = [];
112  $dataSource = [];
113 
115  $dataProviderMock =
116  $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class)
117  ->getMock();
118  $dataProviderMock->expects($this->once())
119  ->method('getRequestFieldName')
120  ->willReturn($requestFieldName);
121  $dataProviderMock->expects($this->once())
122  ->method('getPrimaryFieldName')
123  ->willReturn($primaryFieldName);
124 
125  $this->contextMock->expects($this->any())
126  ->method('getDataProvider')
127  ->willReturn($dataProviderMock);
128  $this->contextMock->expects($this->once())
129  ->method('getRequestParam')
130  ->with($requestFieldName)
131  ->willReturn($fieldId);
132 
134  $filterMock = $this->getMockBuilder(\Magento\Framework\Api\Filter::class)
135  ->disableOriginalConstructor()
136  ->getMock();
137 
138  $this->filterBuilderMock->expects($this->once())
139  ->method('setField')
140  ->with($primaryFieldName)
141  ->willReturnSelf();
142  $this->filterBuilderMock->expects($this->once())
143  ->method('setValue')
144  ->with($fieldId)
145  ->willReturnSelf();
146  $this->filterBuilderMock->expects($this->once())
147  ->method('create')
148  ->willReturn($filterMock);
149 
150  $dataProviderMock->expects($this->once())
151  ->method('addFilter')
152  ->with($filterMock);
153  $dataProviderMock->expects($this->once())
154  ->method('getData')
155  ->willReturn($data);
156 
157  $this->assertEquals($dataSource, $this->model->getDataSourceData());
158  }
159 
160  public function testGetDataSourceDataWithoutId()
161  {
162  $requestFieldName = 'request_id';
163  $primaryFieldName = 'primary_id';
164  $fieldId = null;
165  $row = ['key' => 'value'];
166  $data = [
167  $fieldId => $row,
168  ];
169  $dataSource = [
170  'data' => $row,
171  ];
172 
174  $dataProviderMock =
175  $this->getMockBuilder(\Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface::class)
176  ->getMock();
177  $dataProviderMock->expects($this->once())
178  ->method('getRequestFieldName')
179  ->willReturn($requestFieldName);
180  $dataProviderMock->expects($this->once())
181  ->method('getPrimaryFieldName')
182  ->willReturn($primaryFieldName);
183 
184  $this->contextMock->expects($this->any())
185  ->method('getDataProvider')
186  ->willReturn($dataProviderMock);
187  $this->contextMock->expects($this->once())
188  ->method('getRequestParam')
189  ->with($requestFieldName)
190  ->willReturn($fieldId);
191 
193  $filterMock = $this->getMockBuilder(\Magento\Framework\Api\Filter::class)
194  ->disableOriginalConstructor()
195  ->getMock();
196 
197  $this->filterBuilderMock->expects($this->once())
198  ->method('setField')
199  ->with($primaryFieldName)
200  ->willReturnSelf();
201  $this->filterBuilderMock->expects($this->once())
202  ->method('setValue')
203  ->with($fieldId)
204  ->willReturnSelf();
205  $this->filterBuilderMock->expects($this->once())
206  ->method('create')
207  ->willReturn($filterMock);
208 
209  $dataProviderMock->expects($this->once())
210  ->method('addFilter')
211  ->with($filterMock);
212  $dataProviderMock->expects($this->once())
213  ->method('getData')
214  ->willReturn($data);
215 
216  $this->assertEquals($dataSource, $this->model->getDataSourceData());
217  }
218 }
$fieldId
Definition: element.phtml:16