ToModelTest.php 9.7 KB

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