Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CompareProductsTest.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
19 
20 class CompareProductsTest extends \PHPUnit\Framework\TestCase
21 {
25  private $model;
26 
30  private $helperMock;
31 
35  private $productUrlMock;
36 
40  private $outputHelperMock;
41 
45  private $objectManagerHelper;
46 
50  private $productValueMap = [
51  'id' => 'getId',
52  ProductInterface::NAME => 'getName'
53  ];
54 
55  protected function setUp()
56  {
57  parent::setUp();
58 
59  $this->helperMock = $this->getMockBuilder(Compare::class)
60  ->disableOriginalConstructor()
61  ->getMock();
62  $this->productUrlMock = $this->getMockBuilder(Url::class)
63  ->disableOriginalConstructor()
64  ->getMock();
65  $this->outputHelperMock = $this->getMockBuilder(Output::class)
66  ->disableOriginalConstructor()
67  ->getMock();
68 
69  $this->objectManagerHelper = new ObjectManagerHelper($this);
70 
71  $this->model = $this->objectManagerHelper->getObject(
72  CompareProducts::class,
73  [
74  'helper' => $this->helperMock,
75  'productUrl' => $this->productUrlMock,
76  'outputHelper' => $this->outputHelperMock
77  ]
78  );
79  }
80 
87  private function getItemCollectionMock(array $items) : \PHPUnit_Framework_MockObject_MockObject
88  {
89  $itemCollectionMock = $this->getMockBuilder(Collection::class)
90  ->disableOriginalConstructor()
91  ->getMock();
92 
93  $itemCollectionMock->expects($this->any())
94  ->method('getIterator')
95  ->willReturn(new \ArrayIterator($items));
96 
97  return $itemCollectionMock;
98  }
99 
106  private function prepareProductsWithCorrespondingMocks(array $dataSet) : array
107  {
108  $items = [];
109  $urlMap = [];
110  $outputMap = [];
111  $helperMap = [];
112 
113  $count = count($dataSet);
114 
115  foreach ($dataSet as $data) {
116  $item = $this->getProductMock($data);
117  $items[] = $item;
118 
119  $outputMap[] = [$item, $data['name'], 'name', 'productName#' . $data['id']];
120  $helperMap[] = [$item, 'http://remove.url/' . $data['id']];
121  $urlMap[] = [$item, [], 'http://product.url/' . $data['id']];
122  }
123 
124  $this->productUrlMock->expects($this->exactly($count))
125  ->method('getUrl')
126  ->will($this->returnValueMap($urlMap));
127 
128  $this->outputHelperMock->expects($this->exactly($count))
129  ->method('productAttribute')
130  ->will($this->returnValueMap($outputMap));
131 
132  $this->helperMock->expects($this->exactly($count))
133  ->method('getPostDataRemove')
134  ->will($this->returnValueMap($helperMap));
135 
136  return $items;
137  }
138 
145  private function getProductMock(array $data) : \PHPUnit_Framework_MockObject_MockObject
146  {
147  $product = $this->getMockBuilder(Product::class)
148  ->disableOriginalConstructor()
149  ->getMock();
150 
151  foreach ($data as $index => $value) {
152  $product->expects($this->once())
153  ->method($this->productValueMap[$index])
154  ->willReturn($value);
155  }
156 
157  return $product;
158  }
159 
160  public function testGetSectionData()
161  {
162  $dataSet = [
163  ['id' => 1, 'name' => 'product#1'],
164  ['id' => 2, 'name' => 'product#2'],
165  ['id' => 3, 'name' => 'product#3']
166  ];
167 
168  $count = count($dataSet);
169 
170  $this->helperMock->expects($this->once())
171  ->method('getItemCount')
172  ->willReturn($count);
173 
174  $items = $this->prepareProductsWithCorrespondingMocks($dataSet);
175 
176  $itemCollectionMock = $this->getItemCollectionMock($items);
177 
178  $this->helperMock->expects($this->once())
179  ->method('getItemCollection')
180  ->willReturn($itemCollectionMock);
181 
182  $this->helperMock->expects($this->once())
183  ->method('getListUrl')
184  ->willReturn('http://list.url');
185 
186  $this->assertEquals(
187  [
188  'count' => $count,
189  'countCaption' => __('%1 items', $count),
190  'listUrl' => 'http://list.url',
191  'items' => [
192  [
193  'id' => 1,
194  'product_url' => 'http://product.url/1',
195  'name' => 'productName#1',
196  'remove_url' => 'http://remove.url/1'
197  ],
198  [
199  'id' => 2,
200  'product_url' => 'http://product.url/2',
201  'name' => 'productName#2',
202  'remove_url' => 'http://remove.url/2'
203  ],
204  [
205  'id' => 3,
206  'product_url' => 'http://product.url/3',
207  'name' => 'productName#3',
208  'remove_url' => 'http://remove.url/3'
209  ]
210  ]
211  ],
212  $this->model->getSectionData()
213  );
214  }
215 
216  public function testGetSectionDataNoItems()
217  {
218  $count = 0;
219 
220  $this->helperMock->expects($this->once())
221  ->method('getItemCount')
222  ->willReturn($count);
223 
224  $this->helperMock->expects($this->never())
225  ->method('getItemCollection');
226 
227  $this->helperMock->expects($this->once())
228  ->method('getListUrl')
229  ->willReturn('http://list.url');
230 
231  $this->assertEquals(
232  [
233  'count' => $count,
234  'countCaption' => __('%1 items', $count),
235  'listUrl' => 'http://list.url',
236  'items' => []
237  ],
238  $this->model->getSectionData()
239  );
240  }
241 
243  {
244  $count = 1;
245 
246  $this->helperMock->expects($this->once())
247  ->method('getItemCount')
248  ->willReturn($count);
249 
250  $items = $this->prepareProductsWithCorrespondingMocks(
251  [
252  [
253  'id' => 12345,
254  'name' => 'SingleProduct'
255  ]
256  ]
257  );
258 
259  $itemCollectionMock = $this->getItemCollectionMock($items);
260 
261  $this->helperMock->expects($this->once())
262  ->method('getItemCollection')
263  ->willReturn($itemCollectionMock);
264 
265  $this->helperMock->expects($this->once())
266  ->method('getListUrl')
267  ->willReturn('http://list.url');
268 
269  $this->assertEquals(
270  [
271  'count' => 1,
272  'countCaption' => __('1 item'),
273  'listUrl' => 'http://list.url',
274  'items' => [
275  [
276  'id' => 12345,
277  'product_url' => 'http://product.url/12345',
278  'name' => 'productName#12345',
279  'remove_url' => 'http://remove.url/12345'
280  ]
281  ]
282  ],
283  $this->model->getSectionData()
284  );
285  }
286 }
$count
Definition: recent.phtml:13
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
$index
Definition: list.phtml:44
$items