Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ToModelTest.php
Go to the documentation of this file.
1 <?php
7 
8 class ToModelTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $ruleFactory;
14 
19 
23  protected $model;
24 
25  protected function setUp()
26  {
27  $this->ruleFactory = $this->getMockBuilder(\Magento\SalesRule\Model\RuleFactory::class)
28  ->disableOriginalConstructor()
29  ->setMethods(['create'])
30  ->getMock();
31 
32  $this->dataObjectProcessor = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
33  ->disableOriginalConstructor()
34  ->setMethods([])
35  ->getMock();
36 
37  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
38  $this->model = $helper->getObject(
39  \Magento\SalesRule\Model\Converter\ToModel::class,
40  [
41  'ruleFactory' => $this->ruleFactory,
42  'dataObjectProcessor' => $this->dataObjectProcessor,
43  ]
44  );
45  }
46 
47  public function testDataModelToArray()
48  {
49  $array = [
50  'type' => 'conditionType',
51  'value' => 'value',
52  'attribute' => 'getAttributeName',
53  'operator' => 'getOperator',
54  'aggregator' => 'getAggregatorType',
55  'conditions' => [
56  [
57  'type' => null,
58  'value' => null,
59  'attribute' => null,
60  'operator' => null,
61  ],
62  [
63  'type' => null,
64  'value' => null,
65  'attribute' => null,
66  'operator' => null,
67  ],
68  ],
69  ];
70 
74  $dataCondition = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Condition::class)
75  ->disableOriginalConstructor()
76  ->setMethods(['create', 'load', 'getConditionType', 'getValue', 'getAttributeName', 'getOperator',
77  'getAggregatorType', 'getConditions'])
78  ->getMock();
79 
80  $dataCondition
81  ->expects($this->atLeastOnce())
82  ->method('getConditionType')
83  ->willReturn('conditionType');
84 
85  $dataCondition
86  ->expects($this->atLeastOnce())
87  ->method('getValue')
88  ->willReturn('value');
89 
90  $dataCondition
91  ->expects($this->atLeastOnce())
92  ->method('getAttributeName')
93  ->willReturn('getAttributeName');
94 
95  $dataCondition
96  ->expects($this->atLeastOnce())
97  ->method('getOperator')
98  ->willReturn('getOperator');
99 
100  $dataCondition
101  ->expects($this->atLeastOnce())
102  ->method('getAggregatorType')
103  ->willReturn('getAggregatorType');
104 
105  $dataCondition1 = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Condition::class)
106  ->disableOriginalConstructor()
107  ->setMethods(['create', 'load', 'getConditionType', 'getValue', 'getAttributeName', 'getOperator',
108  'getAggregatorType', 'getConditions'])
109  ->getMock();
110 
111  $dataCondition2 = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Condition::class)
112  ->disableOriginalConstructor()
113  ->setMethods(['create', 'load', 'getConditionType', 'getValue', 'getAttributeName', 'getOperator',
114  'getAggregatorType', 'getConditions'])
115  ->getMock();
116 
117  $dataCondition
118  ->expects($this->atLeastOnce())
119  ->method('getConditions')
120  ->willReturn([$dataCondition1, $dataCondition2]);
121 
122  $result = $this->model->dataModelToArray($dataCondition);
123 
124  $this->assertEquals($array, $result);
125  }
126 
127  public function testToModel()
128  {
132  $dataModel = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Rule::class)
133  ->disableOriginalConstructor()
134  ->setMethods(['create', 'load', 'getData', 'getRuleId', 'getCondition', 'getActionCondition',
135  'getStoreLabels'])
136  ->getMock();
137  $dataModel
138  ->expects($this->atLeastOnce())
139  ->method('getRuleId')
140  ->willReturn(1);
141 
142  $dataModel
143  ->expects($this->atLeastOnce())
144  ->method('getCondition')
145  ->willReturn(false);
146 
147  $dataModel
148  ->expects($this->atLeastOnce())
149  ->method('getActionCondition')
150  ->willReturn(false);
151 
152  $dataModel
153  ->expects($this->atLeastOnce())
154  ->method('getStoreLabels')
155  ->willReturn([]);
156 
157  $ruleModel = $this->getMockBuilder(\Magento\SalesRule\Model\Rule::class)
158  ->disableOriginalConstructor()
159  ->setMethods(['create', 'load', 'getId', 'getData'])
160  ->getMock();
161 
162  $ruleModel
163  ->expects($this->atLeastOnce())
164  ->method('load')
165  ->willReturn($ruleModel);
166  $ruleModel
167  ->expects($this->atLeastOnce())
168  ->method('getId')
169  ->willReturn(1);
170 
171  $ruleModel
172  ->expects($this->atLeastOnce())
173  ->method('getData')
174  ->willReturn(['data_1'=>1]);
175 
176  $this->dataObjectProcessor
177  ->expects($this->any())
178  ->method('buildOutputDataArray')
179  ->willReturn(['data_2'=>2]);
180 
181  $this->ruleFactory
182  ->expects($this->any())
183  ->method('create')
184  ->willReturn($ruleModel);
185 
186  $result = $this->model->toModel($dataModel);
187  $this->assertEquals($ruleModel, $result);
188  }
189 
193  public function testFormattingDate($data)
194  {
198  $dataModel = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Rule::class)
199  ->disableOriginalConstructor()
200  ->setMethods(
201  [
202  'create',
203  'load',
204  'getData',
205  'getRuleId',
206  'getCondition',
207  'getActionCondition',
208  'getStoreLabels',
209  'getFromDate',
210  'setFromDate',
211  'getToDate',
212  'setToDate',
213  ]
214  )
215  ->getMock();
216  $dataModel
217  ->expects($this->atLeastOnce())
218  ->method('getRuleId')
219  ->willReturn(null);
220 
221  $dataModel
222  ->expects($this->atLeastOnce())
223  ->method('getCondition')
224  ->willReturn(false);
225 
226  $dataModel
227  ->expects($this->atLeastOnce())
228  ->method('getActionCondition')
229  ->willReturn(false);
230  $dataModel
231  ->expects($this->atLeastOnce())
232  ->method('getStoreLabels')
233  ->willReturn([]);
234  $ruleModel = $this->getMockBuilder(\Magento\SalesRule\Model\Rule::class)
235  ->disableOriginalConstructor()
236  ->setMethods(['create', 'load', 'getId', 'getData'])
237  ->getMock();
238  $ruleModel
239  ->expects($this->atLeastOnce())
240  ->method('getData')
241  ->willReturn(['data_1'=>1]);
242 
243  $this->dataObjectProcessor
244  ->expects($this->any())
245  ->method('buildOutputDataArray')
246  ->willReturn(['data_2'=>2]);
247 
248  $this->ruleFactory
249  ->expects($this->any())
250  ->method('create')
251  ->willReturn($ruleModel);
252 
253  $dataModel
254  ->expects($this->atLeastOnce())
255  ->method('getFromDate')
256  ->willReturn($data['from_date']);
257 
258  $dataModel
259  ->expects($this->atLeastOnce())
260  ->method('getToDate')
261  ->willReturn($data['to_date']);
262 
263  $dataModel
264  ->expects($this->atLeastOnce())
265  ->method('setFromDate')
266  ->with($data['expected_from_date']);
267 
268  $dataModel
269  ->expects($this->atLeastOnce())
270  ->method('setToDate')
271  ->with($data['expected_to_date']);
272 
273  $this->model->toModel($dataModel);
274  }
275 
279  public function expectedDatesProvider()
280  {
281  return [
282  'mm/dd/yyyy to yyyy-mm-dd' => [
283  [
284  'from_date' => '03/24/2016',
285  'to_date' => '03/25/2016',
286  'expected_from_date' => '2016-03-24T00:00:00-0700',
287  'expected_to_date' => '2016-03-25T00:00:00-0700',
288  ]
289  ],
290  'yyyy-mm-dd to yyyy-mm-dd' => [
291  [
292  'from_date' => '2016-03-24',
293  'to_date' => '2016-03-25',
294  'expected_from_date' => '2016-03-24T00:00:00-0700',
295  'expected_to_date' => '2016-03-25T00:00:00-0700',
296  ]
297  ],
298  'yymmdd to yyyy-mm-dd' => [
299  [
300  'from_date' => '20160324',
301  'to_date' => '20160325',
302  'expected_from_date' => '2016-03-24T00:00:00-0700',
303  'expected_to_date' => '2016-03-25T00:00:00-0700',
304  ]
305  ],
306  ];
307  }
308 }
$helper
Definition: iframe.phtml:13