Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AdvancedTest.php
Go to the documentation of this file.
1 <?php
7 
13 class AdvancedTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $collection;
19 
23  protected $resource;
24 
28  protected $resourceProvider;
29 
33  protected $attributes;
34 
38  protected $dataCollection;
39 
43  private $currency;
44 
48  private $storeManager;
49 
53  private $store;
54 
55  protected function setUp()
56  {
57  $this->collection = $this->createPartialMock(
58  \Magento\CatalogSearch\Model\ResourceModel\Advanced\Collection::class,
59  [
60  'addAttributeToSelect',
61  'setStore',
62  'addMinimalPrice',
63  'addTaxPercents',
64  'addStoreFilter',
65  'setVisibility',
66  'addFieldsToFilter'
67  ]
68  );
69  $this->resource = $this->createPartialMock(
70  \Magento\CatalogSearch\Model\ResourceModel\Advanced::class,
71  ['prepareCondition', '__wakeup', 'getIdFieldName']
72  );
73 
74  $this->resourceProvider = $this->getMockBuilder(
75  \Magento\CatalogSearch\Model\ResourceModel\ResourceProvider::class
76  )
77  ->setMethods(['getResource', 'getResourceCollection', 'getAdvancedResultCollection'])
78  ->disableOriginalConstructor()
79  ->getMock();
80 
81  $this->dataCollection = $this->createPartialMock(\Magento\Framework\Data\Collection::class, ['getIterator']);
82 
83  $this->currency = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
84  ->setMethods(['getRate'])
85  ->disableOriginalConstructor()
86  ->getMock();
87  $this->store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
88  ->setMethods(['getCurrentCurrencyCode', 'getBaseCurrencyCode', 'getBaseCurrency'])
89  ->disableOriginalConstructor()
90  ->getMock();
91  $this->store->expects($this->any())
92  ->method('getBaseCurrency')
93  ->willReturn($this->currency);
94  $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
95  ->setMethods(['getStore'])
96  ->getMockForAbstractClass();
97  $this->storeManager->expects($this->any())
98  ->method('getStore')
99  ->willReturn($this->store);
100  }
101 
105  public function addFiltersDataProvider()
106  {
107  return array_merge(
108  [
109  'sku' => [
110  'attributes' => [
111  $this->createAttribute(
112  $this->createBackend('catalog_product_entity'),
113  $this->createSource(),
114  'sku',
115  'SKU',
116  'text',
117  'static'
118  )
119  ],
120  'values' => ['sku' => 'simple']
121  ],
122  'color_multiselect' => [
123  'attributes' => [
124  $this->createAttribute(
125  $this->createBackend('color_multiselect'),
126  $this->createSource(['label' => 'Color']),
127  'color',
128  'Color',
129  'multiselect',
130  'static'
131  )
132  ],
133  'values' => ['color' => [100 => 'red', 200 => 'blue']],
134  'currentCurrencyCode' => 'GBP',
135  'baseCurrencyCode' => 'USD'
136  ],
137  'color_select' => [
138  'attributes' => [
139  $this->createAttribute(
140  $this->createBackend('color_select'),
141  $this->createSource(['label' => 'Color']),
142  'color',
143  'Color',
144  'select',
145  'static'
146  )
147  ],
148  'values' => ['color' => 'red'],
149  'currentCurrencyCode' => 'GBP',
150  'baseCurrencyCode' => 'USD'
151  ],
152  'boolean' => [
153  'attributes' => [
154  $this->createAttribute(
155  $this->createBackend('boolean'),
156  $this->createSource(['label' => 'Color']),
157  'is_active',
158  'Is active?',
159  'boolean',
160  'static'
161  )
162  ],
163  'values' => ['is_active' => 0],
164  'currentCurrencyCode' => 'GBP',
165  'baseCurrencyCode' => 'USD'
166  ],
167  ],
168  $this->addFiltersPriceDataProvider()
169  );
170  }
171 
179  public function testAddFiltersVerifyAddConditionsToRegistry(
180  array $attributes,
181  array $values,
182  $currentCurrencyCode = 'GBP',
183  $baseCurrencyCode = 'USD'
184  ) {
185  $registry = new \Magento\Framework\Registry();
186 
187  $this->collection->expects($this->any())->method('addAttributeToSelect')->will($this->returnSelf());
188  $this->collection->expects($this->any())->method('setStore')->will($this->returnSelf());
189  $this->collection->expects($this->any())->method('addMinimalPrice')->will($this->returnSelf());
190  $this->collection->expects($this->any())->method('addTaxPercents')->will($this->returnSelf());
191  $this->collection->expects($this->any())->method('addStoreFilter')->will($this->returnSelf());
192  $this->collection->expects($this->any())->method('setVisibility')->will($this->returnSelf());
193  $this->resource->expects($this->any())->method('prepareCondition')
194  ->will($this->returnValue(['like' => '%simple%']));
195  $this->resource->expects($this->any())->method('getIdFieldName')->will($this->returnValue('entity_id'));
196  $this->dataCollection->expects($this->any())->method('getIterator')
197  ->will($this->returnValue(new \ArrayIterator($attributes)));
198  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
199 
200  $advancedFactory = $this->getMockBuilder(\Magento\CatalogSearch\Model\ResourceModel\AdvancedFactory::class)
201  ->setMethods(['create'])
202  ->disableOriginalConstructor()
203  ->getMock();
204  $advancedFactory->expects($this->once())->method('create')->willReturn($this->resource);
205 
206  $productCollectionFactory =
207  $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory::class)
208  ->setMethods(['create'])
209  ->disableOriginalConstructor()
210  ->getMock();
211  $productCollectionFactory->expects($this->any())->method('create')->willReturn($this->collection);
212 
213  $this->store->expects($this->any())
214  ->method('getCurrentCurrencyCode')
215  ->willReturn($currentCurrencyCode);
216  $this->store->expects($this->any())
217  ->method('getBaseCurrencyCode')
218  ->willReturn($baseCurrencyCode);
219  $this->currency->expects($this->any())
220  ->method('getRate')
221  ->with($currentCurrencyCode)
222  ->willReturn(1.5);
223 
224  $currency = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
225  ->setMethods(['load', 'format'])
226  ->disableOriginalConstructor()
227  ->getMock();
228  $currency->expects($this->any())
229  ->method('load')
230  ->willReturnSelf();
231  $currency->expects($this->any())
232  ->method('format')
233  ->willReturnArgument(0);
234  $currencyFactory = $this->getMockBuilder(\Magento\Directory\Model\CurrencyFactory::class)
235  ->setMethods(['create'])
236  ->disableOriginalConstructor()
237  ->getMock();
238  $currencyFactory->expects($this->any())
239  ->method('create')
240  ->willReturn($currency);
241 
243  $instance = $objectManager->getObject(
244  \Magento\CatalogSearch\Model\Advanced::class,
245  [
246  'registry' => $registry,
247  'resourceProvider' => $this->resourceProvider,
248  'data' => ['attributes' => $this->dataCollection],
249  'advancedFactory' => $advancedFactory,
250  'productCollectionFactory' => $productCollectionFactory,
251  'storeManager' => $this->storeManager,
252  'currencyFactory' => $currencyFactory,
253  ]
254  );
255  $instance->addFilters($values);
256  $this->assertNotNull($registry->registry('advanced_search_conditions'));
257  }
258 
263  private function createBackend($table)
264  {
265  $backend = $this->createPartialMock(
266  \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend::class,
267  ['getTable']
268  );
269  $backend->expects($this->once())
270  ->method('getTable')
271  ->willReturn($table);
272  return $backend;
273  }
274 
279  private function createSource($optionText = 'optionText')
280  {
281  $source = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource::class)
282  ->setMethods(['getOptionText'])
283  ->disableOriginalConstructor()
284  ->getMockForAbstractClass();
285  $source->expects($this->any())
286  ->method('getOptionText')
287  ->willReturn($optionText);
288  return $source;
289  }
290 
294  private function addFiltersPriceDataProvider()
295  {
296  return [
297  'price_without_currency' => [
298  'attributes' => [
299  $this->createAttribute(
300  $this->createBackend('table_price_without_currency'),
301  $this->createSource(),
302  'price',
303  'Price',
304  'multiselect',
305  'static'
306  )
307  ],
308  'values' => ['price' => ['from' => 10, 'to' => 40]],
309  'currentCurrencyCode' => 'GBP',
310  'baseCurrencyCode' => 'USD'
311  ],
312  'price_without_to' => [
313  'attributes' => [
314  $this->createAttribute(
315  $this->createBackend('price_without_to'),
316  $this->createSource(),
317  'price',
318  'Price',
319  'multiselect',
320  'static'
321  )
322  ],
323  'values' => ['price' => ['from' => 10, 'to' => '']],
324  'currentCurrencyCode' => 'GBP',
325  'baseCurrencyCode' => 'USD'
326  ],
327  'price_without_from' => [
328  'attributes' => [
329  $this->createAttribute(
330  $this->createBackend('price_without_from'),
331  $this->createSource(),
332  'price',
333  'Price',
334  'multiselect',
335  'static'
336  )
337  ],
338  'values' => ['price' => ['from' => '', 'to' => 30]],
339  'currentCurrencyCode' => 'GBP',
340  'baseCurrencyCode' => 'USD'
341  ],
342  'price_empty' => [
343  'attributes' => [
344  $this->createAttribute(
345  $this->createBackend('price_empty'),
346  $this->createSource(),
347  'price',
348  'Price',
349  'multiselect',
350  'static'
351  )
352  ],
353  'values' => ['price' => ['from' => '', 'to' => '']],
354  'currentCurrencyCode' => 'GBP',
355  'baseCurrencyCode' => 'USD'
356  ],
357  'price_with_currency' => [
358  'attributes' => [
359  $this->createAttribute(
360  $this->createBackend('price_with_currency'),
361  $this->createSource(),
362  'price',
363  'Price',
364  'multiselect',
365  'static'
366  )
367  ],
368  'values' => ['price' => ['from' => 10, 'to' => 40, 'currency' => 'ASD']],
369  'currentCurrencyCode' => 'GBP',
370  'baseCurrencyCode' => 'USD'
371  ]
372  ];
373  }
374 
384  private function createAttribute(
385  $backend,
386  $source = null,
387  $attributeCode = null,
388  $storeLabel = null,
389  $frontendInput = null,
390  $backendType = null
391  ) {
392  $attribute = $this->createPartialMock(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class, [
393  'getAttributeCode',
394  'getStoreLabel',
395  'getFrontendInput',
396  'getBackend',
397  'getBackendType',
398  'getSource',
399  '__wakeup'
400  ]);
401  $attribute->expects($this->any())->method('getBackend')->willReturn($backend);
402  $attribute->expects($this->any())->method('getSource')->willReturn($source);
403  $attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode));
404  $attribute->expects($this->any())->method('getStoreLabel')->will($this->returnValue($storeLabel));
405  $attribute->expects($this->any())->method('getFrontendInput')->will($this->returnValue($frontendInput));
406  $attribute->expects($this->any())->method('getBackendType')->will($this->returnValue($backendType));
407  return $attribute;
408  }
409 }
$currentCurrencyCode
Definition: currency.phtml:17
$objectManager
Definition: bootstrap.php:17
$source
Definition: source.php:23
$values
Definition: options.phtml:88
$attributeCode
Definition: extend.phtml:12
$table
Definition: trigger.php:14