Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeTest.php
Go to the documentation of this file.
1 <?php
8 
15 
20 class AttributeTest extends \PHPUnit\Framework\TestCase
21 {
25  protected $attribute;
26 
30  protected $contextMock;
31 
35  protected $registryMock;
36 
41 
45  protected $configMock;
46 
50  protected $typeFactoryMock;
51 
55  protected $storeManagerMock;
56 
60  protected $helperMock;
61 
66 
70  protected $timezoneMock;
71 
75  private $resourceMock;
76 
81 
85  protected $resolverMock;
86 
90  private $cacheManager;
91 
95  private $eventDispatcher;
96 
100  private $attributeOptionFactoryMock;
101 
105  private $dataObjectProcessorMock;
106 
110  private $dataObjectHelperMock;
111 
115  private $indexerRegistryMock;
116 
120  private $extensionAttributesFactory;
121 
125  private $dateTimeFormatter;
126 
130  private $attributeMetadataCacheMock;
131 
136  protected function setUp()
137  {
138  $objectManagerHelper = new ObjectManagerHelper($this);
139  $this->contextMock = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
140  ->disableOriginalConstructor()
141  ->getMock();
142  $this->registryMock = $this->getMockBuilder(\Magento\Framework\Registry::class)
143  ->getMock();
144  $this->extensionAttributesFactory = $this->getMockBuilder(
145  \Magento\Framework\Api\ExtensionAttributesFactory::class
146  )
147  ->disableOriginalConstructor()
148  ->getMock();
149  $this->attributeValueFactoryMock = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class)
150  ->disableOriginalConstructor()
151  ->getMock();
152  $this->configMock = $this->getMockBuilder(\Magento\Eav\Model\Config::class)
153  ->disableOriginalConstructor()
154  ->getMock();
155  $this->typeFactoryMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\TypeFactory::class)
156  ->disableOriginalConstructor()
157  ->getMock();
158  $this->storeManagerMock = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
159  ->getMock();
160  $this->helperMock = $this->getMockBuilder(\Magento\Eav\Model\ResourceModel\Helper::class)
161  ->disableOriginalConstructor()
162  ->getMock();
163  $this->universalFactoryMock = $this->getMockBuilder(\Magento\Framework\Validator\UniversalFactory::class)
164  ->disableOriginalConstructor()
165  ->getMock();
166  $this->attributeOptionFactoryMock =
167  $this->getMockBuilder(\Magento\Eav\Api\Data\AttributeOptionInterfaceFactory::class)
168  ->disableOriginalConstructor()
169  ->getMock();
170  $this->dataObjectProcessorMock = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
171  ->disableOriginalConstructor()
172  ->getMock();
173  $this->dataObjectHelperMock = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
174  ->disableOriginalConstructor()
175  ->getMock();
176  $this->timezoneMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class)
177  ->getMock();
178  $this->reservedAttributeListMock = $this->getMockBuilder(
179  \Magento\Catalog\Model\Product\ReservedAttributeList::class
180  )
181  ->disableOriginalConstructor()
182  ->getMock();
183  $this->resolverMock = $this->getMockBuilder(\Magento\Framework\Locale\ResolverInterface::class)
184  ->getMock();
185  $this->dateTimeFormatter = $this->createMock(
186  \Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface::class
187  );
188 
189  $this->resourceMock = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\AbstractResource::class)
190  ->setMethods(['_construct', 'getConnection', 'getIdFieldName', 'saveInSetIncluding'])
191  ->getMockForAbstractClass();
192  $this->cacheManager = $this->getMockBuilder(\Magento\Framework\App\CacheInterface::class)
193  ->getMock();
194  $this->eventDispatcher = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
195  ->getMock();
196 
197  $this->contextMock
198  ->expects($this->any())
199  ->method('getCacheManager')
200  ->willReturn($this->cacheManager);
201  $this->contextMock
202  ->expects($this->any())
203  ->method('getEventDispatcher')
204  ->willReturn($this->eventDispatcher);
205 
206  $this->indexerRegistryMock = $this->getMockBuilder(\Magento\Framework\Indexer\IndexerRegistry::class)
207  ->disableOriginalConstructor()
208  ->getMock();
209  $this->attributeMetadataCacheMock = $this->getMockBuilder(AttributeMetadataCache::class)
210  ->disableOriginalConstructor()
211  ->getMock();
212  $this->attribute = $objectManagerHelper->getObject(
213  Attribute::class,
214  [
215  'context' => $this->contextMock,
216  'registry' => $this->registryMock,
217  'extensionFactory' => $this->extensionAttributesFactory,
218  'attributeValueFactory' => $this->attributeValueFactoryMock,
219  'eavConfig' => $this->configMock,
220  'typeFactory' => $this->typeFactoryMock,
221  'storeManager' => $this->storeManagerMock,
222  'helper' => $this->helperMock,
223  'universalFactory' => $this->universalFactoryMock,
224  'attributeOptionFactory' => $this->attributeOptionFactoryMock,
225  'dataObjectProcessor' => $this->dataObjectProcessorMock,
226  'dataObjectHelper' => $this->dataObjectHelperMock,
227  'timezone' => $this->timezoneMock,
228  'reservedAttributeList' => $this->reservedAttributeListMock,
229  'resolver' => $this->resolverMock,
230  'dateTimeFormatter' => $this->dateTimeFormatter,
231  'indexerRegistry' => $this->indexerRegistryMock,
232  'resource' => $this->resourceMock,
233  'attributeMetadataCache' => $this->attributeMetadataCacheMock
234  ]
235  );
236  }
237 
238  public function testAfterSaveEavCache()
239  {
240  $this->configMock
241  ->expects($this->once())
242  ->method('clear');
243  $this->attributeMetadataCacheMock
244  ->expects($this->once())
245  ->method('clean');
246  $this->attribute->afterSave();
247  }
248 
249  public function testAfterDeleteEavCache()
250  {
251  $this->configMock
252  ->expects($this->once())
253  ->method('clear');
254  $this->attributeMetadataCacheMock
255  ->expects($this->once())
256  ->method('clean');
257  $this->attribute->afterDelete();
258  }
259 
260  public function testInvalidate()
261  {
263  $indexerMock = $this->getMockBuilder(\Magento\Framework\Indexer\IndexerInterface::class)
264  ->getMockForAbstractClass();
265 
266  $this->indexerRegistryMock->expects($this->once())
267  ->method('get')
269  ->willReturn($indexerMock);
270 
271  $indexerMock->expects($this->once())
272  ->method('invalidate');
273 
274  $this->attribute->invalidate();
275  }
276 
283  public function testCanBeSearchableInGrid($isSearchableInGrid, $frontendInput, $result)
284  {
285  $this->attribute->setData('is_searchable_in_grid', $isSearchableInGrid);
286  $this->attribute->setData(AttributeInterface::FRONTEND_INPUT, $frontendInput);
287 
288  $this->assertEquals($result, $this->attribute->canBeSearchableInGrid());
289  }
290 
295  {
296  return [
297  [0, 'text', false],
298  [0, 'textarea', false],
299  [1, 'text', true],
300  [1, 'textarea', true],
301  [1, 'date', false],
302  [1, 'boolean', false],
303  [1, 'select', false],
304  [1, 'media_image', false],
305  [1, 'gallery', false],
306  [1, 'multiselect', false],
307  [1, 'image', false],
308  [1, 'price', false],
309  [1, 'weight', false],
310  ];
311  }
312 
319  public function testCanBeFilterableInGrid($isFilterableInGrid, $frontendInput, $result)
320  {
321  $this->attribute->setData('is_filterable_in_grid', $isFilterableInGrid);
322  $this->attribute->setData(AttributeInterface::FRONTEND_INPUT, $frontendInput);
323 
324  $this->assertEquals($result, $this->attribute->canBeFilterableInGrid());
325  }
326 
331  {
332  return [
333  [0, 'text', false],
334  [0, 'date', false],
335  [0, 'select', false],
336  [0, 'boolean', false],
337  [1, 'text', true],
338  [1, 'date', true],
339  [1, 'select', true],
340  [1, 'boolean', true],
341  [1, 'textarea', false],
342  [1, 'media_image', false],
343  [1, 'gallery', false],
344  [1, 'multiselect', false],
345  [1, 'image', false],
346  [1, 'price', false],
347  [1, 'weight', false],
348  ];
349  }
350 }
testCanBeSearchableInGrid($isSearchableInGrid, $frontendInput, $result)
testCanBeFilterableInGrid($isFilterableInGrid, $frontendInput, $result)