|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Category; |
| 8 | + |
| 9 | +use Magento\Catalog\Model\ResourceModel\Category\Flat\CollectionFactory; |
| 10 | +use Magento\Catalog\Model\ResourceModel\Category\Flat\Collection; |
| 11 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Category\Flat; |
| 13 | +use Magento\Framework\DB\Select; |
| 14 | +use Magento\Framework\DB\Adapter\AdapterInterface as Adapter; |
| 15 | +use Magento\Framework\App\ResourceConnection; |
| 16 | +use Magento\Framework\Model\ResourceModel\Db\Context; |
| 17 | +use Magento\Store\Model\Store; |
| 18 | +use Magento\Store\Model\StoreManagerInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * Category flat model test |
| 22 | + * |
| 23 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 24 | + */ |
| 25 | +class FlatTest extends \PHPUnit_Framework_TestCase |
| 26 | +{ |
| 27 | + const STORE_ID = 1; |
| 28 | + const TABLE_NAME = 'test_table'; |
| 29 | + const PARENT_PATH = '1'; |
| 30 | + const SORTED = false; |
| 31 | + const PARENT = 1; |
| 32 | + const RECURSION_LEVEL = 0; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject |
| 36 | + */ |
| 37 | + private $categoryCollectionFactoryMock; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var Collection|\PHPUnit_Framework_MockObject_MockObject |
| 41 | + */ |
| 42 | + private $categoryCollectionMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var Flat |
| 46 | + */ |
| 47 | + private $model; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var ObjectManager |
| 51 | + */ |
| 52 | + private $objectManager; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var Select|\PHPUnit_Framework_MockObject_MockObject |
| 56 | + */ |
| 57 | + private $selectMock; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var Adapter|\PHPUnit_Framework_MockObject_MockObject |
| 61 | + */ |
| 62 | + private $connectionMock; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject |
| 66 | + */ |
| 67 | + private $resourceMock; |
| 68 | + |
| 69 | + /** |
| 70 | + * @var Context|\PHPUnit_Framework_MockObject_MockObject |
| 71 | + */ |
| 72 | + private $contextMock; |
| 73 | + |
| 74 | + /** |
| 75 | + * @var Store|\PHPUnit_Framework_MockObject_MockObject |
| 76 | + */ |
| 77 | + private $storeMock; |
| 78 | + |
| 79 | + /** |
| 80 | + * @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 81 | + */ |
| 82 | + private $storeManagerMock; |
| 83 | + |
| 84 | + /** |
| 85 | + * {@inheritdoc} |
| 86 | + */ |
| 87 | + protected function setUp() |
| 88 | + { |
| 89 | + $this->objectManager = new ObjectManager($this); |
| 90 | + |
| 91 | + $this->selectMock = $this->getMockBuilder(Select::class) |
| 92 | + ->disableOriginalConstructor() |
| 93 | + ->setMethods(['where', 'from']) |
| 94 | + ->getMock(); |
| 95 | + $this->selectMock->expects($this->once()) |
| 96 | + ->method('where') |
| 97 | + ->willReturn($this->selectMock); |
| 98 | + $this->selectMock->expects($this->once()) |
| 99 | + ->method('from') |
| 100 | + ->willReturn($this->selectMock); |
| 101 | + $this->connectionMock = $this->getMockBuilder(Adapter::class) |
| 102 | + ->getMockForAbstractClass(); |
| 103 | + $this->connectionMock->expects($this->once()) |
| 104 | + ->method('select') |
| 105 | + ->willReturn($this->selectMock); |
| 106 | + $this->connectionMock->expects($this->once()) |
| 107 | + ->method('fetchOne') |
| 108 | + ->with($this->selectMock) |
| 109 | + ->willReturn(self::PARENT_PATH); |
| 110 | + $this->resourceMock = $this->getMockBuilder(ResourceConnection::class) |
| 111 | + ->disableOriginalConstructor() |
| 112 | + ->setMethods(['getConnection', 'getTableName']) |
| 113 | + ->getMock(); |
| 114 | + $this->resourceMock->expects($this->any()) |
| 115 | + ->method('getConnection') |
| 116 | + ->willReturn($this->connectionMock); |
| 117 | + $this->resourceMock->expects($this->any()) |
| 118 | + ->method('getTableName') |
| 119 | + ->willReturn(self::TABLE_NAME); |
| 120 | + $this->contextMock = $this->getMockBuilder(Context::class) |
| 121 | + ->disableOriginalConstructor() |
| 122 | + ->setMethods(['getResources']) |
| 123 | + ->getMock(); |
| 124 | + $this->contextMock->expects($this->any()) |
| 125 | + ->method('getResources') |
| 126 | + ->willReturn($this->resourceMock); |
| 127 | + |
| 128 | + $this->storeMock = $this->getMockBuilder(Store::class) |
| 129 | + ->disableOriginalConstructor() |
| 130 | + ->setMethods(['getId']) |
| 131 | + ->getMock(); |
| 132 | + $this->storeMock->expects($this->any()) |
| 133 | + ->method('getId') |
| 134 | + ->willReturn(self::STORE_ID); |
| 135 | + $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) |
| 136 | + ->getMockForAbstractClass(); |
| 137 | + $this->storeManagerMock->expects($this->any()) |
| 138 | + ->method('getStore') |
| 139 | + ->willReturn($this->storeMock); |
| 140 | + } |
| 141 | + |
| 142 | + public function testGetCategories() |
| 143 | + { |
| 144 | + $this->categoryCollectionFactoryMock = $this->getMockBuilder(CollectionFactory::class) |
| 145 | + ->disableOriginalConstructor() |
| 146 | + ->setMethods(['create']) |
| 147 | + ->getMock(); |
| 148 | + $this->categoryCollectionMock = $this->getMockBuilder(Collection::class) |
| 149 | + ->disableOriginalConstructor() |
| 150 | + ->setMethods( |
| 151 | + [ |
| 152 | + 'addNameToResult', |
| 153 | + 'addUrlRewriteToResult', |
| 154 | + 'addParentPathFilter', |
| 155 | + 'addStoreFilter', |
| 156 | + 'addIsActiveFilter', |
| 157 | + 'addAttributeToFilter', |
| 158 | + 'addSortedField', |
| 159 | + 'load' |
| 160 | + ] |
| 161 | + ) |
| 162 | + ->getMock(); |
| 163 | + $this->categoryCollectionMock->expects($this->once()) |
| 164 | + ->method('addNameToResult') |
| 165 | + ->willReturn($this->categoryCollectionMock); |
| 166 | + $this->categoryCollectionMock->expects($this->once()) |
| 167 | + ->method('addUrlRewriteToResult') |
| 168 | + ->willReturn($this->categoryCollectionMock); |
| 169 | + $this->categoryCollectionMock->expects($this->once()) |
| 170 | + ->method('addParentPathFilter') |
| 171 | + ->with(self::PARENT_PATH) |
| 172 | + ->willReturn($this->categoryCollectionMock); |
| 173 | + $this->categoryCollectionMock->expects($this->once()) |
| 174 | + ->method('addStoreFilter') |
| 175 | + ->willReturn($this->categoryCollectionMock); |
| 176 | + $this->categoryCollectionMock->expects($this->once()) |
| 177 | + ->method('addIsActiveFilter') |
| 178 | + ->willReturn($this->categoryCollectionMock); |
| 179 | + $this->categoryCollectionMock->expects($this->once()) |
| 180 | + ->method('addSortedField') |
| 181 | + ->with(self::SORTED) |
| 182 | + ->willReturn($this->categoryCollectionMock); |
| 183 | + $this->categoryCollectionMock->expects($this->once()) |
| 184 | + ->method('addAttributeToFilter') |
| 185 | + ->with('include_in_menu', 1) |
| 186 | + ->willReturn($this->categoryCollectionMock); |
| 187 | + $this->categoryCollectionMock->expects($this->once()) |
| 188 | + ->method('load') |
| 189 | + ->willReturn($this->categoryCollectionMock); |
| 190 | + $this->categoryCollectionFactoryMock->expects($this->once()) |
| 191 | + ->method('create') |
| 192 | + ->willReturn($this->categoryCollectionMock); |
| 193 | + |
| 194 | + $this->model = $this->objectManager->getObject( |
| 195 | + Flat::class, |
| 196 | + [ |
| 197 | + 'context' => $this->contextMock, |
| 198 | + 'storeManager' => $this->storeManagerMock, |
| 199 | + ] |
| 200 | + ); |
| 201 | + |
| 202 | + $reflection = new \ReflectionClass(get_class($this->model)); |
| 203 | + $reflectionProperty = $reflection->getProperty('categoryFlatCollectionFactory'); |
| 204 | + $reflectionProperty->setAccessible(true); |
| 205 | + $reflectionProperty->setValue($this->model, $this->categoryCollectionFactoryMock); |
| 206 | + |
| 207 | + $this->assertEquals( |
| 208 | + $this->model->getCategories(self::PARENT, self::RECURSION_LEVEL, self::SORTED, true), |
| 209 | + $this->categoryCollectionMock |
| 210 | + ); |
| 211 | + } |
| 212 | +} |
0 commit comments