diff --git a/src/Dto/Search/Query/Sort/Criteria.php b/src/Dto/Search/Query/Sort/Criteria.php index e73e92d..54ac7af 100644 --- a/src/Dto/Search/Query/Sort/Criteria.php +++ b/src/Dto/Search/Query/Sort/Criteria.php @@ -10,7 +10,7 @@ abstract class Criteria { public function __construct( - public readonly Direction $direction + public readonly Direction $direction = Direction::ASC ) { } } diff --git a/src/Service/Search/InternalMediaResourceFactory.php b/src/Service/Search/InternalMediaResourceFactory.php index bdf7a2f..f5b62f4 100644 --- a/src/Service/Search/InternalMediaResourceFactory.php +++ b/src/Service/Search/InternalMediaResourceFactory.php @@ -6,6 +6,7 @@ use Atoolo\Resource\Resource; use Atoolo\Resource\ResourceLoader; +use LogicException; use Solarium\QueryType\Select\Result\Document; /** @@ -25,18 +26,27 @@ public function __construct( public function accept(Document $document): bool { $metaLocation = $this->getMetaLocation($document); + if ($metaLocation === null) { + return false; + } return $this->resourceLoader->exists($metaLocation); } public function create(Document $document): Resource { $metaLocation = $this->getMetaLocation($document); + if ($metaLocation === null) { + throw new LogicException('document should contains a url'); + } return $this->resourceLoader->load($metaLocation); } - private function getMetaLocation(Document $document): string + private function getMetaLocation(Document $document): ?string { - $location = $this->getField($document, 'url') ?? ''; + $location = $this->getField($document, 'url'); + if ($location === null) { + return null; + } return $location . '.meta.php'; } diff --git a/src/Service/Search/InternalResourceFactory.php b/src/Service/Search/InternalResourceFactory.php index 0568e7e..30620f5 100644 --- a/src/Service/Search/InternalResourceFactory.php +++ b/src/Service/Search/InternalResourceFactory.php @@ -6,6 +6,7 @@ use Atoolo\Resource\Resource; use Atoolo\Resource\ResourceLoader; +use LogicException; use Solarium\QueryType\Select\Result\Document; /** @@ -22,13 +23,19 @@ public function __construct( public function accept(Document $document): bool { - $location = $this->getField($document, 'url') ?? ''; + $location = $this->getField($document, 'url'); + if ($location === null) { + return false; + } return str_ends_with($location, '.php'); } public function create(Document $document): Resource { - $location = $this->getField($document, 'url') ?? ''; + $location = $this->getField($document, 'url'); + if ($location === null) { + throw new LogicException('document should contains a url'); + } return $this->resourceLoader->load($location); } diff --git a/src/Service/Search/SolrMoreLikeThis.php b/src/Service/Search/SolrMoreLikeThis.php index de553e5..2036517 100644 --- a/src/Service/Search/SolrMoreLikeThis.php +++ b/src/Service/Search/SolrMoreLikeThis.php @@ -49,9 +49,9 @@ private function buildSolrQuery( // Filter foreach ($query->filter as $filter) { - $solrQuery->createFilterQuery($filter->getKey()) + $solrQuery->createFilterQuery($filter->key) ->setQuery($filter->getQuery()) - ->setTags($filter->getTags()); + ->setTags($filter->tags); } return $solrQuery; diff --git a/src/Service/Search/SolrSelect.php b/src/Service/Search/SolrSelect.php index 6787a04..87d979e 100644 --- a/src/Service/Search/SolrSelect.php +++ b/src/Service/Search/SolrSelect.php @@ -171,6 +171,7 @@ private function addFilterQueriesToSolrQuery( foreach ($filterList as $filter) { $key = $filter->key ?? uniqid('', true); + $fq = $solrQuery->createFilterQuery($key); $solrQuery->createFilterQuery($key) ->setQuery($filter->getQuery()) ->setTags($filter->tags); diff --git a/src/Service/Search/SolrSuggest.php b/src/Service/Search/SolrSuggest.php index 960ee49..9a829ef 100644 --- a/src/Service/Search/SolrSuggest.php +++ b/src/Service/Search/SolrSuggest.php @@ -8,7 +8,7 @@ use Atoolo\Search\Dto\Search\Result\Suggestion; use Atoolo\Search\Dto\Search\Result\SuggestResult; use Atoolo\Search\Exception\UnexpectedResultException; -use Atoolo\Search\Service\SolrParameterClientFactory; +use Atoolo\Search\Service\SolrClientFactory; use Atoolo\Search\SuggestSearcher; use JsonException; use Solarium\Core\Client\Client; @@ -29,7 +29,7 @@ class SolrSuggest implements SuggestSearcher private const INDEX_SUGGEST_FIELD = 'raw_content'; public function __construct( - private readonly SolrParameterClientFactory $clientFactory + private readonly SolrClientFactory $clientFactory ) { } @@ -75,9 +75,9 @@ private function buildSolrQuery( // Filter foreach ($query->filter as $filter) { - $solrQuery->createFilterQuery($filter->getKey()) + $solrQuery->createFilterQuery($filter->key) ->setQuery($filter->getQuery()) - ->setTags($filter->getTags()); + ->setTags($filter->tags); } return $solrQuery; diff --git a/test/Dto/Search/Query/SelectQueryBuilderTest.php b/test/Dto/Search/Query/SelectQueryBuilderTest.php index 6c5257a..65ab23f 100644 --- a/test/Dto/Search/Query/SelectQueryBuilderTest.php +++ b/test/Dto/Search/Query/SelectQueryBuilderTest.php @@ -10,8 +10,10 @@ use Atoolo\Search\Dto\Search\Query\SelectQueryBuilder; use Atoolo\Search\Dto\Search\Query\Sort\Criteria; use InvalidArgumentException; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; +#[CoversClass(SelectQueryBuilder::class)] class SelectQueryBuilderTest extends TestCase { private SelectQueryBuilder $builder; diff --git a/test/Service/Indexer/SolrIndexerTest.php b/test/Service/Indexer/SolrIndexerTest.php index 80c9c3c..b87e89c 100644 --- a/test/Service/Indexer/SolrIndexerTest.php +++ b/test/Service/Indexer/SolrIndexerTest.php @@ -17,6 +17,7 @@ use Atoolo\Search\Service\SolrClientFactory; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\MockObject\Exception; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\Stub; use PHPUnit\Framework\TestCase; use Solarium\Client; @@ -28,9 +29,11 @@ #[CoversClass(SolrIndexer::class)] class SolrIndexerTest extends TestCase { + private array $availableIndexes = ['test', 'test-en_US']; + private ResourceLoader&Stub $resourceLoader; - private IndexerProgressHandler $indexerProgressHandler; + private IndexerProgressHandler&MockObject $indexerProgressHandler; private SolrIndexer $indexer; @@ -44,7 +47,7 @@ class SolrIndexerTest extends TestCase private IndexingAborter&Stub $aborter; - private DocumentEnricher $documentEnricher; + private DocumentEnricher&MockObject $documentEnricher; private TranslationSplitter $translationSplitter; @@ -74,11 +77,17 @@ public function setUp(): void $this->updateQuery->method('createDocument') ->willReturn($this->createStub(IndexSchema2xDocument::class)); $coreAdminResult = $this->createStub(CoreAdminResult::class); - $coreAdminResultStatus = $this->createStub(StatusResult::class); - $coreAdminResultStatus->method('getCoreName') - ->willReturn('test'); $coreAdminResult->method('getStatusResults') - ->willReturn([$coreAdminResultStatus]); + ->willReturnCallback(function () { + $results = []; + foreach($this->availableIndexes as $index) { + $result = $this->createStub(StatusResult::class); + $result->method('getCoreName') + ->willReturn($index); + $results[] = $result; + } + return $results; + }); $this->solrClient->method('createUpdate') ->willReturn($this->updateQuery); $this->solrClient->method('update') @@ -131,7 +140,9 @@ public function testIndexAllWithChunks(): void $this->finder->method('findAll') ->willReturn([ '/a/b.php', + '/a/b.php.translations/en_US.php', '/a/c.php', + '/a/c.php.translations/fr_FR.php', '/a/d.php', '/a/e.php', '/a/f.php', @@ -140,7 +151,8 @@ public function testIndexAllWithChunks(): void '/a/i.php', '/a/j.php', '/a/k.php', - '/a/l.php' + '/a/l.php', + '/a/error.php' ]); $this->updateResult->method('getStatus') @@ -149,11 +161,17 @@ public function testIndexAllWithChunks(): void $this->documentEnricher->method('isIndexable') ->willReturn(true); - $this->documentEnricher->expects($this->exactly(11)) - ->method('enrichDocument'); + $this->documentEnricher + ->method('enrichDocument') + ->willReturnCallback(function ($resource, $doc) { + if ($resource->getLocation() === '/a/error.php') { + throw new \Exception('test'); + } + return $doc; + }); $addDocumentsCalls = 0; - $this->updateQuery->expects($this->exactly(2)) + $this->updateQuery->expects($this->exactly(3)) ->method('addDocuments') ->willReturnCallback( function ($documents) use (&$addDocumentsCalls) { @@ -181,6 +199,9 @@ function ($documents) use (&$addDocumentsCalls) { 10 ); + $this->indexerProgressHandler->expects($this->exactly(2)) + ->method('error'); + $this->indexer->index($parameter); } @@ -403,4 +424,35 @@ public function testIndexPathWithLocParameterAndPath(): void $this->indexer->index($parameter); } + + public function testWithoutAvailableIndexes(): void + { + + $this->availableIndexes = []; + $this->finder->method('findAll') + ->willReturn([ + '/a/b.php', + '/a/c.php', + '/a/d.php', + '/a/e.php', + '/a/f.php', + '/a/g.php', + '/a/h.php', + '/a/i.php', + '/a/j.php', + '/a/k.php', + '/a/l.php' + ]); + + $parameter = new IndexerParameter( + 'test', + 10, + 10 + ); + + $this->indexerProgressHandler->expects($this->once()) + ->method('error'); + + $this->indexer->index($parameter); + } } diff --git a/test/Service/Search/ExternalResourceFactoryTest.php b/test/Service/Search/ExternalResourceFactoryTest.php new file mode 100644 index 0000000..4f37a52 --- /dev/null +++ b/test/Service/Search/ExternalResourceFactoryTest.php @@ -0,0 +1,93 @@ +factory = new ExternalResourceFactory(); + } + + public function testAcceptHttps(): void + { + $document = $this->createDocument('https://www.sitepark.com'); + $this->assertTrue( + $this->factory->accept($document), + 'should be accepted' + ); + } + + public function testAcceptHttp(): void + { + $document = $this->createDocument('http://www.sitepark.com'); + $this->assertTrue( + $this->factory->accept($document), + 'should be accepted' + ); + } + + public function testAcceptWithoutUrl(): void + { + $document = $this->createStub(Document::class); + $this->assertFalse( + $this->factory->accept($document), + 'should be accepted' + ); + } + + public function testCreate(): void + { + $document = $this->createDocument('https://www.sitepark.com'); + $resource = $this->factory->create($document); + + $this->assertEquals( + 'https://www.sitepark.com', + $resource->getLocation(), + 'unexpected location' + ); + } + + public function testCreateWithName(): void + { + $document = $this->createDocument('https://www.sitepark.com', 'Test'); + $resource = $this->factory->create($document); + + $this->assertEquals( + 'Test', + $resource->getName(), + 'unexpected name' + ); + } + + public function testCreateWithMissingUrl(): void + { + $document = $this->createStub(Document::class); + + $this->expectException(\LogicException::class); + $this->factory->create($document); + } + + private function createDocument(string $url, string $title = ''): Document + { + $document = $this->createStub(Document::class); + $document + ->method('getFields') + ->willReturn([ + 'url' => $url, + 'title' => $title + ]); + return $document; + } +} diff --git a/test/Service/Search/InternalMediaResourceFactoryTest.php b/test/Service/Search/InternalMediaResourceFactoryTest.php new file mode 100644 index 0000000..12d5f73 --- /dev/null +++ b/test/Service/Search/InternalMediaResourceFactoryTest.php @@ -0,0 +1,99 @@ +resourceLoader = $this->createStub( + ResourceLoader::class + ); + $this->factory = new InternalMediaResourceFactory( + $this->resourceLoader + ); + } + + public function testAccept(): void + { + $document = $this->createDocument('/image.jpg'); + $this->resourceLoader + ->method('exists') + ->willReturn(true); + + $this->assertTrue( + $this->factory->accept($document), + 'should be accepted' + ); + } + + public function testAcceptWithoutUrl(): void + { + $document = $this->createStub(Document::class); + $this->assertFalse( + $this->factory->accept($document), + 'should not be accepted' + ); + } + + public function testAcceptNotExists(): void + { + $document = $this->createDocument('/image.jpg'); + $this->resourceLoader + ->method('exists') + ->willReturn(false); + + $this->assertFalse( + $this->factory->accept($document), + 'should not be accepted' + ); + } + + public function testCreate(): void + { + $document = $this->createDocument('/image.jpg'); + $resource = $this->createStub(Resource::class); + $this->resourceLoader + ->method('load') + ->willReturn($resource); + + $this->assertEquals( + $resource, + $this->factory->create($document), + 'unexpected resource' + ); + } + + public function testCreateWithoutUrl(): void + { + $document = $this->createStub(Document::class); + $this->expectException(LogicException::class); + $this->factory->create($document); + } + + private function createDocument(string $url): Document + { + $document = $this->createStub(Document::class); + $document + ->method('getFields') + ->willReturn([ + 'url' => $url + ]); + return $document; + } +} diff --git a/test/Service/Search/InternalResourceFactoryTest.php b/test/Service/Search/InternalResourceFactoryTest.php new file mode 100644 index 0000000..0d83fc5 --- /dev/null +++ b/test/Service/Search/InternalResourceFactoryTest.php @@ -0,0 +1,91 @@ +resourceLoader = $this->createStub( + ResourceLoader::class + ); + $this->factory = new InternalResourceFactory( + $this->resourceLoader + ); + } + + public function testAccept(): void + { + $document = $this->createDocument('/test.php'); + $this->assertTrue( + $this->factory->accept($document), + 'should be accepted' + ); + } + + public function testAcceptWithoutUrl(): void + { + $document = $this->createStub(Document::class); + $this->assertFalse( + $this->factory->accept($document), + 'should not be accepted' + ); + } + + public function testAcceptWithWrongUrl(): void + { + $document = $this->createDocument('/test.txt'); + $this->assertFalse( + $this->factory->accept($document), + 'should not be accepted' + ); + } + + public function testCreate(): void + { + $document = $this->createDocument('/test.php'); + $resource = $this->createStub(Resource::class); + $this->resourceLoader + ->method('load') + ->willReturn($resource); + + $this->assertEquals( + $resource, + $this->factory->create($document), + 'unexpected resource' + ); + } + + public function testCreateWithoutUrl(): void + { + $document = $this->createStub(Document::class); + $this->expectException(LogicException::class); + $this->factory->create($document); + } + + private function createDocument(string $url): Document + { + $document = $this->createStub(Document::class); + $document + ->method('getFields') + ->willReturn([ + 'url' => $url + ]); + return $document; + } +} diff --git a/test/Service/Search/SiteKit/DefaultBoostModifierTest.php b/test/Service/Search/SiteKit/DefaultBoostModifierTest.php new file mode 100644 index 0000000..0a08579 --- /dev/null +++ b/test/Service/Search/SiteKit/DefaultBoostModifierTest.php @@ -0,0 +1,22 @@ +modify($query); + + $this->assertNotNull($modifiedQuery->getDisMax()->getBoostQueries()); + } +} diff --git a/test/Service/Search/SolrMoreLikeThisTest.php b/test/Service/Search/SolrMoreLikeThisTest.php new file mode 100644 index 0000000..01a27f3 --- /dev/null +++ b/test/Service/Search/SolrMoreLikeThisTest.php @@ -0,0 +1,80 @@ +createStub( + SolrClientFactory::class + ); + $client = $this->createStub(Client::class); + $clientFactory->method('create')->willReturn($client); + + $query = $this->createStub(SolrMoreLikeThisQuery::class); + $filterQuery = new FilterQuery(); + $query->method('createFilterQuery')->willReturn($filterQuery); + + $client->method('createMoreLikeThis')->willReturn($query); + + $result = $this->createStub(SelectResult::class); + $client->method('execute')->willReturn($result); + + $this->resource = $this->createStub(Resource::class); + + $resultToResourceResolver = $this->createStub( + SolrResultToResourceResolver::class + ); + $resultToResourceResolver + ->method('loadResourceList') + ->willReturn([$this->resource]); + + $this->searcher = new SolrMoreLikeThis( + $clientFactory, + $resultToResourceResolver + ); + } + + public function testMoreLikeThis(): void + { + $filter = $this->getMockBuilder(Filter::class) + ->setConstructorArgs(['test', []]) + ->getMock(); + + $query = new MoreLikeThisQuery( + 'myindex', + '/test.php', + [$filter] + ); + + $searchResult = $this->searcher->moreLikeThis($query); + + $this->assertEquals( + [$this->resource], + $searchResult->results, + 'unexpected results' + ); + } +} diff --git a/test/Service/Search/SolrResultToResourceResolverTest.php b/test/Service/Search/SolrResultToResourceResolverTest.php new file mode 100644 index 0000000..efe1a31 --- /dev/null +++ b/test/Service/Search/SolrResultToResourceResolverTest.php @@ -0,0 +1,88 @@ +createStub(Document::class); + $result = $this->createStub(SelectResult::class); + $result->method('getIterator')->willReturn( + new ArrayIterator([$document]) + ); + + $resource = $this->createStub(Resource::class); + + $resourceFactory = $this->createStub(ResourceFactory::class); + $resourceFactory->method('accept')->willReturn(true); + $resourceFactory->method('create')->willReturn($resource); + + $resolver = new SolrResultToResourceResolver([$resourceFactory]); + + $resourceList = $resolver->loadResourceList($result); + + $this->assertEquals( + [$resource], + $resourceList, + 'unexpected resourceList' + ); + } + + public function testLoadResourceListWithoutAcceptedFactory(): void + { + $document = $this->createStub(Document::class); + $document->method('getFields')->willReturn(['url' => 'test']); + $result = $this->createStub(SelectResult::class); + $result->method('getIterator')->willReturn( + new ArrayIterator([$document]) + ); + + $resourceFactory = $this->createStub(ResourceFactory::class); + $resourceFactory->method('accept')->willReturn(false); + + $resolver = new SolrResultToResourceResolver([$resourceFactory]); + + $resourceList = $resolver->loadResourceList($result); + + $this->assertEmpty( + $resourceList, + 'resourceList should be empty' + ); + } + + public function testLoadResourceListWithoutAcceptedFactoryNoUrl(): void + { + $document = $this->createStub(Document::class); + $result = $this->createStub(SelectResult::class); + $result->method('getIterator')->willReturn( + new ArrayIterator([$document]) + ); + + $resourceFactory = $this->createStub(ResourceFactory::class); + $resourceFactory->method('accept')->willReturn(false); + + $resolver = new SolrResultToResourceResolver([$resourceFactory]); + + $resourceList = $resolver->loadResourceList($result); + + $this->assertEmpty( + $resourceList, + 'resourceList should be empty' + ); + } + +} diff --git a/test/Service/Search/SolrSelectTest.php b/test/Service/Search/SolrSelectTest.php new file mode 100644 index 0000000..b8e3e96 --- /dev/null +++ b/test/Service/Search/SolrSelectTest.php @@ -0,0 +1,362 @@ +createStub( + SolrClientFactory::class + ); + $client = $this->createStub(Client::class); + $clientFactory->method('create')->willReturn($client); + + $query = $this->createStub(SolrSelectQuery::class); + + $query->method('createFilterQuery') + ->willReturn(new FilterQuery()); + $query->method('getFacetSet') + ->willReturn(new FacetSet()); + + $client->method('createSelect')->willReturn($query); + + $this->result = $this->createStub(SelectResult::class); + $client->method('execute')->willReturn($this->result); + + $this->resource = $this->createStub(Resource::class); + + $resultToResourceResolver = $this->createStub( + SolrResultToResourceResolver::class + ); + + $solrQueryModifier = $this->createStub(SolrQueryModifier::class); + $solrQueryModifier->method('modify')->willReturn($query); + + $resultToResourceResolver + ->method('loadResourceList') + ->willReturn([$this->resource]); + + $this->searcher = new SolrSelect( + $clientFactory, + [$solrQueryModifier], + $resultToResourceResolver + ); + } + + public function testSelectEmpty(): void + { + $query = new SelectQuery( + 'myindex', + '', + 0, + 1, + [ + ], + [], + [], + QueryDefaultOperator::OR + ); + + $searchResult = $this->searcher->select($query); + + $this->assertEquals( + [$this->resource], + $searchResult->results, + 'unexpected results' + ); + } + + public function testSelectWithText(): void + { + $query = new SelectQuery( + 'myindex', + 'cat dog', + 0, + 10, + [ + ], + [], + [], + QueryDefaultOperator::OR + ); + + $searchResult = $this->searcher->select($query); + + $this->assertEquals( + [$this->resource], + $searchResult->results, + 'unexpected results' + ); + } + + public function testSelectWithSort(): void + { + $query = new SelectQuery( + 'myindex', + '', + 0, + 10, + [ + new Name(), + new Headline(), + new Date(), + new Natural(), + new Score(), + ], + [], + [], + QueryDefaultOperator::OR + ); + + $searchResult = $this->searcher->select($query); + + $this->assertEquals( + [$this->resource], + $searchResult->results, + 'unexpected results' + ); + } + + public function testSelectWithInvalidSort(): void + { + $sort = $this->createStub(Criteria::class); + + $query = new SelectQuery( + 'myindex', + '', + 0, + 10, + [$sort], + [], + [], + QueryDefaultOperator::OR + ); + + $this->expectException(InvalidArgumentException::class); + $this->searcher->select($query); + } + + public function testSelectWithAndDefaultOperator(): void + { + $query = new SelectQuery( + 'myindex', + '', + 0, + 10, + [], + [], + [], + QueryDefaultOperator::AND + ); + + $searchResult = $this->searcher->select($query); + + $this->assertEquals( + [$this->resource], + $searchResult->results, + 'unexpected results' + ); + } + + public function testSelectWithFilter(): void + { + $filter = $this->getMockBuilder(Filter::class) + ->setConstructorArgs(['test', []]) + ->getMock(); + + $query = new SelectQuery( + 'myindex', + '', + 0, + 10, + [], + [$filter], + [], + QueryDefaultOperator::OR + ); + + $searchResult = $this->searcher->select($query); + + $this->assertEquals( + [$this->resource], + $searchResult->results, + 'unexpected results' + ); + } + + public function testSelectWithFacets(): void + { + + $facets = [ + new ObjectTypeFacet('objectType', ['content'], 'ob'), + new FacetQuery('query', 'sp_id:123', 'ob'), + new FacetMultiQuery( + 'multiquery', + [new FacetQuery('query', 'sp_id:123', null)], + 'ob' + ) + ]; + + $query = new SelectQuery( + 'myindex', + '', + 0, + 10, + [], + [], + $facets, + QueryDefaultOperator::OR + ); + + $searchResult = $this->searcher->select($query); + + $this->assertEquals( + [$this->resource], + $searchResult->results, + 'unexpected results' + ); + } + + public function testSelectWithInvalidFacets(): void + { + + $facets = [ + $this->createStub(Facet::class) + ]; + + $query = new SelectQuery( + 'myindex', + '', + 0, + 10, + [], + [], + $facets, + QueryDefaultOperator::OR + ); + + $this->expectException(InvalidArgumentException::class); + $this->searcher->select($query); + } + + public function testResultFacets(): void + { + + $facet = new Field([ + 'content' => 10, + 'media' => 5 + ]); + $facetSet = new \Solarium\Component\Result\FacetSet([ + 'objectType' => $facet + ]); + + $this->result->method('getFacetSet') + ->willReturn($facetSet); + + $facets = [ + new ObjectTypeFacet('objectType', ['content'], 'ob'), + new FacetQuery('query', 'sp_id:123', 'ob'), + new FacetMultiQuery( + 'multiquery', + [new FacetQuery('query', 'sp_id:123', null)], + 'ob' + ) + ]; + + $query = new SelectQuery( + 'myindex', + '', + 0, + 10, + [], + [], + $facets, + QueryDefaultOperator::OR + ); + + $searchResult = $this->searcher->select($query); + + $this->assertEquals( + 'objectType', + $searchResult->facetGroups[0]->key, + 'unexpected results' + ); + } + + public function testInvalidResultFacets(): void + { + + $facet = new Field([ + 'content' => 'nonint', + ]); + $facetSet = new \Solarium\Component\Result\FacetSet([ + 'objectType' => $facet + ]); + + $this->result->method('getFacetSet') + ->willReturn($facetSet); + + $facets = [ + new ObjectTypeFacet('objectType', ['content'], 'ob'), + new FacetQuery('query', 'sp_id:123', 'ob'), + new FacetMultiQuery( + 'multiquery', + [new FacetQuery('query', 'sp_id:123', null)], + 'ob' + ) + ]; + + $query = new SelectQuery( + 'myindex', + '', + 0, + 10, + [], + [], + $facets, + QueryDefaultOperator::OR + ); + + $this->expectException(InvalidArgumentException::class); + $this->searcher->select($query); + } +} diff --git a/test/Service/Search/SolrSuggestTest.php b/test/Service/Search/SolrSuggestTest.php new file mode 100644 index 0000000..3eef755 --- /dev/null +++ b/test/Service/Search/SolrSuggestTest.php @@ -0,0 +1,131 @@ +createStub( + SolrClientFactory::class + ); + $client = $this->createStub(Client::class); + $clientFactory->method('create')->willReturn($client); + + $query = $this->createStub(SolrSelectQuery::class); + + $query->method('createFilterQuery') + ->willReturn(new FilterQuery()); + + $client->method('createSelect')->willReturn($query); + + $this->result = $this->createStub(SelectResult::class); + $client->method('select')->willReturn($this->result); + + $this->searcher = new SolrSuggest($clientFactory); + } + + public function testSuggest(): void + { + $filter = $this->getMockBuilder(Filter::class) + ->setConstructorArgs(['test', []]) + ->getMock(); + + $query = new SuggestQuery( + 'myindex', + 'cat', + [$filter] + ); + + $response = new Response(<<result->method('getResponse')->willReturn($response); + + $suggestResult = $this->searcher->suggest($query); + + $expected = [ + new Suggestion('category', 10), + new Suggestion('catalog', 5), + ]; + + $this->assertEquals( + $expected, + $suggestResult->suggestions, + 'unexpected suggestion' + ); + } + + public function testEmptySuggest(): void + { + $query = new SuggestQuery( + 'myindex', + 'cat', + ); + + $response = new Response(<<result->method('getResponse')->willReturn($response); + + $suggestResult = $this->searcher->suggest($query); + + $this->assertEmpty( + $suggestResult->suggestions, + 'suggestion should be empty' + ); + } + + public function testInvalidSuggestResponse(): void + { + $query = new SuggestQuery( + 'myindex', + 'cat', + ); + + $response = new Response("none json"); + + $this->result->method('getResponse')->willReturn($response); + + $this->expectException(UnexpectedResultException::class); + $this->searcher->suggest($query); + } +} diff --git a/test/Service/SolrParameterClientFactoryTest.php b/test/Service/SolrParameterClientFactoryTest.php new file mode 100644 index 0000000..49cd0af --- /dev/null +++ b/test/Service/SolrParameterClientFactoryTest.php @@ -0,0 +1,24 @@ +create('myindex'); + $this->assertNotNull($client, 'client instance expected'); + } +}