Пример #1
0
 /**
  * Retrieve customers addresses matching the specified criteria.
  *
  * @param SearchCriteriaInterface $searchCriteria
  * @return \Magento\Customer\Api\Data\AddressSearchResultsInterface
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function getList(SearchCriteriaInterface $searchCriteria)
 {
     $searchResults = $this->addressSearchResultsFactory->create();
     /** @var Collection $collection */
     $collection = $this->addressCollectionFactory->create();
     $this->extensionAttributesJoinProcessor->process($collection, 'Magento\\Customer\\Api\\Data\\AddressInterface');
     // Add filters from root filter group to the collection
     foreach ($searchCriteria->getFilterGroups() as $group) {
         $this->addFilterGroupToCollection($group, $collection);
     }
     $searchResults->setTotalCount($collection->getSize());
     /** @var SortOrder $sortOrder */
     foreach ((array) $searchCriteria->getSortOrders() as $sortOrder) {
         $field = $sortOrder->getField();
         $collection->addOrder($field, $sortOrder->getDirection() == SearchCriteriaInterface::SORT_ASC ? 'ASC' : 'DESC');
     }
     $collection->setCurPage($searchCriteria->getCurrentPage());
     $collection->setPageSize($searchCriteria->getPageSize());
     /** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */
     $addresses = [];
     /** @var \Magento\Customer\Model\Address $address */
     foreach ($collection->getItems() as $address) {
         $addresses[] = $this->getById($address->getId());
     }
     $searchResults->setItems($addresses);
     $searchResults->setSearchCriteria($searchCriteria);
     return $searchResults;
 }
 public function testGetList()
 {
     $filterGroup = $this->getMock('Magento\\Framework\\Api\\Search\\FilterGroup', [], [], '', false);
     $filter = $this->getMock('Magento\\Framework\\Api\\Filter', [], [], '', false);
     $collection = $this->getMock('Magento\\Customer\\Model\\ResourceModel\\Address\\Collection', [], [], '', false);
     $searchResults = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressSearchResultsInterface', [], '', false);
     $searchCriteria = $this->getMockForAbstractClass('Magento\\Framework\\Api\\SearchCriteriaInterface', [], '', false);
     $this->addressSearchResultsFactory->expects($this->once())->method('create')->willReturn($searchResults);
     $this->addressCollectionFactory->expects($this->once())->method('create')->willReturn($collection);
     $this->extensionAttributesJoinProcessor->expects($this->once())->method('process')->with($collection, 'Magento\\Customer\\Api\\Data\\AddressInterface');
     $searchCriteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]);
     $filterGroup->expects($this->once())->method('getFilters')->willReturn([$filter]);
     $filter->expects($this->once())->method('getConditionType')->willReturn(false);
     $filter->expects($this->once())->method('getField')->willReturn('Field');
     $filter->expects($this->atLeastOnce())->method('getValue')->willReturn('Value');
     $collection->expects($this->once())->method('addFieldToFilter')->with([['attribute' => 'Field', 'eq' => 'Value']], [['eq' => 'Value']]);
     $collection->expects($this->once())->method('getSize')->willReturn(23);
     $searchResults->expects($this->once())->method('setTotalCount')->with(23);
     $sortOrder = $this->getMock('Magento\\Framework\\Api\\SortOrder', [], [], '', false);
     $searchCriteria->expects($this->once())->method('getSortOrders')->willReturn([$sortOrder]);
     $sortOrder->expects($this->once())->method('getField')->willReturn('Field');
     $sortOrder->expects($this->once())->method('getDirection')->willReturn(\Magento\Framework\Api\SortOrder::SORT_ASC);
     $collection->expects($this->once())->method('addOrder')->with('Field', 'ASC');
     $searchCriteria->expects($this->once())->method('getCurrentPage')->willReturn(1);
     $collection->expects($this->once())->method('setCurPage')->with(1);
     $searchCriteria->expects($this->once())->method('getPageSize')->willReturn(10);
     $collection->expects($this->once())->method('setPageSize')->with(10);
     $collection->expects($this->once())->method('getItems')->willReturn([$this->address]);
     $this->address->expects($this->once())->method('getId')->willReturn(12);
     $customerAddress = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressInterface', [], '', false);
     $this->addressRegistry->expects($this->once())->method('retrieve')->with(12)->willReturn($this->address);
     $this->address->expects($this->once())->method('getDataModel')->willReturn($customerAddress);
     $searchResults->expects($this->once())->method('setItems')->with([$customerAddress]);
     $searchResults->expects($this->once())->method('setSearchCriteria')->with($searchCriteria);
     $this->assertSame($searchResults, $this->repository->getList($searchCriteria));
 }