/** * Prepare price filter model * * @param \Magento\TestFramework\Request|null $request */ protected function _prepareFilter($request = null) { /** @var $layer \Magento\Catalog\Model\Layer */ $layer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Layer\\Category'); $layer->setCurrentCategory(4); $layer->setState(\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Layer\\State')); /** @var $filter \Magento\Catalog\Model\Layer\Filter\Price */ $filter = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Layer\\Filter\\Price', array('layer' => $layer)); $filter->setLayer($layer)->setAttributeModel(new \Magento\Framework\Object(array('attribute_code' => 'price'))); if (!is_null($request)) { $filter->apply($request, \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Framework\\View\\LayoutInterface')->createBlock('Magento\\Framework\\View\\Element\\Text')); $interval = $filter->getInterval(); if ($interval) { $this->_model->setLimits($interval[0], $interval[1]); } } $collection = $layer->getProductCollection(); $this->_model->setPricesModel($filter)->setStatistics($collection->getMinPrice(), $collection->getMaxPrice(), $collection->getPriceStandardDeviation(), $collection->getSize()); }
/** * @dataProvider pricesSegmentationDataProvider */ public function testPricesSegmentation($categoryId, $intervalsNumber, $intervalItems) { $this->_layer->setCurrentCategory($categoryId); $collection = $this->_layer->getProductCollection(); $memoryUsedBefore = memory_get_usage(); $this->_model->setPricesModel($this->_filter)->setStatistics($collection->getMinPrice(), $collection->getMaxPrice(), $collection->getPriceStandardDeviation(), $collection->getSize()); if (!is_null($intervalsNumber)) { $this->assertEquals($intervalsNumber, $this->_model->getIntervalsNumber()); } $items = $this->_model->calculateSeparators(); $this->assertEquals(array_keys($intervalItems), array_keys($items)); for ($i = 0; $i < count($intervalItems); ++$i) { $this->assertInternalType('array', $items[$i]); $this->assertEquals($intervalItems[$i]['from'], $items[$i]['from']); $this->assertEquals($intervalItems[$i]['to'], $items[$i]['to']); $this->assertEquals($intervalItems[$i]['count'], $items[$i]['count']); } // Algorythm should use less than 10M $this->assertLessThan(10 * 1024 * 1024, memory_get_usage() - $memoryUsedBefore); }
/** * Get data generated by algorithm for build price filter items * * @return array */ protected function _getCalculatedItemsData() { $collection = $this->getLayer()->getProductCollection(); $appliedInterval = $this->getInterval(); if ($appliedInterval && $collection->getPricesCount() <= $this->getIntervalDivisionLimit()) { return array(); } $this->_priceAlgorithm->setPricesModel($this)->setStatistics($collection->getMinPrice(), $collection->getMaxPrice(), $collection->getPriceStandardDeviation(), $collection->getPricesCount()); if ($appliedInterval) { if ($appliedInterval[0] == $appliedInterval[1] || $appliedInterval[1] === '0') { return array(); } $this->_priceAlgorithm->setLimits($appliedInterval[0], $appliedInterval[1]); } $items = array(); foreach ($this->_priceAlgorithm->calculateSeparators() as $separator) { $items[] = array('label' => $this->_renderRangeLabel($separator['from'], $separator['to']), 'value' => ($separator['from'] == 0 ? '' : $separator['from']) . '-' . $separator['to'] . $this->_getAdditionalRequestData(), 'count' => $separator['count']); } return $items; }