Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexableAttributeFilterTest.php
Go to the documentation of this file.
1 <?php
7 
8 class IndexableAttributeFilterTest extends \PHPUnit\Framework\TestCase
9 {
10  public function testFilter()
11  {
12  $catalogResourceMock = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
13  ->disableOriginalConstructor()
14  ->setMethods(['load', 'isIndexable', '__wakeup'])
15  ->getMock();
16  $catalogResourceMock->expects($this->any())
17  ->method('load')
18  ->will($this->returnSelf());
19  $catalogResourceMock->expects($this->at(1))
20  ->method('isIndexable')
21  ->will($this->returnValue(true));
22  $catalogResourceMock->expects($this->at(2))
23  ->method('isIndexable')
24  ->will($this->returnValue(false));
25 
26  $eavAttributeFactoryMock = $this->getMockBuilder(
27  \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory::class
28  )
29  ->disableOriginalConstructor()
30  ->setMethods(['create'])
31  ->getMock();
32  $eavAttributeFactoryMock->expects($this->once())
33  ->method('create')
34  ->will($this->returnValue($catalogResourceMock));
35 
36  $attributeMock1 = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
37  ->disableOriginalConstructor()
38  ->setMethods(['getId', 'getAttributeId', 'getAttributeCode', 'load', '__wakeup'])
39  ->getMock();
40  $attributeMock1->expects($this->any())
41  ->method('getAttributeCode')
42  ->will($this->returnValue('indexable_attribute'));
43  $attributeMock1->expects($this->any())
44  ->method('load')
45  ->will($this->returnSelf());
46 
47  $attributeMock2 = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
48  ->disableOriginalConstructor()
49  ->setMethods(['getId', 'getAttributeId', 'getAttributeCode', 'load', '__wakeup'])
50  ->getMock();
51  $attributeMock2->expects($this->any())
52  ->method('getAttributeCode')
53  ->will($this->returnValue('non_indexable_attribute'));
54  $attributeMock2->expects($this->any())
55  ->method('load')
56  ->will($this->returnSelf());
57 
58  $attributes = [$attributeMock1, $attributeMock2];
59 
60  $groupMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Group::class)
61  ->disableOriginalConstructor()
62  ->setMethods(['getAttributes', '__wakeup'])
63  ->getMock();
64  $groupMock->expects($this->once())
65  ->method('getAttributes')
66  ->will($this->returnValue($attributes));
67 
68  $attributeSetMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Set::class)
69  ->disableOriginalConstructor()
70  ->setMethods(['getGroups', '__wakeup'])
71  ->getMock();
72  $attributeSetMock->expects($this->once())
73  ->method('getGroups')
74  ->will($this->returnValue([$groupMock]));
75 
76  $model = new \Magento\Catalog\Model\Indexer\Product\Eav\Plugin\AttributeSet\IndexableAttributeFilter(
77  $eavAttributeFactoryMock
78  );
79 
80  $this->assertEquals(['indexable_attribute'], $model->filter($attributeSetMock));
81  }
82 }
$attributes
Definition: matrix.phtml:13