BillingAddressManagementTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Quote\Test\Unit\Model;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\Quote\Model\BillingAddressManagement;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class BillingAddressManagementTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  17. */
  18. protected $objectManager;
  19. /**
  20. * @var BillingAddressManagement
  21. */
  22. protected $model;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $quoteRepositoryMock;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $validatorMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $addressRepository;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $shippingAssignmentMock;
  39. /**
  40. * @return void
  41. */
  42. protected function setUp()
  43. {
  44. $this->objectManager = new ObjectManager($this);
  45. $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
  46. $this->validatorMock = $this->createMock(\Magento\Quote\Model\QuoteAddressValidator::class);
  47. $this->addressRepository = $this->createMock(\Magento\Customer\Api\AddressRepositoryInterface::class);
  48. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  49. $this->model = $this->objectManager->getObject(
  50. \Magento\Quote\Model\BillingAddressManagement::class,
  51. [
  52. 'quoteRepository' => $this->quoteRepositoryMock,
  53. 'addressValidator' => $this->validatorMock,
  54. 'logger' => $logger,
  55. 'addressRepository' => $this->addressRepository
  56. ]
  57. );
  58. $this->shippingAssignmentMock = $this->createPartialMock(
  59. \Magento\Quote\Model\ShippingAddressAssignment::class,
  60. ['setAddress']
  61. );
  62. $this->objectManager->setBackwardCompatibleProperty(
  63. $this->model,
  64. 'shippingAddressAssignment',
  65. $this->shippingAssignmentMock
  66. );
  67. }
  68. /**
  69. * @return void
  70. */
  71. public function testGetAddress()
  72. {
  73. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  74. $this->quoteRepositoryMock->expects($this->once())->method('getActive')
  75. ->with('cartId')->will($this->returnValue($quoteMock));
  76. $addressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  77. $quoteMock->expects($this->any())->method('getBillingAddress')->will($this->returnValue($addressMock));
  78. $this->assertEquals($addressMock, $this->model->get('cartId'));
  79. }
  80. /**
  81. * @return void
  82. */
  83. public function testSetAddress()
  84. {
  85. $cartId = 100;
  86. $useForShipping = true;
  87. $addressId = 1;
  88. $address = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, ['getId']);
  89. $quoteMock = $this->createPartialMock(
  90. \Magento\Quote\Model\Quote::class,
  91. ['removeAddress', 'getBillingAddress', 'setBillingAddress', 'setDataChanges']
  92. );
  93. $this->quoteRepositoryMock->expects($this->once())
  94. ->method('getActive')
  95. ->with($cartId)
  96. ->willReturn($quoteMock);
  97. $address->expects($this->exactly(2))->method('getId')->willReturn($addressId);
  98. $quoteMock->expects($this->exactly(2))->method('getBillingAddress')->willReturn($address);
  99. $quoteMock->expects($this->once())->method('removeAddress')->with($addressId)->willReturnSelf();
  100. $quoteMock->expects($this->once())->method('setBillingAddress')->with($address)->willReturnSelf();
  101. $quoteMock->expects($this->once())->method('setDataChanges')->with(1)->willReturnSelf();
  102. $this->shippingAssignmentMock->expects($this->once())
  103. ->method('setAddress')
  104. ->with($quoteMock, $address, $useForShipping);
  105. $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
  106. $this->assertEquals($addressId, $this->model->assign($cartId, $address, $useForShipping));
  107. }
  108. /**
  109. * @return void
  110. * @expectedException \Magento\Framework\Exception\InputException
  111. * @expectedExceptionMessage The address failed to save. Verify the address and try again.
  112. */
  113. public function testSetAddressWithInabilityToSaveQuote()
  114. {
  115. $cartId = 100;
  116. $addressId = 1;
  117. $address = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, ['getId']);
  118. $quoteMock = $this->createPartialMock(
  119. \Magento\Quote\Model\Quote::class,
  120. ['removeAddress', 'getBillingAddress', 'setBillingAddress', 'setDataChanges']
  121. );
  122. $this->quoteRepositoryMock->expects($this->once())
  123. ->method('getActive')
  124. ->with($cartId)
  125. ->willReturn($quoteMock);
  126. $address->expects($this->once())->method('getId')->willReturn($addressId);
  127. $quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($address);
  128. $quoteMock->expects($this->once())->method('removeAddress')->with($addressId)->willReturnSelf();
  129. $quoteMock->expects($this->once())->method('setBillingAddress')->with($address)->willReturnSelf();
  130. $quoteMock->expects($this->once())->method('setDataChanges')->with(1)->willReturnSelf();
  131. $this->shippingAssignmentMock->expects($this->once())
  132. ->method('setAddress')
  133. ->with($quoteMock, $address, false);
  134. $this->quoteRepositoryMock->expects($this->once())
  135. ->method('save')
  136. ->with($quoteMock)
  137. ->willThrowException(
  138. new \Exception('Some DB Error')
  139. );
  140. $this->model->assign($cartId, $address);
  141. }
  142. }