|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Category; |
| 9 | + |
| 10 | +use Magento\Framework\Data\Collection\EntityFactory; |
| 11 | +use Psr\Log\LoggerInterface; |
| 12 | +use Magento\Framework\Data\Collection\Db\FetchStrategyInterface; |
| 13 | +use Magento\Framework\Event\ManagerInterface; |
| 14 | +use Magento\Eav\Model\Config; |
| 15 | +use Magento\Framework\App\ResourceConnection; |
| 16 | +use Magento\Eav\Model\EntityFactory as EavEntityFactory; |
| 17 | +use Magento\Eav\Model\ResourceModel\Helper; |
| 18 | +use Magento\Framework\Validator\UniversalFactory; |
| 19 | +use Magento\Store\Model\StoreManagerInterface; |
| 20 | +use Magento\Store\Api\Data\StoreInterface; |
| 21 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 22 | +use Magento\Framework\DB\Select; |
| 23 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 24 | +use Magento\Catalog\Model\Product\Visibility; |
| 25 | +use Magento\Catalog\Model\ResourceModel\Category\Collection; |
| 26 | +use Magento\Catalog\Model\ResourceModel\Category as CategoryEntity; |
| 27 | +use PHPUnit\Framework\MockObject\MockObject; |
| 28 | +use PHPUnit\Framework\TestCase; |
| 29 | + |
| 30 | +/** |
| 31 | + * @SuppressWarnings(PHPMD.TooManyFields) |
| 32 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 33 | + */ |
| 34 | +class CollectionTest extends TestCase |
| 35 | +{ |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Collection |
| 39 | + */ |
| 40 | + private $collection; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var EntityFactory|MockObject |
| 44 | + */ |
| 45 | + private $entityFactory; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var LoggerInterface|MockObject |
| 49 | + */ |
| 50 | + private $logger; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var FetchStrategyInterface|MockObject |
| 54 | + */ |
| 55 | + private $fetchStrategy; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var ManagerInterface|MockObject |
| 59 | + */ |
| 60 | + private $eventManager; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var Config|MockObject |
| 64 | + */ |
| 65 | + private $eavConfig; |
| 66 | + |
| 67 | + /** |
| 68 | + * @var ResourceConnection|MockObject |
| 69 | + */ |
| 70 | + private $resource; |
| 71 | + |
| 72 | + /** |
| 73 | + * @var EavEntityFactory|MockObject |
| 74 | + */ |
| 75 | + private $eavEntityFactory; |
| 76 | + |
| 77 | + /** |
| 78 | + * @var Helper|MockObject |
| 79 | + */ |
| 80 | + private $resourceHelper; |
| 81 | + |
| 82 | + /** |
| 83 | + * @var UniversalFactory|MockObject |
| 84 | + */ |
| 85 | + private $universalFactory; |
| 86 | + |
| 87 | + /** |
| 88 | + * @var StoreManagerInterface|MockObject |
| 89 | + */ |
| 90 | + private $storeManager; |
| 91 | + |
| 92 | + /** |
| 93 | + * @var AdapterInterface|MockObject |
| 94 | + */ |
| 95 | + private $connection; |
| 96 | + |
| 97 | + /** |
| 98 | + * @var ScopeConfigInterface|MockObject |
| 99 | + */ |
| 100 | + private $scopeConfig; |
| 101 | + |
| 102 | + /** |
| 103 | + * @var Visibility|MockObject |
| 104 | + */ |
| 105 | + private $catalogProductVisibility; |
| 106 | + |
| 107 | + /** |
| 108 | + * @var CategoryEntity|MockObject |
| 109 | + */ |
| 110 | + private $categoryEntity; |
| 111 | + |
| 112 | + /** |
| 113 | + * @var Select|MockObject |
| 114 | + */ |
| 115 | + private $select; |
| 116 | + |
| 117 | + /** |
| 118 | + * @var StoreInterface|MockObject |
| 119 | + */ |
| 120 | + private $store; |
| 121 | + |
| 122 | + /** |
| 123 | + * {@inheritdoc} |
| 124 | + */ |
| 125 | + public function setUp(): void |
| 126 | + { |
| 127 | + $this->entityFactory = $this->getMockBuilder(EntityFactory::class) |
| 128 | + ->disableOriginalConstructor(true) |
| 129 | + ->getMock(); |
| 130 | + $this->logger = $this->getMockBuilder(LoggerInterface::class) |
| 131 | + ->getMock(); |
| 132 | + $this->fetchStrategy = $this->getMockBuilder(FetchStrategyInterface::class) |
| 133 | + ->getMock(); |
| 134 | + $this->eventManager = $this->getMockBuilder(ManagerInterface::class) |
| 135 | + ->getMock(); |
| 136 | + $this->eavConfig = $this->getMockBuilder(Config::class) |
| 137 | + ->disableOriginalConstructor(true) |
| 138 | + ->getMock(); |
| 139 | + $this->resource = $this->getMockBuilder(ResourceConnection::class) |
| 140 | + ->disableOriginalConstructor(true) |
| 141 | + ->getMock(); |
| 142 | + $this->eavEntityFactory = $this->getMockBuilder(EavEntityFactory::class) |
| 143 | + ->disableOriginalConstructor(true) |
| 144 | + ->getMock(); |
| 145 | + $this->resourceHelper = $this->getMockBuilder(Helper::class) |
| 146 | + ->disableOriginalConstructor(true) |
| 147 | + ->getMock(); |
| 148 | + $this->universalFactory = $this->getMockBuilder(UniversalFactory::class) |
| 149 | + ->disableOriginalConstructor(true) |
| 150 | + ->getMock(); |
| 151 | + $this->storeManager = $this->getMockBuilder(StoreManagerInterface::class) |
| 152 | + ->getMock(); |
| 153 | + $this->connection = $this->getMockBuilder(AdapterInterface::class) |
| 154 | + ->getMock(); |
| 155 | + $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class) |
| 156 | + ->getMock(); |
| 157 | + $this->catalogProductVisibility = $this->getMockBuilder(Visibility::class) |
| 158 | + ->disableOriginalConstructor(true) |
| 159 | + ->getMock(); |
| 160 | + |
| 161 | + $this->categoryEntity = $this->getMockBuilder(CategoryEntity::class) |
| 162 | + ->disableOriginalConstructor() |
| 163 | + ->getMock(); |
| 164 | + $this->universalFactory->expects($this->any()) |
| 165 | + ->method('create') |
| 166 | + ->willReturn($this->categoryEntity); |
| 167 | + $this->categoryEntity->expects($this->any()) |
| 168 | + ->method('getConnection') |
| 169 | + ->willReturn($this->connection); |
| 170 | + $this->categoryEntity->expects($this->any()) |
| 171 | + ->method('getDefaultAttributes') |
| 172 | + ->willReturn([]); |
| 173 | + |
| 174 | + $this->select = $this->getMockBuilder(Select::class) |
| 175 | + ->disableOriginalConstructor() |
| 176 | + ->getMock(); |
| 177 | + $this->connection->expects($this->any()) |
| 178 | + ->method('select') |
| 179 | + ->willReturn($this->select); |
| 180 | + |
| 181 | + $this->store = $this->getMockBuilder(StoreInterface::class) |
| 182 | + ->getMock(); |
| 183 | + $this->storeManager->expects($this->any()) |
| 184 | + ->method('getStore') |
| 185 | + ->willReturn($this->store); |
| 186 | + |
| 187 | + $this->collection = new Collection( |
| 188 | + $this->entityFactory, |
| 189 | + $this->logger, |
| 190 | + $this->fetchStrategy, |
| 191 | + $this->eventManager, |
| 192 | + $this->eavConfig, |
| 193 | + $this->resource, |
| 194 | + $this->eavEntityFactory, |
| 195 | + $this->resourceHelper, |
| 196 | + $this->universalFactory, |
| 197 | + $this->storeManager, |
| 198 | + $this->connection, |
| 199 | + $this->scopeConfig, |
| 200 | + $this->catalogProductVisibility |
| 201 | + ); |
| 202 | + } |
| 203 | + |
| 204 | + public function testLoadProductCount() : void |
| 205 | + { |
| 206 | + $this->select->expects($this->exactly(3)) |
| 207 | + ->method('from') |
| 208 | + ->willReturnSelf(); |
| 209 | + $this->select->expects($this->exactly(3)) |
| 210 | + ->method('where') |
| 211 | + ->willReturnSelf(); |
| 212 | + $this->select->expects($this->exactly(1)) |
| 213 | + ->method('group') |
| 214 | + ->willReturnSelf(); |
| 215 | + $this->connection->expects($this->exactly(2)) |
| 216 | + ->method('fetchPairs') |
| 217 | + ->with($this->select) |
| 218 | + ->willReturn([]); |
| 219 | + $this->collection->loadProductCount([]); |
| 220 | + } |
| 221 | +} |
0 commit comments