|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product; |
| 7 | + |
| 8 | +use Magento\Catalog\Controller\Adminhtml\Product\AddAttributeToTemplate; |
| 9 | +use Magento\Framework\Exception\LocalizedException; |
| 10 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 11 | +use Magento\Backend\App\Action\Context; |
| 12 | +use Magento\Catalog\Controller\Adminhtml\Product\Builder as ProductBuilder; |
| 13 | +use Magento\Framework\Controller\Result\JsonFactory; |
| 14 | +use Magento\Framework\App\RequestInterface; |
| 15 | +use Magento\Catalog\Api\AttributeSetRepositoryInterface; |
| 16 | +use Magento\Eav\Api\Data\AttributeSetInterface; |
| 17 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 18 | +use Magento\Framework\Api\SearchCriteria; |
| 19 | +use Magento\Eav\Api\AttributeGroupRepositoryInterface; |
| 20 | +use Magento\Eav\Api\Data\AttributeGroupSearchResultsInterface; |
| 21 | +use Magento\Eav\Api\Data\AttributeGroupInterfaceFactory; |
| 22 | +use Magento\Eav\Api\Data\AttributeGroupInterface; |
| 23 | +use Magento\Framework\Controller\Result\Json; |
| 24 | + |
| 25 | +/** |
| 26 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 27 | + */ |
| 28 | +class AddAttributeToTemplateTest extends \PHPUnit_Framework_TestCase |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var ObjectManager |
| 32 | + */ |
| 33 | + private $objectManager; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var AddAttributeToTemplate |
| 37 | + */ |
| 38 | + private $controller; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var Context|\PHPUnit_Framework_MockObject_MockObject |
| 42 | + */ |
| 43 | + private $contextMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var ProductBuilder|\PHPUnit_Framework_MockObject_MockObject |
| 47 | + */ |
| 48 | + private $productBuilderMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var JsonFactory|\PHPUnit_Framework_MockObject_MockObject |
| 52 | + */ |
| 53 | + private $resultJsonFactoryMock; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject |
| 57 | + */ |
| 58 | + private $requestMock; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var AttributeSetRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
| 62 | + */ |
| 63 | + private $attributeSetRepositoryMock; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var AttributeSetInterface|\PHPUnit_Framework_MockObject_MockObject |
| 67 | + */ |
| 68 | + private $attributeSetInterfaceMock; |
| 69 | + |
| 70 | + /** |
| 71 | + * @var SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject |
| 72 | + */ |
| 73 | + private $searchCriteriaBuilderMock; |
| 74 | + |
| 75 | + /** |
| 76 | + * @var SearchCriteria|\PHPUnit_Framework_MockObject_MockObject |
| 77 | + */ |
| 78 | + private $searchCriteriaMock; |
| 79 | + |
| 80 | + /** |
| 81 | + * @var AttributeGroupRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject |
| 82 | + */ |
| 83 | + private $attributeGroupRepositoryMock; |
| 84 | + |
| 85 | + /** |
| 86 | + * @var AttributeGroupSearchResultsInterface|\PHPUnit_Framework_MockObject_MockObject |
| 87 | + */ |
| 88 | + private $attributeGroupSearchResultsMock; |
| 89 | + |
| 90 | + /** |
| 91 | + * @var AttributeGroupInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject |
| 92 | + */ |
| 93 | + private $attributeGroupInterfaceFactoryMock; |
| 94 | + |
| 95 | + /** |
| 96 | + * @var AttributeGroupInterface|\PHPUnit_Framework_MockObject_MockObject |
| 97 | + */ |
| 98 | + private $attributeGroupInterfaceMock; |
| 99 | + |
| 100 | + /** |
| 101 | + * @var Json|\PHPUnit_Framework_MockObject_MockObject |
| 102 | + */ |
| 103 | + private $jsonMock; |
| 104 | + |
| 105 | + protected function setUp() |
| 106 | + { |
| 107 | + $this->objectManager = new ObjectManager($this); |
| 108 | + $this->contextMock = $this->getMockBuilder(Context::class) |
| 109 | + ->disableOriginalConstructor() |
| 110 | + ->getMock(); |
| 111 | + $this->productBuilderMock = $this->getMockBuilder(ProductBuilder::class) |
| 112 | + ->disableOriginalConstructor() |
| 113 | + ->getMock(); |
| 114 | + $this->resultJsonFactoryMock = $this->getMockBuilder(JsonFactory::class) |
| 115 | + ->disableOriginalConstructor() |
| 116 | + ->setMethods(['create']) |
| 117 | + ->getMock(); |
| 118 | + $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class) |
| 119 | + ->setMethods(['getParam', 'setParam']) |
| 120 | + ->getMockForAbstractClass(); |
| 121 | + $this->contextMock->expects($this->once()) |
| 122 | + ->method('getRequest') |
| 123 | + ->willReturn($this->requestMock); |
| 124 | + $this->attributeSetRepositoryMock = $this->getMockBuilder(AttributeSetRepositoryInterface::class) |
| 125 | + ->setMethods(['get']) |
| 126 | + ->getMockForAbstractClass(); |
| 127 | + $this->attributeSetInterfaceMock = $this->getMockBuilder(AttributeSetInterface::class) |
| 128 | + ->getMockForAbstractClass(); |
| 129 | + $this->searchCriteriaBuilderMock = $this->getMockBuilder(SearchCriteriaBuilder::class) |
| 130 | + ->disableOriginalConstructor() |
| 131 | + ->setMethods(['addFilter', 'create', 'setPageSize']) |
| 132 | + ->getMockForAbstractClass(); |
| 133 | + $this->searchCriteriaMock = $this->getMockBuilder(SearchCriteria::class) |
| 134 | + ->disableOriginalConstructor() |
| 135 | + ->getMock(); |
| 136 | + $this->attributeGroupRepositoryMock = $this->getMockBuilder(AttributeGroupRepositoryInterface::class) |
| 137 | + ->setMethods(['getList']) |
| 138 | + ->getMockForAbstractClass(); |
| 139 | + $this->attributeGroupSearchResultsMock = $this->getMockBuilder(AttributeGroupSearchResultsInterface::class) |
| 140 | + ->setMethods(['getItems']) |
| 141 | + ->getMockForAbstractClass(); |
| 142 | + $this->attributeGroupInterfaceFactoryMock = $this->getMockBuilder(AttributeGroupInterfaceFactory::class) |
| 143 | + ->setMethods(['create']) |
| 144 | + ->disableOriginalConstructor() |
| 145 | + ->getMock(); |
| 146 | + $this->attributeGroupInterfaceMock = $this->getMockBuilder(AttributeGroupInterface::class) |
| 147 | + ->setMethods(['getExtensionAttributes']) |
| 148 | + ->getMockForAbstractClass(); |
| 149 | + $this->jsonMock = $this->getMockBuilder(Json::class) |
| 150 | + ->disableOriginalConstructor() |
| 151 | + ->getMock(); |
| 152 | + |
| 153 | + $this->controller = $this->objectManager->getObject( |
| 154 | + AddAttributeToTemplate::class, |
| 155 | + [ |
| 156 | + 'context' => $this->contextMock, |
| 157 | + 'productBuilder' => $this->productBuilderMock, |
| 158 | + 'resultJsonFactory' => $this->resultJsonFactoryMock, |
| 159 | + ] |
| 160 | + ); |
| 161 | + |
| 162 | + $this->objectManager->setBackwardCompatibleProperty( |
| 163 | + $this->controller, |
| 164 | + 'attributeSetRepository', |
| 165 | + $this->attributeSetRepositoryMock |
| 166 | + ); |
| 167 | + $this->objectManager->setBackwardCompatibleProperty( |
| 168 | + $this->controller, |
| 169 | + 'searchCriteriaBuilder', |
| 170 | + $this->searchCriteriaBuilderMock |
| 171 | + ); |
| 172 | + $this->objectManager->setBackwardCompatibleProperty( |
| 173 | + $this->controller, |
| 174 | + 'attributeGroupRepository', |
| 175 | + $this->attributeGroupRepositoryMock |
| 176 | + ); |
| 177 | + $this->objectManager->setBackwardCompatibleProperty( |
| 178 | + $this->controller, |
| 179 | + 'attributeGroupFactory', |
| 180 | + $this->attributeGroupInterfaceFactoryMock |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + public function testExecuteWithoutAttributeGroupItems() |
| 185 | + { |
| 186 | + $groupCode = 'attributes'; |
| 187 | + $groupName = 'Attributes'; |
| 188 | + $groupSortOrder = '15'; |
| 189 | + $templateId = '4'; |
| 190 | + $attributeIds = [ |
| 191 | + 'selected' => ["178"], |
| 192 | + 'total' => '1' |
| 193 | + ]; |
| 194 | + |
| 195 | + $this->requestMock |
| 196 | + ->expects($this->any()) |
| 197 | + ->method('getParam') |
| 198 | + ->willReturnMap( |
| 199 | + [ |
| 200 | + ['groupCode', null, $groupCode], |
| 201 | + ['groupName', null, $groupName], |
| 202 | + ['groupSortOrder', null, $groupSortOrder], |
| 203 | + ['templateId', null, $templateId], |
| 204 | + ['attributeIds', [], $attributeIds] |
| 205 | + ] |
| 206 | + ); |
| 207 | + |
| 208 | + $this->attributeSetRepositoryMock->expects($this->once()) |
| 209 | + ->method('get') |
| 210 | + ->willReturn($this->attributeSetInterfaceMock); |
| 211 | + |
| 212 | + $this->searchCriteriaBuilderMock->expects($this->any()) |
| 213 | + ->method('addFilter') |
| 214 | + ->willReturnSelf(); |
| 215 | + $this->searchCriteriaBuilderMock->expects($this->any()) |
| 216 | + ->method('create') |
| 217 | + ->willReturn($this->searchCriteriaMock); |
| 218 | + $this->searchCriteriaBuilderMock->expects($this->once()) |
| 219 | + ->method('setPageSize') |
| 220 | + ->willReturnSelf(); |
| 221 | + $this->searchCriteriaBuilderMock->expects($this->never()) |
| 222 | + ->method('addSortOrder') |
| 223 | + ->willReturnSelf(); |
| 224 | + |
| 225 | + $this->attributeGroupRepositoryMock->expects($this->once()) |
| 226 | + ->method('getList') |
| 227 | + ->willReturn($this->attributeGroupSearchResultsMock); |
| 228 | + $this->attributeGroupSearchResultsMock->expects($this->once()) |
| 229 | + ->method('getItems') |
| 230 | + ->willReturn(null); |
| 231 | + |
| 232 | + $this->attributeGroupInterfaceFactoryMock->expects($this->once()) |
| 233 | + ->method('create') |
| 234 | + ->willReturn($this->attributeGroupInterfaceMock); |
| 235 | + $this->attributeGroupInterfaceMock->expects($this->once()) |
| 236 | + ->method('getExtensionAttributes') |
| 237 | + ->willThrowException(new LocalizedException(__('Could not get extension attributes'))); |
| 238 | + |
| 239 | + $this->resultJsonFactoryMock->expects($this->once()) |
| 240 | + ->method('create') |
| 241 | + ->willReturn($this->jsonMock); |
| 242 | + $this->jsonMock->expects($this->once())->method('setJsonData') |
| 243 | + ->willReturnSelf(); |
| 244 | + |
| 245 | + $this->controller->execute(); |
| 246 | + } |
| 247 | +} |
0 commit comments