Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShippingMethodUpdaterTest.php
Go to the documentation of this file.
1 <?php
7 
13 
19 class ShippingMethodUpdaterTest extends \PHPUnit\Framework\TestCase
20 {
21  const TEST_SHIPPING_METHOD = 'test-shipping-method';
22 
24 
28  private $configMock;
29 
33  private $quoteRepositoryMock;
34 
38  private $shippingAddressMock;
39 
43  private $shippingMethodUpdater;
44 
45  protected function setUp()
46  {
47  $this->configMock = $this->getMockBuilder(Config::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50  $this->quoteRepositoryMock = $this->getMockBuilder(CartRepositoryInterface::class)
51  ->getMockForAbstractClass();
52 
53  $this->shippingAddressMock = $this->getMockBuilder(Address::class)
54  ->setMethods(
55  [
56  'setShouldIgnoreValidation',
57  'getShippingMethod',
58  'setShippingMethod',
59  'setCollectShippingRates'
60  ]
61  )->disableOriginalConstructor()
62  ->getMock();
63 
64  $this->shippingMethodUpdater = new ShippingMethodUpdater(
65  $this->configMock,
66  $this->quoteRepositoryMock
67  );
68  }
69 
74  public function testExecuteException()
75  {
76  $quoteMock = $this->getQuoteMock();
77 
78  $this->shippingMethodUpdater->execute('', $quoteMock);
79  }
80 
81  public function testExecute()
82  {
83  $quoteMock = $this->getQuoteMock();
84 
85  $quoteMock->expects(self::exactly(2))
86  ->method('getIsVirtual')
87  ->willReturn(false);
88 
89  $quoteMock->expects(self::exactly(2))
90  ->method('getShippingAddress')
91  ->willReturn($this->shippingAddressMock);
92 
93  $this->shippingAddressMock->expects(self::once())
94  ->method('getShippingMethod')
95  ->willReturn(self::TEST_SHIPPING_METHOD . '-bad');
96 
97  $this->disabledQuoteAddressValidationStep($quoteMock);
98 
99  $this->shippingAddressMock->expects(self::once())
100  ->method('setShippingMethod')
101  ->willReturn(self::TEST_SHIPPING_METHOD);
102  $this->shippingAddressMock->expects(self::once())
103  ->method('setCollectShippingRates')
104  ->willReturn(true);
105 
106  $quoteMock->expects(self::once())
107  ->method('collectTotals');
108 
109  $this->quoteRepositoryMock->expects(self::once())
110  ->method('save')
111  ->with($quoteMock);
112 
113  $this->shippingMethodUpdater->execute(self::TEST_SHIPPING_METHOD, $quoteMock);
114  }
115 
119  private function disabledQuoteAddressValidationStep(\PHPUnit_Framework_MockObject_MockObject $quoteMock)
120  {
121  $billingAddressMock = $this->getBillingAddressMock($quoteMock);
122 
123  $billingAddressMock->expects(self::once())
124  ->method('setShouldIgnoreValidation')
125  ->with(true)
126  ->willReturnSelf();
127 
128  $this->shippingAddressMock->expects(self::once())
129  ->method('setShouldIgnoreValidation')
130  ->with(true)
131  ->willReturnSelf();
132 
133  $billingAddressMock->expects(self::at(1))
134  ->method('getEmail')
135  ->willReturn(self::TEST_EMAIL);
136 
137  $billingAddressMock->expects(self::never())
138  ->method('setSameAsBilling');
139  }
140 
145  private function getBillingAddressMock(\PHPUnit_Framework_MockObject_MockObject $quoteMock)
146  {
147  if (!isset($this->billingAddressMock)) {
148  $this->billingAddressMock = $this->getMockBuilder(Address::class)
149  ->setMethods(['setShouldIgnoreValidation', 'getEmail', 'setSameAsBilling'])
150  ->disableOriginalConstructor()
151  ->getMock();
152  }
153 
154  $quoteMock->expects(self::any())
155  ->method('getBillingAddress')
156  ->willReturn($this->billingAddressMock);
157 
158  return $this->billingAddressMock;
159  }
160 
164  private function getQuoteMock()
165  {
166  return $this->getMockBuilder(Quote::class)
167  ->setMethods(
168  [
169  'collectTotals',
170  'getBillingAddress',
171  'getShippingAddress',
172  'getIsVirtual'
173  ]
174  )->disableOriginalConstructor()
175  ->getMock();
176  }
177 }