Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ToDataModelTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\SalesRule\Api\Data\RuleExtensionFactory;
9 use Magento\SalesRule\Api\Data\RuleExtensionInterface;
10 
14 class ToDataModelTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $ruleFactory;
20 
24  protected $ruleDataFactory;
25 
30 
35 
39  protected $ruleLabelFactory;
40 
44  protected $salesRule;
45 
49  protected $model;
50 
54  protected $serializer;
55 
59  private $extensionFactoryMock;
60 
61  protected function setUp()
62  {
63  $this->ruleFactory = $this->getMockBuilder(\Magento\SalesRule\Model\RuleFactory::class)
64  ->disableOriginalConstructor()
65  ->setMethods([])
66  ->getMock();
67 
68  $this->ruleDataFactory = $this->getMockBuilder(\Magento\SalesRule\Api\Data\RuleInterfaceFactory::class)
69  ->disableOriginalConstructor()
70  ->setMethods(['create'])
71  ->getMock();
72 
73  $this->conditionDataFactory = $this->getMockBuilder(
74  \Magento\SalesRule\Api\Data\ConditionInterfaceFactory::class
75  )->disableOriginalConstructor()
76  ->setMethods(['create'])
77  ->getMock();
78 
79  $this->dataObjectProcessor = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
80  ->disableOriginalConstructor()
81  ->setMethods([])
82  ->getMock();
83 
84  $this->ruleLabelFactory = $this->getMockBuilder(\Magento\SalesRule\Api\Data\RuleLabelInterfaceFactory::class)
85  ->disableOriginalConstructor()
86  ->setMethods(['create'])
87  ->getMock();
88 
89  $this->salesRule = $this->getMockBuilder(\Magento\SalesRule\Model\Rule::class)
90  ->disableOriginalConstructor()
91  ->setMethods(['_construct', 'getData', 'getConditionsSerialized', 'getActionsSerialized'])
92  ->getMock();
93 
94  $this->serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
95  ->setMethods(null)
96  ->getMock();
97 
98  $this->extensionFactoryMock = $this->getMockBuilder(RuleExtensionFactory::class)
99  ->setMethods(['create'])
100  ->disableOriginalConstructor()
101  ->getMock();
102 
103  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
104  $this->model = $helper->getObject(
105  \Magento\SalesRule\Model\Converter\ToDataModel::class,
106  [
107  'ruleFactory' => $this->ruleFactory,
108  'ruleDataFactory' => $this->ruleDataFactory,
109  'conditionDataFactory' => $this->conditionDataFactory,
110  'ruleLabelFactory' => $this->ruleLabelFactory,
111  'dataObjectProcessor' => $this->dataObjectProcessor,
112  'serializer' => $this->serializer,
113  'extensionFactory' => $this->extensionFactoryMock,
114  ]
115  );
116  }
117 
121  private function getArrayData()
122  {
123  return [
124  'rule_id' => '1',
125  'name' => 'testrule',
126  'is_active' => '1',
127  'conditions_serialized' => json_encode([
128  'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
129  'attribute' => null,
130  'operator' => null,
131  'value' => '1',
132  'is_value_processed' => null,
133  'aggregator' => 'all',
134  'conditions' => [
135  [
136  'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
137  'attribute' => 'base_subtotal',
138  'operator' => '>=',
139  'value' => '100',
140  'is_value_processed' => false,
141  ],
142  ],
143  ]),
144  'actions_serialized' => json_encode([
145  'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Combine::class,
146  'attribute' => null,
147  'operator' => null,
148  'value' => '1',
149  'is_value_processed' => null,
150  'aggregator' => 'all',
151  'conditions' => [
152  [
153  'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
154  'attribute' => 'attribute_set_id',
155  'operator' => '==',
156  'value' => '4',
157  'is_value_processed' => false,
158  ],
159  ],
160  ]),
161  'coupon_type' => '1',
162  'coupon_code' => '',
163  'store_labels' => [
164  0 => 'TestRule',
165  1 => 'TestRuleForDefaultStore',
166  ],
167  'extension_attributes' => [
168  'some_extension_attributes' => 123,
169  ],
170  ];
171  }
172 
173  public function testToDataModel()
174  {
175  $array = $this->getArrayData();
176  $arrayAttributes = $array;
177 
179  $attributesMock = $this->getMockBuilder(RuleExtensionInterface::class)
180  ->getMock();
181  $arrayAttributes['extension_attributes'] = $attributesMock;
182 
183  $this->extensionFactoryMock->expects($this->any())
184  ->method('create')
185  ->with(['data' => $array['extension_attributes']])
186  ->willReturn($attributesMock);
187 
188  $dataModel = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Rule::class)
189  ->disableOriginalConstructor()
190  ->setMethods(['create', 'getStoreLabels', 'setStoreLabels', 'getCouponType', 'setCouponType'])
191  ->getMock();
192 
193  $dataLabel = $this->getMockBuilder(\Magento\SalesRule\Api\Data\RuleLabel::class)
194  ->setMethods(['setStoreId', 'setStoreLabel', 'setStoreLabels'])
195  ->disableOriginalConstructor()
196  ->getMock();
197 
198  $dataCondition = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Condition::class)
199  ->setMethods(['setData'])
200  ->disableOriginalConstructor()
201  ->getMock();
202 
203  $this->ruleLabelFactory
204  ->expects($this->any())
205  ->method('create')
206  ->willReturn($dataLabel);
207 
208  $this->conditionDataFactory
209  ->expects($this->any())
210  ->method('create')
211  ->willReturn($dataCondition);
212 
213  $this->ruleDataFactory
214  ->expects($this->any())
215  ->method('create')
216  ->with(['data' => $arrayAttributes])
217  ->willReturn($dataModel);
218 
219  $this->salesRule
220  ->expects($this->any())
221  ->method('getData')
222  ->willReturn($array);
223 
224  $this->salesRule
225  ->expects($this->once())
226  ->method('getConditionsSerialized')
227  ->willReturn($array['conditions_serialized']);
228 
229  $dataModel
230  ->expects($this->atLeastOnce())
231  ->method('getStoreLabels')
232  ->willReturn($array['store_labels']);
233 
234  $dataModel
235  ->expects($this->atLeastOnce())
236  ->method('setStoreLabels');
237 
238  $dataModel
239  ->expects($this->atLeastOnce())
240  ->method('getCouponType')
241  ->willReturn(\Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON);
242 
243  $dataModel
244  ->expects($this->atLeastOnce())
245  ->method('setCouponType');
246 
247  $return = $this->model->toDataModel($this->salesRule);
248 
249  $this->assertSame($dataModel, $return);
250  }
251 
253  {
254 
255  $array=[
256  'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
257  'attribute' => null,
258  'operator' => null,
259  'value' => 1,
260  'is_value_processed' => null,
261  'aggregator' => 'all',
262  'conditions' => [
263  [
264  'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
265  'attribute' => 'base_subtotal',
266  'operator' => '>=',
267  'value' => 100,
268  'is_value_processed' => null,
269  ],
270  [
271  'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
272  'attribute' => 'total_qty',
273  'operator' => '>',
274  'value' => 2,
275  'is_value_processed' => null
276  ],
277  [
278  'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
279  'attribute' => null,
280  'operator' => null,
281  'value' => 1,
282  'is_value_processed' => null,
283  'aggregator' => 'all',
284  'conditions' => [
285  [
286  'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
287  'attribute' => 'category_ids',
288  'operator' => '==',
289  'value' => 3,
290  'is_value_processed' => null
291  ]
292 
293  ]
294 
295  ],
296  ]
297 
298  ];
299 
300  $dataCondition = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Condition::class)
301  ->setMethods(['setData'])
302  ->disableOriginalConstructor()
303  ->getMock();
304 
305  $this->conditionDataFactory
306  ->expects($this->any())
307  ->method('create')
308  ->willReturn($dataCondition);
309 
310  $return = $this->model->arrayToConditionDataModel($array);
311 
312  $this->assertEquals($dataCondition, $return);
313  }
314 }
$helper
Definition: iframe.phtml:13