CollectQuoteTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Checkout\Test\Unit\Model\Cart;
  8. use Magento\Customer\Api\AddressRepositoryInterface;
  9. use Magento\Customer\Api\CustomerRepositoryInterface;
  10. use Magento\Customer\Api\Data\AddressInterface;
  11. use Magento\Customer\Api\Data\CustomerInterface;
  12. use Magento\Customer\Api\Data\RegionInterface;
  13. use Magento\Checkout\Model\Cart\CollectQuote;
  14. use Magento\Customer\Model\Session as CustomerSession;
  15. use Magento\Quote\Api\CartRepositoryInterface;
  16. use Magento\Quote\Api\Data\EstimateAddressInterface;
  17. use Magento\Quote\Api\Data\EstimateAddressInterfaceFactory;
  18. use Magento\Quote\Api\ShippingMethodManagementInterface;
  19. use Magento\Quote\Model\Quote;
  20. use PHPUnit\Framework\TestCase;
  21. /**
  22. * Class CollectQuoteTest
  23. */
  24. class CollectQuoteTest extends TestCase
  25. {
  26. /**
  27. * @var CollectQuote
  28. */
  29. private $model;
  30. /**
  31. * @var CustomerSession|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $customerSessionMock;
  34. /**
  35. * @var CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $customerRepositoryMock;
  38. /**
  39. * @var AddressRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $addressRepositoryMock;
  42. /**
  43. * @var EstimateAddressInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $estimateAddressFactoryMock;
  46. /**
  47. * @var EstimateAddressInterface|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $estimateAddressMock;
  50. /**
  51. * @var ShippingMethodManagementInterface|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $shippingMethodManagerMock;
  54. /**
  55. * @var CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. private $quoteRepositoryMock;
  58. /**
  59. * @var Quote|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. private $quoteMock;
  62. /**
  63. * @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
  64. */
  65. private $customerMock;
  66. /**
  67. * @var AddressInterface|\PHPUnit_Framework_MockObject_MockObject
  68. */
  69. private $addressMock;
  70. /**
  71. * Set up
  72. */
  73. protected function setUp()
  74. {
  75. $this->customerSessionMock = $this->createMock(CustomerSession::class);
  76. $this->customerRepositoryMock = $this->getMockForAbstractClass(
  77. CustomerRepositoryInterface::class,
  78. [],
  79. '',
  80. false,
  81. true,
  82. true,
  83. ['getById']
  84. );
  85. $this->addressRepositoryMock = $this->createMock(AddressRepositoryInterface::class);
  86. $this->estimateAddressMock = $this->createMock(EstimateAddressInterface::class);
  87. $this->estimateAddressFactoryMock =
  88. $this->createPartialMock(EstimateAddressInterfaceFactory::class, ['create']);
  89. $this->shippingMethodManagerMock = $this->createMock(ShippingMethodManagementInterface::class);
  90. $this->quoteRepositoryMock = $this->createMock(CartRepositoryInterface::class);
  91. $this->quoteMock = $this->createMock(Quote::class);
  92. $this->customerMock = $this->createMock(CustomerInterface::class);
  93. $this->addressMock = $this->createMock(AddressInterface::class);
  94. $this->model = new CollectQuote(
  95. $this->customerSessionMock,
  96. $this->customerRepositoryMock,
  97. $this->addressRepositoryMock,
  98. $this->estimateAddressFactoryMock,
  99. $this->shippingMethodManagerMock,
  100. $this->quoteRepositoryMock
  101. );
  102. }
  103. /**
  104. * Test collect method
  105. */
  106. public function testCollect()
  107. {
  108. $customerId = 1;
  109. $defaultAddressId = 999;
  110. $countryId = 'USA';
  111. $regionId = 'CA';
  112. $regionMock = $this->createMock(RegionInterface::class);
  113. $this->customerSessionMock->expects(self::once())
  114. ->method('isLoggedIn')
  115. ->willReturn(true);
  116. $this->customerSessionMock->expects(self::once())
  117. ->method('getCustomerId')
  118. ->willReturn($customerId);
  119. $this->customerRepositoryMock->expects(self::once())
  120. ->method('getById')
  121. ->willReturn($this->customerMock);
  122. $this->customerMock->expects(self::once())
  123. ->method('getDefaultShipping')
  124. ->willReturn($defaultAddressId);
  125. $this->addressMock->expects(self::once())
  126. ->method('getCountryId')
  127. ->willReturn($countryId);
  128. $regionMock->expects(self::once())
  129. ->method('getRegion')
  130. ->willReturn($regionId);
  131. $this->addressMock->expects(self::once())
  132. ->method('getRegion')
  133. ->willReturn($regionMock);
  134. $this->addressRepositoryMock->expects(self::once())
  135. ->method('getById')
  136. ->with($defaultAddressId)
  137. ->willReturn($this->addressMock);
  138. $this->estimateAddressFactoryMock->expects(self::once())
  139. ->method('create')
  140. ->willReturn($this->estimateAddressMock);
  141. $this->quoteRepositoryMock->expects(self::once())
  142. ->method('save')
  143. ->with($this->quoteMock);
  144. $this->model->collect($this->quoteMock);
  145. }
  146. /**
  147. * Test with a not logged in customer
  148. */
  149. public function testCollectWhenCustomerIsNotLoggedIn()
  150. {
  151. $this->customerSessionMock->expects(self::once())
  152. ->method('isLoggedIn')
  153. ->willReturn(false);
  154. $this->customerRepositoryMock->expects(self::never())
  155. ->method('getById');
  156. $this->model->collect($this->quoteMock);
  157. }
  158. }