Skip to content

Commit

Permalink
test: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Feb 23, 2024
1 parent e05ba48 commit 0b441b5
Show file tree
Hide file tree
Showing 17 changed files with 1,083 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/Dto/Search/Query/Sort/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
abstract class Criteria
{
public function __construct(
public readonly Direction $direction
public readonly Direction $direction = Direction::ASC
) {
}
}
14 changes: 12 additions & 2 deletions src/Service/Search/InternalMediaResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Atoolo\Resource\Resource;
use Atoolo\Resource\ResourceLoader;
use LogicException;
use Solarium\QueryType\Select\Result\Document;

/**
Expand All @@ -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';
}

Expand Down
11 changes: 9 additions & 2 deletions src/Service/Search/InternalResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Atoolo\Resource\Resource;
use Atoolo\Resource\ResourceLoader;
use LogicException;
use Solarium\QueryType\Select\Result\Document;

/**
Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Service/Search/SolrMoreLikeThis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Service/Search/SolrSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/Service/Search/SolrSuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
) {
}

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions test/Dto/Search/Query/SelectQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
72 changes: 62 additions & 10 deletions test/Service/Indexer/SolrIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -44,7 +47,7 @@ class SolrIndexerTest extends TestCase

private IndexingAborter&Stub $aborter;

private DocumentEnricher $documentEnricher;
private DocumentEnricher&MockObject $documentEnricher;

private TranslationSplitter $translationSplitter;

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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',
Expand All @@ -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')
Expand All @@ -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) {
Expand Down Expand Up @@ -181,6 +199,9 @@ function ($documents) use (&$addDocumentsCalls) {
10
);

$this->indexerProgressHandler->expects($this->exactly(2))
->method('error');

$this->indexer->index($parameter);
}

Expand Down Expand Up @@ -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);
}
}
93 changes: 93 additions & 0 deletions test/Service/Search/ExternalResourceFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Test\Service\Search;

use Atoolo\Search\Service\Search\ExternalResourceFactory;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Solarium\QueryType\Select\Result\Document;

#[CoversClass(ExternalResourceFactory::class)]
class ExternalResourceFactoryTest extends TestCase
{

private ExternalResourceFactory $factory;

protected function setUp(): void
{
$this->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;
}
}
Loading

0 comments on commit 0b441b5

Please sign in to comment.