Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RemoveItemTest.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class RemoveItemTest extends \PHPUnit\Framework\TestCase
15 {
17  protected $removeItem;
18 
21 
23  protected $sidebarMock;
24 
26  protected $loggerMock;
27 
29  protected $jsonHelperMock;
30 
32  protected $requestMock;
33 
35  protected $responseMock;
36 
39 
43  private $resultRedirectFactory;
44 
45  protected function setUp()
46  {
47  $this->sidebarMock = $this->createMock(\Magento\Checkout\Model\Sidebar::class);
48  $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
49  $this->jsonHelperMock = $this->createMock(\Magento\Framework\Json\Helper\Data::class);
50  $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
51  $this->responseMock = $this->getMockForAbstractClass(
52  \Magento\Framework\App\ResponseInterface::class,
53  [],
54  '',
55  false,
56  true,
57  true,
58  ['representJson']
59  );
60  $this->resultPageFactoryMock = $this->createMock(\Magento\Framework\View\Result\PageFactory::class);
61  $this->resultRedirectFactory = $this->createPartialMock(
62  \Magento\Framework\Controller\Result\RedirectFactory::class,
63  ['create']
64  );
65 
66  $this->objectManagerHelper = new ObjectManagerHelper($this);
67  $this->removeItem = $this->objectManagerHelper->getObject(
68  \Magento\Checkout\Controller\Sidebar\RemoveItem::class,
69  [
70  'sidebar' => $this->sidebarMock,
71  'logger' => $this->loggerMock,
72  'jsonHelper' => $this->jsonHelperMock,
73  'request' => $this->requestMock,
74  'response' => $this->responseMock,
75  'resultPageFactory' => $this->resultPageFactoryMock,
76  'resultRedirectFactory' => $this->resultRedirectFactory
77 
78  ]
79  );
80  $formKeyValidatorMock = $this->createMock(\Magento\Framework\Data\Form\FormKey\Validator::class);
81  $this->setPropertyValue($this->removeItem, 'formKeyValidator', $formKeyValidatorMock);
82  }
83 
84  public function testExecute()
85  {
86  $this->getPropertyValue($this->removeItem, 'formKeyValidator')
87  ->expects($this->once())
88  ->method('validate')
89  ->with($this->requestMock)
90  ->willReturn(true);
91  $this->requestMock->expects($this->once())
92  ->method('getParam')
93  ->with('item_id', null)
94  ->willReturn('1');
95 
96  $this->sidebarMock->expects($this->once())
97  ->method('checkQuoteItem')
98  ->with(1)
99  ->willReturnSelf();
100  $this->sidebarMock->expects($this->once())
101  ->method('removeQuoteItem')
102  ->with(1)
103  ->willReturnSelf();
104  $this->sidebarMock->expects($this->once())
105  ->method('getResponseData')
106  ->with('')
107  ->willReturn(
108  [
109  'cleanup' => true,
110  'data' => [
111  'summary_qty' => 0,
112  'summary_text' => __(' items'),
113  'subtotal' => 0,
114  ],
115  ]
116  );
117 
118  $this->jsonHelperMock->expects($this->once())
119  ->method('jsonEncode')
120  ->with(
121  [
122  'cleanup' => true,
123  'data' => [
124  'summary_qty' => 0,
125  'summary_text' => __(' items'),
126  'subtotal' => 0,
127  ],
128  ]
129  )
130  ->willReturn('json encoded');
131 
132  $this->responseMock->expects($this->once())
133  ->method('representJson')
134  ->with('json encoded')
135  ->willReturn('json represented');
136 
137  $this->assertEquals('json represented', $this->removeItem->execute());
138  }
139 
141  {
142  $this->getPropertyValue($this->removeItem, 'formKeyValidator')
143  ->expects($this->once())
144  ->method('validate')
145  ->with($this->requestMock)
146  ->willReturn(true);
147  $this->requestMock->expects($this->once())
148  ->method('getParam')
149  ->with('item_id', null)
150  ->willReturn('1');
151 
152  $this->sidebarMock->expects($this->once())
153  ->method('checkQuoteItem')
154  ->with(1)
155  ->willThrowException(new LocalizedException(__('Error message!')));
156 
157  $this->sidebarMock->expects($this->once())
158  ->method('getResponseData')
159  ->with('Error message!')
160  ->willReturn(
161  [
162  'success' => false,
163  'error_message' => 'Error message!',
164  ]
165  );
166 
167  $this->jsonHelperMock->expects($this->once())
168  ->method('jsonEncode')
169  ->with(
170  [
171  'success' => false,
172  'error_message' => 'Error message!',
173  ]
174  )
175  ->willReturn('json encoded');
176 
177  $this->responseMock->expects($this->once())
178  ->method('representJson')
179  ->with('json encoded')
180  ->willReturn('json represented');
181 
182  $this->assertEquals('json represented', $this->removeItem->execute());
183  }
184 
185  public function testExecuteWithException()
186  {
187  $this->getPropertyValue($this->removeItem, 'formKeyValidator')
188  ->expects($this->once())
189  ->method('validate')
190  ->with($this->requestMock)
191  ->willReturn(true);
192  $this->requestMock->expects($this->once())
193  ->method('getParam')
194  ->with('item_id', null)
195  ->willReturn('1');
196 
197  $exception = new \Exception('Error message!');
198 
199  $this->sidebarMock->expects($this->once())
200  ->method('checkQuoteItem')
201  ->with(1)
202  ->willThrowException($exception);
203 
204  $this->loggerMock->expects($this->once())
205  ->method('critical')
206  ->with($exception)
207  ->willReturn(null);
208 
209  $this->sidebarMock->expects($this->once())
210  ->method('getResponseData')
211  ->with('Error message!')
212  ->willReturn(
213  [
214  'success' => false,
215  'error_message' => 'Error message!',
216  ]
217  );
218 
219  $this->jsonHelperMock->expects($this->once())
220  ->method('jsonEncode')
221  ->with(
222  [
223  'success' => false,
224  'error_message' => 'Error message!',
225  ]
226  )
227  ->willReturn('json encoded');
228 
229  $this->responseMock->expects($this->once())
230  ->method('representJson')
231  ->with('json encoded')
232  ->willReturn('json represented');
233 
234  $this->assertEquals('json represented', $this->removeItem->execute());
235  }
236 
238  {
239  $resultRedirect = $this->createMock(\Magento\Framework\Controller\Result\Redirect::class);
240  $resultRedirect->expects($this->once())->method('setPath')->with('*/cart/')->willReturnSelf();
241  $this->resultRedirectFactory->expects($this->once())->method('create')->willReturn($resultRedirect);
242  $this->getPropertyValue($this->removeItem, 'formKeyValidator')
243  ->expects($this->once())
244  ->method('validate')
245  ->with($this->requestMock)
246  ->willReturn(false);
247  $this->assertEquals($resultRedirect, $this->removeItem->execute());
248  }
249 
258  protected function getPropertyValue($object, $property)
259  {
260  $reflection = new \ReflectionClass(get_class($object));
261  $reflectionProperty = $reflection->getProperty($property);
262  $reflectionProperty->setAccessible(true);
263 
264  return $reflectionProperty->getValue($object);
265  }
266 
275  protected function setPropertyValue(&$object, $property, $value)
276  {
277  $reflection = new \ReflectionClass(get_class($object));
278  $reflectionProperty = $reflection->getProperty($property);
279  $reflectionProperty->setAccessible(true);
280  $reflectionProperty->setValue($object, $value);
281 
282  return $object;
283  }
284 }
__()
Definition: __.php:13
$value
Definition: gender.phtml:16