GetAddressesTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Customer;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Customer\Api\Data\AddressInterface;
  10. use Magento\Customer\Api\Data\CustomerInterface;
  11. use Magento\TestFramework\ObjectManager;
  12. use Magento\TestFramework\TestCase\GraphQlAbstract;
  13. use Magento\Integration\Api\CustomerTokenServiceInterface;
  14. class GetAddressesTest extends GraphQlAbstract
  15. {
  16. /**
  17. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  18. * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php
  19. */
  20. public function testGetCustomerWithAddresses()
  21. {
  22. $query
  23. = <<<QUERY
  24. {
  25. customer {
  26. id
  27. addresses {
  28. id
  29. customer_id
  30. region_id
  31. country_id
  32. telephone
  33. postcode
  34. city
  35. firstname
  36. lastname
  37. }
  38. }
  39. }
  40. QUERY;
  41. $userName = 'customer@example.com';
  42. $password = 'password';
  43. /** @var CustomerTokenServiceInterface $customerTokenService */
  44. $customerTokenService = ObjectManager::getInstance()
  45. ->get(\Magento\Integration\Api\CustomerTokenServiceInterface::class);
  46. $customerToken = $customerTokenService->createCustomerAccessToken($userName, $password);
  47. $headerMap = ['Authorization' => 'Bearer ' . $customerToken];
  48. /** @var CustomerRepositoryInterface $customerRepository */
  49. $customerRepository = ObjectManager::getInstance()->get(CustomerRepositoryInterface::class);
  50. $customer = $customerRepository->get('customer@example.com');
  51. $response = $this->graphQlQuery($query, [], '', $headerMap);
  52. $this->assertArrayHasKey('customer', $response);
  53. $this->assertArrayHasKey('addresses', $response['customer']);
  54. $this->assertTrue(
  55. is_array([$response['customer']['addresses']]),
  56. " Addresses field must be of an array type."
  57. );
  58. self::assertEquals($customer->getId(), $response['customer']['id']);
  59. $this->assertCustomerAddressesFields($customer, $response);
  60. }
  61. /**
  62. * Verify the fields for CustomerAddress object
  63. *
  64. * @param CustomerInterface $customer
  65. * @param array $actualResponse
  66. */
  67. public function assertCustomerAddressesFields($customer, $actualResponse)
  68. {
  69. /** @var AddressInterface $addresses */
  70. $addresses = $customer->getAddresses();
  71. foreach ($addresses as $addressKey => $addressValue) {
  72. $this->assertNotEmpty($addressValue);
  73. $assertionMap = [
  74. ['response_field' => 'id', 'expected_value' => $addresses[$addressKey]->getId()],
  75. ['response_field' => 'customer_id', 'expected_value' => $addresses[$addressKey]->getCustomerId()],
  76. ['response_field' => 'region_id', 'expected_value' => $addresses[$addressKey]->getRegionId()],
  77. ['response_field' => 'country_id', 'expected_value' => $addresses[$addressKey]->getCountryId()],
  78. ['response_field' => 'telephone', 'expected_value' => $addresses[$addressKey]->getTelephone()],
  79. ['response_field' => 'postcode', 'expected_value' => $addresses[$addressKey]->getPostcode()],
  80. ['response_field' => 'city', 'expected_value' => $addresses[$addressKey]->getCity()],
  81. ['response_field' => 'firstname', 'expected_value' => $addresses[$addressKey]->getFirstname()],
  82. ['response_field' => 'lastname', 'expected_value' => $addresses[$addressKey]->getLastname()]
  83. ];
  84. $this->assertResponseFields($actualResponse['customer']['addresses'][$addressKey], $assertionMap);
  85. }
  86. }
  87. }