BeforeAddressSaveObserverTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Observer;
  7. use Magento\Customer\Helper\Address as HelperAddress;
  8. use Magento\Customer\Model\Address\AbstractAddress;
  9. use Magento\Customer\Observer\BeforeAddressSaveObserver;
  10. use Magento\Framework\App\Area;
  11. use Magento\Framework\App\State as AppState;
  12. use Magento\Framework\Registry;
  13. class BeforeAddressSaveObserverTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Customer\Observer\BeforeAddressSaveObserver
  17. */
  18. protected $model;
  19. /**
  20. * @var Registry |\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $registry;
  23. /**
  24. * @var \Magento\Customer\Model\Customer|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $customerMock;
  27. /**
  28. * @var HelperAddress |\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $helperAddress;
  31. protected function setUp()
  32. {
  33. $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class)
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->helperAddress = $this->getMockBuilder(\Magento\Customer\Helper\Address::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->model = new BeforeAddressSaveObserver(
  40. $this->helperAddress,
  41. $this->registry
  42. );
  43. }
  44. public function testBeforeAddressSaveWithCustomerAddressId()
  45. {
  46. $customerAddressId = 1;
  47. $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $address->expects($this->exactly(2))
  51. ->method('getId')
  52. ->willReturn($customerAddressId);
  53. $observer = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
  54. ->disableOriginalConstructor()
  55. ->setMethods([
  56. 'getCustomerAddress',
  57. ])
  58. ->getMock();
  59. $observer->expects($this->once())
  60. ->method('getCustomerAddress')
  61. ->willReturn($address);
  62. $this->registry->expects($this->once())
  63. ->method('registry')
  64. ->with(BeforeAddressSaveObserver::VIV_CURRENTLY_SAVED_ADDRESS)
  65. ->willReturn(true);
  66. $this->registry->expects($this->once())
  67. ->method('unregister')
  68. ->with(BeforeAddressSaveObserver::VIV_CURRENTLY_SAVED_ADDRESS)
  69. ->willReturnSelf();
  70. $this->registry->expects($this->once())
  71. ->method('register')
  72. ->with(BeforeAddressSaveObserver::VIV_CURRENTLY_SAVED_ADDRESS, $customerAddressId)
  73. ->willReturnSelf();
  74. $this->model->execute($observer);
  75. }
  76. /**
  77. * @param string $configAddressType
  78. * @param $isDefaultBilling
  79. * @param $isDefaultShipping
  80. * @dataProvider dataProviderBeforeAddressSaveWithoutCustomerAddressId
  81. */
  82. public function testBeforeAddressSaveWithoutCustomerAddressId(
  83. $configAddressType,
  84. $isDefaultBilling,
  85. $isDefaultShipping
  86. ) {
  87. $customerAddressId = null;
  88. $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
  89. ->disableOriginalConstructor()
  90. ->setMethods(['getId', 'getIsDefaultBilling', 'getIsDefaultShipping', 'setForceProcess'])
  91. ->getMock();
  92. $address->expects($this->once())
  93. ->method('getId')
  94. ->willReturn($customerAddressId);
  95. $address->expects($this->any())
  96. ->method('getIsDefaultBilling')
  97. ->willReturn($isDefaultBilling);
  98. $address->expects($this->any())
  99. ->method('getIsDefaultShipping')
  100. ->willReturn($isDefaultShipping);
  101. $address->expects($this->any())
  102. ->method('setForceProcess')
  103. ->with(true)
  104. ->willReturnSelf();
  105. $observer = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
  106. ->disableOriginalConstructor()
  107. ->setMethods([
  108. 'getCustomerAddress',
  109. ])
  110. ->getMock();
  111. $observer->expects($this->once())
  112. ->method('getCustomerAddress')
  113. ->willReturn($address);
  114. $this->helperAddress->expects($this->once())
  115. ->method('getTaxCalculationAddressType')
  116. ->willReturn($configAddressType);
  117. $this->registry->expects($this->once())
  118. ->method('registry')
  119. ->with(BeforeAddressSaveObserver::VIV_CURRENTLY_SAVED_ADDRESS)
  120. ->willReturn(true);
  121. $this->registry->expects($this->once())
  122. ->method('unregister')
  123. ->with(BeforeAddressSaveObserver::VIV_CURRENTLY_SAVED_ADDRESS)
  124. ->willReturnSelf();
  125. $this->registry->expects($this->any())
  126. ->method('register')
  127. ->willReturnMap([
  128. [BeforeAddressSaveObserver::VIV_CURRENTLY_SAVED_ADDRESS, $customerAddressId, false, $this->registry],
  129. [BeforeAddressSaveObserver::VIV_CURRENTLY_SAVED_ADDRESS, 'new_address', false, $this->registry],
  130. ]);
  131. $this->model->execute($observer);
  132. }
  133. /**
  134. * @return array
  135. */
  136. public function dataProviderBeforeAddressSaveWithoutCustomerAddressId()
  137. {
  138. return [
  139. [
  140. 'TaxCalculationAddressType' => AbstractAddress::TYPE_BILLING,
  141. 'isDefaultBilling' => true,
  142. 'isDefaultShipping' => false,
  143. ],
  144. [
  145. 'TaxCalculationAddressType' => AbstractAddress::TYPE_SHIPPING,
  146. 'isDefaultBilling' => false,
  147. 'isDefaultShipping' => true,
  148. ],
  149. ];
  150. }
  151. }