Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SidebarTest.php
Go to the documentation of this file.
1 <?php
8 
10 
11 class SidebarTest extends \PHPUnit\Framework\TestCase
12 {
14  protected $sidebar;
15 
17  protected $cartMock;
18 
21 
23  protected $resolverMock;
24 
25  protected function setUp()
26  {
27  $this->cartMock = $this->createMock(\Magento\Checkout\Model\Cart::class);
28  $this->checkoutHelperMock = $this->createMock(\Magento\Checkout\Helper\Data::class);
29  $this->resolverMock = $this->createMock(\Magento\Framework\Locale\ResolverInterface::class);
30 
31  $this->sidebar = new Sidebar(
32  $this->cartMock,
33  $this->checkoutHelperMock,
34  $this->resolverMock
35  );
36  }
37 
44  public function testGetResponseData($error, $result)
45  {
46  $this->assertEquals($result, $this->sidebar->getResponseData($error));
47  }
48 
52  public function dataProviderGetResponseData()
53  {
54  return [
55  [
56  '',
57  ['success' => true],
58  ],
59  [
60  '',
61  ['success' => true],
62  ],
63  [
64  '',
65  ['success' => true],
66  ],
67  [
68  'Error',
69  [
70  'success' => false,
71  'error_message' => 'Error',
72  ],
73  ],
74  ];
75  }
76 
77  public function testCheckQuoteItem()
78  {
79  $itemId = 1;
80 
81  $itemMock = $this->getMockBuilder(\Magento\Quote\Api\Data\CartItemInterface::class)
82  ->getMock();
83 
84  $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
85  ->disableOriginalConstructor()
86  ->getMock();
87  $quoteMock->expects($this->once())
88  ->method('getItemById')
89  ->with($itemId)
90  ->willReturn($itemMock);
91 
92  $this->cartMock->expects($this->any())
93  ->method('getQuote')
94  ->willReturn($quoteMock);
95 
96  $this->assertEquals($this->sidebar, $this->sidebar->checkQuoteItem($itemId));
97  }
98 
104  {
105  $itemId = 2;
106 
107  $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
108  ->disableOriginalConstructor()
109  ->getMock();
110  $quoteMock->expects($this->once())
111  ->method('getItemById')
112  ->with($itemId)
113  ->willReturn(null);
114 
115  $this->cartMock->expects($this->any())
116  ->method('getQuote')
117  ->willReturn($quoteMock);
118 
119  $this->sidebar->checkQuoteItem($itemId);
120  }
121 
122  public function testRemoveQuoteItem()
123  {
124  $itemId = 1;
125 
126  $this->cartMock->expects($this->once())
127  ->method('removeItem')
128  ->with($itemId)
129  ->willReturnSelf();
130  $this->cartMock->expects($this->once())
131  ->method('save')
132  ->willReturnSelf();
133 
134  $this->assertEquals($this->sidebar, $this->sidebar->removeQuoteItem($itemId));
135  }
136 
137  public function testUpdateQuoteItem()
138  {
139  $itemId = 1;
140  $itemQty = 2;
141 
142  $this->resolverMock->expects($this->once())
143  ->method('getLocale')
144  ->willReturn('en');
145 
146  $this->cartMock->expects($this->once())
147  ->method('updateItems')
148  ->with([$itemId => ['qty' => $itemQty]])
149  ->willReturnSelf();
150  $this->cartMock->expects($this->once())
151  ->method('save')
152  ->willReturnSelf();
153 
154  $this->assertEquals($this->sidebar, $this->sidebar->updateQuoteItem($itemId, $itemQty));
155  }
156 
158  {
159  $itemId = 1;
160  $itemQty = 0;
161 
162  $this->resolverMock->expects($this->never())
163  ->method('getLocale');
164 
165  $this->cartMock->expects($this->once())
166  ->method('updateItems')
167  ->with([$itemId => ['qty' => $itemQty]])
168  ->willReturnSelf();
169  $this->cartMock->expects($this->once())
170  ->method('save')
171  ->willReturnSelf();
172 
173  $this->assertEquals($this->sidebar, $this->sidebar->updateQuoteItem($itemId, $itemQty));
174  }
175 }