/**
  * {@inheritdoc}
  */
 public function getList(SearchCriteriaInterface $searchCriteria)
 {
     $searchResults = $this->searchResultsFactory->create();
     $searchResults->setSearchCriteria($searchCriteria);
     /** @var \Magento\Customer\Model\Resource\Customer\Collection $collection */
     $collection = $this->customerFactory->create()->getCollection();
     // This is needed to make sure all the attributes are properly loaded
     foreach ($this->customerMetadata->getAllAttributesMetadata() as $metadata) {
         $collection->addAttributeToSelect($metadata->getAttributeCode());
     }
     // Needed to enable filtering on name as a whole
     $collection->addNameToSelect();
     // Needed to enable filtering based on billing address attributes
     $collection->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left')->joinAttribute('company', 'customer_address/company', 'default_billing', null, 'left');
     //Add filters from root filter group to the collection
     foreach ($searchCriteria->getFilterGroups() as $group) {
         $this->addFilterGroupToCollection($group, $collection);
     }
     $searchResults->setTotalCount($collection->getSize());
     $sortOrders = $searchCriteria->getSortOrders();
     if ($sortOrders) {
         foreach ($searchCriteria->getSortOrders() as $sortOrder) {
             $collection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SearchCriteriaInterface::SORT_ASC ? 'ASC' : 'DESC');
         }
     }
     $collection->setCurPage($searchCriteria->getCurrentPage());
     $collection->setPageSize($searchCriteria->getPageSize());
     $customers = [];
     /** @var \Magento\Customer\Model\Customer $customerModel */
     foreach ($collection as $customerModel) {
         $customers[] = $customerModel->getDataModel();
     }
     $searchResults->setItems($customers);
     return $searchResults;
 }
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testGetList()
 {
     $sortOrder = $this->getMock('Magento\\Framework\\Api\\SortOrder', [], [], '', false);
     $filterGroup = $this->getMock('Magento\\Framework\\Api\\Search\\FilterGroup', [], [], '', false);
     $filter = $this->getMock('Magento\\Framework\\Api\\Filter', [], [], '', false);
     $collection = $this->getMock('Magento\\Customer\\Model\\ResourceModel\\Customer\\Collection', [], [], '', false);
     $searchResults = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressSearchResultsInterface', [], '', false);
     $searchCriteria = $this->getMockForAbstractClass('Magento\\Framework\\Api\\SearchCriteriaInterface', [], '', false);
     $customerModel = $this->getMock('Magento\\Customer\\Model\\Customer', ['getId', 'setId', 'setStoreId', 'getStoreId', 'getAttributeSetId', 'setAttributeSetId', 'setRpToken', 'setRpTokenCreatedAt', 'getDataModel', 'setPasswordHash', 'getCollection'], [], 'customerModel', false);
     $metadata = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AttributeMetadataInterface', [], '', false);
     $this->searchResultsFactory->expects($this->once())->method('create')->willReturn($searchResults);
     $searchResults->expects($this->once())->method('setSearchCriteria')->with($searchCriteria);
     $this->customerFactory->expects($this->once())->method('create')->willReturn($customerModel);
     $customerModel->expects($this->once())->method('getCollection')->willReturn($collection);
     $this->extensionAttributesJoinProcessor->expects($this->once())->method('process')->with($collection, 'Magento\\Customer\\Api\\Data\\CustomerInterface');
     $this->customerMetadata->expects($this->once())->method('getAllAttributesMetadata')->willReturn([$metadata]);
     $metadata->expects($this->once())->method('getAttributeCode')->willReturn('attribute-code');
     $collection->expects($this->once())->method('addAttributeToSelect')->with('attribute-code');
     $collection->expects($this->once())->method('addNameToSelect');
     $collection->expects($this->at(2))->method('joinAttribute')->with('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')->willReturnSelf();
     $collection->expects($this->at(3))->method('joinAttribute')->with('billing_city', 'customer_address/city', 'default_billing', null, 'left')->willReturnSelf();
     $collection->expects($this->at(4))->method('joinAttribute')->with('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')->willReturnSelf();
     $collection->expects($this->at(5))->method('joinAttribute')->with('billing_region', 'customer_address/region', 'default_billing', null, 'left')->willReturnSelf();
     $collection->expects($this->at(6))->method('joinAttribute')->with('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left')->willReturnSelf();
     $collection->expects($this->at(7))->method('joinAttribute')->with('company', 'customer_address/company', 'default_billing', null, 'left')->willReturnSelf();
     $searchCriteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]);
     $collection->expects($this->once())->method('addFieldToFilter')->with([['attribute' => 'Field', 'eq' => 'Value']], []);
     $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('addOrder')->with('Field', 'ASC');
     $searchCriteria->expects($this->atLeastOnce())->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);
     $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('getSize')->willReturn(23);
     $searchResults->expects($this->once())->method('setTotalCount')->with(23);
     $collection->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator([$customerModel]));
     $customerModel->expects($this->atLeastOnce())->method('getDataModel')->willReturn($this->customer);
     $searchResults->expects($this->once())->method('setItems')->with([$this->customer]);
     $this->assertSame($searchResults, $this->model->getList($searchCriteria));
 }