CreateEanAttributeTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 47
ccs 34
cts 34
cp 1
crap 1
rs 9.1563
c 0
b 0
f 0
1
<?php
2
3
4
namespace Stockbase\Integration\Test\Unit\Controller\Adminhtml\System\Config;
5
6
use Magento\Catalog\Api\Data\ProductAttributeInterface;
7
use Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory;
8
use Magento\Eav\Api\AttributeManagementInterface;
9
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory as AttributeSetCollectionFactory;
10
use Magento\Framework\ObjectManagerInterface;
11
use Magento\Framework\TestFramework\Unit\Matcher\MethodInvokedAtIndex;
12
use PHPUnit\Framework\TestCase;
13
use Stockbase\Integration\Controller\Adminhtml\System\Config\CreateEanAttribute;
14
15
/**
16
 * Class CreateEanAttributeTest
17
 */
18
class CreateEanAttributeTest extends TestCase
19
{
20
    const TEST_PRODUCT_TYPE_ID = 0xdeadbeef;
21
    
22
    /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
23
    private $request;
24
    
25
    /** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
26
    private $response;
27
    
28
    /** @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */
29
    private $context;
30
31
    /** @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject */
32
    private $resultFactory;
33
34
    /** @var \Magento\Framework\Controller\Result\Json|\PHPUnit_Framework_MockObject_MockObject */
35
    private $jsonResult;
36
37
    /** @var AttributeFactory|\PHPUnit_Framework_MockObject_MockObject */
38
    private $attributeFactory;
39
40
    /** @var AttributeManagementInterface|\PHPUnit_Framework_MockObject_MockObject */
41
    private $attributeManagement;
42
43
    /** @var AttributeSetCollectionFactory|\PHPUnit_Framework_MockObject_MockObject */
44
    private $attributeSetCollectionFactory;
45
46
    /** @var ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
47
    private $objectManager;
48
49
    /** @var \Magento\Eav\Model\Entity|\PHPUnit_Framework_MockObject_MockObject */
50
    private $eavEntity;
51
52
    /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute|\PHPUnit_Framework_MockObject_MockObject */
53
    private $attribute;
54
55
    /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection|\PHPUnit_Framework_MockObject_MockObject */
56
    private $attributeSetCollection;
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 3
    public function setUp()
62
    {
63 3
        $this->attribute = $this->createMock(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
64
        
65 3
        $this->attributeFactory = $this->getMockBuilder(AttributeFactory::class)->setMethods(['create'])->getMock();
66 3
        $this->attributeFactory->method('create')
67 3
            ->willReturn($this->attribute);
68
        
69 3
        $this->attributeManagement = $this->createMock(AttributeManagementInterface::class);
70
        
71 3
        $this->eavEntity = $this->createMock(\Magento\Eav\Model\Entity::class);
72 3
        $this->eavEntity
73 3
            ->method('setType')
74 3
            ->willReturnSelf();
75 3
        $this->eavEntity
76 3
            ->method('getTypeId')
77 3
            ->willReturn(self::TEST_PRODUCT_TYPE_ID);
78
79
80 3
        $this->attributeSetCollection = $this->createMock(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection::class);
81
        
82 3
        $this->attributeSetCollectionFactory = $this->getMockBuilder(AttributeSetCollectionFactory::class)
83 3
            ->setMethods(['create'])->getMock();
84
85 3
        $this->attributeSetCollectionFactory
86 3
            ->method('create')
87 3
            ->willReturn($this->attributeSetCollection);
88
89 3
        $this->objectManager = $this->createMock(ObjectManagerInterface::class);
90 3
        $this->objectManager
91 3
            ->method('create')
92 3
            ->with(\Magento\Eav\Model\Entity::class)
93 3
            ->willReturn($this->eavEntity);
94
95 3
        $this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
96 3
        $this->response = $this->createMock(\Magento\Framework\App\ResponseInterface::class);
97
98 3
        $this->jsonResult = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
99
100 3
        $this->resultFactory = $this->createMock(\Magento\Framework\Controller\ResultFactory::class);
101 3
        $this->resultFactory->method('create')->willReturn($this->jsonResult);
102
103 3
        $this->context = $this->createMock(\Magento\Backend\App\Action\Context::class);
104 3
        $this->context->method('getRequest')->willReturn($this->request);
105 3
        $this->context->method('getResultFactory')->willReturn($this->resultFactory);
106 3
        $this->context->method('getObjectManager')->willReturn($this->objectManager);
107 3
    }
108
109
    /**
110
     * testInvalidRequest
111
     */
112 1
    public function testInvalidRequest()
113
    {
114 1
        $this->jsonResult->expects($this->once())
115 1
            ->method('setHttpResponseCode')
116 1
            ->with(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST);
117
        
118 1
        $controller = $this->createController();
119 1
        $this->assertEquals($this->jsonResult, $controller->execute());
120 1
    }
121
122
    /**
123
     * testLoadExistingAttribute
124
     */
125 1
    public function testLoadExistingAttribute()
126
    {
127 1
        $this->request->expects($this->once())->method('isPost')->willReturn(true);
128
        
129 1
        $this->attribute->expects($this->once())->method('loadByCode')
130 1
            ->with(ProductAttributeInterface::ENTITY_TYPE_CODE, 'ean');
131 1
        $this->attribute->expects($this->once())->method('isEmpty')->willReturn(false);
132 1
        $this->attribute->expects($this->any())->method('getDefaultFrontendLabel')->willReturn('TEST_LABEL');
133 1
        $this->attribute->expects($this->any())->method('getAttributeCode')->willReturn('TEST_VALUE');
134
135 1
        $this->attribute->expects($this->never())->method('save');
136
        
137 1
        $this->jsonResult->expects($this->once())
138 1
            ->method('setData')
139 1
            ->with([
140 1
                'eanField' => [
141
                    'label' => 'TEST_LABEL',
142
                    'value' => 'TEST_VALUE',
143
                ],
144
            ]);
145
        
146 1
        $controller = $this->createController();
147 1
        $this->assertEquals($this->jsonResult, $controller->execute());
148 1
    }
149
150
    /**
151
     * testCreateNewAttribute
152
     */
153 1
    public function testCreateNewAttribute()
154
    {
155 1
        $this->request->expects($this->once())->method('isPost')->willReturn(true);
156
157 1
        $this->attribute->expects($this->once())->method('loadByCode')
158 1
            ->with(ProductAttributeInterface::ENTITY_TYPE_CODE, 'ean');
159 1
        $this->attribute->expects($this->once())->method('isEmpty')->willReturn(true);
160
161 1
        $this->attribute->expects($this->once())->method('setEntityTypeId')->with(self::TEST_PRODUCT_TYPE_ID);
162 1
        $this->attribute->expects($this->once())->method('setAttributeCode')->with('ean');
163 1
        $this->attribute->expects($this->once())->method('setDefaultFrontendLabel')->with('EAN');
164 1
        $this->attribute->expects($this->any())->method('getDefaultFrontendLabel')->willReturn('EAN');
165 1
        $this->attribute->expects($this->any())->method('getAttributeCode')->willReturn('ean');
166 1
        $this->attribute->expects($this->once())->method('save');
167
        
168 1
        $this->attributeSetCollection->expects($this->once())
169 1
            ->method('setEntityTypeFilter')
170 1
            ->with(self::TEST_PRODUCT_TYPE_ID);
171
        
172 1
        $attributeSets = [];
173 1
        for ($i = 0; $i < 2; $i++) {
174 1
            $attributeSet = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Set::class)
175 1
                ->disableOriginalConstructor()
176 1
                ->setMethods(['getAttributeSetId', 'getDefaultGroupId'])
177 1
                ->getMock();
178
            
179 1
            $attributeSet->expects($this->any())->method('getAttributeSetId')->willReturn($i*100+1);
180 1
            $attributeSet->expects($this->any())->method('getDefaultGroupId')->willReturn($i*100+2);
181
            
182 1
            $attributeSets[$i] = $attributeSet;
183
        }
184
        
185 1
        $this->attributeSetCollection->expects($this->once())->method('getItems')->willReturn($attributeSets);
186
187 1
        foreach ($attributeSets as $index => $attributeSet) {
188 1
            $this->attributeManagement->expects(new MethodInvokedAtIndex($index))->method('assign')
189 1
                ->with(
190 1
                    ProductAttributeInterface::ENTITY_TYPE_CODE,
191 1
                    $attributeSet->getAttributeSetId(),
192 1
                    $attributeSet->getDefaultGroupId(),
193 1
                    'ean',
194
                    0
195
                );
196
        }
197
198 1
        $this->jsonResult->expects($this->once())
199 1
            ->method('setData')
200 1
            ->with([
201 1
                'eanField' => [
202
                    'label' => 'EAN',
203
                    'value' => 'ean',
204
                ],
205
            ]);
206
207 1
        $controller = $this->createController();
208 1
        $this->assertEquals($this->jsonResult, $controller->execute());
209 1
    }
210
211 3
    protected function createController()
212
    {
213 3
        return new CreateEanAttribute(
214 3
            $this->context,
215 3
            $this->attributeFactory,
216 3
            $this->attributeManagement,
217 3
            $this->attributeSetCollectionFactory
218
        );
219
    }
220
}
221