Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SetTest.php
Go to the documentation of this file.
1 <?php
8 
12 
16 class SetTest extends \PHPUnit\Framework\TestCase
17 {
21  protected $model;
22 
26  protected $eavConfigMock;
27 
31  protected $objectMock;
32 
36  protected $typeMock;
37 
42 
46  protected $resourceMock;
47 
51  protected $relationProcessor;
52 
56  private $serializerMock;
57 
61  protected function setUp()
62  {
63  $objectManager = new ObjectManager($this);
64  $this->resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
65  ->disableOriginalConstructor()
66  ->setMethods(['getConnection', 'getTableName'])
67  ->getMock();
68  $this->transactionManagerMock = $this->createMock(
69  \Magento\Framework\Model\ResourceModel\Db\TransactionManagerInterface::class
70  );
71  $this->relationProcessor = $this->createMock(
72  \Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor::class
73  );
74  $contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
75  $contextMock->expects($this->once())
76  ->method('getTransactionManager')
77  ->willReturn($this->transactionManagerMock);
78  $contextMock->expects($this->once())
79  ->method('getObjectRelationProcessor')
80  ->willReturn($this->relationProcessor);
81  $contextMock->expects($this->once())->method('getResources')->willReturn($this->resourceMock);
82 
83  $this->eavConfigMock = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
84  ->setMethods(['isCacheEnabled', 'getEntityType', 'getCache'])
85  ->disableOriginalConstructor()
86  ->getMock();
87 
88  $this->serializerMock = $this->createMock(Json::class);
89 
90  $attributeGroupFactoryMock = $this->createMock(
91  \Magento\Eav\Model\ResourceModel\Entity\Attribute\GroupFactory::class
92  );
93 
94  $this->model = $objectManager->getObject(
95  \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set::class,
96  [
97  'context' => $contextMock,
98  'attrGroupFactory' => $attributeGroupFactoryMock,
99  'eavConfig' => $this->eavConfigMock
100  ]
101  );
102 
103  $objectManager->setBackwardCompatibleProperty($this->model, 'serializer', $this->serializerMock);
104 
105  $this->typeMock = $this->createMock(\Magento\Eav\Model\Entity\Type::class);
106  $this->objectMock = $this->createPartialMock(\Magento\Framework\Model\AbstractModel::class, [
107  'getEntityTypeId',
108  'getAttributeSetId',
109  'beforeDelete',
110  'getId',
111  'isDeleted',
112  'afterDelete',
113  'afterDeleteCommit',
114  '__wakeup'
115  ]);
116  }
117 
124  {
125  $this->resourceMock->expects($this->any())
126  ->method('getConnection')
127  ->willReturn($this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class));
128 
129  $this->transactionManagerMock->expects($this->once())
130  ->method('start')
131  ->with($this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class))
132  ->willReturn($this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class));
133 
134  $this->objectMock->expects($this->once())->method('getEntityTypeId')->willReturn(665);
135  $this->eavConfigMock->expects($this->once())->method('getEntityType')->with(665)->willReturn($this->typeMock);
136  $this->typeMock->expects($this->once())->method('getDefaultAttributeSetId')->willReturn(4);
137  $this->objectMock->expects($this->once())->method('getAttributeSetId')->willReturn(4);
138 
139  $this->model->delete($this->objectMock);
140  }
141 
147  public function testBeforeDelete()
148  {
149  $this->resourceMock->expects($this->any())
150  ->method('getConnection')
151  ->willReturn($this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class));
152 
153  $this->transactionManagerMock->expects($this->once())
154  ->method('start')
155  ->with($this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class))
156  ->willReturn($this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class));
157 
158  $this->objectMock->expects($this->once())->method('getEntityTypeId')->willReturn(665);
159  $this->eavConfigMock->expects($this->once())->method('getEntityType')->with(665)->willReturn($this->typeMock);
160  $this->typeMock->expects($this->once())->method('getDefaultAttributeSetId')->willReturn(4);
161  $this->objectMock->expects($this->once())->method('getAttributeSetId')->willReturn(5);
162  $this->relationProcessor->expects($this->once())
163  ->method('delete')
164  ->willThrowException(new \Exception('test exception'));
165 
166  $this->model->delete($this->objectMock);
167  }
168 
172  public function testGetSetInfoCacheMiss()
173  {
174  $serializedData = 'serialized data';
175  $setElement = [
176  10000 => [
177  'group_id' => 10,
178  'group_sort' => 100,
179  'sort' => 1000
180  ]
181  ];
182  $setData = [
183  1 => $setElement,
184  2 => [],
185  3 => []
186  ];
187  $cached = [
188  1 => $setElement
189  ];
190  $cacheMock = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)
191  ->disableOriginalConstructor()
192  ->setMethods(['load', 'save', 'getFrontend', 'remove', 'clean'])
193  ->getMock();
194  $cacheKey = Set::ATTRIBUTES_CACHE_ID . 1;
195  $cacheMock
196  ->expects($this->once())
197  ->method('load')
198  ->with($cacheKey)
199  ->willReturn(false);
200  $this->serializerMock->expects($this->once())
201  ->method('serialize')
202  ->with($cached)
203  ->willReturn($serializedData);
204  $cacheMock
205  ->expects($this->once())
206  ->method('save')
207  ->with(
208  $serializedData,
209  $cacheKey,
210  [\Magento\Eav\Model\Cache\Type::CACHE_TAG, \Magento\Eav\Model\Entity\Attribute::CACHE_TAG]
211  );
212 
213  $this->eavConfigMock->expects($this->any())->method('isCacheEnabled')->willReturn(true);
214  $this->eavConfigMock->expects($this->any())->method('getCache')->willReturn($cacheMock);
215 
216  $fetchResult = [
217  [
218  'attribute_id' => 1,
219  'attribute_group_id' => 10,
220  'group_sort_order' => 100,
221  'sort_order' => 1000,
222  'attribute_set_id' => 10000
223  ]
224  ];
225 
226  $selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
227  ->disableOriginalConstructor()
228  ->setMethods(['from', 'joinLeft', 'where'])
229  ->getMock();
230  $selectMock->expects($this->once())->method('from')->will($this->returnSelf());
231  $selectMock->expects($this->once())->method('joinLeft')->will($this->returnSelf());
232  $selectMock->expects($this->atLeastOnce())->method('where')->will($this->returnSelf());
233 
234  $connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\Pdo\Mysql::class)
235  ->disableOriginalConstructor()
236  ->setMethods(['select', 'fetchAll'])
237  ->getMock();
238  $connectionMock->expects($this->atLeastOnce())->method('select')->willReturn($selectMock);
239  $connectionMock->expects($this->atLeastOnce())->method('fetchAll')->willReturn($fetchResult);
240 
241  $this->resourceMock->expects($this->any())->method('getConnection')->willReturn($connectionMock);
242  $this->resourceMock->expects($this->any())->method('getTableName')->willReturn('_TABLE_');
243  $this->assertEquals(
244  $setData,
245  $this->model->getSetInfo([1, 2, 3], 1)
246  );
247  }
248 
252  public function testGetSetInfoCacheHit()
253  {
254  $setElement = [
255  10000 => [
256  'group_id' => 10,
257  'group_sort' => 100,
258  'sort' => 1000
259  ]
260  ];
261  $setData = [
262  1 => $setElement,
263  2 => [],
264  3 => []
265  ];
266  $cached = [
267  1 => $setElement
268  ];
269  $serializedData = 'serialized data';
270  $this->resourceMock->expects($this->never())->method('getConnection');
271  $this->eavConfigMock->expects($this->any())->method('isCacheEnabled')->willReturn(true);
272  $cacheMock = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)
273  ->disableOriginalConstructor()
274  ->setMethods(['load', 'save', 'getFrontend', 'remove', 'clean'])
275  ->getMock();
276  $cacheMock->expects($this->once())
277  ->method('load')
278  ->with(Set::ATTRIBUTES_CACHE_ID . 1)
279  ->willReturn($serializedData);
280  $this->serializerMock->expects($this->once())
281  ->method('unserialize')
282  ->with($serializedData)
283  ->willReturn($cached);
284 
285  $this->eavConfigMock->expects($this->any())->method('getCache')->willReturn($cacheMock);
286 
287  $this->assertEquals(
288  $setData,
289  $this->model->getSetInfo([1, 2, 3], 1)
290  );
291  }
292 }
$objectManager
Definition: bootstrap.php:17