Skip to content

Commit

Permalink
Introduce IndexNotFoundException
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Dec 27, 2024
1 parent a3101ae commit e7bfa08
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/seal-memory-adapter/src/MemoryStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CmsIg\Seal\Adapter\Memory;

use CmsIg\Seal\Schema\Exception\IndexNotFoundException;
use CmsIg\Seal\Schema\Index;

/**
Expand All @@ -36,7 +37,7 @@ final class MemoryStorage
public static function getDocuments(Index $index): array
{
if (!\array_key_exists($index->name, self::$indexes)) {
throw new \RuntimeException('Index "' . $index->name . '" does not exist.');
throw new IndexNotFoundException($index->name);
}

return self::$documents[$index->name];
Expand All @@ -50,7 +51,7 @@ public static function getDocuments(Index $index): array
public static function save(Index $index, array $document): array
{
if (!\array_key_exists($index->name, self::$indexes)) {
throw new \RuntimeException('Index "' . $index->name . '" does not exist.');
throw new IndexNotFoundException($index->name);
}

$identifierField = $index->getIdentifierField();
Expand All @@ -66,7 +67,7 @@ public static function save(Index $index, array $document): array
public static function delete(Index $index, string $identifier): void
{
if (!\array_key_exists($index->name, self::$indexes)) {
throw new \RuntimeException('Index "' . $index->name . '" does not exist.');
throw new IndexNotFoundException($index->name);
}

unset(self::$documents[$index->name][$identifier]);
Expand Down
26 changes: 26 additions & 0 deletions packages/seal/src/Schema/Exception/IndexNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CMS-IG SEAL project.
*
* (c) Alexander Schranz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CmsIg\Seal\Schema\Exception;

final class IndexNotFoundException extends \RuntimeException
{
public function __construct(string $indexName, \Throwable|null $previous = null)
{
parent::__construct(
'Index "' . $indexName . '" does not exist.',
0,
$previous,
);
}
}
40 changes: 40 additions & 0 deletions packages/seal/src/Testing/AbstractAdapterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CmsIg\Seal\Engine;
use CmsIg\Seal\EngineInterface;
use CmsIg\Seal\Exception\DocumentNotFoundException;
use CmsIg\Seal\Schema\Exception\IndexNotFoundException;
use CmsIg\Seal\Schema\Schema;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -67,6 +68,45 @@ public function testIndex(): void
$this->assertFalse($engine->existIndex($indexName));
}

public function testIndexNotFoundSave(): void
{
$this->expectException(IndexNotFoundException::class);

$engine = self::getEngine();
$indexName = TestingHelper::INDEX_SIMPLE;

$documents = TestingHelper::createSimpleFixtures();
$document = $documents[0];

$task = $engine->saveDocument($indexName, $document, ['return_slow_promise_result' => true]);
$task->wait();
}

public function testIndexNotFoundDelete(): void
{
$this->expectException(IndexNotFoundException::class);

$engine = self::getEngine();
$indexName = TestingHelper::INDEX_SIMPLE;

$documents = TestingHelper::createSimpleFixtures();
$document = $documents[0];

$task = $engine->deleteDocument($indexName, $document['id'], ['return_slow_promise_result' => true]);
$task->wait();
}

public function testIndexNotFoundSearch(): void
{
$this->expectException(IndexNotFoundException::class);

$engine = self::getEngine();
$indexName = TestingHelper::INDEX_SIMPLE;

$engine->createSearchBuilder($indexName)
->getResult();
}

public function testSchema(): void
{
$engine = self::getEngine();
Expand Down

0 comments on commit e7bfa08

Please sign in to comment.