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 Apr 2, 2024
1 parent c3fff86 commit 9653151
Show file tree
Hide file tree
Showing 26 changed files with 952 additions and 152 deletions.
3 changes: 3 additions & 0 deletions src/Dto/Indexer/IndexerConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Atoolo\Resource\DataBag;

/**
* @codeCoverageIgnore
*/
class IndexerConfiguration
{
public function __construct(
Expand Down
6 changes: 1 addition & 5 deletions src/Dto/Indexer/IndexerParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@

class IndexerParameter
{
/**
* @param string[] $paths
*/
public function __construct(
public readonly string $name,
public readonly int $cleanupThreshold = 0,
public readonly int $chunkSize = 500,
public readonly array $paths = []
public readonly int $chunkSize = 500
) {
if ($this->chunkSize < 10) {
throw new \InvalidArgumentException(
Expand Down
1 change: 1 addition & 0 deletions src/Service/Indexer/BackgroundIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function getProgressHandler(): IndexerProgressHandler
public function setProgressHandler(
IndexerProgressHandler $progressHandler
): void {
$this->indexer->setProgressHandler($progressHandler);
}

public function getName(): string
Expand Down
25 changes: 5 additions & 20 deletions src/Service/Indexer/IndexerConfigurationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ public function loadAll(): array
return [];
}

$files = glob($dir . '/*.php');
if ($files === false) {
return [];
}
$files = glob($dir . '/*.php') ?: [];

$configurations = [];
foreach ($files as $file) {
$configurations[] = $this->load($file);
$source = pathinfo($file, PATHINFO_FILENAME);
$configurations[] = $this->load($source);
}
return $configurations;
}
Expand Down Expand Up @@ -75,22 +73,9 @@ public function load(string $source): IndexerConfiguration
);
}

if (isset($data['source']) === false) {
throw new RuntimeException(
'The indexer configuration ' .
$file . ' should have a source key'
);
}

if ($data['source'] !== $source) {
throw new RuntimeException(
'source key in ' . $file . ' should match the filename'
);
}

return new IndexerConfiguration(
$data['source'],
$data['name'] ?? $data['source'],
$source,
$data['name'] ?? $source,
new DataBag($data['data'] ?? []),
);
} finally {
Expand Down
48 changes: 27 additions & 21 deletions src/Service/Indexer/InternalResourceIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,27 +118,46 @@ public function index(): IndexerStatus
{
$lock = $this->lockFactory->createLock('indexer.' . $this->source);
if (!$lock->acquire()) {
$this->logger->notice(
'Indexer with source "' . $this->source . '" is already running'
);
return $this->progressHandler->getStatus();
}
try {
$paths = $this->finder->findAll();
$this->deleteErrorProtocol($this->getBaseIndex());
$total = count($paths);
$this->progressHandler->start($total);

$param = $this->getIndexerParameter();
return $this->indexResources($param, $this->finder->findAll());
$this->indexResources($param, $this->finder->findAll());
} finally {
$lock->release();
$this->progressHandler->finish();
gc_collect_cycles();
}

return $this->progressHandler->getStatus();
}

/**
* @param string[] $paths
*/
public function update(array $paths): IndexerStatus
{
$param = $this->loadIndexerParameter();
return $this->indexResources(
$param,
$this->finder->findPaths($paths)
);
$collectedPaths = $this->finder->findPaths($paths);
$total = count($collectedPaths);
$this->progressHandler->startUpdate($total);

try {
$param = $this->loadIndexerParameter();
$this->indexResources($param, $collectedPaths);
} finally {
$this->progressHandler->finish();
gc_collect_cycles();
}

return $this->progressHandler->getStatus();
}

private function getIndexerParameter(): IndexerParameter
Expand Down Expand Up @@ -174,29 +193,16 @@ private function getBaseIndex(): string
private function indexResources(
IndexerParameter $parameter,
array $pathList
): IndexerStatus {
): void {
if (count($pathList) === 0) {
return IndexerStatus::empty();
}

$total = count($pathList);
if (empty($parameter->paths)) {
$this->deleteErrorProtocol($this->getBaseIndex());
$this->progressHandler->start($total);
} else {
$this->progressHandler->startUpdate($total);
return;
}

$splitterResult = $this->translationSplitter->split($pathList);

$this->indexTranslationSplittedResources(
$parameter,
$splitterResult
);

$this->progressHandler->finish();

return $this->progressHandler->getStatus();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,10 @@ private function contactPointToContent(array $contactPoint): string
}

if (isset($contactPoint['addressData'])) {
$addressData = $contactPoint['addressData'];
$content[] = $addressData['street'] ?? '';
$content[] = $addressData['buildingName'] ?? '';
$content[] = $addressData['postOfficeBoxData']['buildingName']
?? '';
$data = $contactPoint['addressData'];
$content[] = $data['street'] ?? '';
$content[] = $data['buildingName'] ?? '';
$content[] = $data['postOfficeBoxData']['buildingName'] ?? '';
}

return implode(' ', $content);
Expand Down
17 changes: 10 additions & 7 deletions src/Service/Indexer/SiteKit/SubDirTranslationSplitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,28 @@ public function split(array $pathList): TranslationSplitterResult
$bases = [];
$translations = [];
foreach ($pathList as $path) {
$locale = $this->extractLocaleFromPath($path);
$normalizedPath = $this->normalizePath($path);
if (empty($normalizedPath)) {
continue;
}
$locale = $this->extractLocaleFromPath($normalizedPath);
if ($locale === 'default') {
$bases[] = $path;
$bases[] = $normalizedPath;
continue;
}
if (!isset($translations[$locale])) {
$translations[$locale] = [];
}
$translations[$locale][] = $path;
$translations[$locale][] = $normalizedPath;
}

return new TranslationSplitterResult($bases, $translations);
}

private function extractLocaleFromPath(string $path): string
{
$normalizedPath = $this->normalizePath($path);
$filename = basename($normalizedPath);
$parentDirName = basename(dirname($normalizedPath));
$filename = basename($path);
$parentDirName = basename(dirname($path));

if (!str_ends_with($parentDirName, '.php.translations')) {
return 'default';
Expand All @@ -64,7 +67,7 @@ private function normalizePath(string $path): string
}
$urlPath = parse_url($path, PHP_URL_PATH);
if (!is_string($urlPath)) {
return $path;
return '';
}
parse_str($queryString, $params);
if (!isset($params['loc']) || !is_string($params['loc'])) {
Expand Down
3 changes: 1 addition & 2 deletions test/Console/Command/IndexerInternalResourceUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Atoolo\Resource\ResourceChannel;
use Atoolo\Resource\ResourceChannelFactory;
use Atoolo\Search\Console\Application;
use Atoolo\Search\Console\Command\Indexer;
use Atoolo\Search\Console\Command\IndexerInternalResourceUpdate;
use Atoolo\Search\Console\Command\Io\IndexerProgressBar;
use Atoolo\Search\Service\Indexer\InternalResourceIndexer;
Expand All @@ -17,7 +16,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;

#[CoversClass(Indexer::class)]
#[CoversClass(IndexerInternalResourceUpdate::class)]
class IndexerInternalResourceUpdateTest extends TestCase
{
private ResourceChannelFactory $resourceChannelFactory;
Expand Down
62 changes: 56 additions & 6 deletions test/Console/Command/IndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,28 @@ public function setUp(): void
);
$this->resourceChannelFactory->method('create')
->willReturn($resourceChannel);
$indexer = $this->createStub(
$indexerA = $this->createStub(
\Atoolo\Search\Indexer::class
);
$indexer->method('enabled')
$indexerA->method('enabled')
->willReturn(true);
$indexers = new IndexerCollection([$indexer]);
$indexerA->method('getSource')
->willReturn('indexer_a');
$indexerA->method('getName')
->willReturn('Indexer A');
$indexerB = $this->createStub(
\Atoolo\Search\Indexer::class
);
$indexerB->method('enabled')
->willReturn(false);
$indexerB->method('getSource')
->willReturn('indexer_b');
$indexerB->method('getName')
->willReturn('Indexer B');
$indexers = new IndexerCollection([
$indexerA,
$indexerB
]);
$progressBar = $this->createStub(IndexerProgressBar::class);

$command = new Indexer(
Expand All @@ -65,7 +81,7 @@ public function setUp(): void
$this->commandTester = new CommandTester($command);
}

public function testExecuteIndexAll(): void
public function testExecuteAllEnabledIndexer(): void
{
$this->commandTester->execute([]);

Expand All @@ -80,8 +96,8 @@ public function testExecuteIndexAll(): void
============
Index with Indexer ""
---------------------
Index with Indexer "Indexer A"
------------------------------
Expand All @@ -96,6 +112,40 @@ public function testExecuteIndexAll(): void
);
}

public function testExecuteIndexerA(): void
{
$this->commandTester->execute(
[
'--source' => 'indexer_a'
]
);

$this->commandTester->assertCommandIsSuccessful();

// the output of the command in the console
$output = $this->commandTester->getDisplay();
$this->assertEquals(
<<<EOF
Channel: WWW
============
Index with Indexer "Indexer A"
------------------------------
Status
------
EOF,
$output
);
}
/**
* @throws Exception
*/
Expand Down
Loading

0 comments on commit 9653151

Please sign in to comment.