Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OptionSelectBuilderTest.php
Go to the documentation of this file.
1 <?php
7 
16 
20 class OptionSelectBuilderTest extends \PHPUnit\Framework\TestCase
21 {
25  private $model;
26 
30  private $objectManagerHelper;
31 
35  private $attributeResourceMock;
36 
40  private $attributeOptionProviderMock;
41 
45  private $select;
46 
50  private $connectionMock;
51 
55  private $abstractAttributeMock;
56 
60  private $scope;
61 
62  protected function setUp()
63  {
64  $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
65  ->setMethods(['select', 'getIfNullSql'])
66  ->disableOriginalConstructor()
67  ->getMockForAbstractClass();
68  $this->select = $this->getMockBuilder(Select::class)
69  ->setMethods(['from', 'joinInner', 'joinLeft', 'where', 'columns', 'order'])
70  ->disableOriginalConstructor()
71  ->getMock();
72  $this->connectionMock->expects($this->atLeastOnce())
73  ->method('select', 'getIfNullSql')
74  ->willReturn($this->select);
75 
76  $this->attributeResourceMock = $this->getMockBuilder(Attribute::class)
77  ->setMethods(['getTable', 'getConnection'])
78  ->disableOriginalConstructor()
79  ->getMock();
80  $this->attributeResourceMock->expects($this->atLeastOnce())
81  ->method('getConnection')
82  ->willReturn($this->connectionMock);
83 
84  $this->attributeOptionProviderMock = $this->getMockBuilder(OptionProvider::class)
85  ->setMethods(['getProductEntityLinkField'])
86  ->disableOriginalConstructor()
87  ->getMock();
88 
89  $this->abstractAttributeMock = $this->getMockBuilder(AbstractAttribute::class)
90  ->setMethods(['getBackendTable', 'getAttributeId', 'getSourceModel'])
91  ->disableOriginalConstructor()
92  ->getMockForAbstractClass();
93 
94  $this->scope = $this->getMockBuilder(ScopeInterface::class)
95  ->disableOriginalConstructor()
96  ->getMockForAbstractClass();
97 
98  $this->objectManagerHelper = new ObjectManagerHelper($this);
99  $this->model = $this->objectManagerHelper->getObject(
100  OptionSelectBuilder::class,
101  [
102  'attributeResource' => $this->attributeResourceMock,
103  'attributeOptionProvider' => $this->attributeOptionProviderMock,
104  ]
105  );
106  }
107 
111  public function testGetSelect()
112  {
113  $this->select->expects($this->exactly(1))->method('from')->willReturnSelf();
114  $this->select->expects($this->exactly(1))->method('columns')->willReturnSelf();
115  $this->select->expects($this->exactly(5))->method('joinInner')->willReturnSelf();
116  $this->select->expects($this->exactly(4))->method('joinLeft')->willReturnSelf();
117  $this->select->expects($this->exactly(1))->method('order')->willReturnSelf();
118  $this->select->expects($this->exactly(2))->method('where')->willReturnSelf();
119 
120  $this->attributeResourceMock->expects($this->exactly(9))
121  ->method('getTable')
122  ->will(
123  $this->returnValueMap(
124  [
125  ['catalog_product_super_attribute', 'catalog_product_super_attribute value'],
126  ['catalog_product_entity', 'catalog_product_entity value'],
127  ['catalog_product_super_link', 'catalog_product_super_link value'],
128  ['eav_attribute', 'eav_attribute value'],
129  ['catalog_product_entity', 'catalog_product_entity value'],
130  ['catalog_product_super_attribute_label', 'catalog_product_super_attribute_label value'],
131  ['eav_attribute_option', 'eav_attribute_option value'],
132  ['eav_attribute_option_value', 'eav_attribute_option_value value']
133  ]
134  )
135  );
136 
137  $this->abstractAttributeMock->expects($this->atLeastOnce())
138  ->method('getAttributeId')
139  ->willReturn('getAttributeId value');
140  $this->abstractAttributeMock->expects($this->atLeastOnce())
141  ->method('getBackendTable')
142  ->willReturn('getMainTable value');
143 
144  $this->scope->expects($this->any())->method('getId')->willReturn(123);
145 
146  $this->assertEquals(
147  $this->select,
148  $this->model->getSelect($this->abstractAttributeMock, 4, $this->scope)
149  );
150  }
151 
156  {
157  $this->select->expects($this->exactly(1))->method('from')->willReturnSelf();
158  $this->select->expects($this->exactly(0))->method('columns')->willReturnSelf();
159  $this->select->expects($this->exactly(5))->method('joinInner')->willReturnSelf();
160  $this->select->expects($this->exactly(2))->method('joinLeft')->willReturnSelf();
161  $this->select->expects($this->exactly(1))->method('order')->willReturnSelf();
162  $this->select->expects($this->exactly(2))->method('where')->willReturnSelf();
163 
164  $this->attributeResourceMock->expects($this->exactly(7))
165  ->method('getTable')
166  ->will(
167  $this->returnValueMap(
168  [
169  ['catalog_product_super_attribute', 'catalog_product_super_attribute value'],
170  ['catalog_product_entity', 'catalog_product_entity value'],
171  ['catalog_product_super_link', 'catalog_product_super_link value'],
172  ['eav_attribute', 'eav_attribute value'],
173  ['catalog_product_entity', 'catalog_product_entity value'],
174  ['catalog_product_super_attribute_label', 'catalog_product_super_attribute_label value'],
175  ['eav_attribute_option', 'eav_attribute_option value']
176  ]
177  )
178  );
179 
180  $this->abstractAttributeMock->expects($this->atLeastOnce())
181  ->method('getAttributeId')
182  ->willReturn('getAttributeId value');
183  $this->abstractAttributeMock->expects($this->atLeastOnce())
184  ->method('getBackendTable')
185  ->willReturn('getMainTable value');
186  $this->abstractAttributeMock->expects($this->atLeastOnce())
187  ->method('getSourceModel')
188  ->willReturn('source model value');
189 
190  $this->scope->expects($this->any())->method('getId')->willReturn(123);
191 
192  $this->assertEquals(
193  $this->select,
194  $this->model->getSelect($this->abstractAttributeMock, 4, $this->scope)
195  );
196  }
197 }