Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SetRepositoryTest.php
Go to the documentation of this file.
1 <?php
8 
12 class SetRepositoryTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $model;
18 
23 
28 
32  protected $filterBuilderMock;
33 
37  protected $eavConfig;
38 
39  protected function setUp()
40  {
41  $this->attrSetRepositoryMock = $this->createMock(\Magento\Eav\Api\AttributeSetRepositoryInterface::class);
42  $this->searchCriteriaBuilderMock = $this->createMock(\Magento\Framework\Api\SearchCriteriaBuilder::class);
43  $this->filterBuilderMock = $this->createMock(\Magento\Framework\Api\FilterBuilder::class);
44  $this->eavConfig = $this->createMock(\Magento\Eav\Model\Config::class);
45 
46  $this->model = new \Magento\Catalog\Model\Product\Attribute\SetRepository(
47  $this->attrSetRepositoryMock,
48  $this->searchCriteriaBuilderMock,
49  $this->filterBuilderMock,
50  $this->eavConfig
51  );
52  }
53 
54  public function testSave()
55  {
56  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
57  $this->setMockForValidation($attributeSetMock, 4);
58 
59  $this->attrSetRepositoryMock->expects($this->once())
60  ->method('save')
61  ->with($attributeSetMock)
62  ->willReturn($attributeSetMock);
63  $this->assertEquals($attributeSetMock, $this->model->save($attributeSetMock));
64  }
65 
71  {
72  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
73  $this->setMockForValidation($attributeSetMock, 3);
74  $this->attrSetRepositoryMock->expects($this->never())->method('save');
75  $this->model->save($attributeSetMock);
76  }
77 
78  public function testGet()
79  {
80  $attributeSetId = 1;
81  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
82  $this->setMockForValidation($attributeSetMock, 4);
83 
84  $this->attrSetRepositoryMock->expects($this->once())
85  ->method('get')
86  ->with($attributeSetId)
87  ->willReturn($attributeSetMock);
88  $this->assertEquals($attributeSetMock, $this->model->get($attributeSetId));
89  }
90 
96  {
97  $attributeSetId = 1;
98  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
99  $this->setMockForValidation($attributeSetMock, 3);
100 
101  $this->attrSetRepositoryMock->expects($this->once())
102  ->method('get')
103  ->with($attributeSetId)
104  ->willReturn($attributeSetMock);
105  $this->assertEquals($attributeSetMock, $this->model->get($attributeSetId));
106  }
107 
108  public function testDelete()
109  {
110  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
111  $this->setMockForValidation($attributeSetMock, 4);
112  $this->attrSetRepositoryMock->expects($this->once())
113  ->method('delete')
114  ->with($attributeSetMock)
115  ->willReturn(true);
116  $this->assertTrue($this->model->delete($attributeSetMock));
117  }
118 
124  {
125  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
126  $this->setMockForValidation($attributeSetMock, 3);
127  $this->attrSetRepositoryMock->expects($this->never())
128  ->method('delete');
129  $this->assertTrue($this->model->delete($attributeSetMock));
130  }
131 
132  public function testDeleteById()
133  {
134  $attributeSetId = 1;
135  $attributeSetMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetInterface::class);
136  $this->setMockForValidation($attributeSetMock, 4);
137 
138  $this->attrSetRepositoryMock->expects($this->once())
139  ->method('get')
140  ->with($attributeSetId)
141  ->willReturn($attributeSetMock);
142 
143  $this->attrSetRepositoryMock->expects($this->once())
144  ->method('deleteById')
145  ->with($attributeSetId)
146  ->willReturn(true);
147  $this->assertTrue($this->model->deleteById($attributeSetId));
148  }
149 
150  public function testGetList()
151  {
152  $searchResultMock = $this->createMock(\Magento\Eav\Api\Data\AttributeSetSearchResultsInterface::class);
153 
154  $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteriaInterface::class);
155  $searchCriteriaMock->expects($this->once())->method('getCurrentPage')->willReturn(1);
156  $searchCriteriaMock->expects($this->once())->method('getPageSize')->willReturn(2);
157 
158  $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
159 
160  $this->filterBuilderMock->expects($this->once())
161  ->method('setField')
162  ->with('entity_type_code')
163  ->willReturnSelf();
164  $this->filterBuilderMock->expects($this->once())
165  ->method('setValue')
166  ->with(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)
167  ->willReturnSelf();
168  $this->filterBuilderMock->expects($this->once())
169  ->method('setConditionType')
170  ->with('eq')
171  ->willReturnSelf();
172  $this->filterBuilderMock->expects($this->once())->method('create')->willReturn($filterMock);
173 
174  $this->searchCriteriaBuilderMock->expects($this->once())
175  ->method('addFilters')
176  ->with([$filterMock])
177  ->willReturnSelf();
178  $this->searchCriteriaBuilderMock->expects($this->once())
179  ->method('setCurrentPage')
180  ->with(1)
181  ->willReturnSelf();
182  $this->searchCriteriaBuilderMock->expects($this->once())
183  ->method('setPageSize')
184  ->with(2)
185  ->willReturnSelf();
186  $this->searchCriteriaBuilderMock->expects($this->once())
187  ->method('create')
188  ->willReturn($this->createMock(\Magento\Framework\Api\SearchCriteriaInterface::class));
189 
190  $this->attrSetRepositoryMock->expects($this->once())
191  ->method('getList')
192  ->with($searchCriteriaMock)
193  ->willReturn($searchResultMock);
194  $this->assertEquals($searchResultMock, $this->model->getList($searchCriteriaMock));
195  }
196 
203  protected function setMockForValidation(
204  \PHPUnit_Framework_MockObject_MockObject $attributeSetMock,
205  $setEntityTypeId
206  ) {
207  $typeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
208  $typeMock->expects($this->once())->method('getId')->willReturn(4);
209  $this->eavConfig->expects($this->once())
210  ->method('getEntityType')
211  ->with(\Magento\Catalog\Model\Product::ENTITY)
212  ->willReturn($typeMock);
213  $attributeSetMock->expects($this->once())->method('getEntityTypeId')->willReturn($setEntityTypeId);
214  }
215 }
setMockForValidation(\PHPUnit_Framework_MockObject_MockObject $attributeSetMock, $setEntityTypeId)