/** * Tests if search works. * * @param array $expected * @param Request $request * @param string|int|float $fuzziness * @param int $prefixLength * @param int $maxExpansions * * @dataProvider getTestingData */ public function testFiltering($expected, $request, $fuzziness = null, $prefixLength = null, $maxExpansions = null) { $container = new FiltersContainer(); $fuzzy = new FuzzySearch(); $fuzzy->setRequestField('q'); $fuzzy->setField('color'); $fuzzy->setFuzziness($fuzziness); $fuzzy->setPrefixLength($prefixLength); $fuzzy->setMaxExpansions($maxExpansions); $container->set('fuzzy', $fuzzy); $fmb = new FiltersManager($container, $this->getManager()->getRepository('AcmeTestBundle:Product')); $result = $fmb->execute($request); $actual = []; foreach ($result->getResult() as $doc) { $actual[] = $doc->getId(); } sort($actual); $this->assertEquals($expected, $actual); }
/** * Tests execute method. */ public function testExecute() { $mockFilterState = $this->getMock('ONGR\\FilterManagerBundle\\Filters\\FilterState'); $mockFilterState->expects($this->any())->method('getURLParameters')->will($this->returnValue([])); $mockFilterInterface = $this->getMock('ONGR\\FilterManagerBundle\\Filters\\FilterInterface'); $mockFilterInterface->expects($this->once())->method('preProcessSearch'); $mockFilterInterface->expects($this->once())->method('getSearchRelation')->will($this->returnValue(null)); $searchRequest = new SearchRequest(); $searchRequest->set('filter', $mockFilterState); $mockFiltersContainer = $this->getMock('ONGR\\FilterManagerBundle\\Search\\FiltersContainer'); $mockFiltersContainer->expects($this->once())->method('buildSearchRequest')->will($this->returnValue($searchRequest)); $mockFiltersContainer->expects($this->exactly(2))->method('buildSearch')->will($this->returnValue($this->getMock('ONGR\\ElasticsearchBundle\\DSL\\Search'))); $mockFiltersContainer->expects($this->exactly(2))->method('all')->will($this->returnValue(['filter' => $mockFilterInterface])); $mockFiltersContainer->expects($this->once())->method('getFiltersByRelation')->will($this->returnValue(null)); $mockDocumentIterator = $this->getMockBuilder('ONGR\\ElasticsearchBundle\\Result\\DocumentIterator')->disableOriginalConstructor()->getMock(); $mockRepository = $this->getMockBuilder('ONGR\\ElasticsearchBundle\\ORM\\Repository')->disableOriginalConstructor()->getMock(); $mockRepository->expects($this->once())->method('execute')->will($this->returnValue($mockDocumentIterator)); $filtersManager = new FiltersManager($mockFiltersContainer, $mockRepository); $filtersManager->execute($this->getMock('Symfony\\Component\\HttpFoundation\\Request')); }