Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddAttributeToTemplateTest.php
Go to the documentation of this file.
1 <?php
7 
21 use Magento\Eav\Api\Data\AttributeGroupInterfaceFactory;
24 
28 class AddAttributeToTemplateTest extends \PHPUnit\Framework\TestCase
29 {
33  private $objectManager;
34 
38  private $controller;
39 
43  private $contextMock;
44 
48  private $productBuilderMock;
49 
53  private $resultJsonFactoryMock;
54 
58  private $requestMock;
59 
63  private $attributeSetRepositoryMock;
64 
68  private $attributeSetInterfaceMock;
69 
73  private $searchCriteriaBuilderMock;
74 
78  private $searchCriteriaMock;
79 
83  private $attributeGroupRepositoryMock;
84 
88  private $attributeGroupSearchResultsMock;
89 
93  private $attributeGroupInterfaceFactoryMock;
94 
98  private $attributeGroupInterfaceMock;
99 
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', 'addSortOrder'])
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 
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 }
__()
Definition: __.php:13
$templateId
Definition: queue.php:15