Skip to content

Commit c1a01fc

Browse files
committed
coverage
1 parent 83545c0 commit c1a01fc

File tree

1 file changed

+153
-60
lines changed
  • app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Attribute

1 file changed

+153
-60
lines changed

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Attribute/SaveTest.php

Lines changed: 153 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,116 +3,142 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\Attribute;
79

10+
use Magento\Backend\Model\Session;
11+
use Magento\Backend\Model\View\Result\Redirect as ResultRedirect;
812
use Magento\Catalog\Api\Data\ProductAttributeInterface;
913
use Magento\Catalog\Controller\Adminhtml\Product\Attribute\Save;
10-
use Magento\Eav\Model\Validator\Attribute\Code as AttributeCodeValidator;
11-
use Magento\Framework\Serialize\Serializer\FormData;
12-
use Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\AttributeTest;
13-
use Magento\Catalog\Model\Product\AttributeSet\BuildFactory;
14+
use Magento\Catalog\Helper\Product as ProductHelper;
15+
use Magento\Catalog\Model\Product\Attribute\Frontend\Inputtype\Presentation;
1416
use Magento\Catalog\Model\Product\AttributeSet\Build;
17+
use Magento\Catalog\Model\Product\AttributeSet\BuildFactory;
1518
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
19+
use Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\AttributeTest;
1620
use Magento\Eav\Api\Data\AttributeSetInterface;
21+
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator as InputTypeValidator;
1722
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\ValidatorFactory;
1823
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
24+
use Magento\Eav\Model\Validator\Attribute\Code as AttributeCodeValidator;
1925
use Magento\Framework\Controller\ResultFactory;
26+
use Magento\Framework\Exception\NotFoundException;
2027
use Magento\Framework\Filter\FilterManager;
21-
use Magento\Catalog\Helper\Product as ProductHelper;
28+
use Magento\Framework\Serialize\Serializer\FormData;
2229
use Magento\Framework\View\Element\Messages;
2330
use Magento\Framework\View\LayoutFactory;
24-
use Magento\Backend\Model\View\Result\Redirect as ResultRedirect;
25-
use Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator as InputTypeValidator;
2631
use Magento\Framework\View\LayoutInterface;
32+
use PHPUnit\Framework\MockObject\MockObject;
2733

