Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerAuthenticationTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 
18 {
26  public function testRegisteredCustomerWithValidCredentials()
27  {
28  $query
29  = <<<QUERY
30 {
31  customer
32  {
33  created_at
34  group_id
35  prefix
36  firstname
37  middlename
38  lastname
39  suffix
40  email
41  default_billing
42  default_shipping
43  id
44  addresses{
45  id
46  customer_id
47  region_id
48  country_id
49  telephone
50  postcode
51  city
52  firstname
53  lastname
54  }
55  }
56 }
57 QUERY;
58 
60  $password = 'password';
62  $customerTokenService = ObjectManager::getInstance()
63  ->get(\Magento\Integration\Api\CustomerTokenServiceInterface::class);
64  $customerToken = $customerTokenService->createCustomerAccessToken($userName, $password);
65  $headerMap = ['Authorization' => 'Bearer ' . $customerToken];
67  $customerRepository = ObjectManager::getInstance()->get(CustomerRepositoryInterface::class);
69 
70  $response = $this->graphQlQuery($query, [], '', $headerMap);
71  $this->assertArrayHasKey('customer', $response);
72  $this->assertArrayHasKey('addresses', $response['customer']);
73  $this->assertTrue(
74  is_array([$response['customer']['addresses']]),
75  " Addresses field must be of an array type."
76  );
77  $this->assertCustomerFields($customer, $response['customer']);
78  $this->assertCustomerAddressesFields($customer, $response);
79  }
80 
87  {
88  $query
89  = <<<QUERY
90 {
91  customer
92  {
93  created_at
94  group_id
95  prefix
96  firstname
97  middlename
98  lastname
99  suffix
100  email
101  default_billing
102  default_shipping
103  id
104  }
105 }
106 QUERY;
107 
108  $this->expectException(\Exception::class);
109  $this->expectExceptionMessage('GraphQL response contains errors: Current customer' . ' ' .
110  'does not have access to the resource "customer"');
111  $this->graphQlQuery($query);
112  }
113 
120  public function assertCustomerFields($customer, $actualResponse)
121  {
122  // ['customer_object_field_name', 'expected_value']
123  $assertionMap = [
124  ['response_field' => 'id', 'expected_value' => $customer->getId()],
125  ['response_field' => 'created_at', 'expected_value' => $customer->getCreatedAt()],
126  ['response_field' => 'group_id', 'expected_value' => $customer->getGroupId()],
127  ['response_field' => 'prefix', 'expected_value' => $customer->getPrefix()],
128  ['response_field' => 'firstname', 'expected_value' => $customer->getFirstname()],
129  ['response_field' => 'middlename', 'expected_value' => $customer->getMiddlename()],
130  ['response_field' => 'lastname', 'expected_value' => $customer->getLastname()],
131  ['response_field' => 'suffix', 'expected_value' => $customer->getSuffix()],
132  ['response_field' => 'email', 'expected_value' => $customer->getEmail()],
133  ['response_field' => 'default_shipping', 'expected_value' => (bool)$customer->getDefaultShipping()],
134  ['response_field' => 'default_billing', 'expected_value' => (bool)$customer->getDefaultBilling()],
135  ['response_field' => 'id', 'expected_value' => $customer->getId()]
136  ];
137 
138  $this->assertResponseFields($actualResponse, $assertionMap);
139  }
140 
147  public function assertCustomerAddressesFields($customer, $actualResponse)
148  {
150  $addresses = $customer->getAddresses();
151  foreach ($addresses as $addressKey => $addressValue) {
152  $this->assertNotEmpty($addressValue);
153  $assertionMap = [
154  ['response_field' => 'id', 'expected_value' => $addresses[$addressKey]->getId()],
155  ['response_field' => 'customer_id', 'expected_value' => $addresses[$addressKey]->getCustomerId()],
156  ['response_field' => 'region_id', 'expected_value' => $addresses[$addressKey]->getRegionId()],
157  ['response_field' => 'country_id', 'expected_value' => $addresses[$addressKey]->getCountryId()],
158  ['response_field' => 'telephone', 'expected_value' => $addresses[$addressKey]->getTelephone()],
159  ['response_field' => 'postcode', 'expected_value' => $addresses[$addressKey]->getPostcode()],
160  ['response_field' => 'city', 'expected_value' => $addresses[$addressKey]->getCity()],
161  ['response_field' => 'firstname', 'expected_value' => $addresses[$addressKey]->getFirstname()],
162  ['response_field' => 'lastname', 'expected_value' => $addresses[$addressKey]->getLastname()]
163  ];
164  $this->assertResponseFields($actualResponse['customer']['addresses'][$addressKey], $assertionMap);
165  }
166  }
167 }
$response
Definition: 404.php:11
$customer
Definition: customers.php:11
$addresses
Definition: address_list.php:7
$customerRepository
assertResponseFields($actualResponse, $assertionMap)
graphQlQuery(string $query, array $variables=[], string $operationName='', array $headers=[])