protected function setUp() { $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Layer\\Filter\\Price\\Algorithm'); $this->_layer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Layer\\Category'); $this->_filter = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Layer\\Filter\\Price', array('layer' => $this->_layer)); $this->_filter->setAttributeModel(new \Magento\Framework\Object(array('attribute_code' => 'price'))); }
public function testGetSetCurrencyRate() { $this->assertEquals(1, $this->_model->getCurrencyRate()); $currencyRate = 42; $this->_model->setCurrencyRate($currencyRate); $this->assertEquals($currencyRate, $this->_model->getData('currency_rate')); }
public function testGetItems() { $this->target->setAttributeModel($this->attribute); $attributeCode = 'attributeCode'; $this->attribute->expects($this->any())->method('getAttributeCode')->will($this->returnValue($attributeCode)); $this->algorithm->expects($this->any())->method('getItemsData')->will($this->returnValue([])); $this->target->getItems(); }
/** * Apply price range filter to product collection * * @param \Magento\Catalog\Model\Layer\Filter\Price $filter * @return $this */ public function applyPriceRange($filter) { $interval = $filter->getInterval(); if (!$interval) { return $this; } list($from, $to) = $interval; if ($from === '' && $to === '') { return $this; } $select = $filter->getLayer()->getProductCollection()->getSelect(); $priceExpr = $this->_getPriceExpression($filter, $select, false); if ($to !== '') { $to = (double) $to; if ($from == $to) { $to += self::MIN_POSSIBLE_PRICE; } } if ($from !== '') { $select->where($priceExpr . ' >= ' . $this->_getComparingValue($from, $filter)); } if ($to !== '') { $select->where($priceExpr . ' < ' . $this->_getComparingValue($to, $filter)); } return $this; }
/** * Find price separator for the quantile * * @param int $quantileNumber should be from 1 to n-1 where n is number of intervals * @return array|null */ protected function _findPriceSeparator($quantileNumber) { if ($quantileNumber < 1 || $quantileNumber >= $this->getIntervalsNumber()) { return null; } $prices = array(); $quantileInterval = $this->_getQuantileInterval($quantileNumber); $intervalPricesCount = $quantileInterval[1] - $quantileInterval[0] + 1; $offset = $quantileInterval[0]; if (!is_null($this->_lastPriceLimiter[0])) { $offset -= $this->_lastPriceLimiter[0]; } if ($offset < 0) { $intervalPricesCount += $offset; $prices = array_slice($this->_prices, $this->_lastPriceLimiter[0] + $offset - $this->_quantileInterval[0], -$offset); $offset = 0; } $lowerPrice = $this->_lastPriceLimiter[1]; if (!is_null($this->_lowerLimit)) { $lowerPrice = max($lowerPrice, $this->_lowerLimit); } if ($intervalPricesCount >= 0) { $prices = array_merge($prices, $this->_pricesModel->loadPrices($intervalPricesCount + 1, $offset, $lowerPrice, $this->_upperLimit)); } $lastPrice = $prices[$intervalPricesCount - 1]; $bestRoundPrice = array(); if ($lastPrice == $prices[0]) { if ($quantileNumber == 1 && $offset) { $additionalPrices = $this->_pricesModel->loadPreviousPrices($lastPrice, $quantileInterval[0], $this->_lowerLimit); if ($additionalPrices) { $quantileInterval[0] -= count($additionalPrices); $prices = array_merge($additionalPrices, $prices); $bestRoundPrice = $this->_findRoundPrice($prices[0] + \Magento\Catalog\Model\Resource\Layer\Filter\Price::MIN_POSSIBLE_PRICE / 10, $lastPrice, false); } } if ($quantileNumber == $this->getIntervalsNumber() - 1) { $pricesCount = count($prices); if ($prices[$pricesCount - 1] > $lastPrice) { $additionalPrices = array($prices[$pricesCount - 1]); } else { $additionalPrices = $this->_pricesModel->loadNextPrices($lastPrice, $this->_count - $quantileInterval[0] - count($prices), $this->_upperLimit); } if ($additionalPrices) { $quantileInterval[1] = $quantileInterval[0] + count($prices) - 1; if ($prices[$pricesCount - 1] <= $lastPrice) { $quantileInterval[1] += count($additionalPrices); $prices = array_merge($prices, $additionalPrices); } $upperBestRoundPrice = $this->_findRoundPrice($lastPrice + \Magento\Catalog\Model\Resource\Layer\Filter\Price::MIN_POSSIBLE_PRICE / 10, $prices[count($prices) - 1], false); $this->_mergeRoundPrices($bestRoundPrice, $upperBestRoundPrice); } } } else { $bestRoundPrice = $this->_findRoundPrice($prices[0] + \Magento\Catalog\Model\Resource\Layer\Filter\Price::MIN_POSSIBLE_PRICE / 10, $lastPrice); } $this->_quantileInterval = $quantileInterval; $this->_prices = $prices; if (empty($bestRoundPrice)) { $this->_skippedQuantilesUpperLimits[$quantileNumber] = $quantileInterval[1]; return $bestRoundPrice; } $pricesCount = count($prices); if ($prices[$pricesCount - 1] > $lastPrice) { $this->_lastPriceLimiter = array($quantileInterval[0] + $pricesCount - 1, $prices[$pricesCount - 1]); } ksort($bestRoundPrice, SORT_NUMERIC); foreach ($bestRoundPrice as $index => &$bestRoundPriceValues) { if (empty($bestRoundPriceValues)) { unset($bestRoundPrice[$index]); } else { sort($bestRoundPriceValues); } } return array_reverse($bestRoundPrice); }