2834
/**
35+
* Test product attribute controller
36+
*
2937
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
38+
* @SuppressWarnings(PHPMD.TooManyFields)
3039
*/
3140
class SaveTest extends AttributeTest
3241
{
3342
/**
34-
* @var BuildFactory|\PHPUnit_Framework_MockObject_MockObject
43+
* @var BuildFactory|MockObject
3544
*/
3645
protected $buildFactoryMock;
3746

3847
/**
39-
* @var FilterManager|\PHPUnit_Framework_MockObject_MockObject
48+
* @var FilterManager|MockObject
4049
*/
4150
protected $filterManagerMock;
4251

4352
/**
44-
* @var ProductHelper|\PHPUnit_Framework_MockObject_MockObject
53+
* @var ProductHelper|MockObject
4554
*/
4655
protected $productHelperMock;
4756

4857
/**
49-
* @var AttributeFactory|\PHPUnit_Framework_MockObject_MockObject
58+
* @var AttributeFactory|MockObject
5059
*/
5160
protected $attributeFactoryMock;
5261

5362
/**
54-
* @var ValidatorFactory|\PHPUnit_Framework_MockObject_MockObject
63+
* @var ValidatorFactory|MockObject
5564
*/
5665
protected $validatorFactoryMock;
5766

5867
/**
59-
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
68+
* @var CollectionFactory|MockObject
6069
*/
6170
protected $groupCollectionFactoryMock;
6271

6372
/**
64-
* @var LayoutFactory|\PHPUnit_Framework_MockObject_MockObject
73+
* @var LayoutFactory|MockObject
6574
*/
6675
protected $layoutFactoryMock;
6776

6877
/**
69-
* @var ResultRedirect|\PHPUnit_Framework_MockObject_MockObject
78+
* @var ResultRedirect|MockObject
7079
*/
7180
protected $redirectMock;
7281

7382
/**
74-
* @var AttributeSet|\PHPUnit_Framework_MockObject_MockObject
83+
* @var AttributeSet|MockObject
7584
*/
7685
protected $attributeSetMock;
7786

7887
/**
79-
* @var Build|\PHPUnit_Framework_MockObject_MockObject
88+
* @var Build|MockObject
8089
*/
8190
protected $builderMock;
8291

8392
/**
84-
* @var InputTypeValidator|\PHPUnit_Framework_MockObject_MockObject
93+
* @var InputTypeValidator|MockObject
8594
*/
8695
protected $inputTypeValidatorMock;
8796

8897
/**
89-
* @var FormData|\PHPUnit_Framework_MockObject_MockObject
98+
* @var FormData|MockObject
99+
*/
100+
protected $formDataSerializerMock;
101+
102+
/**
103+
* @var ProductAttributeInterface|MockObject
104+
*/
105+
protected $productAttributeMock;
106+
107+
/**
108+
* @var Presentation|MockObject
90109
*/
91-
private $formDataSerializerMock;
110+
protected $presentationMock;
92111

93112
/**
94-
* @var ProductAttributeInterface|\PHPUnit_Framework_MockObject_MockObject
113+
* @var Session|MockObject
95114
*/
96-
private $productAttributeMock;
115+
protected $sessionMock;
97116

98117
/**
99-
* @var AttributeCodeValidator|\PHPUnit_Framework_MockObject_MockObject
118+
* @var AttributeCodeValidator|MockObject
100119
*/
101-
private $attributeCodeValidatorMock;
120+
protected $attributeCodeValidatorMock;
102121

103-
protected function setUp()
122+
/**
123+
* @inheritDoc
124+
*/
125+
public function setUp(): void
104126
{
105127
parent::setUp();
128+
$this->filterManagerMock = $this->createMock(FilterManager::class);
129+
$this->productHelperMock = $this->createMock(ProductHelper::class);
130+
$this->attributeSetMock = $this->createMock(AttributeSetInterface::class);
131+
$this->builderMock = $this->createMock(Build::class);
132+
$this->inputTypeValidatorMock = $this->createMock(InputTypeValidator::class);
133+
$this->formDataSerializerMock = $this->createMock(FormData::class);
134+
$this->attributeCodeValidatorMock = $this->createMock(AttributeCodeValidator::class);
135+
$this->presentationMock = $this->createMock(Presentation::class);
136+
$this->sessionMock = $this->createMock(Session::class);
137+
$this->layoutFactoryMock = $this->createMock(LayoutFactory::class);
106138
$this->buildFactoryMock = $this->getMockBuilder(BuildFactory::class)
107139
->setMethods(['create'])
108140
->disableOriginalConstructor()
109141
->getMock();
110-
$this->filterManagerMock = $this->getMockBuilder(FilterManager::class)
111-
->disableOriginalConstructor()
112-
->getMock();
113-
$this->productHelperMock = $this->getMockBuilder(ProductHelper::class)
114-
->disableOriginalConstructor()
115-
->getMock();
116142
$this->attributeFactoryMock = $this->getMockBuilder(AttributeFactory::class)
117143
->setMethods(['create'])
118144
->disableOriginalConstructor()
@@ -125,32 +151,23 @@ protected function setUp()
125151
->setMethods(['create'])
126152
->disableOriginalConstructor()
127153
->getMock();
128-
$this->layoutFactoryMock = $this->getMockBuilder(LayoutFactory::class)
129-
->disableOriginalConstructor()
130-
->getMock();
131154
$this->redirectMock = $this->getMockBuilder(ResultRedirect::class)
132155
->setMethods(['setData', 'setPath'])
133156
->disableOriginalConstructor()
134157
->getMock();
135-
$this->attributeSetMock = $this->getMockBuilder(AttributeSetInterface::class)
136-
->disableOriginalConstructor()
137-
->getMock();
138-
$this->builderMock = $this->getMockBuilder(Build::class)
139-
->disableOriginalConstructor()
140-
->getMock();
141-
$this->inputTypeValidatorMock = $this->getMockBuilder(InputTypeValidator::class)
142-
->disableOriginalConstructor()
143-
->getMock();
144-
$this->formDataSerializerMock = $this->getMockBuilder(FormData::class)
145-
->disableOriginalConstructor()
146-
->getMock();
147-
$this->attributeCodeValidatorMock = $this->getMockBuilder(AttributeCodeValidator::class)
148-
->disableOriginalConstructor()
149-
->getMock();
150158
$this->productAttributeMock = $this->getMockBuilder(ProductAttributeInterface::class)
151-
->setMethods(['getId', 'get'])
152-
->getMockForAbstractClass();
153-
159+
->setMethods(
160+
[
161+
'getId',
162+
'get',
163+
'getBackendTypeByInput',
164+
'getDefaultValueByInput',
165+
'getBackendType',
166+
'getFrontendClass',
167+
'addData',
168+
'save'
169+
]
170+
)->getMockForAbstractClass();
154171
$this->buildFactoryMock->expects($this->any())
155172
->method('create')
156173
->willReturn($this->builderMock);
@@ -163,7 +180,7 @@ protected function setUp()
163180
}
164181

165182
/**
166-
* {@inheritdoc}
183+
* @inheritdoc
167184
*/
168185
protected function getModel()
169186
{
@@ -180,11 +197,17 @@ protected function getModel()
180197
'groupCollectionFactory' => $this->groupCollectionFactoryMock,
181198
'layoutFactory' => $this->layoutFactoryMock,
182199
'formDataSerializer' => $this->formDataSerializerMock,
183-
'attributeCodeValidator' => $this->attributeCodeValidatorMock
200+
'attributeCodeValidator' => $this->attributeCodeValidatorMock,
201+
'presentation' => $this->presentationMock,
202+
'_session' => $this->sessionMock
184203
]);
185204
}
186205

