|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Reports\Test\Unit\Model\ResourceModel\Product; |
| 8 | + |
| 9 | +use Magento\Catalog\Model\Indexer\Product\Flat\State; |
| 10 | +use Magento\Catalog\Model\Product\Attribute\DefaultAttributes; |
| 11 | +use Magento\Catalog\Model\Product\OptionFactory; |
| 12 | +use Magento\Catalog\Model\Product\Type as ProductType; |
| 13 | +use Magento\Catalog\Model\ResourceModel\Helper; |
| 14 | +use Magento\Catalog\Model\ResourceModel\Product as ResourceProduct; |
| 15 | +use Magento\Catalog\Model\ResourceModel\Url; |
| 16 | +use Magento\Customer\Api\GroupManagementInterface; |
| 17 | +use Magento\Customer\Model\Session; |
| 18 | +use Magento\Eav\Model\Config; |
| 19 | +use Magento\Eav\Model\Entity\Context; |
| 20 | +use Magento\Eav\Model\Entity\Type; |
| 21 | +use Magento\Eav\Model\EntityFactory as EavEntityFactory; |
| 22 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 23 | +use Magento\Framework\App\ResourceConnection; |
| 24 | +use Magento\Framework\Data\Collection\Db\FetchStrategyInterface; |
| 25 | +use Magento\Framework\Data\Collection\EntityFactory; |
| 26 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 27 | +use Magento\Framework\DB\Select; |
| 28 | +use Magento\Framework\Event\ManagerInterface; |
| 29 | +use Magento\Framework\Module\Manager; |
| 30 | +use Magento\Framework\Stdlib\DateTime; |
| 31 | +use Magento\Framework\Stdlib\DateTime\TimezoneInterface; |
| 32 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 33 | +use Magento\Framework\Validator\UniversalFactory; |
| 34 | +use Magento\Quote\Model\ResourceModel\Quote\Collection; |
| 35 | +use Magento\Reports\Model\Event\TypeFactory; |
| 36 | +use Magento\Reports\Model\ResourceModel\Product\Collection as ProductCollection; |
| 37 | +use Magento\Store\Model\StoreManagerInterface; |
| 38 | +use Psr\Log\LoggerInterface; |
| 39 | + |
| 40 | +/** |
| 41 | + * Test for Magento\Reports\Model\ResourceModel\Product\Collection. |
| 42 | + * |
| 43 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 44 | + * |
| 45 | + */ |
| 46 | +class CollectionTest extends \PHPUnit_Framework_TestCase |
| 47 | +{ |
| 48 | + /** |
| 49 | + * @var ProductCollection |
| 50 | + */ |
| 51 | + private $collection; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 55 | + */ |
| 56 | + private $eventTypeFactoryMock; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var ObjectManager |
| 60 | + */ |
| 61 | + private $objectManager; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 65 | + */ |
| 66 | + private $connectionMock; |
| 67 | + |
| 68 | + /** |
| 69 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 70 | + */ |
| 71 | + private $resourceMock; |
| 72 | + |
| 73 | + /** |
| 74 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 75 | + */ |
| 76 | + private $selectMock; |
| 77 | + |
| 78 | + protected function setUp() |
| 79 | + { |
| 80 | + $this->objectManager = new ObjectManager($this); |
| 81 | + $context = $this->createPartialMock(Context::class, ['getResource', 'getEavConfig']); |
| 82 | + $entityFactoryMock = $this->createMock(EntityFactory::class); |
| 83 | + $loggerMock = $this->createMock(LoggerInterface::class); |
| 84 | + $fetchStrategyMock = $this->createMock(FetchStrategyInterface::class); |
| 85 | + $eventManagerMock = $this->createMock(ManagerInterface::class); |
| 86 | + $eavConfigMock = $this->createMock(Config::class); |
| 87 | + $this->resourceMock = $this->createPartialMock(ResourceConnection::class, ['getTableName', 'getConnection']); |
| 88 | + $eavEntityFactoryMock = $this->createMock(EavEntityFactory::class); |
| 89 | + $resourceHelperMock = $this->createMock(Helper::class); |
| 90 | + $universalFactoryMock = $this->createMock(UniversalFactory::class); |
| 91 | + $storeManagerMock = $this->createPartialMockForAbstractClass( |
| 92 | + StoreManagerInterface::class, |
| 93 | + ['getStore', 'getId'] |
| 94 | + ); |
| 95 | + $moduleManagerMock = $this->createMock(Manager::class); |
| 96 | + $productFlatStateMock = $this->createMock(State::class); |
| 97 | + $scopeConfigMock = $this->createMock(ScopeConfigInterface::class); |
| 98 | + $optionFactoryMock = $this->createMock(OptionFactory::class); |
| 99 | + $catalogUrlMock = $this->createMock(Url::class); |
| 100 | + $localeDateMock = $this->createMock(TimezoneInterface::class); |
| 101 | + $customerSessionMock = $this->createMock(Session::class); |
| 102 | + $dateTimeMock = $this->createMock(DateTime::class); |
| 103 | + $groupManagementMock = $this->createMock(GroupManagementInterface::class); |
| 104 | + $eavConfig = $this->createPartialMock(Config::class, ['getEntityType']); |
| 105 | + $entityType = $this->createMock(Type::class); |
| 106 | + |
| 107 | + $eavConfig->expects($this->atLeastOnce())->method('getEntityType')->willReturn($entityType); |
| 108 | + $context->expects($this->atLeastOnce())->method('getResource')->willReturn($this->resourceMock); |
| 109 | + $context->expects($this->atLeastOnce())->method('getEavConfig')->willReturn($eavConfig); |
| 110 | + |
| 111 | + $defaultAttributes = $this->createPartialMock(DefaultAttributes::class, ['_getDefaultAttributes']); |
| 112 | + $productMock = $this->objectManager->getObject( |
| 113 | + ResourceProduct::class, |
| 114 | + ['context' => $context, 'defaultAttributes' => $defaultAttributes] |
| 115 | + ); |
| 116 | + |
| 117 | + $this->eventTypeFactoryMock = $this->createPartialMock(TypeFactory::class, ['create']); |
| 118 | + $productTypeMock = $this->createMock(ProductType::class); |
| 119 | + $quoteResourceMock = $this->createMock(Collection::class); |
| 120 | + $this->connectionMock = $this->createPartialMockForAbstractClass(AdapterInterface::class, ['select']); |
| 121 | + $this->selectMock = $this->createPartialMock( |
| 122 | + Select::class, |
| 123 | + [ |
| 124 | + 'reset', |
| 125 | + 'from', |
| 126 | + 'join', |
| 127 | + 'where', |
| 128 | + 'group', |
| 129 | + 'order', |
| 130 | + 'having', |
| 131 | + ] |
| 132 | + ); |
| 133 | + |
| 134 | + $storeManagerMock->expects($this->atLeastOnce())->method('getStore')->willReturn($storeManagerMock); |
| 135 | + $storeManagerMock->expects($this->atLeastOnce())->method('getId')->willReturn(1); |
| 136 | + $universalFactoryMock->expects($this->atLeastOnce())->method('create')->willReturn($productMock); |
| 137 | + $this->resourceMock->expects($this->atLeastOnce())->method('getTableName')->willReturn('test_table'); |
| 138 | + $this->resourceMock->expects($this->atLeastOnce())->method('getConnection')->willReturn($this->connectionMock); |
| 139 | + $this->connectionMock->expects($this->atLeastOnce())->method('select')->willReturn($this->selectMock); |
| 140 | + |
| 141 | + $objectManagerMock = $this->getMock(\Magento\Framework\ObjectManagerInterface::class); |
| 142 | + \Magento\Framework\App\ObjectManager::setInstance($objectManagerMock); |
| 143 | + |
| 144 | + $this->collection = new ProductCollection( |
| 145 | + $entityFactoryMock, |
| 146 | + $loggerMock, |
| 147 | + $fetchStrategyMock, |
| 148 | + $eventManagerMock, |
| 149 | + $eavConfigMock, |
| 150 | + $this->resourceMock, |
| 151 | + $eavEntityFactoryMock, |
| 152 | + $resourceHelperMock, |
| 153 | + $universalFactoryMock, |
| 154 | + $storeManagerMock, |
| 155 | + $moduleManagerMock, |
| 156 | + $productFlatStateMock, |
| 157 | + $scopeConfigMock, |
| 158 | + $optionFactoryMock, |
| 159 | + $catalogUrlMock, |
| 160 | + $localeDateMock, |
| 161 | + $customerSessionMock, |
| 162 | + $dateTimeMock, |
| 163 | + $groupManagementMock, |
| 164 | + $productMock, |
| 165 | + $this->eventTypeFactoryMock, |
| 166 | + $productTypeMock, |
| 167 | + $quoteResourceMock, |
| 168 | + $this->connectionMock |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Test addViewsCount behavior. |
| 174 | + */ |
| 175 | + public function testAddViewsCount() |
| 176 | + { |
| 177 | + $context = $this->createPartialMock( |
| 178 | + \Magento\Framework\Model\ResourceModel\Db\Context::class, |
| 179 | + ['getResources'] |
| 180 | + ); |
| 181 | + $context->expects($this->atLeastOnce()) |
| 182 | + ->method('getResources') |
| 183 | + ->willReturn($this->resourceMock); |
| 184 | + $abstractResourceMock = $this->getMockForAbstractClass( |
| 185 | + \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class, |
| 186 | + ['context' => $context], |
| 187 | + '', |
| 188 | + true, |
| 189 | + true, |
| 190 | + true, |
| 191 | + [ |
| 192 | + 'getTableName', |
| 193 | + 'getConnection', |
| 194 | + 'getMainTable', |
| 195 | + ] |
| 196 | + ); |
| 197 | + |
| 198 | + $abstractResourceMock->expects($this->atLeastOnce()) |
| 199 | + ->method('getConnection') |
| 200 | + ->willReturn($this->connectionMock); |
| 201 | + $abstractResourceMock->expects($this->atLeastOnce()) |
| 202 | + ->method('getMainTable') |
| 203 | + ->willReturn('catalog_product'); |
| 204 | + |
| 205 | + /** @var \Magento\Reports\Model\ResourceModel\Event\Type\Collection $eventTypesCollection */ |
| 206 | + $eventTypesCollection = $this->objectManager->getObject( |
| 207 | + \Magento\Reports\Model\ResourceModel\Event\Type\Collection::class, |
| 208 | + ['resource' => $abstractResourceMock] |
| 209 | + ); |
| 210 | + $eventTypeMock = $this->createPartialMock( |
| 211 | + \Magento\Reports\Model\Event\Type::class, |
| 212 | + [ |
| 213 | + 'getEventName', |
| 214 | + 'getId', |
| 215 | + 'getCollection', |
| 216 | + ] |
| 217 | + ); |
| 218 | + |
| 219 | + $eventTypesCollection->addItem($eventTypeMock); |
| 220 | + |
| 221 | + $this->eventTypeFactoryMock->expects($this->once()) |
| 222 | + ->method('create') |
| 223 | + ->willReturn($eventTypeMock); |
| 224 | + $eventTypeMock->expects($this->atLeastOnce()) |
| 225 | + ->method('getCollection') |
| 226 | + ->willReturn($eventTypesCollection); |
| 227 | + $eventTypeMock->expects($this->atLeastOnce()) |
| 228 | + ->method('getEventName') |
| 229 | + ->willReturn('catalog_product_view'); |
| 230 | + $eventTypeMock->expects($this->atLeastOnce()) |
| 231 | + ->method('getId') |
| 232 | + ->willReturn(1); |
| 233 | + |
| 234 | + $this->selectMock->expects($this->atLeastOnce()) |
| 235 | + ->method('reset') |
| 236 | + ->willReturn($this->selectMock); |
| 237 | + $this->selectMock->expects($this->atLeastOnce()) |
| 238 | + ->method('from') |
| 239 | + ->with( |
| 240 | + ['report_table_views' => 'test_table'], |
| 241 | + ['views' => 'COUNT(report_table_views.event_id)'] |
| 242 | + )->willReturn($this->selectMock); |
| 243 | + $this->selectMock->expects($this->atLeastOnce()) |
| 244 | + ->method('join') |
| 245 | + ->with( |
| 246 | + ['e' => 'test_table'], |
| 247 | + 'e.entity_id = report_table_views.object_id' |
| 248 | + )->willReturn($this->selectMock); |
| 249 | + $this->selectMock->expects($this->atLeastOnce()) |
| 250 | + ->method('where') |
| 251 | + ->with('report_table_views.event_type_id = ?', 1) |
| 252 | + ->willReturn($this->selectMock); |
| 253 | + $this->selectMock->expects($this->atLeastOnce()) |
| 254 | + ->method('group') |
| 255 | + ->with('e.entity_id') |
| 256 | + ->willReturn($this->selectMock); |
| 257 | + $this->selectMock->expects($this->atLeastOnce()) |
| 258 | + ->method('order') |
| 259 | + ->with('views DESC') |
| 260 | + ->willReturn($this->selectMock); |
| 261 | + $this->selectMock->expects($this->atLeastOnce()) |
| 262 | + ->method('having') |
| 263 | + ->with('COUNT(report_table_views.event_id) > ?', 0) |
| 264 | + ->willReturn($this->selectMock); |
| 265 | + |
| 266 | + $this->collection->addViewsCount(); |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * Get mock for abstract class with methods. |
| 271 | + * |
| 272 | + * @param string $className |
| 273 | + * @param array $methods |
| 274 | + * |
| 275 | + * @return \PHPUnit_Framework_MockObject_MockObject |
| 276 | + */ |
| 277 | + private function createPartialMockForAbstractClass($className, $methods) |
| 278 | + { |
| 279 | + return $this->getMockForAbstractClass( |
| 280 | + $className, |
| 281 | + [], |
| 282 | + '', |
| 283 | + true, |
| 284 | + true, |
| 285 | + true, |
| 286 | + $methods |
| 287 | + ); |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * Returns a partial test double for the specified class. |
| 292 | + * |
| 293 | + * @param string $originalClassName |
| 294 | + * @param string[] $methods |
| 295 | + * |
| 296 | + * @return \PHPUnit_Framework_MockObject_MockObject |
| 297 | + */ |
| 298 | + private function createPartialMock($originalClassName, array $methods) |
| 299 | + { |
| 300 | + return $this->getMockBuilder($originalClassName) |
| 301 | + ->disableOriginalConstructor() |
| 302 | + ->disableOriginalClone() |
| 303 | + ->disableArgumentCloning() |
| 304 | + ->setMethods(empty($methods) ? null : $methods) |
| 305 | + ->getMock(); |
| 306 | + } |
| 307 | + |
| 308 | + /** |
| 309 | + * Returns a test double for the specified class. |
| 310 | + * |
| 311 | + * @param string $originalClassName |
| 312 | + * |
| 313 | + * @return \PHPUnit_Framework_MockObject_MockObject |
| 314 | + */ |
| 315 | + private function createMock($originalClassName) |
| 316 | + { |
| 317 | + return $this->getMockBuilder($originalClassName) |
| 318 | + ->disableOriginalConstructor() |
| 319 | + ->disableOriginalClone() |
| 320 | + ->disableArgumentCloning() |
| 321 | + ->getMock(); |
| 322 | + } |
| 323 | +} |
0 commit comments