ToDataModelTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Test\Unit\Model\Converter;
  7. use Magento\SalesRule\Api\Data\RuleExtensionFactory;
  8. use Magento\SalesRule\Api\Data\RuleExtensionInterface;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class ToDataModelTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\SalesRule\Model\RuleFactory|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $ruleFactory;
  18. /**
  19. * @var \Magento\SalesRule\Api\Data\RuleInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $ruleDataFactory;
  22. /**
  23. * @var \Magento\SalesRule\Api\Data\ConditionInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $conditionDataFactory;
  26. /**
  27. * @var \Magento\Framework\Reflection\DataObjectProcessor|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $dataObjectProcessor;
  30. /**
  31. * @var \Magento\SalesRule\Api\Data\RuleLabelInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $ruleLabelFactory;
  34. /**
  35. * @var \Magento\SalesRule\Model\Rule|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $salesRule;
  38. /**
  39. * @var \Magento\SalesRule\Model\Converter\ToDataModel
  40. */
  41. protected $model;
  42. /**
  43. * @var \Magento\Framework\Serialize\Serializer\Json
  44. */
  45. protected $serializer;
  46. /**
  47. * @var RuleExtensionFactory|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $extensionFactoryMock;
  50. protected function setUp()
  51. {
  52. $this->ruleFactory = $this->getMockBuilder(\Magento\SalesRule\Model\RuleFactory::class)
  53. ->disableOriginalConstructor()
  54. ->setMethods([])
  55. ->getMock();
  56. $this->ruleDataFactory = $this->getMockBuilder(\Magento\SalesRule\Api\Data\RuleInterfaceFactory::class)
  57. ->disableOriginalConstructor()
  58. ->setMethods(['create'])
  59. ->getMock();
  60. $this->conditionDataFactory = $this->getMockBuilder(
  61. \Magento\SalesRule\Api\Data\ConditionInterfaceFactory::class
  62. )->disableOriginalConstructor()
  63. ->setMethods(['create'])
  64. ->getMock();
  65. $this->dataObjectProcessor = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
  66. ->disableOriginalConstructor()
  67. ->setMethods([])
  68. ->getMock();
  69. $this->ruleLabelFactory = $this->getMockBuilder(\Magento\SalesRule\Api\Data\RuleLabelInterfaceFactory::class)
  70. ->disableOriginalConstructor()
  71. ->setMethods(['create'])
  72. ->getMock();
  73. $this->salesRule = $this->getMockBuilder(\Magento\SalesRule\Model\Rule::class)
  74. ->disableOriginalConstructor()
  75. ->setMethods(['_construct', 'getData', 'getConditionsSerialized', 'getActionsSerialized'])
  76. ->getMock();
  77. $this->serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
  78. ->setMethods(null)
  79. ->getMock();
  80. $this->extensionFactoryMock = $this->getMockBuilder(RuleExtensionFactory::class)
  81. ->setMethods(['create'])
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  85. $this->model = $helper->getObject(
  86. \Magento\SalesRule\Model\Converter\ToDataModel::class,
  87. [
  88. 'ruleFactory' => $this->ruleFactory,
  89. 'ruleDataFactory' => $this->ruleDataFactory,
  90. 'conditionDataFactory' => $this->conditionDataFactory,
  91. 'ruleLabelFactory' => $this->ruleLabelFactory,
  92. 'dataObjectProcessor' => $this->dataObjectProcessor,
  93. 'serializer' => $this->serializer,
  94. 'extensionFactory' => $this->extensionFactoryMock,
  95. ]
  96. );
  97. }
  98. /**
  99. * @return array
  100. */
  101. private function getArrayData()
  102. {
  103. return [
  104. 'rule_id' => '1',
  105. 'name' => 'testrule',
  106. 'is_active' => '1',
  107. 'conditions_serialized' => json_encode([
  108. 'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
  109. 'attribute' => null,
  110. 'operator' => null,
  111. 'value' => '1',
  112. 'is_value_processed' => null,
  113. 'aggregator' => 'all',
  114. 'conditions' => [
  115. [
  116. 'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
  117. 'attribute' => 'base_subtotal',
  118. 'operator' => '>=',
  119. 'value' => '100',
  120. 'is_value_processed' => false,
  121. ],
  122. ],
  123. ]),
  124. 'actions_serialized' => json_encode([
  125. 'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Combine::class,
  126. 'attribute' => null,
  127. 'operator' => null,
  128. 'value' => '1',
  129. 'is_value_processed' => null,
  130. 'aggregator' => 'all',
  131. 'conditions' => [
  132. [
  133. 'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
  134. 'attribute' => 'attribute_set_id',
  135. 'operator' => '==',
  136. 'value' => '4',
  137. 'is_value_processed' => false,
  138. ],
  139. ],
  140. ]),
  141. 'coupon_type' => '1',
  142. 'coupon_code' => '',
  143. 'store_labels' => [
  144. 0 => 'TestRule',
  145. 1 => 'TestRuleForDefaultStore',
  146. ],
  147. 'extension_attributes' => [
  148. 'some_extension_attributes' => 123,
  149. ],
  150. ];
  151. }
  152. public function testToDataModel()
  153. {
  154. $array = $this->getArrayData();
  155. $arrayAttributes = $array;
  156. /** @var RuleExtensionInterface|\PHPUnit_Framework_MockObject_MockObject $attributesMock */
  157. $attributesMock = $this->getMockBuilder(RuleExtensionInterface::class)
  158. ->getMock();
  159. $arrayAttributes['extension_attributes'] = $attributesMock;
  160. $this->extensionFactoryMock->expects($this->any())
  161. ->method('create')
  162. ->with(['data' => $array['extension_attributes']])
  163. ->willReturn($attributesMock);
  164. $dataModel = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Rule::class)
  165. ->disableOriginalConstructor()
  166. ->setMethods(['create', 'getStoreLabels', 'setStoreLabels', 'getCouponType', 'setCouponType'])
  167. ->getMock();
  168. $dataLabel = $this->getMockBuilder(\Magento\SalesRule\Api\Data\RuleLabel::class)
  169. ->setMethods(['setStoreId', 'setStoreLabel', 'setStoreLabels'])
  170. ->disableOriginalConstructor()
  171. ->getMock();
  172. $dataCondition = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Condition::class)
  173. ->setMethods(['setData'])
  174. ->disableOriginalConstructor()
  175. ->getMock();
  176. $this->ruleLabelFactory
  177. ->expects($this->any())
  178. ->method('create')
  179. ->willReturn($dataLabel);
  180. $this->conditionDataFactory
  181. ->expects($this->any())
  182. ->method('create')
  183. ->willReturn($dataCondition);
  184. $this->ruleDataFactory
  185. ->expects($this->any())
  186. ->method('create')
  187. ->with(['data' => $arrayAttributes])
  188. ->willReturn($dataModel);
  189. $this->salesRule
  190. ->expects($this->any())
  191. ->method('getData')
  192. ->willReturn($array);
  193. $this->salesRule
  194. ->expects($this->once())
  195. ->method('getConditionsSerialized')
  196. ->willReturn($array['conditions_serialized']);
  197. $dataModel
  198. ->expects($this->atLeastOnce())
  199. ->method('getStoreLabels')
  200. ->willReturn($array['store_labels']);
  201. $dataModel
  202. ->expects($this->atLeastOnce())
  203. ->method('setStoreLabels');
  204. $dataModel
  205. ->expects($this->atLeastOnce())
  206. ->method('getCouponType')
  207. ->willReturn(\Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON);
  208. $dataModel
  209. ->expects($this->atLeastOnce())
  210. ->method('setCouponType');
  211. $return = $this->model->toDataModel($this->salesRule);
  212. $this->assertSame($dataModel, $return);
  213. }
  214. public function testArrayToConditionDataModel()
  215. {
  216. $array=[
  217. 'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
  218. 'attribute' => null,
  219. 'operator' => null,
  220. 'value' => 1,
  221. 'is_value_processed' => null,
  222. 'aggregator' => 'all',
  223. 'conditions' => [
  224. [
  225. 'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
  226. 'attribute' => 'base_subtotal',
  227. 'operator' => '>=',
  228. 'value' => 100,
  229. 'is_value_processed' => null,
  230. ],
  231. [
  232. 'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
  233. 'attribute' => 'total_qty',
  234. 'operator' => '>',
  235. 'value' => 2,
  236. 'is_value_processed' => null
  237. ],
  238. [
  239. 'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
  240. 'attribute' => null,
  241. 'operator' => null,
  242. 'value' => 1,
  243. 'is_value_processed' => null,
  244. 'aggregator' => 'all',
  245. 'conditions' => [
  246. [
  247. 'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
  248. 'attribute' => 'category_ids',
  249. 'operator' => '==',
  250. 'value' => 3,
  251. 'is_value_processed' => null
  252. ]
  253. ]
  254. ],
  255. ]
  256. ];
  257. $dataCondition = $this->getMockBuilder(\Magento\SalesRule\Model\Data\Condition::class)
  258. ->setMethods(['setData'])
  259. ->disableOriginalConstructor()
  260. ->getMock();
  261. $this->conditionDataFactory
  262. ->expects($this->any())
  263. ->method('create')
  264. ->willReturn($dataCondition);
  265. $return = $this->model->arrayToConditionDataModel($array);
  266. $this->assertEquals($dataCondition, $return);
  267. }
  268. }