Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FreeShippingTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
17 use PHPUnit_Framework_MockObject_MockObject as MockObject;
18 
19 class FreeShippingTest extends \PHPUnit\Framework\TestCase
20 {
21  private static $websiteId = 1;
22 
23  private static $customerGroupId = 2;
24 
25  private static $couponCode = 3;
26 
27  private static $storeId = 1;
28 
32  private $model;
33 
37  private $storeManager;
38 
42  private $calculator;
43 
44  protected function setUp()
45  {
46  $this->storeManager = $this->createMock(StoreManagerInterface::class);
47  $this->calculator = $this->createMock(Calculator::class);
48 
49  $this->model = new FreeShipping(
50  $this->storeManager,
51  $this->calculator
52  );
53  }
54 
64  public function testIsFreeShipping(int $addressFree, int $fItemFree, int $sItemFree, bool $expected)
65  {
66  $address = $this->getShippingAddress();
67  $this->withStore();
68  $quote = $this->getQuote($address);
69  $fItem = $this->getItem($quote);
70  $sItem = $this->getItem($quote);
71  $items = [$fItem, $sItem];
72 
73  $this->calculator->method('init')
74  ->with(self::$websiteId, self::$customerGroupId, self::$couponCode);
75  $this->calculator->method('processFreeShipping')
76  ->withConsecutive(
77  [$fItem],
78  [$sItem]
79  )
80  ->willReturnCallback(function () use ($fItem, $sItem, $addressFree, $fItemFree, $sItemFree) {
81  // emulate behavior of cart rule calculator
82  $fItem->getAddress()->setFreeShipping($addressFree);
83  $fItem->setFreeShipping($fItemFree);
84  $sItem->setFreeShipping($sItemFree);
85  });
86 
87  $actual = $this->model->isFreeShipping($quote, $items);
88  self::assertEquals($expected, $actual);
89  self::assertEquals($expected, $address->getFreeShipping());
90  }
91 
97  public function itemsDataProvider(): array
98  {
99  return [
100  ['addressFree' => 1, 'fItemFree' => 0, 'sItemFree' => 0, 'expected' => true],
101  ['addressFree' => 0, 'fItemFree' => 1, 'sItemFree' => 0, 'expected' => false],
102  ['addressFree' => 0, 'fItemFree' => 0, 'sItemFree' => 1, 'expected' => false],
103  ['addressFree' => 0, 'fItemFree' => 1, 'sItemFree' => 1, 'expected' => true],
104  ];
105  }
106 
110  private function withStore()
111  {
112  $store = $this->getMockBuilder(StoreInterface::class)
113  ->disableOriginalConstructor()
114  ->getMock();
115  $this->storeManager->method('getStore')
116  ->with(self::$storeId)
117  ->willReturn($store);
118 
119  $store->method('getWebsiteId')
120  ->willReturn(self::$websiteId);
121  }
122 
129  private function getQuote(Address $address): Quote
130  {
132  $quote = $this->getMockBuilder(Quote::class)
133  ->disableOriginalConstructor()
134  ->setMethods(
135  [
136  'getCouponCode', 'getCustomerGroupId', 'getShippingAddress', 'getStoreId', 'getItemsQty',
137  'getVirtualItemsQty'
138  ]
139  )
140  ->getMock();
141 
142  $quote->method('getStoreId')
143  ->willReturn(self::$storeId);
144  $quote->method('getCustomerGroupId')
145  ->willReturn(self::$customerGroupId);
146  $quote->method('getCouponCode')
147  ->willReturn(self::$couponCode);
148  $quote->method('getShippingAddress')
149  ->willReturn($address);
150  $quote->method('getItemsQty')
151  ->willReturn(2);
152  $quote->method('getVirtualItemsQty')
153  ->willReturn(0);
154 
155  return $quote;
156  }
157 
163  private function getShippingAddress(): Address
164  {
166  $address = $this->getMockBuilder(Address::class)
167  ->disableOriginalConstructor()
168  ->setMethods(['beforeSave'])
169  ->getMock();
170 
171  return $address;
172  }
173 
180  private function getItem(Quote $quote): Item
181  {
183  $item = $this->getMockBuilder(Item::class)
184  ->disableOriginalConstructor()
185  ->setMethods(['getHasChildren'])
186  ->getMock();
187  $item->setQuote($quote);
188  $item->setNoDiscount(0);
189  $item->setParentItemId(0);
190  $item->method('getHasChildren')
191  ->willReturn(0);
192 
193  return $item;
194  }
195 }
$quote
$address
Definition: customer.php:38
testIsFreeShipping(int $addressFree, int $fItemFree, int $sItemFree, bool $expected)
$items