187-
public function testExecuteWithEmptyData()
206+
/**
207+
* @return void
208+
* @throws NotFoundException
209+
*/
210+
public function testExecuteWithEmptyData(): void
188211
{
189212
$this->requestMock->expects($this->any())
190213
->method('getParam')
@@ -210,7 +233,76 @@ public function testExecuteWithEmptyData()
210233
$this->assertInstanceOf(ResultRedirect::class, $this->getModel()->execute());
211234
}
212235

213-
public function testExecute()
236+
/**
237+
* @return void
238+
* @throws NotFoundException
239+
*/
240+
public function testExecuteSaveFrontendClass(): void
241+
{
242+
$data = [
243+
'frontend_input' => 'test_frontend_input',
244+
];
245+
246+
$this->requestMock->expects($this->any())
247+
->method('getParam')
248+
->willReturnMap([
249+
['isAjax', null, null],
250+
['serialized_options', '[]', ''],
251+
['set', null, 1],
252+
['attribute_code', null, 'test_attribute_code'],
253+
]);
254+
$this->formDataSerializerMock
255+
->expects($this->once())
256+
->method('unserialize')
257+
->with('')
258+
->willReturn([]);
259+
$this->requestMock->expects($this->once())
260+
->method('getPostValue')
261+
->willReturn($data);
262+
$this->inputTypeValidatorMock->expects($this->any())
263+
->method('isValid')
264+
->with($data['frontend_input'])
265+
->willReturn(true);
266+
$this->presentationMock->expects($this->once())
267+
->method('convertPresentationDataToInputType')
268+
->willReturn($data);
269+
$this->productHelperMock->expects($this->once())
270+
->method('getAttributeSourceModelByInputType')
271+
->with($data['frontend_input'])
272+
->willReturn(null);
273+
$this->productHelperMock->expects($this->once())
274+
->method('getAttributeBackendModelByInputType')
275+
->with($data['frontend_input'])
276+
->willReturn(null);
277+
$this->productAttributeMock->expects($this->once())
278+
->method('getBackendTypeByInput')
279+
->with($data['frontend_input'])
280+
->willReturnSelf('test_backend_type');
281+
$this->productAttributeMock->expects($this->once())
282+
->method('getDefaultValueByInput')
283+
->with($data['frontend_input'])
284+
->willReturn(null);
285+
$this->productAttributeMock->expects($this->once())
286+
->method('getBackendType')
287+
->willReturn('static');
288+
$this->productAttributeMock->expects($this->once())
289+
->method('getFrontendClass')
290+
->willReturn('static');
291+
$this->resultFactoryMock->expects($this->any())
292+
->method('create')
293+
->willReturn($this->redirectMock);
294+
$this->redirectMock->expects($this->any())
295+
->method('setPath')
296+
->willReturnSelf();
297+
298+
$this->assertInstanceOf(ResultRedirect::class, $this->getModel()->execute());
299+
}
300+
301+
/**
302+
* @return void
303+
* @throws NotFoundException
304+
*/
305+
public function testExecute(): void
214306
{
215307
$data = [
216308
'new_attribute_set_name' => 'Test attribute set name',
@@ -273,9 +365,10 @@ public function testExecute()
273365
}
274366

275367
/**
276-
* @throws \Magento\Framework\Exception\NotFoundException
368+
* @return void
369+
* @throws NotFoundException
277370
*/
278-
public function testExecuteWithOptionsDataError()
371+
public function testExecuteWithOptionsDataError(): void
279372
{
280373
$serializedOptions = '{"key":"value"}';
281374
$message = "The attribute couldn't be saved due to an error. Verify your information and try again. "
@@ -305,10 +398,10 @@ public function testExecuteWithOptionsDataError()
305398
* @param string $path
306399
* @param array $params
307400
* @param array $response
308-
* @return mixed
401+
* @return void
309402
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
310403
*/
311-
private function addReturnResultConditions(string $path = '', array $params = [], array $response = [])
404+
private function addReturnResultConditions(string $path = '', array $params = [], array $response = []): void
312405
{
313406
$layoutMock = $this->getMockBuilder(LayoutInterface::class)
314407
->setMethods(['initMessages', 'getMessagesBlock'])

0 commit comments

Comments
 (0)