8 use Magento\SalesRule\Api\Data\RuleExtensionFactory;
9 use Magento\SalesRule\Api\Data\RuleExtensionInterface;
59 private $extensionFactoryMock;
63 $this->ruleFactory = $this->getMockBuilder(\
Magento\SalesRule\Model\RuleFactory::class)
64 ->disableOriginalConstructor()
68 $this->ruleDataFactory = $this->getMockBuilder(\
Magento\SalesRule\Api\Data\RuleInterfaceFactory::class)
69 ->disableOriginalConstructor()
70 ->setMethods([
'create'])
73 $this->conditionDataFactory = $this->getMockBuilder(
74 \
Magento\SalesRule\Api\Data\ConditionInterfaceFactory::class
75 )->disableOriginalConstructor()
76 ->setMethods([
'create'])
79 $this->dataObjectProcessor = $this->getMockBuilder(\
Magento\Framework\Reflection\DataObjectProcessor::class)
80 ->disableOriginalConstructor()
84 $this->ruleLabelFactory = $this->getMockBuilder(\
Magento\SalesRule\Api\Data\RuleLabelInterfaceFactory::class)
85 ->disableOriginalConstructor()
86 ->setMethods([
'create'])
89 $this->salesRule = $this->getMockBuilder(\
Magento\SalesRule\Model\Rule::class)
90 ->disableOriginalConstructor()
91 ->setMethods([
'_construct',
'getData',
'getConditionsSerialized',
'getActionsSerialized'])
94 $this->serializer = $this->getMockBuilder(\
Magento\Framework\Serialize\Serializer\Json::class)
98 $this->extensionFactoryMock = $this->getMockBuilder(RuleExtensionFactory::class)
99 ->setMethods([
'create'])
100 ->disableOriginalConstructor()
103 $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
104 $this->model =
$helper->getObject(
105 \
Magento\SalesRule\Model\Converter\ToDataModel::class,
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,
121 private function getArrayData()
125 'name' =>
'testrule',
127 'conditions_serialized' => json_encode([
128 'type' => \
Magento\SalesRule\Model\Rule\Condition\Combine::class,
132 'is_value_processed' =>
null,
133 'aggregator' =>
'all',
136 'type' => \
Magento\SalesRule\Model\Rule\Condition\Address::class,
137 'attribute' =>
'base_subtotal',
140 'is_value_processed' =>
false,
144 'actions_serialized' => json_encode([
145 'type' => \
Magento\SalesRule\Model\Rule\Condition\Product\Combine::class,
149 'is_value_processed' =>
null,
150 'aggregator' =>
'all',
153 'type' => \
Magento\SalesRule\Model\Rule\Condition\Product::class,
154 'attribute' =>
'attribute_set_id',
157 'is_value_processed' =>
false,
161 'coupon_type' =>
'1',
165 1 =>
'TestRuleForDefaultStore',
167 'extension_attributes' => [
168 'some_extension_attributes' => 123,
173 public function testToDataModel()
175 $array = $this->getArrayData();
176 $arrayAttributes = $array;
179 $attributesMock = $this->getMockBuilder(RuleExtensionInterface::class)
181 $arrayAttributes[
'extension_attributes'] = $attributesMock;
183 $this->extensionFactoryMock->expects($this->any())
185 ->with([
'data' => $array[
'extension_attributes']])
186 ->willReturn($attributesMock);
188 $dataModel = $this->getMockBuilder(\
Magento\SalesRule\Model\Data\Rule::class)
189 ->disableOriginalConstructor()
190 ->setMethods([
'create',
'getStoreLabels',
'setStoreLabels',
'getCouponType',
'setCouponType'])
193 $dataLabel = $this->getMockBuilder(\
Magento\SalesRule\Api\Data\RuleLabel::class)
194 ->setMethods([
'setStoreId',
'setStoreLabel',
'setStoreLabels'])
195 ->disableOriginalConstructor()
198 $dataCondition = $this->getMockBuilder(\
Magento\SalesRule\Model\Data\Condition::class)
199 ->setMethods([
'setData'])
200 ->disableOriginalConstructor()
203 $this->ruleLabelFactory
204 ->expects($this->any())
206 ->willReturn($dataLabel);
208 $this->conditionDataFactory
209 ->expects($this->any())
211 ->willReturn($dataCondition);
213 $this->ruleDataFactory
214 ->expects($this->any())
216 ->with([
'data' => $arrayAttributes])
217 ->willReturn($dataModel);
220 ->expects($this->any())
222 ->willReturn($array);
225 ->expects($this->once())
226 ->method(
'getConditionsSerialized')
227 ->willReturn($array[
'conditions_serialized']);
230 ->expects($this->atLeastOnce())
231 ->method(
'getStoreLabels')
232 ->willReturn($array[
'store_labels']);
235 ->expects($this->atLeastOnce())
236 ->method(
'setStoreLabels');
239 ->expects($this->atLeastOnce())
240 ->method(
'getCouponType')
241 ->willReturn(\
Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON);
244 ->expects($this->atLeastOnce())
245 ->method(
'setCouponType');
247 $return = $this->model->toDataModel($this->salesRule);
249 $this->assertSame($dataModel, $return);
256 'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
260 'is_value_processed' =>
null,
261 'aggregator' =>
'all',
264 'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
265 'attribute' =>
'base_subtotal',
268 'is_value_processed' =>
null,
271 'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
272 'attribute' =>
'total_qty',
275 'is_value_processed' => null
278 'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
282 'is_value_processed' =>
null,
283 'aggregator' =>
'all',
286 'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
287 'attribute' =>
'category_ids',
290 'is_value_processed' => null
300 $dataCondition = $this->getMockBuilder(\
Magento\SalesRule\Model\Data\Condition::class)
301 ->setMethods([
'setData'])
302 ->disableOriginalConstructor()
305 $this->conditionDataFactory
306 ->expects($this->any())
308 ->willReturn($dataCondition);
310 $return = $this->model->arrayToConditionDataModel($array);
312 $this->assertEquals($dataCondition, $return);
testArrayToConditionDataModel()