Ejemplo n.º 1
0
 public function testToDataModel()
 {
     $array = ['rule_id' => '1', 'name' => 'testrule', 'is_active' => '1', 'conditions_serialized' => 'a:7:{s:4:"type";s:46:"Magento\\SalesRule\\Model\\Rule\\Condition\\Combine";s:9:"attribute";N;' . 's:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";' . 's:10:"conditions";a:1:{i:0;a:5:{s:4:"type";s:46:"Magento\\SalesRule\\Model\\Rule\\Condition\\Address";' . 's:9:"attribute";s:13:"base_subtotal";s:8:"operator";s:2:">=";s:5:"value";s:3:"100";' . 's:18:"is_value_processed";b:0;}}}', 'actions_serialized' => 'a:7:{s:4:"type";s:54:"Magento\\SalesRule\\Model\\Rule\\Condition\\Product\\Combine";s:9:"attribute";N;' . 's:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";' . 's:10:"conditions";a:1:{i:0;a:5:{s:4:"type";s:46:"Magento\\SalesRule\\Model\\Rule\\Condition\\Product";' . 's:9:"attribute";s:16:"attribute_set_id";s:8:"operator";s:2:"==";s:5:"value";s:1:"4";' . 's:18:"is_value_processed";b:0;}}}', 'coupon_type' => '1', 'coupon_code' => '', 'store_labels' => [0 => 'TestRule', 1 => 'TestRuleForDefaultStore']];
     $dataModel = $this->getMockBuilder('\\Magento\\SalesRule\\Model\\Data\\Rule')->disableOriginalConstructor()->setMethods(['create', 'getStoreLabels', 'setStoreLabels', 'getCouponType', 'setCouponType'])->getMock();
     $dataLabel = $this->getMockBuilder('\\Magento\\SalesRule\\Api\\Data\\RuleLabel')->setMethods(['setStoreId', 'setStoreLabel', 'setStoreLabels'])->disableOriginalConstructor()->getMock();
     $dataCondition = $this->getMockBuilder('\\Magento\\SalesRule\\Model\\Data\\Condition')->setMethods(['setData'])->disableOriginalConstructor()->getMock();
     $this->ruleLabelFactory->expects($this->any())->method('create')->willReturn($dataLabel);
     $this->conditionDataFactory->expects($this->any())->method('create')->willReturn($dataCondition);
     $this->ruleDataFactory->expects($this->any())->method('create')->willReturn($dataModel);
     $this->salesRule->expects($this->any())->method('getData')->willReturn($array);
     $this->salesRule->expects($this->once())->method('getConditionsSerialized')->willReturn($array['conditions_serialized']);
     $dataModel->expects($this->atLeastOnce())->method('getStoreLabels')->willReturn($array['store_labels']);
     $dataModel->expects($this->atLeastOnce())->method('setStoreLabels');
     $dataModel->expects($this->atLeastOnce())->method('getCouponType')->willReturn(\Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON);
     $dataModel->expects($this->atLeastOnce())->method('setCouponType');
     $return = $this->model->toDataModel($this->salesRule);
     $this->assertSame($dataModel, $return);
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function getList(SearchCriteriaInterface $searchCriteria)
 {
     /** @var \Magento\SalesRule\Model\ResourceModel\Rule\Collection $collection */
     $collection = $this->ruleCollectionFactory->create();
     $ruleInterfaceName = 'Magento\\SalesRule\\Api\\Data\\RuleInterface';
     $this->extensionAttributesJoinProcessor->process($collection, $ruleInterfaceName);
     $collection->addWebsitesToResult();
     //Add filters from root filter group to the collection
     /** @var FilterGroup $group */
     foreach ($searchCriteria->getFilterGroups() as $group) {
         $this->addFilterGroupToCollection($group, $collection);
     }
     $sortOrders = $searchCriteria->getSortOrders();
     if ($sortOrders === null) {
         $sortOrders = [];
     }
     /** @var \Magento\Framework\Api\SortOrder $sortOrder */
     foreach ($sortOrders as $sortOrder) {
         $field = $sortOrder->getField();
         $collection->addOrder($field, $sortOrder->getDirection() == SortOrder::SORT_ASC ? 'ASC' : 'DESC');
     }
     $collection->setCurPage($searchCriteria->getCurrentPage());
     $collection->setPageSize($searchCriteria->getPageSize());
     $collection->load();
     $rules = [];
     /** @var \Magento\SalesRule\Model\Rule $ruleModel */
     foreach ($collection->getItems() as $ruleModel) {
         $ruleModel->getCustomerGroupIds();
         $ruleModel->getStoreLabels();
         $rules[] = $this->toDataModelConverter->toDataModel($ruleModel);
     }
     $searchResults = $this->searchResultFactory->create();
     $searchResults->setSearchCriteria($searchCriteria);
     $searchResults->setItems($rules);
     $searchResults->setTotalCount($collection->getSize());
     return $searchResults;
 }