AddressRepositoryTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Api;
  7. use Magento\TestFramework\Helper\Bootstrap;
  8. class AddressRepositoryTest extends \Magento\TestFramework\TestCase\WebapiAbstract
  9. {
  10. const SOAP_SERVICE_NAME = 'customerAddressRepositoryV1';
  11. const SOAP_SERVICE_VERSION = 'V1';
  12. /** @var \Magento\Customer\Api\AddressRepositoryInterface */
  13. protected $addressRepository;
  14. /** @var \Magento\Customer\Api\CustomerRepositoryInterface */
  15. protected $customerRepository;
  16. protected function setUp()
  17. {
  18. $objectManager = Bootstrap::getObjectManager();
  19. $this->customerRepository = $objectManager->get(
  20. \Magento\Customer\Api\CustomerRepositoryInterface::class
  21. );
  22. $this->addressRepository = $objectManager->get(
  23. \Magento\Customer\Api\AddressRepositoryInterface::class
  24. );
  25. parent::setUp();
  26. }
  27. /**
  28. * Ensure that fixture customer and his addresses are deleted.
  29. */
  30. protected function tearDown()
  31. {
  32. /** @var \Magento\Framework\Registry $registry */
  33. $registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
  34. $registry->unregister('isSecureArea');
  35. $registry->register('isSecureArea', true);
  36. try {
  37. $fixtureFirstAddressId = 1;
  38. $this->addressRepository->deleteById($fixtureFirstAddressId);
  39. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  40. /** First address fixture was not used */
  41. }
  42. try {
  43. $fixtureSecondAddressId = 2;
  44. $this->addressRepository->deleteById($fixtureSecondAddressId);
  45. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  46. /** Second address fixture was not used */
  47. }
  48. try {
  49. $fixtureCustomerId = 1;
  50. $this->customerRepository->deleteById($fixtureCustomerId);
  51. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  52. /** Customer fixture was not used */
  53. }
  54. $registry->unregister('isSecureArea');
  55. $registry->register('isSecureArea', false);
  56. parent::tearDown();
  57. }
  58. /**
  59. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  60. * @magentoApiDataFixture Magento/Customer/_files/customer_address.php
  61. */
  62. public function testGetAddress()
  63. {
  64. $fixtureAddressId = 1;
  65. $serviceInfo = [
  66. 'rest' => [
  67. 'resourcePath' => "/V1/customers/addresses/{$fixtureAddressId}",
  68. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  69. ],
  70. 'soap' => [
  71. 'service' => self::SOAP_SERVICE_NAME,
  72. 'serviceVersion' => self::SOAP_SERVICE_VERSION,
  73. 'operation' => self::SOAP_SERVICE_NAME . 'GetById',
  74. ],
  75. ];
  76. $requestData = ['addressId' => $fixtureAddressId];
  77. $addressData = $this->_webApiCall($serviceInfo, $requestData);
  78. $this->assertEquals(
  79. $this->getFirstFixtureAddressData(),
  80. $addressData,
  81. "Address data is invalid."
  82. );
  83. }
  84. /**
  85. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  86. * @magentoApiDataFixture Magento/Customer/_files/customer_address.php
  87. */
  88. public function testDeleteAddress()
  89. {
  90. $fixtureAddressId = 1;
  91. $serviceInfo = [
  92. 'rest' => [
  93. 'resourcePath' => "/V1/addresses/{$fixtureAddressId}",
  94. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
  95. ],
  96. 'soap' => [
  97. 'service' => self::SOAP_SERVICE_NAME,
  98. 'serviceVersion' => self::SOAP_SERVICE_VERSION,
  99. 'operation' => self::SOAP_SERVICE_NAME . 'DeleteById',
  100. ],
  101. ];
  102. $requestData = ['addressId' => $fixtureAddressId];
  103. $response = $this->_webApiCall($serviceInfo, $requestData);
  104. $this->assertTrue($response, 'Expected response should be true.');
  105. $this->expectException(\Magento\Framework\Exception\NoSuchEntityException::class);
  106. $this->expectExceptionMessage('No such entity with addressId = 1');
  107. $this->addressRepository->getById($fixtureAddressId);
  108. }
  109. /**
  110. * Retrieve data of the first fixture address.
  111. *
  112. * @return array
  113. */
  114. protected function getFirstFixtureAddressData()
  115. {
  116. return [
  117. 'firstname' => 'John',
  118. 'lastname' => 'Smith',
  119. 'city' => 'CityM',
  120. 'country_id' => 'US',
  121. 'company' => 'CompanyName',
  122. 'postcode' => '75477',
  123. 'telephone' => '3468676',
  124. 'street' => ['Green str, 67'],
  125. 'id' => 1,
  126. 'default_billing' => true,
  127. 'default_shipping' => true,
  128. 'customer_id' => '1',
  129. 'region' => ['region' => 'Alabama', 'region_id' => 1, 'region_code' => 'AL'],
  130. 'region_id' => 1,
  131. ];
  132. }
  133. /**
  134. * Retrieve data of the second fixture address.
  135. *
  136. * @return array
  137. */
  138. protected function getSecondFixtureAddressData()
  139. {
  140. return [
  141. 'firstname' => 'John',
  142. 'lastname' => 'Smith',
  143. 'city' => 'CityX',
  144. 'country_id' => 'US',
  145. 'postcode' => '47676',
  146. 'telephone' => '3234676',
  147. 'street' => ['Black str, 48'],
  148. 'id' => 2,
  149. 'default_billing' => false,
  150. 'default_shipping' => false,
  151. 'customer_id' => '1',
  152. 'region' => ['region' => 'Alabama', 'region_id' => 1, 'region_code' => 'AL'],
  153. 'region_id' => 1,
  154. ];
  155. }
  156. }