Skip to content

Commit

Permalink
Merge pull request #56 from shopwareLabs/clear-and-destroy
Browse files Browse the repository at this point in the history
feat: Added clear and destroy function for all kind of storages
  • Loading branch information
OliverSkroblin authored Mar 5, 2024
2 parents fb06339 + d6202f7 commit eb4e061
Show file tree
Hide file tree
Showing 22 changed files with 193 additions and 115 deletions.
10 changes: 10 additions & 0 deletions src/Array/ArrayKeyStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ class ArrayKeyStorage implements Storage
*/
private array $storage = [];

public function destroy(): void
{
$this->storage = [];
}

public function clear(): void
{
$this->storage = [];
}

public function store(Documents $documents): void
{
foreach ($documents as $document) {
Expand Down
13 changes: 12 additions & 1 deletion src/Array/ArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@
use Shopware\Storage\Common\Schema\Collection;
use Shopware\Storage\Common\Schema\FieldType;
use Shopware\Storage\Common\Schema\SchemaUtil;
use Shopware\Storage\Common\Storage;
use Shopware\Storage\Common\StorageContext;
use Shopware\Storage\Common\Total;

class ArrayStorage extends ArrayKeyStorage implements FilterAware, AggregationAware
class ArrayStorage implements Storage, FilterAware, AggregationAware
{
/**
* @var array<string, Document>
Expand All @@ -54,6 +55,16 @@ public function __construct(
private readonly Collection $collection
) {}

public function clear(): void
{
$this->storage = [];
}

public function destroy(): void
{
$this->storage = [];
}

public function setup(): void
{
$this->storage = [];
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function store(Documents $documents): void;
*/
public function setup(): void;

// public function clear(): void;
public function clear(): void;

// public function destroy();
public function destroy(): void;
}
11 changes: 11 additions & 0 deletions src/DynamoDB/DynamoDBKeyStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public function __construct(
private readonly Collection $collection
) {}

public function destroy(): void
{
$this->client->deleteTable(['TableName' => $this->collection->name]);
}

public function setup(): void
{
$table = new CreateTableInput([
Expand All @@ -49,6 +54,12 @@ public function setup(): void
}
}

public function clear(): void
{
$this->destroy();
$this->setup();
}

public function remove(array $keys): void
{
$mapped = [];
Expand Down
10 changes: 10 additions & 0 deletions src/Meilisearch/MeilisearchStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public function __construct(
private readonly Collection $collection
) {}

public function destroy(): void
{
$this->client->deleteIndex($this->collection->name);
}

public function clear(): void
{
$this->index()->deleteAllDocuments();
}

public function get(string $key, StorageContext $context): ?Document
{
try {
Expand Down
10 changes: 10 additions & 0 deletions src/MongoDB/MongoDBKeyStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ public function __construct(
private readonly Client $client
) {}

public function clear(): void
{
$this->collection()->deleteMany([]);
}

public function destroy(): void
{
$this->collection()->drop();
}

public function mget(array $keys, StorageContext $context): Documents
{
$query['key'] = ['$in' => $keys];
Expand Down
10 changes: 10 additions & 0 deletions src/MongoDB/MongoDBStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public function __construct(
private readonly Client $client
) {}

public function destroy(): void
{
$this->collection()->drop();
}

public function clear(): void
{
$this->collection()->deleteMany([]);
}

public function setup(): void {}

public function mget(array $keys, StorageContext $context): Documents
Expand Down
14 changes: 14 additions & 0 deletions src/MySQL/MySQLKeyStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ private function table(): string
return '`' . $this->collection->name . '`';
}

public function destroy(): void
{
$this->connection->executeStatement(
sql: 'DROP TABLE IF EXISTS ' . $this->table()
);
}

public function clear(): void
{
$this->connection->executeStatement(
sql: 'DELETE FROM ' . $this->table()
);
}

public function setup(): void
{
$table = new Table(name: $this->collection->name);
Expand Down
14 changes: 14 additions & 0 deletions src/MySQL/MySQLStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ private static function escape(string $source): string
return '`' . $source . '`';
}

public function destroy(): void
{
$this->connection->executeStatement(
sql: 'DROP TABLE IF EXISTS ' . self::escape($this->collection->name)
);
}

public function clear(): void
{
$this->connection->executeStatement(
sql: 'DELETE FROM ' . self::escape($this->collection->name)
);
}

public function setup(): void
{
$table = new Table(name: $this->collection->name);
Expand Down
21 changes: 21 additions & 0 deletions src/Opensearch/OpensearchStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ public function __construct(
private readonly Collection $collection
) {}

public function destroy(): void
{
if (!$this->exists()) {
return;
}

$this->client->indices()->delete(['index' => $this->collection->name]);
}

public function clear(): void
{
if (!$this->exists()) {
return;
}

$this->client->deleteByQuery([
'index' => $this->collection->name,
'body' => ['query' => ['match_all' => new \stdClass()]],
]);
}

public function setup(): void
{
$properties = $this->setupProperties($this->collection);
Expand Down
10 changes: 10 additions & 0 deletions src/Redis/RedisKeyStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public function __construct(
private readonly Hydrator $hydrator
) {}

public function clear(): void
{
$this->client->flushDB();
}

public function destroy(): void
{
$this->client->flushDB();
}

public function setup(): void {}

public function remove(array $keys): void
Expand Down
12 changes: 12 additions & 0 deletions tests/Meilisearch/MeilisearchLiveStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ public function __construct(
private readonly Client $client
) {}

public function destroy(): void
{
$this->storage->destroy();
$this->wait();
}

public function clear(): void
{
$this->storage->clear();
$this->wait();
}

public function mget(array $keys, StorageContext $context): Documents
{
return $this->storage->mget($keys, $context);
Expand Down
56 changes: 13 additions & 43 deletions tests/Meilisearch/MeilisearchTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace Shopware\StorageTests\Meilisearch;

use Meilisearch\Client;
use Meilisearch\Contracts\TasksQuery;
use Meilisearch\Endpoints\Indexes;
use Meilisearch\Exceptions\ApiException;
use Shopware\Storage\Common\Aggregation\AggregationCaster;
use Shopware\Storage\Common\Document\Hydrator;
use Shopware\Storage\Meilisearch\MeilisearchStorage;
Expand All @@ -15,31 +12,25 @@ trait MeilisearchTestTrait
{
private static ?Client $client = null;

private function exists(): bool
public static function setUpBeforeClass(): void
{
try {
$this->getClient()->getIndex(TestSchema::getCollection()->name);
} catch (ApiException) {
return false;
}

return true;
parent::setUpBeforeClass();
self::createStorage()->setup();
}

protected function setUp(): void
{
parent::setUp();
$this->getStorage()->clear();
}

if ($this->exists()) {
$this->index()->deleteAllDocuments();
$this->wait();
return;
}

$this->getStorage()->setup();
public static function tearDownAfterClass(): void
{
self::createStorage()->destroy();
parent::tearDownAfterClass();
}

private function getClient(): Client
private static function getClient(): Client
{
if (self::$client === null) {
self::$client = new Client(
Expand All @@ -51,37 +42,16 @@ private function getClient(): Client
return self::$client;
}

public function createStorage(): MeilisearchLiveStorage
private static function createStorage(): MeilisearchLiveStorage
{
return new MeilisearchLiveStorage(
storage: new MeilisearchStorage(
caster: new AggregationCaster(),
hydrator: new Hydrator(),
client: $this->getClient(),
client: self::getClient(),
collection: TestSchema::getCollection()
),
client: $this->getClient(),
client: self::getClient(),
);
}

private function index(): Indexes
{
return $this->getClient()->index(TestSchema::getCollection()->name);
}

private function wait(): void
{
$tasks = new TasksQuery();
$tasks->setStatuses(['enqueued', 'processing']);

$tasks = $this->getClient()->getTasks($tasks);

$ids = array_map(fn($task) => $task['uid'], $tasks->getResults());

if (count($ids) === 0) {
return;
}

$this->getClient()->waitForTasks($ids);
}
}
5 changes: 3 additions & 2 deletions tests/MongoDB/MongoDBAggregateStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ private function getClient(): Client
protected function setUp(): void
{
parent::setUp();
$this->getClient()->dropDatabase('test');
$this->getStorage()->destroy();
$this->getStorage()->setup();
}

protected function tearDown(): void
{
parent::tearDown();
$this->getClient()->dropDatabase('test');
$this->getStorage()->destroy();
}

public function getStorage(): AggregationAware&Storage
Expand Down
5 changes: 3 additions & 2 deletions tests/MongoDB/MongoDBFilterStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ private function getClient(): Client
protected function setUp(): void
{
parent::setUp();
$this->getClient()->dropDatabase('test');
$this->getStorage()->destroy();
$this->getStorage()->setup();
}

protected function tearDown(): void
{
parent::tearDown();
$this->getClient()->dropDatabase('test');
$this->getStorage()->destroy();
}

public function getStorage(): FilterAware&Storage
Expand Down
6 changes: 2 additions & 4 deletions tests/MySQL/MySQLKeyValueStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ protected function setUp(): void
{
parent::setUp();

$this->getConnection()
->executeStatement('DROP TABLE IF EXISTS `' . TestSchema::getCollection()->name . '`');
$this->getStorage()->destroy();

$this->getStorage()->setup();
}
Expand All @@ -31,8 +30,7 @@ protected function tearDown(): void
{
parent::tearDown();

$this->getConnection()
->executeStatement('DROP TABLE IF EXISTS `' . TestSchema::getCollection()->name . '`');
$this->getStorage()->destroy();
}

public function getStorage(): Storage
Expand Down
2 changes: 1 addition & 1 deletion tests/MySQL/MySQLTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function setUp(): void
{
parent::setUp();

self::getConnection()->executeStatement('DROP TABLE IF EXISTS ' . TestSchema::getCollection()->name);
$this->getStorage()->destroy();

$this->getStorage()->setup();
}
Expand Down
Loading

0 comments on commit eb4e061

Please sign in to comment.