Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartTest.php
Go to the documentation of this file.
1 <?php
8 
12 class CartTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $model;
18 
23 
27  protected $catalogUrlMock;
28 
32  protected $checkoutCartMock;
33 
38 
43 
47  protected $layoutMock;
48 
49  protected function setUp()
50  {
51  $this->checkoutSessionMock = $this->createMock(\Magento\Checkout\Model\Session::class);
52  $this->catalogUrlMock = $this->createPartialMock(
53  \Magento\Catalog\Model\ResourceModel\Url::class,
54  ['getRewriteByProductStore']
55  );
56  $this->checkoutCartMock = $this->createMock(\Magento\Checkout\Model\Cart::class);
57  $this->checkoutHelperMock = $this->createMock(\Magento\Checkout\Helper\Data::class);
58  $this->layoutMock = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
59  $this->itemPoolInterfaceMock = $this->createMock(\Magento\Checkout\CustomerData\ItemPoolInterface::class);
60 
61  $this->model = new \Magento\Checkout\CustomerData\Cart(
62  $this->checkoutSessionMock,
63  $this->catalogUrlMock,
64  $this->checkoutCartMock,
65  $this->checkoutHelperMock,
66  $this->itemPoolInterfaceMock,
67  $this->layoutMock
68  );
69  }
70 
71  public function testIsGuestCheckoutAllowed()
72  {
73  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
74  $this->checkoutSessionMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
75  $this->checkoutHelperMock->expects($this->once())->method('isAllowedGuestCheckout')->with($quoteMock)
76  ->willReturn(true);
77 
78  $this->assertTrue($this->model->isGuestCheckoutAllowed());
79  }
80 
81  public function testGetSectionData()
82  {
83  $summaryQty = 100;
84  $subtotalValue = 200;
85  $productId = 10;
86  $storeId = 20;
87  $productRewrite = [$productId => ['rewrite' => 'product']];
88  $itemData = ['item' => 'data'];
89  $shortcutButtonsHtml = '<span>Buttons</span>';
90  $websiteId = 100;
91 
92  $subtotalMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getValue']);
93  $subtotalMock->expects($this->once())->method('getValue')->willReturn($subtotalValue);
94  $totals = ['subtotal' => $subtotalMock];
95 
96  $quoteMock = $this->createPartialMock(
97  \Magento\Quote\Model\Quote::class,
98  ['getTotals', 'getHasError', 'getAllVisibleItems', 'getStore']
99  );
100  $this->checkoutSessionMock->expects($this->exactly(2))->method('getQuote')->willReturn($quoteMock);
101  $quoteMock->expects($this->once())->method('getTotals')->willReturn($totals);
102  $quoteMock->expects($this->once())->method('getHasError')->willReturn(false);
103 
104  $this->checkoutCartMock->expects($this->once())->method('getSummaryQty')->willReturn($summaryQty);
105  $this->checkoutHelperMock->expects($this->once())
106  ->method('formatPrice')
107  ->with($subtotalValue)
108  ->willReturn($subtotalValue);
109  $this->checkoutHelperMock->expects($this->once())->method('canOnepageCheckout')->willReturn(true);
110 
111  $quoteItemMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Item::class, ['getProduct', 'getStoreId']);
112  $quoteMock->expects($this->once())->method('getAllVisibleItems')->willReturn([$quoteItemMock]);
113 
114  $storeMock = $this->createPartialMock(\Magento\Store\Model\System\Store::class, ['getWebsiteId']);
115  $storeMock->expects($this->once())->method('getWebsiteId')->willReturn($websiteId);
116  $quoteMock->expects($this->once())->method('getStore')->willReturn($storeMock);
117 
118  $productMock = $this->createPartialMock(
119  \Magento\Catalog\Model\Product::class,
120  ['isVisibleInSiteVisibility', 'getId', 'setUrlDataObject']
121  );
122  $quoteItemMock->expects($this->exactly(3))->method('getProduct')->willReturn($productMock);
123  $quoteItemMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
124 
125  $productMock->expects($this->once())->method('isVisibleInSiteVisibility')->willReturn(false);
126  $productMock->expects($this->exactly(3))->method('getId')->willReturn($productId);
127  $productMock->expects($this->once())
128  ->method('setUrlDataObject')
129  ->with(new \Magento\Framework\DataObject($productRewrite[$productId]))
130  ->willReturnSelf();
131 
132  $this->catalogUrlMock->expects($this->once())
133  ->method('getRewriteByProductStore')
134  ->with([$productId => $storeId])
135  ->willReturn($productRewrite);
136 
137  $this->itemPoolInterfaceMock->expects($this->once())
138  ->method('getItemData')
139  ->with($quoteItemMock)
140  ->willReturn($itemData);
141 
142  $shortcutButtonsMock = $this->createMock(\Magento\Catalog\Block\ShortcutButtons::class);
143  $this->layoutMock->expects($this->once())
144  ->method('createBlock')
145  ->with(\Magento\Catalog\Block\ShortcutButtons::class)
146  ->willReturn($shortcutButtonsMock);
147 
148  $shortcutButtonsMock->expects($this->once())->method('toHtml')->willReturn($shortcutButtonsHtml);
149  $this->checkoutHelperMock->expects($this->once())
150  ->method('isAllowedGuestCheckout')
151  ->with($quoteMock)
152  ->willReturn(true);
153 
154  $expectedResult = [
155  'summary_count' => 100,
156  'subtotal' => 200,
157  'possible_onepage_checkout' => 1,
158  'items' => [
159  ['item' => 'data']
160  ],
161  'extra_actions' => '<span>Buttons</span>',
162  'isGuestCheckoutAllowed' => 1,
163  'website_id' => $websiteId,
164  'subtotalAmount' => 200,
165  ];
166  $this->assertEquals($expectedResult, $this->model->getSectionData());
167  }
168 
173  {
174  $summaryQty = 100;
175  $subtotalValue = 200;
176  $productId = 10;
177  $storeId = 20;
178  $websiteId = 100;
179 
180  $productRewrite = [$productId => ['rewrite' => 'product']];
181  $itemData = ['item' => 'data'];
182  $shortcutButtonsHtml = '<span>Buttons</span>';
183  $subtotalMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getValue']);
184  $subtotalMock->expects($this->once())->method('getValue')->willReturn($subtotalValue);
185  $totals = ['subtotal' => $subtotalMock];
186 
187  $quoteMock = $this->createPartialMock(
188  \Magento\Quote\Model\Quote::class,
189  ['getTotals', 'getHasError', 'getAllVisibleItems', 'getStore']
190  );
191  $quoteItemMock = $this->createPartialMock(
192  \Magento\Quote\Model\Quote\Item::class,
193  ['getProduct', 'getOptionByCode', 'getStoreId']
194  );
195 
196  $this->checkoutSessionMock->expects($this->exactly(2))->method('getQuote')->willReturn($quoteMock);
197  $quoteMock->expects($this->once())->method('getTotals')->willReturn($totals);
198  $quoteMock->expects($this->once())->method('getHasError')->willReturn(false);
199 
200  $storeMock = $this->createPartialMock(\Magento\Store\Model\System\Store::class, ['getWebsiteId']);
201  $storeMock->expects($this->once())->method('getWebsiteId')->willReturn($websiteId);
202  $quoteMock->expects($this->once())->method('getStore')->willReturn($storeMock);
203 
204  $this->checkoutCartMock->expects($this->once())->method('getSummaryQty')->willReturn($summaryQty);
205  $this->checkoutHelperMock->expects($this->once())
206  ->method('formatPrice')
207  ->with($subtotalValue)
208  ->willReturn($subtotalValue);
209  $this->checkoutHelperMock->expects($this->once())->method('canOnepageCheckout')->willReturn(true);
210 
211  $quoteMock->expects($this->once())->method('getAllVisibleItems')->willReturn([$quoteItemMock]);
212 
213  $productMock = $this->createPartialMock(
214  \Magento\Catalog\Model\Product::class,
215  ['isVisibleInSiteVisibility', 'getId', 'setUrlDataObject']
216  );
217 
218  $optionsMock = $this->createMock(\Magento\Quote\Model\Quote\Item\Option::class);
219  $optionsMock->expects($this->once())->method('getProduct')->willReturn($productMock);
220 
221  $quoteItemMock->expects($this->exactly(2))->method('getProduct')->willReturn($productMock);
222  $quoteItemMock->expects($this->exactly(2))
223  ->method('getOptionByCode')
224  ->with('product_type')
225  ->willReturn($optionsMock);
226  $quoteItemMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
227 
228  $productMock->expects($this->once())->method('isVisibleInSiteVisibility')->willReturn(false);
229  $productMock->expects($this->exactly(3))->method('getId')->willReturn($productId);
230  $productMock->expects($this->once())
231  ->method('setUrlDataObject')
232  ->with(new \Magento\Framework\DataObject($productRewrite[$productId]))
233  ->willReturnSelf();
234 
235  $this->catalogUrlMock->expects($this->once())
236  ->method('getRewriteByProductStore')
237  ->with([$productId => $storeId])
238  ->willReturn($productRewrite);
239 
240  $shortcutButtonsMock = $this->createMock(\Magento\Catalog\Block\ShortcutButtons::class);
241  $this->layoutMock->expects($this->once())
242  ->method('createBlock')
243  ->with(\Magento\Catalog\Block\ShortcutButtons::class)
244  ->willReturn($shortcutButtonsMock);
245 
246  $shortcutButtonsMock->expects($this->once())->method('toHtml')->willReturn($shortcutButtonsHtml);
247  $this->checkoutHelperMock->expects($this->once())
248  ->method('isAllowedGuestCheckout')
249  ->with($quoteMock)
250  ->willReturn(true);
251 
252  $this->itemPoolInterfaceMock->expects($this->once())
253  ->method('getItemData')
254  ->with($quoteItemMock)
255  ->willReturn($itemData);
256 
257  $expectedResult = [
258  'summary_count' => 100,
259  'subtotal' => 200,
260  'possible_onepage_checkout' => 1,
261  'items' => [
262  ['item' => 'data']
263  ],
264  'extra_actions' => '<span>Buttons</span>',
265  'isGuestCheckoutAllowed' => 1,
266  'website_id' => $websiteId,
267  'subtotalAmount' => 200,
268  ];
269  $this->assertEquals($expectedResult, $this->model->getSectionData());
270  }
271 }
$totals
Definition: totalbar.phtml:10