11
11
use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
12
12
use Magento \Catalog \Model \ResourceModel \Product ;
13
13
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
14
+ use Magento \Catalog \Model \ResourceModel \Product \Collection \ProductLimitation ;
14
15
use Magento \CatalogWidget \Model \Rule \Condition \Product as ProductWidget ;
15
16
use Magento \Eav \Model \Config ;
16
17
use Magento \Eav \Model \Entity \AbstractEntity ;
@@ -57,9 +58,13 @@ protected function setUp(): void
57
58
$ storeManager = $ this ->getMockForAbstractClass (StoreManagerInterface::class);
58
59
$ storeMock = $ this ->getMockForAbstractClass (StoreInterface::class);
59
60
$ storeManager ->expects ($ this ->any ())->method ('getStore ' )->willReturn ($ storeMock );
61
+ $ storeMock ->method ('getId ' )
62
+ ->willReturn (1 );
60
63
$ this ->productResource = $ this ->createMock (Product::class);
61
64
$ this ->productResource ->expects ($ this ->once ())->method ('loadAllAttributes ' )->willReturnSelf ();
62
65
$ this ->productResource ->expects ($ this ->once ())->method ('getAttributesByCode ' )->willReturn ([]);
66
+ $ connection = $ this ->getMockForAbstractClass (AdapterInterface::class);
67
+ $ this ->productResource ->method ('getConnection ' )->willReturn ($ connection );
63
68
$ productCategoryList = $ this ->getMockBuilder (ProductCategoryList::class)
64
69
->disableOriginalConstructor ()
65
70
->getMock ();
@@ -100,9 +105,6 @@ public function testAddToCollection()
100
105
$ entityMock = $ this ->createMock (AbstractEntity::class);
101
106
$ entityMock ->expects ($ this ->once ())->method ('getLinkField ' )->willReturn ('entitiy_id ' );
102
107
$ this ->attributeMock ->expects ($ this ->once ())->method ('getEntity ' )->willReturn ($ entityMock );
103
- $ connection = $ this ->getMockForAbstractClass (AdapterInterface::class);
104
-
105
- $ this ->productResource ->expects ($ this ->atLeastOnce ())->method ('getConnection ' )->willReturn ($ connection );
106
108
107
109
$ this ->model ->addToCollection ($ collectionMock );
108
110
}
@@ -119,4 +121,116 @@ public function testGetMappedSqlFieldSku()
119
121
$ this ->model ->setAttribute ('attribute_set_id ' );
120
122
$ this ->assertEquals ('e.attribute_set_id ' , $ this ->model ->getMappedSqlField ());
121
123
}
124
+
125
+ /**
126
+ * Test getMappedSqlField method for price attribute.
127
+ *
128
+ * @dataProvider getMappedSqlFieldPriceDataProvider
129
+ * @param bool $isScopeGlobal
130
+ * @param bool $isUsingPriceIndex
131
+ * @param string $expectedMappedField
132
+ */
133
+ public function testGetMappedSqlFieldPrice (
134
+ bool $ isScopeGlobal ,
135
+ bool $ isUsingPriceIndex ,
136
+ string $ expectedMappedField
137
+ ): void {
138
+ $ productLimitation = new ProductLimitation ();
139
+ $ productLimitation ['use_price_index ' ] = $ isUsingPriceIndex ;
140
+ $ collectionMock = $ this ->mockCollection (
141
+ [
142
+ 'getLimitationFilters ' => $ productLimitation ,
143
+ 'getAllAttributeValues ' => [
144
+ 1 => [
145
+ 0 => 10 ,
146
+ 1 => 11
147
+ ]
148
+ ]
149
+ ]
150
+ );
151
+ $ this ->mockAttribute (
152
+ [
153
+ 'getAttributeCode ' => 'price ' ,
154
+ 'isScopeGlobal ' => $ isScopeGlobal ,
155
+ 'getBackendType ' => 'decimal '
156
+ ]
157
+ );
158
+ $ this ->model ->setAttribute ('price ' );
159
+ $ this ->model ->setValue (10 );
160
+ $ this ->model ->setOperator ('>= ' );
161
+ $ this ->model ->collectValidatedAttributes ($ collectionMock );
162
+ $ this ->assertEquals ($ expectedMappedField , $ this ->model ->getMappedSqlField ());
163
+ }
164
+
165
+ /**
166
+ * @return array
167
+ */
168
+ public function getMappedSqlFieldPriceDataProvider (): array
169
+ {
170
+ return [
171
+ [
172
+ true ,
173
+ true ,
174
+ 'price_index.min_price '
175
+ ],
176
+ [
177
+ true ,
178
+ false ,
179
+ 'at_price.value '
180
+ ],
181
+ [
182
+ false ,
183
+ true ,
184
+ 'price_index.min_price '
185
+ ],
186
+ [
187
+ false ,
188
+ false ,
189
+ 'e.entity_id '
190
+ ],
191
+ ];
192
+ }
193
+
194
+ /**
195
+ * @param array $configuration
196
+ */
197
+ private function mockAttribute (array $ configuration = []): void
198
+ {
199
+ $ defaultConfiguration = [
200
+ 'getAttributeCode ' => 'code ' ,
201
+ 'isStatic ' => false ,
202
+ 'getBackend ' => true ,
203
+ 'isScopeGlobal ' => true ,
204
+ 'getBackendType ' => 'int ' ,
205
+ ];
206
+ $ configuration = array_merge ($ defaultConfiguration , $ configuration );
207
+ $ this ->attributeMock ->method ('getAttributeCode ' )
208
+ ->willReturn ($ configuration ['getAttributeCode ' ]);
209
+ $ this ->attributeMock ->method ('isStatic ' )
210
+ ->willReturn ($ configuration ['isStatic ' ]);
211
+ $ this ->attributeMock ->method ('getBackend ' )
212
+ ->willReturn ($ configuration ['getBackend ' ]);
213
+ $ this ->attributeMock ->method ('isScopeGlobal ' )
214
+ ->willReturn ($ configuration ['isScopeGlobal ' ]);
215
+ $ this ->attributeMock ->method ('getBackendType ' )
216
+ ->willReturn ($ configuration ['getBackendType ' ]);
217
+ $ entityMock = $ this ->createMock (AbstractEntity::class);
218
+ $ entityMock ->method ('getLinkField ' )
219
+ ->willReturn ('entitiy_id ' );
220
+ $ this ->attributeMock ->method ('getEntity ' )
221
+ ->willReturn ($ entityMock );
222
+ }
223
+
224
+ /**
225
+ * @param array $configuration
226
+ * @return Collection
227
+ */
228
+ private function mockCollection (array $ configuration = []): Collection
229
+ {
230
+ $ collectionMock = $ this ->createConfiguredMock (Collection::class, $ configuration );
231
+ $ selectMock = $ this ->createMock (Select::class);
232
+ $ collectionMock ->method ('getSelect ' )
233
+ ->willReturn ($ selectMock );
234
+ return $ collectionMock ;
235
+ }
122
236
}
0 commit comments