Пример #1
0
 /**
  * Creates a search criteria DTO based on the array of field filters.
  *
  * @return SearchCriteria
  */
 protected function getSearchCriteria()
 {
     foreach ($this->fieldFilters as $filter) {
         // array of fields, put filters in array to use 'or' group
         /** @var Filter[] $filterGroup */
         $filterGroup = array();
         if (!is_array($filter['field'])) {
             // just one field
             $filterGroup = [$this->createFilterData($filter['field'], $filter['condition'])];
         } else {
             foreach ($filter['field'] as $index => $field) {
                 $filterGroup[] = $this->createFilterData($field, $filter['condition'][$index]);
             }
         }
         $this->searchCriteriaBuilder->addFilter($filterGroup);
     }
     foreach ($this->_orders as $field => $direction) {
         /** @var \Magento\Framework\Service\V1\Data\SortOrder $sortOrder */
         /** @var string $direction */
         $direction = $direction == 'ASC' ? SearchCriteria::SORT_ASC : SearchCriteria::SORT_DESC;
         $sortOrder = $this->sortOrderBuilder->setField($field)->setDirection($direction)->create();
         $this->searchCriteriaBuilder->addSortOrder($sortOrder);
     }
     $this->searchCriteriaBuilder->setCurrentPage($this->_curPage);
     $this->searchCriteriaBuilder->setPageSize($this->_pageSize);
     return $this->searchCriteriaBuilder->create();
 }
Пример #2
0
 public function testGetSearchCriteriaAnd()
 {
     // Test ((A > 1) and (B > 1))
     $fieldA = 'A';
     $fieldB = 'B';
     $value = 1;
     $sortOrder = $this->sortOrderBuilder->setField('name')->setDirection(SearchCriteria::SORT_ASC)->create();
     /** @var SearchCriteria $expectedSearchCriteria */
     $expectedSearchCriteria = $this->searchCriteriaBuilder->setCurrentPage(1)->setPageSize(0)->addSortOrder($sortOrder)->addFilter([$this->filterBuilder->setField($fieldA)->setConditionType('gt')->setValue($value)->create()])->addFilter([$this->filterBuilder->setField($fieldB)->setConditionType('gt')->setValue($value)->create()])->create();
     // Verifies that the search criteria Data Object created by the serviceCollection matches expected
     $this->groupServiceMock->expects($this->once())->method('searchGroups')->with($this->equalTo($expectedSearchCriteria))->will($this->returnValue($this->searchResults));
     // Now call service collection to load the data.  This causes it to create the search criteria Data Object
     $this->serviceCollection->addFieldToFilter($fieldA, ['gt' => $value]);
     $this->serviceCollection->addFieldToFilter($fieldB, ['gt' => $value]);
     $this->serviceCollection->setOrder('name', ServiceCollection::SORT_ORDER_ASC);
     $this->serviceCollection->loadData();
 }