|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Catalog\Test\Unit\CustomerData; |
| 10 | + |
| 11 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 12 | +use Magento\Catalog\CustomerData\CompareProducts; |
| 13 | +use Magento\Catalog\Helper\Output; |
| 14 | +use Magento\Catalog\Helper\Product\Compare; |
| 15 | +use Magento\Catalog\Model\Product; |
| 16 | +use Magento\Catalog\Model\Product\Url; |
| 17 | +use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection; |
| 18 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 19 | + |
| 20 | +class CompareProductsTest extends \PHPUnit\Framework\TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var CompareProducts |
| 24 | + */ |
| 25 | + private $model; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Compare|\PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + private $helperMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Url|\PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + private $productUrlMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Output|\PHPUnit_Framework_MockObject_MockObject |
| 39 | + */ |
| 40 | + private $outputHelperMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager |
| 44 | + */ |
| 45 | + private $objectManagerHelper; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var array |
| 49 | + */ |
| 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 | + |
| 81 | + /** |
| 82 | + * Prepare compare items collection. |
| 83 | + * |
| 84 | + * @param array $items |
| 85 | + * @return \PHPUnit_Framework_MockObject_MockObject |
| 86 | + */ |
| 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 | + |
| 100 | + /** |
| 101 | + * Prepare product mocks objects and add corresponding method mocks for helpers. |
| 102 | + * |
| 103 | + * @param array $dataSet |
| 104 | + * @return array |
| 105 | + */ |
| 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 | + |
| 139 | + /** |
| 140 | + * Prepare mock of product object. |
| 141 | + * |
| 142 | + * @param array $data |
| 143 | + * @return \PHPUnit_Framework_MockObject_MockObject |
| 144 | + */ |
| 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 | + |
| 242 | + public function testGetSectionDataSingleItem() |
| 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 | +} |
0 commit comments