Skip to content

Commit

Permalink
webapi test
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Charlie committed Feb 20, 2020
1 parent c9f9d71 commit 5020752
Showing 1 changed file with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

use Magento\Customer\Api\Data\CustomerInterface as Customer;
use Magento\Customer\Api\Data\AddressInterface as Address;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
Expand Down Expand Up @@ -482,25 +484,31 @@ public function testCreateCustomerWithoutAddressRequiresException()

/**
* Test with a single filter
*
* @param bool $subscribeStatus
* @return void
*
* @dataProvider subscriptionDataProvider
*/
public function testSearchCustomers()
public function testSearchCustomers(bool $subscribeStatus): void
{
$builder = Bootstrap::getObjectManager()->create(\Magento\Framework\Api\FilterBuilder::class);
$customerData = $this->_createCustomer();
$builder = Bootstrap::getObjectManager()->create(FilterBuilder::class);
$subscribeData = $this->buildSubscriptionData($subscribeStatus);
$customerData = $this->_createCustomer($subscribeData);
$filter = $builder
->setField(Customer::EMAIL)
->setValue($customerData[Customer::EMAIL])
->create();
$this->searchCriteriaBuilder->addFilters([$filter]);
$searchData = $this->dataObjectProcessor->buildOutputDataArray(
$this->searchCriteriaBuilder->create(),
\Magento\Framework\Api\SearchCriteriaInterface::class
SearchCriteriaInterface::class
);
$requestData = ['searchCriteria' => $searchData];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData),
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
'httpMethod' => Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => self::SERVICE_NAME,
Expand All @@ -511,6 +519,35 @@ public function testSearchCustomers()
$searchResults = $this->_webApiCall($serviceInfo, $requestData);
$this->assertEquals(1, $searchResults['total_count']);
$this->assertEquals($customerData[Customer::ID], $searchResults['items'][0][Customer::ID]);
$this->assertEquals($subscribeStatus, $searchResults['items'][0]['extension_attributes']['is_subscribed']);
}

/**
* Build subscription extension attributes data
*
* @param bool $status
* @return array
*/
private function buildSubscriptionData(bool $status): array
{
return [
'extension_attributes' => [
'is_subscribed' => $status,
],
];
}

/**
* Subscription customer data provider
*
* @return array
*/
public function subscriptionDataProvider(): array
{
return [
'subscribed user' => [true],
'not subscribed user' => [false],
];
}

/**
Expand Down Expand Up @@ -857,11 +894,12 @@ protected function _getCustomerData($customerId)
}

/**
* @param array|null $additionalData
* @return array|bool|float|int|string
*/
protected function _createCustomer()
protected function _createCustomer(?array $additionalData = [])
{
$customerData = $this->customerHelper->createSampleCustomer();
$customerData = $this->customerHelper->createSampleCustomer($additionalData);
$this->currentCustomerId[] = $customerData['id'];
return $customerData;
}
Expand Down

0 comments on commit 5020752

Please sign in to comment.