public function testAddProductCategoriesFilter() { $condition = ['in' => [1, 2]]; $values = [1, 2]; $conditionType = 'nin'; $preparedSql = "category_id IN(1,2)"; $tableName = "catalog_category_product"; $this->connectionMock->expects($this->any())->method('getId')->willReturn(1); $this->connectionMock->expects($this->exactly(2))->method('prepareSqlCondition')->withConsecutive(['cat.category_id', $condition], ['e.entity_id', [$conditionType => $this->selectMock]])->willReturnOnConsecutiveCalls($preparedSql, 'e.entity_id IN (1,2)'); $this->selectMock->expects($this->once())->method('from')->with(['cat' => $tableName], 'cat.product_id')->willReturnSelf(); $this->selectMock->expects($this->exactly(2))->method('where')->withConsecutive([$preparedSql], ['e.entity_id IN (1,2)'])->willReturnSelf(); $this->collection->addCategoriesFilter([$conditionType => $values]); }
/** * Helper function that adds a FilterGroup to the collection. * * @param \Magento\Framework\Api\Search\FilterGroup $filterGroup * @param Collection $collection * @return void */ protected function addFilterGroupToCollection( \Magento\Framework\Api\Search\FilterGroup $filterGroup, Collection $collection ) { $fields = []; $categoryFilter = []; foreach ($filterGroup->getFilters() as $filter) { $conditionType = $filter->getConditionType() ? $filter->getConditionType() : 'eq'; if ($filter->getField() == 'category_id') { $categoryFilter[$conditionType][] = $filter->getValue(); continue; } $fields[] = ['attribute' => $filter->getField(), $conditionType => $filter->getValue()]; } if ($categoryFilter) { $collection->addCategoriesFilter($categoryFilter); } if ($fields) { $collection->addFieldToFilter($fields); } }