diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index a65e53f..b9a31fc 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -49,12 +49,16 @@ jobs: restore-keys: ${{ runner.os }}-composer- - name: Install dependencies run: composer install --prefer-dist + - name: Install Predis optional package + run: composer require predis/predis - name: Test cs-fixer run: vendor/bin/php-cs-fixer fix src --dry-run --diff --no-ansi - name: Run phpstan run: vendor/bin/phpstan analyse src --level=5 - - name: Run tests - run: XDEBUG_MODE=coverage vendor/bin/phpunit + - name: Run tests with redis extension + run: XDEBUG_MODE=coverage REDIS_CLIENT=redis vendor/bin/phpunit + - name: Run tests with predis dependency + run: XDEBUG_MODE=coverage REDIS_CLIENT=predis vendor/bin/phpunit - name: Upload coverage to Codecov uses: codecov/codecov-action@v4 with: diff --git a/composer.json b/composer.json index 609c256..a3f9d14 100644 --- a/composer.json +++ b/composer.json @@ -14,9 +14,12 @@ }, "require": { "php": ">=8.2", - "ext-redis": "*", "ext-json": "*" }, + "suggest": { + "ext-redis": "To use the php extension for Redis (phpredis)", + "predis/predis": "To use Predis as a Redis PHP client" + }, "require-dev": { "phpunit/phpunit": "^11.0", "friendsofphp/php-cs-fixer": "^3.57", diff --git a/src/Client/Helper/Converter.php b/src/Client/Helper/Converter.php new file mode 100644 index 0000000..e694718 --- /dev/null +++ b/src/Client/Helper/Converter.php @@ -0,0 +1,13 @@ +redis = $redis ?? new Predis( + array_key_exists('REDIS_HOST', $_SERVER) ? ['host' => $_SERVER['REDIS_HOST']] : null, + ); + } + + public function createPersistentConnection(?string $host = null, ?int $port = null, ?int $timeout = 0): void + { + /** @var StreamConnection $connection */ + $connection = $this->redis->getConnection(); + $parameters = $connection->getParameters()->toArray(); + + $this->redis = new Predis([ + 'scheme' => 'tcp', + 'host' => $host ?? $parameters['host'], + 'port' => $port ?? $parameters['port'], + 'persistent' => true, + 'timeout' => $timeout, + ]); + ; + } + + /** + * @inheritDoc + */ + public function hMSet(string $key, array $data): void + { + if (!$this->redis->hmset(Converter::prefix($key), $data)) { + $this->handleError(__METHOD__, $this->getLastError()); + } + } + + private function getLastError() + { + try { + $this->redis->executeRaw(['INVALID_COMMAND']); + } catch (\Exception $exception) { + return $exception->getMessage(); + } + } + + + /** + * @inheritdoc + */ + public function hGetAll(string $key): array + { + return $this->redis->hgetall(Converter::prefix($key)); + } + + /** + * @inheritdoc + */ + public function hget(string $key, string $property): string + { + $result = $this->redis->hget(Converter::prefix($key), $property); + + if (!$result) { + $this->handleError(__METHOD__, $this->getLastError()); + } + + return $result; + } + + /** + * @inheritdoc + */ + public function del(string $key): void + { + if (!$this->redis->del(Converter::prefix($key))) { + $this->handleError(__METHOD__, $this->getLastError()); + } + } + + /** + * @inheritdoc + */ + public function jsonGet(string $key): ?string + { + $result = $this->redis->executeRaw([RedisCommands::JSON_GET->value, Converter::prefix($key)]); + + if ($result === false) { + return null; + } + + return $result; + } + + /** + * @inheritdoc + */ + public function jsonGetProperty(string $key, string $property): ?string + { + $result = $this->redis->executeRaw([RedisCommands::JSON_GET->value, Converter::prefix($key), "$.$property"]); + + if ($result === false) { + return null; + } + + return $result; + } + + /** + * @inheritdoc + */ + public function jsonSet(string $key, ?string $path = '$', ?string $value = '{}'): void + { + if (!$this->redis->executeRaw([RedisCommands::JSON_SET->value, Converter::prefix($key), $path, $value])) { + $this->handleError(__METHOD__, $this->getLastError()); + } + } + + /** + * @inheritdoc + */ + public function jsonMSet(...$params): void + { + $arguments = [RedisCommands::JSON_MSET->value]; + foreach ($params as $param) { + if (count($param) % 3 !== 0) { + throw new \InvalidArgumentException("Should provide 3 parameters for each key, path and value"); + } + + for ($i = 0; $i < count($param); $i += 3) { + $arguments[] = Converter::prefix($param[$i]); + $arguments[] = $param[$i + 1] ?? '$'; + $arguments[] = $param[$i + 2] ?? '{}'; + } + } + + if (!call_user_func_array([$this->redis, 'executeRaw'], [$arguments])) { + $this->handleError(__METHOD__, $this->getLastError()); + } + } + + /** + * @inheritdoc + */ + public function jsonDel(string $key, ?string $path = '$'): void + { + if (!$this->redis->executeRaw([RedisCommands::JSON_DELETE->value, Converter::prefix($key), $path])) { + $this->handleError(__METHOD__, $this->getLastError()); + } + } + + /** + * @inheritdoc + */ + public function createIndex(string $prefixKey, ?string $format = RedisFormat::HASH->value, ?array $properties = []): void + { + if ($properties === []) { + return; + } + + $prefixKey = self::convertPrefix($prefixKey); + + $arguments = [ + RedisCommands::CREATE_INDEX->value, + $prefixKey, + 'ON', + $format, + ]; + + if ($format === RedisFormat::HASH->value) { + $arguments[] = 'PREFIX'; + $arguments[] = '1'; + $arguments[] = "$prefixKey:"; + } + + $arguments[] = 'SCHEMA'; + + /** @var PropertyToIndex $propertyToIndex */ + foreach ($properties as $propertyToIndex) { + $arguments[] = $propertyToIndex->name; + $arguments[] = 'AS'; + $arguments[] = $propertyToIndex->indexName; + $arguments[] = $propertyToIndex->indexType; + $arguments[] = 'SORTABLE'; + } + + if (end($arguments) === 'SCHEMA') { + throw new BadPropertyConfigurationException(sprintf('Your class %s does not have any typed property', $prefixKey)); + } + + if (!call_user_func_array([$this->redis, 'executeRaw'], [$arguments])) { + $this->handleError(__METHOD__, $this->getLastError()); + } + } + + /** + * @inheritdoc + */ + public function dropIndex(string $prefixKey): bool + { + try { + $key = self::convertPrefix($prefixKey); + $this->redis->executeRaw([RedisCommands::DROP_INDEX->value, $key]); + } catch (\RedisException) { + return false; + } + + return true; + } + + /** + * @inheritdoc + */ + public function count(string $prefixKey, array $criterias = []): int + { + $arguments = [RedisCommands::SEARCH->value, Converter::prefix($prefixKey)]; + + foreach ($criterias as $property => $value) { + $arguments[] = "@$property:$value"; + } + + $rawResult = call_user_func_array([$this->redis, 'executeRaw'], $arguments); + + if (!$rawResult) { + $this->handleError(__METHOD__, $this->getLastError()); + } + + return (int)$rawResult[0]; + } + + /** + * @inheritdoc + */ + public function scanKeys(string $prefixKey): array + { + $keys = []; + $iterator = 0; + do { + $scans = $this->redis->scan($iterator, [sprintf('%s*', Converter::prefix($prefixKey))]); + if (!empty($scans)) { + foreach ($scans as $scan) { + $keys[] = $scan; + } + } + /** @phpstan-ignore-next-line */ + } while ($iterator !== 0); + + return $keys; + } + + /** + * @inheritdoc + */ + public function flushAll(): void + { + if (!$this->redis->flushall()) { + $this->handleError(__METHOD__, $this->getLastError()); + } + } + + /** + * @inheritdoc + */ + public function keys(string $pattern): array + { + return $this->redis->keys($pattern); + } + + /** + * @inheritdoc + */ + public function search(string $prefixKey, array $search, array $orderBy, ?string $format = RedisFormat::HASH->value, ?int $numberOfResults = null, int $offset = 0, ?string $searchType = Property::INDEX_TAG): array + { + $arguments = [RedisCommands::SEARCH->value, self::convertPrefix($prefixKey)]; + + if ($search === []) { + $arguments[] = '*'; + } else { + $criteria = ''; + foreach ($search as $property => $value) { + if ($searchType === Property::INDEX_TAG) { + $criteria .= sprintf('@%s:{%s}', $property, $value); + } else { + $criteria .= sprintf('@%s:%s', $property, $value); + } + } + + $arguments[] = $criteria; + } + + foreach ($orderBy as $property => $direction) { + $arguments[] = 'SORTBY'; + $arguments[] = $property; + $arguments[] = $direction; + } + + if ($numberOfResults !== null) { + $arguments[] = 'LIMIT'; + $arguments[] = $offset; + $arguments[] = $numberOfResults; + } + + try { + $result = call_user_func_array([$this->redis, 'executeRaw'], [$arguments]); + } catch (\RedisException $e) { + $this->handleError(RedisCommands::SEARCH->value, $e->getMessage(), $e); + } + + if ($result === false) { + $this->handleError(RedisCommands::SEARCH->value, $this->getLastError()); + } + + if ($result[0] === 0) { + return []; + } + + return $this->extractRedisData((array)$result, $format, $numberOfResults); + } + + /** + * @inheritdoc + */ + public function customSearch(string $prefixKey, string $query, string $format): array + { + $arguments = [RedisCommands::SEARCH->value, self::convertPrefix($prefixKey), $query]; + + try { + $result = call_user_func_array([$this->redis, 'executeRaw'], [$arguments]); + } catch (\RedisException $e) { + $this->handleError(RedisCommands::SEARCH->value, $e->getMessage(), $e); + } + + if ($result === false) { + $this->handleError(RedisCommands::SEARCH->value, $this->getLastError()); + } + + if ($result[0] === 0) { + return []; + } + + return $this->extractRedisData($result, $format); + } + + /** + * @inheritdoc + */ + public function searchLike(string $prefixKey, string $search, ?string $format = RedisFormat::HASH->value, ?int $numberOfResults = null): array + { + $arguments = [RedisCommands::SEARCH->value, Converter::prefix($prefixKey)]; + + $arguments[] = $search; + + try { + $result = call_user_func_array([$this->redis, 'executeRaw'], [$arguments]); + } catch (\RedisException $e) { + $this->handleError(RedisCommands::SEARCH->value, $e->getMessage(), $e); + } + + if ($result === false) { + $this->handleError(RedisCommands::SEARCH->value, $this->getLastError()); + } + + if ($result[0] === 0) { + return []; + } + + if (!is_array($result)) { + $this->handleError(RedisCommands::SEARCH->value, 'Unexpected result type from Redis: ' . gettype($result)); + } + + return $this->extractRedisData($result, $format, $numberOfResults); + } + + public static function convertPrefix(string $key): string + { + return str_replace('\\', '_', $key); + } + + private function handleError(string $command, ?string $errorMessage = 'Unknown error', ?\Throwable $previous = null): never + { + throw new RedisClientResponseException( + sprintf('something was wrong when executing %s command, reason: %s', $command, $errorMessage), + $previous?->getCode() ?? 0, + $previous + ); + } + + private function extractRedisData(array $result, string $format, ?int $numberOfResults = null): array + { + $entities = []; + foreach ($result as $key => $redisData) { + if ($key > 0 && $key % 2 == 0) { + + if ($format === RedisFormat::JSON->value) { + foreach ($redisData as $data) { + if (!str_starts_with($data, '{')) { + continue; + } + $entities[] = json_decode($data, true); + break; + } + + continue; + } else { + $data = []; + for ($i = 0; $i < count($redisData); $i += 2) { + $property = $redisData[$i]; + $value = $redisData[$i + 1]; + $data[$property] = $value; + } + } + + $entities[] = $data; + + if (count($entities) === $numberOfResults) { + return $entities; + } + } + } + + return $entities; + } +} diff --git a/src/Client/RedisClient.php b/src/Client/RedisClient.php index b1de3ef..a94c00f 100644 --- a/src/Client/RedisClient.php +++ b/src/Client/RedisClient.php @@ -4,6 +4,7 @@ namespace Talleu\RedisOm\Client; +use Talleu\RedisOm\Client\Helper\Converter; use Talleu\RedisOm\Command\PropertyToIndex; use Talleu\RedisOm\Exception\BadPropertyConfigurationException; use Talleu\RedisOm\Exception\RedisClientResponseException; @@ -34,7 +35,7 @@ public function createPersistentConnection(?string $host = null, ?int $port = nu */ public function hMSet(string $key, array $data): void { - if (!$this->redis->hMSet(RedisClient::convertPrefix($key), $data)) { + if (!$this->redis->hMSet(Converter::prefix($key), $data)) { $this->handleError(__METHOD__, $this->redis->getLastError()); } } @@ -44,7 +45,7 @@ public function hMSet(string $key, array $data): void */ public function hget(string $key, string $property): string { - $result = $this->redis->hget(RedisClient::convertPrefix($key), $property); + $result = $this->redis->hget(Converter::prefix($key), $property); if (!$result) { $this->handleError(__METHOD__, $this->redis->getLastError()); @@ -58,7 +59,7 @@ public function hget(string $key, string $property): string */ public function hGetAll(string $key): array { - $result = $this->redis->hGetAll(RedisClient::convertPrefix($key)); + $result = $this->redis->hGetAll(Converter::prefix($key)); if ($result === false) { $this->handleError(__METHOD__, $this->redis->getLastError()); @@ -72,7 +73,7 @@ public function hGetAll(string $key): array */ public function del(string $key): void { - if (!$this->redis->del(RedisClient::convertPrefix($key))) { + if (!$this->redis->del(Converter::prefix($key))) { $this->handleError(__METHOD__, $this->redis->getLastError()); } } @@ -82,7 +83,7 @@ public function del(string $key): void */ public function jsonGet(string $key): ?string { - $result = $this->redis->rawCommand(RedisCommands::JSON_GET->value, static::convertPrefix($key)); + $result = $this->redis->rawCommand(RedisCommands::JSON_GET->value, Converter::prefix($key)); if ($result === false) { return null; @@ -96,7 +97,7 @@ public function jsonGet(string $key): ?string */ public function jsonGetProperty(string $key, string $property): ?string { - $result = $this->redis->rawCommand(RedisCommands::JSON_GET->value, static::convertPrefix($key), "$.$property"); + $result = $this->redis->rawCommand(RedisCommands::JSON_GET->value, Converter::prefix($key), "$.$property"); if ($result === false) { return null; @@ -110,7 +111,7 @@ public function jsonGetProperty(string $key, string $property): ?string */ public function jsonSet(string $key, ?string $path = '$', ?string $value = '{}'): void { - if (!$this->redis->rawCommand(RedisCommands::JSON_SET->value, static::convertPrefix($key), $path, $value)) { + if (!$this->redis->rawCommand(RedisCommands::JSON_SET->value, Converter::prefix($key), $path, $value)) { $this->handleError(__METHOD__, $this->redis->getLastError()); } } @@ -127,7 +128,7 @@ public function jsonMSet(...$params): void } for ($i = 0; $i < count($param); $i += 3) { - $arguments[] = static::convertPrefix($param[$i]); + $arguments[] = Converter::prefix($param[$i]); $arguments[] = $param[$i + 1] ?? '$'; $arguments[] = $param[$i + 2] ?? '{}'; } @@ -143,7 +144,7 @@ public function jsonMSet(...$params): void */ public function jsonDel(string $key, ?string $path = '$'): void { - if (!$this->redis->rawCommand(RedisCommands::JSON_DELETE->value, static::convertPrefix($key), $path)) { + if (!$this->redis->rawCommand(RedisCommands::JSON_DELETE->value, Converter::prefix($key), $path)) { $this->handleError(__METHOD__, $this->redis->getLastError()); } } @@ -151,13 +152,13 @@ public function jsonDel(string $key, ?string $path = '$'): void /** * @inheritdoc */ - public function createIndex(string $prefixKey, ?string $format = RedisFormat::HASH->value, ?array $properties = []): void + public function createIndex(string $prefixKey, string $format = RedisFormat::HASH->value, ?array $properties = []): void { if ($properties === []) { return; } - $prefixKey = self::convertPrefix($prefixKey); + $prefixKey = Converter::prefix($prefixKey); $arguments = [ RedisCommands::CREATE_INDEX->value, @@ -187,6 +188,7 @@ public function createIndex(string $prefixKey, ?string $format = RedisFormat::HA throw new BadPropertyConfigurationException(sprintf('Your class %s does not have any typed property', $prefixKey)); } + if (!call_user_func_array([$this->redis, 'rawCommand'], $arguments)) { $this->handleError(__METHOD__, $this->redis->getLastError()); } @@ -198,7 +200,7 @@ public function createIndex(string $prefixKey, ?string $format = RedisFormat::HA public function dropIndex(string $prefixKey): bool { try { - $key = self::convertPrefix($prefixKey); + $key = Converter::prefix($prefixKey); $this->redis->rawCommand(RedisCommands::DROP_INDEX->value, $key); } catch (\RedisException) { return false; @@ -212,7 +214,7 @@ public function dropIndex(string $prefixKey): bool */ public function count(string $prefixKey, array $criterias = []): int { - $arguments = [RedisCommands::SEARCH->value, static::convertPrefix($prefixKey)]; + $arguments = [RedisCommands::SEARCH->value, Converter::prefix($prefixKey)]; foreach ($criterias as $property => $value) { $arguments[] = "@$property:$value"; @@ -224,7 +226,7 @@ public function count(string $prefixKey, array $criterias = []): int $this->handleError(__METHOD__, $this->redis->getLastError()); } - return (int) $rawResult[0]; + return (int)$rawResult[0]; } /** @@ -235,7 +237,7 @@ public function scanKeys(string $prefixKey): array $keys = []; $iterator = null; while ($iterator !== 0) { - $scans = $this->redis->scan($iterator, sprintf('%s*', static::convertPrefix($prefixKey))); + $scans = $this->redis->scan($iterator, sprintf('%s*', Converter::prefix($prefixKey))); foreach ($scans as $scan) { $keys[] = $scan; } @@ -267,7 +269,7 @@ public function keys(string $pattern): array */ public function search(string $prefixKey, array $search, array $orderBy, ?string $format = RedisFormat::HASH->value, ?int $numberOfResults = null, int $offset = 0, ?string $searchType = Property::INDEX_TAG): array { - $arguments = [RedisCommands::SEARCH->value, self::convertPrefix($prefixKey)]; + $arguments = [RedisCommands::SEARCH->value, Converter::prefix($prefixKey)]; if ($search === []) { $arguments[] = '*'; @@ -295,6 +297,12 @@ public function search(string $prefixKey, array $search, array $orderBy, ?string $arguments[] = $numberOfResults; } + if ($numberOfResults !== null) { + $arguments[] = 'LIMIT'; + $arguments[] = $offset; + $arguments[] = $numberOfResults; + } + try { $result = call_user_func_array([$this->redis, 'rawCommand'], $arguments); } catch (\RedisException $e) { @@ -317,7 +325,7 @@ public function search(string $prefixKey, array $search, array $orderBy, ?string */ public function customSearch(string $prefixKey, string $query, string $format): array { - $arguments = [RedisCommands::SEARCH->value, self::convertPrefix($prefixKey), $query]; + $arguments = [RedisCommands::SEARCH->value, Converter::prefix($prefixKey), $query]; try { $result = call_user_func_array([$this->redis, 'rawCommand'], $arguments); @@ -341,7 +349,7 @@ public function customSearch(string $prefixKey, string $query, string $format): */ public function searchLike(string $prefixKey, string $search, ?string $format = RedisFormat::HASH->value, ?int $numberOfResults = null): array { - $arguments = [RedisCommands::SEARCH->value, static::convertPrefix($prefixKey)]; + $arguments = [RedisCommands::SEARCH->value, Converter::prefix($prefixKey)]; $arguments[] = $search; @@ -362,11 +370,6 @@ public function searchLike(string $prefixKey, string $search, ?string $format = return $this->extractRedisData($result, $format, $numberOfResults); } - public static function convertPrefix(string $key): string - { - return str_replace('\\', '_', $key); - } - private function handleError(string $command, ?string $errorMessage = 'Unknown error', ?\Throwable $previous = null): never { throw new RedisClientResponseException( diff --git a/src/Client/RedisClientInterface.php b/src/Client/RedisClientInterface.php index 8493043..3d435bf 100644 --- a/src/Client/RedisClientInterface.php +++ b/src/Client/RedisClientInterface.php @@ -65,7 +65,7 @@ public function jsonDel(string $key, ?string $path = '$'): void; * Create index for objects by properties. * @param PropertyToIndex[] $properties */ - public function createIndex(string $prefixKey, ?string $format = 'HASH', ?array $properties = []): void; + public function createIndex(string $prefixKey, string $format = 'HASH', ?array $properties = []): void; /** * Remove all index for given prefix key. diff --git a/src/Command/GenerateSchema.php b/src/Command/GenerateSchema.php index e86ff24..fef0fb3 100644 --- a/src/Command/GenerateSchema.php +++ b/src/Command/GenerateSchema.php @@ -4,17 +4,23 @@ namespace Talleu\RedisOm\Command; +use Talleu\RedisOm\Client\PredisClient; +use Talleu\RedisOm\Client\RedisClient; use Talleu\RedisOm\Exception\BadIdentifierConfigurationException; use Talleu\RedisOm\Om\Converters\AbstractDateTimeConverter; use Talleu\RedisOm\Om\Mapping\Entity; use Talleu\RedisOm\Om\Mapping\Id; use Talleu\RedisOm\Om\Mapping\Property; use Talleu\RedisOm\Om\RedisFormat; +use Talleu\RedisOm\Om\RedisObjectManager; final class GenerateSchema { public static function generateSchema(string $dir): void { + $redisOm = new RedisObjectManager( + getenv('REDIS_CLIENT') === 'predis' ? new PredisClient() : new RedisClient(), + ); $rii = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir)); $phpFiles = []; @@ -46,7 +52,7 @@ public static function generateSchema(string $dir): void /** @var Entity $entity */ $entity = $attributes[0]->newInstance(); - $entity->redisClient->dropIndex($entity->prefix ?? $fqcn); + $redisOm->dropIndex($entity, $fqcn); $format = $entity->format ?? RedisFormat::HASH->value; $idExist = false; @@ -100,16 +106,16 @@ public static function generateSchema(string $dir): void if (in_array($propertyType, AbstractDateTimeConverter::DATETYPES_NAMES)) { if ($format === RedisFormat::HASH->value) { $propertiesToIndex[] = new PropertyToIndex("$propertyName#timestamp", $propertyName, Property::INDEX_TAG); - $propertiesToIndex[] = new PropertyToIndex("$propertyName#timestamp", $propertyName.'_text', Property::INDEX_TEXT); + $propertiesToIndex[] = new PropertyToIndex("$propertyName#timestamp", $propertyName . '_text', Property::INDEX_TEXT); } else { - $propertiesToIndex[] = new PropertyToIndex('$.'. "$propertyName.timestamp", $propertyName, Property::INDEX_TAG); - $propertiesToIndex[] = new PropertyToIndex('$.'. "$propertyName.timestamp", $propertyName."_text", Property::INDEX_TEXT); + $propertiesToIndex[] = new PropertyToIndex('$.' . "$propertyName.timestamp", $propertyName, Property::INDEX_TAG); + $propertiesToIndex[] = new PropertyToIndex('$.' . "$propertyName.timestamp", $propertyName . "_text", Property::INDEX_TEXT); } } elseif ($propertyType === 'int' || $propertyType === 'float') { if ($format === RedisFormat::HASH->value) { $propertiesToIndex[] = new PropertyToIndex($propertyName, $propertyName, Property::INDEX_TAG); } else { - $propertiesToIndex[] = new PropertyToIndex('$.'.$propertyName, $propertyName, Property::INDEX_TAG); + $propertiesToIndex[] = new PropertyToIndex('$.' . $propertyName, $propertyName, Property::INDEX_TAG); } } elseif (class_exists($propertyType)) { $subReflectionClass = new \ReflectionClass($propertyType); @@ -137,20 +143,19 @@ public static function generateSchema(string $dir): void continue; } - $propertiesToIndex[] = new PropertyToIndex(($format === RedisFormat::JSON->value ? '$.' : '')."$propertyName.$subPropertyName", $propertyName.'_'.$subPropertyName.'_text', Property::INDEX_TEXT); - $propertiesToIndex[] = new PropertyToIndex(($format === RedisFormat::JSON->value ? '$.' : ''). "$propertyName.$subPropertyName", $propertyName.'_'.$subPropertyName, Property::INDEX_TAG); + $propertiesToIndex[] = new PropertyToIndex(($format === RedisFormat::JSON->value ? '$.' : '') . "$propertyName.$subPropertyName", $propertyName . '_' . $subPropertyName . '_text', Property::INDEX_TEXT); + $propertiesToIndex[] = new PropertyToIndex(($format === RedisFormat::JSON->value ? '$.' : '') . "$propertyName.$subPropertyName", $propertyName . '_' . $subPropertyName, Property::INDEX_TAG); } } else { - $propertiesToIndex[] = new PropertyToIndex(($format === RedisFormat::JSON->value ? '$.' : '').$propertyName, $propertyName, Property::INDEX_TAG); - $propertiesToIndex[] = new PropertyToIndex(($format === RedisFormat::JSON->value ? '$.' : '').$propertyName, $propertyName.'_text', Property::INDEX_TEXT); + $propertiesToIndex[] = new PropertyToIndex(($format === RedisFormat::JSON->value ? '$.' : '') . $propertyName, $propertyName, Property::INDEX_TAG); + $propertiesToIndex[] = new PropertyToIndex(($format === RedisFormat::JSON->value ? '$.' : '') . $propertyName, $propertyName . '_text', Property::INDEX_TEXT); } } if (!$idExist) { throw new BadIdentifierConfigurationException("No identifier found for $fqcn, or identifier is not mapped by RedisOm"); } - - $entity->redisClient->createIndex($entity->prefix ?? $fqcn, $format, $propertiesToIndex); + $redisOm->createIndex($entity, $fqcn, $propertiesToIndex); } } diff --git a/src/Om/Key/KeyGenerator.php b/src/Om/Key/KeyGenerator.php index 200514e..2c1abf7 100644 --- a/src/Om/Key/KeyGenerator.php +++ b/src/Om/Key/KeyGenerator.php @@ -18,6 +18,7 @@ public function generateKey(Entity $redisEntity, object &$object): string $identifierProperty = $this->getIdentifier(new \ReflectionClass($object)); $identifierValue = $object->{$identifierProperty->getName()}; + if (!$identifierValue) { $identifierValue = uniqid(); /** @var \ReflectionNamedType|null $propertyType */ diff --git a/src/Om/Mapping/Entity.php b/src/Om/Mapping/Entity.php index 5917071..83eb1d8 100644 --- a/src/Om/Mapping/Entity.php +++ b/src/Om/Mapping/Entity.php @@ -5,14 +5,9 @@ namespace Talleu\RedisOm\Om\Mapping; use Attribute; -use Talleu\RedisOm\Client\RedisClient; -use Talleu\RedisOm\Client\RedisClientInterface; use Talleu\RedisOm\Om\Converters\ConverterInterface; use Talleu\RedisOm\Om\Converters\HashModel\HashObjectConverter; use Talleu\RedisOm\Om\Converters\JsonModel\JsonObjectConverter; -use Talleu\RedisOm\Om\Persister\HashModel\HashPersister; -use Talleu\RedisOm\Om\Persister\JsonModel\JsonPersister; -use Talleu\RedisOm\Om\Persister\PersisterInterface; use Talleu\RedisOm\Om\RedisFormat; use Talleu\RedisOm\Om\Repository\HashModel\HashRepository; use Talleu\RedisOm\Om\Repository\JsonModel\JsonRepository; @@ -22,16 +17,12 @@ final class Entity { public function __construct( - public ?string $prefix = null, - public ?string $format = null, - public ?PersisterInterface $persister = null, - public ?ConverterInterface $converter = null, - public ?RepositoryInterface $repository = null, - public ?RedisClientInterface $redisClient = null, + public ?string $prefix = null, + public ?string $format = null, + public ?ConverterInterface $converter = null, + public ?RepositoryInterface $repository = null, ) { - $this->persister = $persister ?? ($format === RedisFormat::JSON->value ? new JsonPersister() : new HashPersister()); $this->converter = $converter ?? ($format === RedisFormat::JSON->value ? new JsonObjectConverter() : new HashObjectConverter()); $this->repository = $repository ?? ($format === RedisFormat::JSON->value ? new JsonRepository() : new HashRepository()); - $this->redisClient = $redisClient ?? (new RedisClient()); } } diff --git a/src/Om/Persister/AbstractPersister.php b/src/Om/Persister/AbstractPersister.php index 451a778..91e96ce 100644 --- a/src/Om/Persister/AbstractPersister.php +++ b/src/Om/Persister/AbstractPersister.php @@ -11,11 +11,11 @@ abstract class AbstractPersister implements PersisterInterface { - protected RedisClientInterface $redis; - - public function __construct(private ?KeyGenerator $keyGenerator = null) - { - $this->redis = (new RedisClient()); + public function __construct( + private ?KeyGenerator $keyGenerator = null, + protected ?RedisClientInterface $redis = null + ) { + $this->redis = $redis ?? (new RedisClient()); $this->keyGenerator = $keyGenerator ?? new KeyGenerator(); } @@ -25,7 +25,7 @@ public function persist(Entity $objectMapper, $object): ObjectToPersist $key = $this->keyGenerator->generateKey($objectMapper, $object); return new ObjectToPersist( - persisterClass: get_class($objectMapper->persister), + persisterClass: get_class($this), operation: PersisterOperations::OPERATION_PERSIST->value, redisKey: $key, converter: $objectMapper->converter, @@ -37,9 +37,8 @@ public function delete(Entity $objectMapper, $object): ObjectToPersist { $identifier = $this->keyGenerator->getIdentifier(new \ReflectionClass($object)); $key = sprintf('%s:%s', $objectMapper->prefix ?: get_class($object), $object->{$identifier->getName()}); - return new ObjectToPersist( - persisterClass: get_class($objectMapper->persister), + persisterClass: get_class($this), operation: PersisterOperations::OPERATION_DELETE->value, redisKey: $key, ); diff --git a/src/Om/RedisObjectManager.php b/src/Om/RedisObjectManager.php index febb62b..bc88999 100644 --- a/src/Om/RedisObjectManager.php +++ b/src/Om/RedisObjectManager.php @@ -4,14 +4,15 @@ namespace Talleu\RedisOm\Om; -use Talleu\RedisOm\Client\RedisClient; -use Talleu\RedisOm\Exception\RedisOmInvalidArgumentException; +use Talleu\RedisOm\Client\RedisClientInterface; use Talleu\RedisOm\Om\Converters\HashModel\HashObjectConverter; use Talleu\RedisOm\Om\Converters\JsonModel\JsonObjectConverter; use Talleu\RedisOm\Om\Key\KeyGenerator; use Talleu\RedisOm\Om\Mapping\Entity; use Talleu\RedisOm\Om\Metadata\ClassMetadata; use Talleu\RedisOm\Om\Metadata\MetadataFactory; +use Talleu\RedisOm\Om\Persister\HashModel\HashPersister; +use Talleu\RedisOm\Om\Persister\JsonModel\JsonPersister; use Talleu\RedisOm\Om\Persister\ObjectToPersist; use Talleu\RedisOm\Om\Persister\PersisterInterface; use Talleu\RedisOm\Om\Repository\RepositoryInterface; @@ -25,8 +26,10 @@ final class RedisObjectManager implements RedisObjectManagerInterface protected array $objectsToFlush = []; protected ?KeyGenerator $keyGenerator = null; - public function __construct(private readonly ?bool $createPersistentConnection = null) - { + public function __construct( + private readonly ?RedisClientInterface $redisClient, + private readonly ?PersisterInterface $persister = null, + ) { $this->keyGenerator = new KeyGenerator(); } @@ -36,7 +39,7 @@ public function __construct(private readonly ?bool $createPersistentConnection = public function persist(object $object): void { $objectMapper = $this->getEntityMapper($object); - $persister = $this->registerPersister($objectMapper, $object); + $persister = $this->registerPersister($objectMapper); $objectToPersist = $persister->persist($objectMapper, $object); $this->objectsToFlush[$objectToPersist->persisterClass][$objectToPersist->operation][$objectToPersist->redisKey] = $objectToPersist; @@ -48,7 +51,7 @@ public function persist(object $object): void public function remove(object $object): void { $objectMapper = $this->getEntityMapper($object); - $persister = $this->registerPersister($objectMapper, $object); + $persister = $this->registerPersister($objectMapper); $objectToRemove = $persister->delete($objectMapper, $object); $this->objectsToFlush[$objectToRemove->persisterClass][$objectToRemove->operation][$objectToRemove->redisKey] = $objectToRemove; @@ -74,7 +77,7 @@ public function find(string $className, $id): ?object { $objectMapper = $this->getEntityMapper($className); - return $objectMapper->repository->find((string) $id); + return $objectMapper->repository->find((string)$id); } /** @@ -94,7 +97,7 @@ public function detach(object $object): void $objectMapper = $this->getEntityMapper($object); $key = sprintf('%s:%s', $objectMapper->prefix ?: get_class($object), $object->{$identifier->getName()}); - $persisterClassName = get_class($objectMapper->persister); + $persisterClassName = get_class($this->registerPersister($objectMapper)); foreach ($this->objectsToFlush[$persisterClassName] as $operation => $objectsToFlush) { foreach ($objectsToFlush as $redisKey => $objectToFlush) { if ($redisKey === $key) { @@ -112,7 +115,7 @@ public function refresh(object $object): object $objectMapper = $this->getEntityMapper($object); $identifierProperty = $this->keyGenerator->getIdentifier(new \ReflectionClass($object)); - return $objectMapper->repository->find($objectMapper->prefix.':'.$object->{$identifierProperty->getName()}); + return $objectMapper->repository->find($objectMapper->prefix . ':' . $object->{$identifierProperty->getName()}); } /** @@ -157,7 +160,7 @@ public function contains(object $object): bool $identifier = $this->keyGenerator->getIdentifier(new \ReflectionClass($object)); $objectMapper = $this->getEntityMapper($object); $key = sprintf('%s:%s', $objectMapper->prefix ?: get_class($object), $object->{$identifier->getName()}); - $persisterClassName = get_class($objectMapper->persister); + $persisterClassName = get_class($this->registerPersister($objectMapper)); foreach ($this->objectsToFlush[$persisterClassName] as $operation => $objectsToFlush) { foreach ($objectsToFlush as $redisKey => $objectToFlush) { if ($redisKey === $key) { @@ -184,27 +187,32 @@ protected function getEntityMapper(string|object $object): Entity $redisEntity->repository->setClassName($reflectionClass->getName()); $redisEntity->repository->setConverter($redisEntity->converter ?? ($redisEntity->format === RedisFormat::HASH->value ? new HashObjectConverter() : new JsonObjectConverter())); - $redisClient = $redisEntity->redisClient ?? new RedisClient(); - if ($this->createPersistentConnection === true) { - $redisClient->createPersistentConnection(); - } - $redisEntity->repository->setRedisClient($redisClient); + $redisEntity->repository->setRedisClient($this->redisClient); $redisEntity->repository->setFormat($redisEntity->format); return $redisEntity; } - protected function registerPersister(Entity $redisEntity, object $object): PersisterInterface + protected function registerPersister(Entity $object): PersisterInterface { - if (is_null($persister = $redisEntity->persister)) { - throw new RedisOmInvalidArgumentException(sprintf('No persister found for %s object.', get_class($object))); - } + $persister = ($object->format === RedisFormat::JSON->value ? new JsonPersister(redis: $this->redisClient) : new HashPersister(redis: $this->redisClient)); - $persisterClass = get_class($persister); + $persisterClass = $persister::class; if (!array_key_exists($persisterClass, $this->persisters)) { $this->persisters[$persisterClass] = $persister; } - return $persister; } + + public function createIndex(object $object, string $fqcn = null, array $propertiesToIndex = []): void + { + $this->redisClient->createIndex($object->prefix ?? $fqcn, $object->format ?? RedisFormat::HASH->value, $propertiesToIndex); + } + + public function dropIndex(object $object, string $fqcn = null): void + { + $this->redisClient->dropIndex($object->prefix ?? $fqcn); + } + + } diff --git a/src/Om/RedisObjectManagerInterface.php b/src/Om/RedisObjectManagerInterface.php index c913557..980d3c9 100644 --- a/src/Om/RedisObjectManagerInterface.php +++ b/src/Om/RedisObjectManagerInterface.php @@ -67,4 +67,8 @@ public function initializeObject(object $obj); * Check if the object is managed by the current unit of work. */ public function contains(object $object): bool; + + public function createIndex(object $object): void; + + public function dropIndex(object $object): void; } diff --git a/tests/Client/Client.php b/tests/Client/Client.php index 4c3c552..f02bed7 100644 --- a/tests/Client/Client.php +++ b/tests/Client/Client.php @@ -4,6 +4,7 @@ namespace Talleu\RedisOm\Tests\Client; +use Talleu\RedisOm\Client\PredisClient; use Talleu\RedisOm\Client\RedisClient; use Talleu\RedisOm\Client\RedisClientInterface; @@ -13,7 +14,7 @@ class Client public function __construct() { - $this->redisClient = new RedisClient(); + $this->redisClient = getenv('REDIS_CLIENT') === 'predis' ? new PredisClient() : new RedisClient(); $this->redisClient->createPersistentConnection($_SERVER['REDIS_HOST']); } } diff --git a/tests/Fixtures/Bar.php b/tests/Fixtures/Bar.php index f44ccab..a753d48 100644 --- a/tests/Fixtures/Bar.php +++ b/tests/Fixtures/Bar.php @@ -2,9 +2,10 @@ namespace Talleu\RedisOm\Tests\Fixtures; +use Talleu\RedisOm\Client\PredisClient; use Talleu\RedisOm\Om\Mapping as RedisOm; -#[RedisOm\Entity] +#[RedisOm\Entity()] class Bar { #[RedisOm\Property] diff --git a/tests/Functionnal/DataConsistency/ArrayConsistencyTest.php b/tests/Functionnal/DataConsistency/ArrayConsistencyTest.php index 4534e5d..07b93e3 100644 --- a/tests/Functionnal/DataConsistency/ArrayConsistencyTest.php +++ b/tests/Functionnal/DataConsistency/ArrayConsistencyTest.php @@ -12,6 +12,13 @@ class ArrayConsistencyTest extends RedisAbstractTestCase { + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testArrayHash(): void { self::emptyRedis(); @@ -32,11 +39,11 @@ public function testArrayHash(): void ] ]; - $objectManager = new RedisObjectManager(); - $objectManager->persist($arrayHash); - $objectManager->flush(); - $this->assertEquals($arrayHash, $objectManager->find(ArrayHash::class, 1)); + $this->objectManager->persist($arrayHash); + $this->objectManager->flush(); + + $this->assertEquals($arrayHash, $this->objectManager->find(ArrayHash::class, 1)); } public function testArrayJson(): void @@ -60,11 +67,10 @@ public function testArrayJson(): void ] ]; - $objectManager = new RedisObjectManager(); - $objectManager->persist($arrayJson); - $objectManager->flush(); + $this->objectManager->persist($arrayJson); + $this->objectManager->flush(); - $this->assertEquals($arrayJson, $objectManager->find(ArrayJson::class, 1)); + $this->assertEquals($arrayJson, $this->objectManager->find(ArrayJson::class, 1)); } public function createBar(int $id, string $title): Bar diff --git a/tests/Functionnal/DataConsistency/DatesConsistencyTest.php b/tests/Functionnal/DataConsistency/DatesConsistencyTest.php index 918381b..406c9c8 100644 --- a/tests/Functionnal/DataConsistency/DatesConsistencyTest.php +++ b/tests/Functionnal/DataConsistency/DatesConsistencyTest.php @@ -11,6 +11,14 @@ class DatesConsistencyTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testDateHash(): void { self::emptyRedis(); @@ -20,11 +28,11 @@ public function testDateHash(): void $dateObject->id = 1; $dateObject->createdAt = new \DateTime('2021-01-01'); - $objectManager = new RedisObjectManager(); - $objectManager->persist($dateObject); - $objectManager->flush(); - $this->assertEquals($dateObject, $objectManager->find(DateHash::class, 1)); + $this->objectManager->persist($dateObject); + $this->objectManager->flush(); + + $this->assertEquals($dateObject, $this->objectManager->find(DateHash::class, 1)); } public function testDateJson(): void @@ -36,10 +44,10 @@ public function testDateJson(): void $dateObject->id = 1; $dateObject->createdAt = new \DateTime('2021-01-01'); - $objectManager = new RedisObjectManager(); - $objectManager->persist($dateObject); - $objectManager->flush(); - $this->assertEquals($dateObject, $objectManager->find(DateJson::class, 1)); + $this->objectManager->persist($dateObject); + $this->objectManager->flush(); + + $this->assertEquals($dateObject, $this->objectManager->find(DateJson::class, 1)); } } diff --git a/tests/Functionnal/DataConsistency/NullConsistencyTest.php b/tests/Functionnal/DataConsistency/NullConsistencyTest.php index d97ac5c..408ac95 100644 --- a/tests/Functionnal/DataConsistency/NullConsistencyTest.php +++ b/tests/Functionnal/DataConsistency/NullConsistencyTest.php @@ -11,6 +11,13 @@ class NullConsistencyTest extends RedisAbstractTestCase { + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testNullHash(): void { self::emptyRedis(); @@ -19,11 +26,11 @@ public function testNullHash(): void $nullObject = new NullHash(); $nullObject->id = 1; - $objectManager = new RedisObjectManager(); - $objectManager->persist($nullObject); - $objectManager->flush(); - $repository = $objectManager->getRepository(NullHash::class); + $this->objectManager->persist($nullObject); + $this->objectManager->flush(); + + $repository = $this->objectManager->getRepository(NullHash::class); $retrieveNullObject = $repository->find(1); $this->assertNull($retrieveNullObject->unknown); } @@ -37,11 +44,11 @@ public function testNotNullHash(): void $nullObject->id = 1; $nullObject->unknown = 'test'; - $objectManager = new RedisObjectManager(); - $objectManager->persist($nullObject); - $objectManager->flush(); - $repository = $objectManager->getRepository(NullHash::class); + $this->objectManager->persist($nullObject); + $this->objectManager->flush(); + + $repository = $this->objectManager->getRepository(NullHash::class); $retrieveNullObject = $repository->find(1); $this->assertEquals($retrieveNullObject->unknown, 'test'); } @@ -53,10 +60,10 @@ public function testNullJson(): void $nullObject = new NullJson(); $nullObject->id = 1; - $objectManager = new RedisObjectManager(); - $objectManager->persist($nullObject); - $objectManager->flush(); - $repository = $objectManager->getRepository(NullJson::class); + + $this->objectManager->persist($nullObject); + $this->objectManager->flush(); + $repository = $this->objectManager->getRepository(NullJson::class); $retrieveNullObject = $repository->find(1); $this->assertNull($retrieveNullObject->unknown); } @@ -70,11 +77,10 @@ public function testNotNullJson(): void $nullObject->id = 1; $nullObject->unknown = 'test'; - $objectManager = new RedisObjectManager(); - $objectManager->persist($nullObject); - $objectManager->flush(); + $this->objectManager->persist($nullObject); + $this->objectManager->flush(); - $repository = $objectManager->getRepository(NullJson::class); + $repository = $this->objectManager->getRepository(NullJson::class); $retrieveNullObject = $repository->find(1); $this->assertEquals($retrieveNullObject->unknown, 'test'); } diff --git a/tests/Functionnal/DataConsistency/ObjectConsistencyTest.php b/tests/Functionnal/DataConsistency/ObjectConsistencyTest.php index 9dcaf68..e26d828 100644 --- a/tests/Functionnal/DataConsistency/ObjectConsistencyTest.php +++ b/tests/Functionnal/DataConsistency/ObjectConsistencyTest.php @@ -14,6 +14,14 @@ class ObjectConsistencyTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testBookHash(): void { self::emptyRedis(); @@ -27,11 +35,11 @@ public function testBookHash(): void $this->createTag('period', ['XIX', 'XX']), ]; - $objectManager = new RedisObjectManager(); - $objectManager->persist($book); - $objectManager->flush(); - $retrieveBook = $objectManager->getRepository(BookHash::class)->find('678CUIO'); + $this->objectManager->persist($book); + $this->objectManager->flush(); + + $retrieveBook = $this->objectManager->getRepository(BookHash::class)->find('678CUIO'); $this->assertEquals($retrieveBook, $book); } @@ -49,11 +57,11 @@ public function testBookJson(): void ]; - $objectManager = new RedisObjectManager(); - $objectManager->persist($book); - $objectManager->flush(); - $retrieveBook = $objectManager->getRepository(BookJson::class)->find('678CUIO'); + $this->objectManager->persist($book); + $this->objectManager->flush(); + + $retrieveBook = $this->objectManager->getRepository(BookJson::class)->find('678CUIO'); $this->assertEquals($retrieveBook, $book); } diff --git a/tests/Functionnal/Om/Mapping/MappingPropertyTest.php b/tests/Functionnal/Om/Mapping/MappingPropertyTest.php index 816812f..598806d 100644 --- a/tests/Functionnal/Om/Mapping/MappingPropertyTest.php +++ b/tests/Functionnal/Om/Mapping/MappingPropertyTest.php @@ -11,18 +11,26 @@ class MappingPropertyTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testPropertyNotMappedJson() { static::emptyRedis(); static::generateIndex(); $dummyJson = DummyJsonWithoutAge::create(id: 33, price: 10.5, age: null, name: 'Clément'); - $objectManager = new RedisObjectManager(); - $objectManager->persist($dummyJson); - $objectManager->flush(); + + $this->objectManager->persist($dummyJson); + $this->objectManager->flush(); /** @var DummyJsonWithoutAge $object */ - $object = $objectManager->find(DummyJsonWithoutAge::class, 33); + $object = $this->objectManager->find(DummyJsonWithoutAge::class, 33); $this->assertInstanceOf(DummyJsonWithoutAge::class, $object); $this->assertNull($object->age); $this->assertEquals('Clément', $object->name); @@ -34,12 +42,12 @@ public function testPropertyNotMappedHash() static::generateIndex(); $dummyJson = DummyHashWithoutAge::create(id: 33, price: 10.5, age: null, name: 'Clément'); - $objectManager = new RedisObjectManager(); - $objectManager->persist($dummyJson); - $objectManager->flush(); + + $this->objectManager->persist($dummyJson); + $this->objectManager->flush(); /** @var DummyHashWithoutAge $object */ - $object = $objectManager->find(DummyHashWithoutAge::class, 33); + $object = $this->objectManager->find(DummyHashWithoutAge::class, 33); $this->assertInstanceOf(DummyHashWithoutAge::class, $object); $this->assertNull($object->age); $this->assertEquals('Clément', $object->name); diff --git a/tests/Functionnal/Om/Mapping/PrefixTest.php b/tests/Functionnal/Om/Mapping/PrefixTest.php index 70ee459..c259104 100644 --- a/tests/Functionnal/Om/Mapping/PrefixTest.php +++ b/tests/Functionnal/Om/Mapping/PrefixTest.php @@ -11,6 +11,14 @@ class PrefixTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testPrefix() { static::emptyRedis(); @@ -18,15 +26,15 @@ public function testPrefix() static::loadRedisFixtures(PrefixDummyHash::class); - $objectManager = new RedisObjectManager(); + $keys = $this->createClient()->keys('*'); foreach ($keys as $key) { $this->assertStringContainsString('dummy:', $key); } - $this->assertInstanceOf(PrefixDummyHash::class, $objectManager->find(PrefixDummyHash::class, 1)); - $this->assertInstanceOf(PrefixDummyHash::class, $objectManager->find(PrefixDummyHash::class, 2)); - $this->assertInstanceOf(PrefixDummyHash::class, $objectManager->find(PrefixDummyHash::class, 3)); + $this->assertInstanceOf(PrefixDummyHash::class, $this->objectManager->find(PrefixDummyHash::class, 1)); + $this->assertInstanceOf(PrefixDummyHash::class, $this->objectManager->find(PrefixDummyHash::class, 2)); + $this->assertInstanceOf(PrefixDummyHash::class, $this->objectManager->find(PrefixDummyHash::class, 3)); } public function testPrefixJson() @@ -36,14 +44,13 @@ public function testPrefixJson() static::loadRedisFixtures(PrefixDummyJson::class); - $objectManager = new RedisObjectManager(); $keys = $this->createClient()->keys('*'); foreach ($keys as $key) { $this->assertStringContainsString('dummy:', $key); } - $this->assertInstanceOf(PrefixDummyJson::class, $objectManager->find(PrefixDummyJson::class, 1)); - $this->assertInstanceOf(PrefixDummyJson::class, $objectManager->find(PrefixDummyJson::class, 2)); - $this->assertInstanceOf(PrefixDummyJson::class, $objectManager->find(PrefixDummyJson::class, 3)); + $this->assertInstanceOf(PrefixDummyJson::class, $this->objectManager->find(PrefixDummyJson::class, 1)); + $this->assertInstanceOf(PrefixDummyJson::class, $this->objectManager->find(PrefixDummyJson::class, 2)); + $this->assertInstanceOf(PrefixDummyJson::class, $this->objectManager->find(PrefixDummyJson::class, 3)); } } diff --git a/tests/Functionnal/Om/Mapping/PrivatePropertiesTest.php b/tests/Functionnal/Om/Mapping/PrivatePropertiesTest.php index 3646102..062e329 100644 --- a/tests/Functionnal/Om/Mapping/PrivatePropertiesTest.php +++ b/tests/Functionnal/Om/Mapping/PrivatePropertiesTest.php @@ -11,18 +11,26 @@ class PrivatePropertiesTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testPropertyNotPublic() { static::emptyRedis(); static::generateIndex(); $dummy = DummyHashWithPrivateProperties::create(id: 12, name: 'Joad', age: 37); - $objectManager = new RedisObjectManager(); - $objectManager->persist($dummy); - $objectManager->flush(); + + $this->objectManager->persist($dummy); + $this->objectManager->flush(); /** @var DummyHashWithPrivateProperties|null $object */ - $object = $objectManager->find(DummyHashWithPrivateProperties::class, 12); + $object = $this->objectManager->find(DummyHashWithPrivateProperties::class, 12); $this->assertInstanceOf(DummyHashWithPrivateProperties::class, $object); $this->assertEquals($object, $dummy); } @@ -33,12 +41,12 @@ public function testPropertyNotPublicJson() static::generateIndex(); $dummy = DummyJsonWithPrivateProperties::create(id: 12, name: 'Joad', age: 37); - $objectManager = new RedisObjectManager(); - $objectManager->persist($dummy); - $objectManager->flush(); + + $this->objectManager->persist($dummy); + $this->objectManager->flush(); /** @var DummyJsonWithPrivateProperties|null $object */ - $object = $objectManager->find(DummyJsonWithPrivateProperties::class, 12); + $object = $this->objectManager->find(DummyJsonWithPrivateProperties::class, 12); $this->assertInstanceOf(DummyJsonWithPrivateProperties::class, $object); $this->assertEquals($object, $dummy); } diff --git a/tests/Functionnal/Om/Mapping/SpecialCharsTest.php b/tests/Functionnal/Om/Mapping/SpecialCharsTest.php index 291bc73..9c7d7fb 100644 --- a/tests/Functionnal/Om/Mapping/SpecialCharsTest.php +++ b/tests/Functionnal/Om/Mapping/SpecialCharsTest.php @@ -11,6 +11,14 @@ class SpecialCharsTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testSpecialCharsFindOneBy() { static::emptyRedis(); @@ -18,7 +26,7 @@ public function testSpecialCharsFindOneBy() /** @var SpecialCharsDummyHash[] $dummies */ $dummies = static::loadRedisFixtures(SpecialCharsDummyHash::class); - $repository = (new RedisObjectManager())->getRepository(SpecialCharsDummyHash::class); + $repository = $this->objectManager->getRepository(SpecialCharsDummyHash::class); $object = $repository->findOneBy(['specialChars' => 'ok:']); $this->assertEquals($dummies[0], $object); } @@ -29,7 +37,7 @@ public function testSpecialCharsFindOneByJson() static::generateIndex(); $dummies = static::loadRedisFixtures(SpecialCharsDummyJson::class); - $repository = (new RedisObjectManager())->getRepository(SpecialCharsDummyJson::class); + $repository = $this->objectManager->getRepository(SpecialCharsDummyJson::class); $object = $repository->findOneBy(['specialChars' => 'ok:']); $this->assertEquals($dummies[0], $object); } @@ -40,7 +48,7 @@ public function testSpecialCharsFindOneByBadSearch() static::generateIndex(); static::loadRedisFixtures(SpecialCharsDummyHash::class); - $repository = (new RedisObjectManager())->getRepository(SpecialCharsDummyHash::class); + $repository = $this->objectManager->getRepository(SpecialCharsDummyHash::class); $object = $repository->findOneBy(['specialChars' => 'ok::']); $this->assertNull($object); } @@ -51,7 +59,7 @@ public function testSpecialCharsFindOneByBadSearchJson() static::generateIndex(); static::loadRedisFixtures(SpecialCharsDummyJson::class); - $repository = (new RedisObjectManager())->getRepository(SpecialCharsDummyJson::class); + $repository = $this->objectManager->getRepository(SpecialCharsDummyJson::class); $object = $repository->findOneBy(['specialChars' => 'ok::']); $this->assertNull($object); } @@ -62,7 +70,7 @@ public function testSpecialCharsFindByBadSearch() static::generateIndex(); static::loadRedisFixtures(SpecialCharsDummyHash::class); - $repository = (new RedisObjectManager())->getRepository(SpecialCharsDummyHash::class); + $repository = $this->objectManager->getRepository(SpecialCharsDummyHash::class); $collection = $repository->findBy(['specialChars' => 'ok::']); $this->assertEmpty($collection); } @@ -73,7 +81,7 @@ public function testSpecialCharsFindByBadSearchJson() static::generateIndex(); static::loadRedisFixtures(SpecialCharsDummyJson::class); - $repository = (new RedisObjectManager())->getRepository(SpecialCharsDummyJson::class); + $repository = $this->objectManager->getRepository(SpecialCharsDummyJson::class); $collection = $repository->findBy(['specialChars' => 'ok::']); $this->assertEmpty($collection); } @@ -84,8 +92,9 @@ public function testSpecialCharsFindBy() static::generateIndex(); $dummies = static::loadRedisFixtures(SpecialCharsDummyHash::class); - $repository = (new RedisObjectManager())->getRepository(SpecialCharsDummyHash::class); + $repository = $this->objectManager->getRepository(SpecialCharsDummyHash::class); $collection = $repository->findBy(['specialChars' => 'ok:']); + $this->assertEquals($dummies[0], $collection[0]); $this->assertEquals($dummies[1], $collection[1]); $this->assertEquals($dummies[2], $collection[2]); @@ -96,7 +105,7 @@ public function testSpecialCharsFindByJson() static::emptyRedis(); static::generateIndex(); $dummies = static::loadRedisFixtures(SpecialCharsDummyJson::class); - $repository = (new RedisObjectManager())->getRepository(SpecialCharsDummyJson::class); + $repository = $this->objectManager->getRepository(SpecialCharsDummyJson::class); $collection = $repository->findBy(['specialChars' => 'ok:']); $this->assertEquals($dummies[0], $collection[0]); @@ -110,7 +119,7 @@ public function testOtherSpecialCharsFindOneBy() static::generateIndex(); static::loadRedisFixtures(SpecialCharsDummyHash::class); - $repository = (new RedisObjectManager())->getRepository(SpecialCharsDummyHash::class); + $repository = $this->objectManager->getRepository(SpecialCharsDummyHash::class); $object = $repository->findOneBy(['specialChars' => 'o//\\']); $this->assertNull($object); } @@ -121,7 +130,7 @@ public function testOtherSpecialCharsFindOneByJson() static::generateIndex(); static::loadRedisFixtures(SpecialCharsDummyJson::class); - $repository = (new RedisObjectManager())->getRepository(SpecialCharsDummyJson::class); + $repository = $this->objectManager->getRepository(SpecialCharsDummyJson::class); $object = $repository->findOneBy(['specialChars' => 'o//\\']); $this->assertNull($object); } diff --git a/tests/Functionnal/Om/ObjectManagerTest.php b/tests/Functionnal/Om/ObjectManagerTest.php index 3a2ef9c..8acce38 100644 --- a/tests/Functionnal/Om/ObjectManagerTest.php +++ b/tests/Functionnal/Om/ObjectManagerTest.php @@ -4,6 +4,7 @@ namespace Talleu\RedisOm\Tests\Functionnal\Om; +use Talleu\RedisOm\Client\Helper\Converter; use Talleu\RedisOm\Client\RedisClient; use Talleu\RedisOm\Om\RedisFormat; use Talleu\RedisOm\Om\RedisObjectManager; @@ -15,6 +16,14 @@ class ObjectManagerTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testPersistAndFlushHash(): void { static::emptyRedis(); @@ -22,7 +31,7 @@ public function testPersistAndFlushHash(): void static::loadRedisFixtures(); $keys = $this->createClient()->keys('*'); - $classNameConverted = RedisClient::convertPrefix(DummyHash::class); + $classNameConverted = Converter::prefix(DummyHash::class); $this->assertTrue(in_array($classNameConverted.':1', $keys)); $this->assertTrue(in_array($classNameConverted.':2', $keys)); $this->assertTrue(in_array($classNameConverted.':3', $keys)); @@ -35,7 +44,7 @@ public function testPersistAndFlushJson(): void static::loadRedisFixtures(DummyJson::class); $keys = $this->createClient()->keys('*'); - $classNameConverted = RedisClient::convertPrefix(DummyJson::class); + $classNameConverted = Converter::prefix(DummyJson::class); $this->assertTrue(in_array($classNameConverted.':1', $keys)); $this->assertTrue(in_array($classNameConverted.':2', $keys)); $this->assertTrue(in_array($classNameConverted.':3', $keys)); @@ -47,18 +56,18 @@ public function testFindJson() static::generateIndex(); $dummies = static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); + /** @var DummyJson $object */ - $object1 = $objectManager->find(DummyJson::class, 1); + $object1 = $this->objectManager->find(DummyJson::class, 1); $this->assertInstanceOf(DummyJson::class, $object1); $this->assertEquals($object1, $dummies[0]); /** @var DummyJson $object */ - $object2 = $objectManager->find(DummyJson::class, 2); + $object2 = $this->objectManager->find(DummyJson::class, 2); $this->assertEquals($object2, $dummies[1]); /** @var DummyJson $object */ - $object3 = $objectManager->find(DummyJson::class, 3); + $object3 = $this->objectManager->find(DummyJson::class, 3); $this->assertEquals($object3, $dummies[2]); } @@ -68,18 +77,18 @@ public function testFindHash() static::generateIndex(); $dummies = static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $object1 = $objectManager->find(DummyHash::class, 1); + + $object1 = $this->objectManager->find(DummyHash::class, 1); $this->assertInstanceOf(DummyHash::class, $object1); $this->assertEquals($object1, $dummies[0]); /** @var DummyJson $object */ - $object2 = $objectManager->find(DummyHash::class, 2); + $object2 = $this->objectManager->find(DummyHash::class, 2); $this->assertEquals($object2, $dummies[1]); /** @var DummyJson $object */ - $object3 = $objectManager->find(DummyHash::class, 3); + $object3 = $this->objectManager->find(DummyHash::class, 3); $this->assertEquals($object3, $dummies[2]); } @@ -91,27 +100,27 @@ public function testRemove() /** @var DummyHash $object */ $object = $dummies[0]; - $objectManager = new RedisObjectManager(); - $objectManager->persist($object); - $objectManager->flush(); - $retrieveObject = $objectManager->find(DummyHash::class, $object->id); + $this->objectManager->persist($object); + $this->objectManager->flush(); + + $retrieveObject = $this->objectManager->find(DummyHash::class, $object->id); $this->assertInstanceOf(DummyHash::class, $retrieveObject); // Then remove the object - $objectManager->remove($object); - $objectManager->flush(); - $retrieveObject = $objectManager->find(DummyHash::class, $object->id); + $this->objectManager->remove($object); + $this->objectManager->flush(); + $retrieveObject = $this->objectManager->find(DummyHash::class, $object->id); $this->assertNull($retrieveObject); } public function testGetRepository() { - $objectManager = new RedisObjectManager(); - $repo = $objectManager->getRepository(DummyHash::class); + + $repo = $this->objectManager->getRepository(DummyHash::class); $this->assertInstanceOf(HashRepository::class, $repo); - $repo = $objectManager->getRepository(DummyJson::class); + $repo = $this->objectManager->getRepository(DummyJson::class); $this->assertInstanceOf(JsonRepository::class, $repo); } @@ -120,12 +129,12 @@ public function testClear() static::emptyRedis(); static::generateIndex(); $dummies = static::loadRedisFixtures(flush: false); - $objectManager = new RedisObjectManager(); + foreach ($dummies as $dummy) { - $objectManager->persist($dummy); + $this->objectManager->persist($dummy); } - $objectManager->clear(); + $this->objectManager->clear(); $keys = $this->createClient()->keys('*'); $this->assertCount(0, $keys); } @@ -135,13 +144,13 @@ public function testDetach() static::emptyRedis(); static::generateIndex(); $dummies = static::loadRedisFixtures(flush: false); - $objectManager = new RedisObjectManager(); + foreach ($dummies as $dummy) { - $objectManager->persist($dummy); + $this->objectManager->persist($dummy); } - $objectManager->detach($dummies[1]); - $objectManager->flush(); + $this->objectManager->detach($dummies[1]); + $this->objectManager->flush(); $keys = $this->createClient()->keys('*'); $this->assertCount(2, $keys); $this->assertNotContains("Talleu_RedisOm_Tests_Fixtures_Hash_DummyHash:2", $keys); @@ -152,13 +161,13 @@ public function testContains() static::emptyRedis(); static::generateIndex(); $dummies = static::loadRedisFixtures(flush: false); - $objectManager = new RedisObjectManager(); + foreach ($dummies as $dummy) { - $objectManager->persist($dummy); + $this->objectManager->persist($dummy); } - $this->assertTrue($objectManager->contains($dummies[0])); - $this->assertTrue($objectManager->contains($dummies[1])); - $this->assertTrue($objectManager->contains($dummies[2])); + $this->assertTrue($this->objectManager->contains($dummies[0])); + $this->assertTrue($this->objectManager->contains($dummies[1])); + $this->assertTrue($this->objectManager->contains($dummies[2])); } } diff --git a/tests/Functionnal/Om/Repository/HashModel/HashBooleanRepositoryTest.php b/tests/Functionnal/Om/Repository/HashModel/HashBooleanRepositoryTest.php index c3cb73e..34ef1e1 100644 --- a/tests/Functionnal/Om/Repository/HashModel/HashBooleanRepositoryTest.php +++ b/tests/Functionnal/Om/Repository/HashModel/HashBooleanRepositoryTest.php @@ -10,14 +10,20 @@ final class HashBooleanRepositoryTest extends RedisAbstractTestCase { + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testFindByEnabled() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $collection = $repository->findBy(['enabled' => true]); foreach ($collection as $dummy) { @@ -32,8 +38,7 @@ public function testFindOneByEnabled() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $objet = $repository->findOneBy(['enabled' => true]); $this->assertTrue($objet->enabled); @@ -45,8 +50,7 @@ public function testFindByDisabled() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $collection = $repository->findBy(['enabled' => false]); foreach ($collection as $dummy) { @@ -61,8 +65,7 @@ public function testFindOneByDisabled() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $objet = $repository->findOneBy(['enabled' => false]); $this->assertFalse($objet->enabled); diff --git a/tests/Functionnal/Om/Repository/HashModel/HashFindLikeTest.php b/tests/Functionnal/Om/Repository/HashModel/HashFindLikeTest.php index e4290be..43c85ad 100644 --- a/tests/Functionnal/Om/Repository/HashModel/HashFindLikeTest.php +++ b/tests/Functionnal/Om/Repository/HashModel/HashFindLikeTest.php @@ -10,14 +10,21 @@ final class HashFindLikeTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testFindByLike() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $collection = $repository->findByLike(['name' => 'Oli']); foreach ($collection as $dummy) { @@ -32,8 +39,7 @@ public function testFindOneByLike() static::generateIndex(); static::loadRedisFixtures(DummyHash::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $object = $repository->findOneByLike(['name' => 'Oli']); $this->assertTrue(str_contains($object->name, 'Oli')); @@ -45,8 +51,7 @@ public function testFindByLikeEndWith() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $collection = $repository->findByLike(['name' => 'vier']); foreach ($collection as $dummy) { @@ -61,8 +66,7 @@ public function testFindOneByLikeEndWith() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $object = $repository->findOneByLike(['name' => 'vier']); $this->assertTrue(str_contains($object->name, 'vier')); diff --git a/tests/Functionnal/Om/Repository/HashModel/HashIdentifierRepositoryTest.php b/tests/Functionnal/Om/Repository/HashModel/HashIdentifierRepositoryTest.php index f4fc5a9..b791a90 100644 --- a/tests/Functionnal/Om/Repository/HashModel/HashIdentifierRepositoryTest.php +++ b/tests/Functionnal/Om/Repository/HashModel/HashIdentifierRepositoryTest.php @@ -10,14 +10,21 @@ final class HashIdentifierRepositoryTest extends RedisAbstractTestCase { + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testFindById() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + + $repository = $this->objectManager->getRepository(DummyHash::class); $collection = $repository->findBy(['id' => 1]); foreach ($collection as $dummy) { @@ -32,8 +39,8 @@ public function testFindOneById() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + + $repository = $this->objectManager->getRepository(DummyHash::class); $objet = $repository->findOneBy(['id' => 2]); $this->assertEquals($objet->id, 2); diff --git a/tests/Functionnal/Om/Repository/HashModel/HashNullableRepositoryTest.php b/tests/Functionnal/Om/Repository/HashModel/HashNullableRepositoryTest.php index 20a852d..65a2bb2 100644 --- a/tests/Functionnal/Om/Repository/HashModel/HashNullableRepositoryTest.php +++ b/tests/Functionnal/Om/Repository/HashModel/HashNullableRepositoryTest.php @@ -10,21 +10,29 @@ final class HashNullableRepositoryTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testFindByNull() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(DummyHashWithNullProperties::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHashWithNullProperties::class); + + $repository = $this->objectManager->getRepository(DummyHashWithNullProperties::class); // Update 1 object to set unknown to not null /** @var DummyHashWithNullProperties $object */ $object = $repository->findOneBy(['name' => 'Kevin']); $object->unknown = 'Not null'; - $objectManager->persist($object); - $objectManager->flush(); + $this->objectManager->persist($object); + $this->objectManager->flush(); $collection = $repository->findBy(['unknown' => null]); foreach ($collection as $dummy) { @@ -39,15 +47,15 @@ public function testFindByNotNull() static::generateIndex(); static::loadRedisFixtures(DummyHashWithNullProperties::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHashWithNullProperties::class); + + $repository = $this->objectManager->getRepository(DummyHashWithNullProperties::class); // Update 1 object to set unknown to not null /** @var DummyHashWithNullProperties $object */ $object = $repository->findOneBy(['name' => 'Kevin']); $object->unknown = 'Notnull'; - $objectManager->persist($object); - $objectManager->flush(); + $this->objectManager->persist($object); + $this->objectManager->flush(); $collection = $repository->findBy(['unknown' => 'Notnull']); foreach ($collection as $dummy) { diff --git a/tests/Functionnal/Om/Repository/HashModel/HashRepositoryTest.php b/tests/Functionnal/Om/Repository/HashModel/HashRepositoryTest.php index dca8c49..dbba83f 100644 --- a/tests/Functionnal/Om/Repository/HashModel/HashRepositoryTest.php +++ b/tests/Functionnal/Om/Repository/HashModel/HashRepositoryTest.php @@ -11,14 +11,20 @@ final class HashRepositoryTest extends RedisAbstractTestCase { + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testFindAll() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $collection = $repository->findAll(); @@ -33,8 +39,7 @@ public function testFindBy() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $collection = $repository->findBy(['name' => 'Olivier']); @@ -51,8 +56,7 @@ public function testFindByTypo() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $collection = $repository->findBy(['name' => 'Lolivier']); $this->assertEmpty($collection); @@ -64,8 +68,7 @@ public function testFindByOrder() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $collection = $repository->findBy(['name' => 'Olivier'], ['age' => 'ASC']); $this->assertCount(2, $collection); @@ -94,8 +97,7 @@ public function testFindByMultiCriterias() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $collection = $repository->findBy(['name' => 'Olivier', 'age' => 34]); @@ -113,8 +115,7 @@ public function testFindLike() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $collection = $repository->findLike('olivier'); @@ -131,8 +132,7 @@ public function testFindOneBy() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $object = $repository->findOneBy(['age' => 34]); $this->assertInstanceOf(DummyHash::class, $object); @@ -145,8 +145,7 @@ public function testGetPropertyValue() static::generateIndex(); $collection = static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $value = $repository->getPropertyValue(1, 'createdAt'); $value1 = $repository->getPropertyValue(2, 'createdAt'); @@ -168,8 +167,7 @@ public function testGetPropertyValueNotScalarType() static::generateIndex(); $collection = static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $this->expectException(BadPropertyException::class); $repository->getPropertyValue(1, 'bar'); @@ -181,8 +179,7 @@ public function testGetPropertyValuePropertyDoesNotExist() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $this->expectException(BadPropertyException::class); $repository->getPropertyValue(1, 'test'); @@ -194,8 +191,7 @@ public function testFindByNestedObjectProperty() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $collection = $repository->findBy(['bar_title' => 'Hello']); foreach ($collection as $dummy) { @@ -210,8 +206,7 @@ public function testFindByNestedObjecId() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $collection = $repository->findBy(['bar_id' => 2]); foreach ($collection as $dummy) { @@ -226,8 +221,7 @@ public function testFindOneByNestedObjectId() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); $object = $repository->findOneBy(['bar_id' => 2]); $this->assertEquals($object->bar->id, 2); diff --git a/tests/Functionnal/Om/Repository/HashModel/HashSpaceCharsRepositoryTest.php b/tests/Functionnal/Om/Repository/HashModel/HashSpaceCharsRepositoryTest.php index 501bc00..af1e78f 100644 --- a/tests/Functionnal/Om/Repository/HashModel/HashSpaceCharsRepositoryTest.php +++ b/tests/Functionnal/Om/Repository/HashModel/HashSpaceCharsRepositoryTest.php @@ -10,14 +10,20 @@ final class HashSpaceCharsRepositoryTest extends RedisAbstractTestCase { + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testFindOneBySpace() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(DummyHashWithSpaceChars::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHashWithSpaceChars::class); + $repository = $this->objectManager->getRepository(DummyHashWithSpaceChars::class); // Update 1 object to set unknown to not null /** @var DummyHashWithSpaceChars $object */ @@ -31,8 +37,7 @@ public function testFindBySpace() static::generateIndex(); static::loadRedisFixtures(DummyHashWithSpaceChars::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHashWithSpaceChars::class); + $repository = $this->objectManager->getRepository(DummyHashWithSpaceChars::class); // Update 1 object to set unknown to not null /** @var DummyHashWithSpaceChars[] $collection */ diff --git a/tests/Functionnal/Om/Repository/HashModel/QueryBuilderTest.php b/tests/Functionnal/Om/Repository/HashModel/QueryBuilderTest.php index 3f53f68..5c416e7 100644 --- a/tests/Functionnal/Om/Repository/HashModel/QueryBuilderTest.php +++ b/tests/Functionnal/Om/Repository/HashModel/QueryBuilderTest.php @@ -10,14 +10,22 @@ final class QueryBuilderTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testCustomQueryOr() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + + $repository = $this->objectManager->getRepository(DummyHash::class); $queryBuilder = $repository->createQueryBuilder(); $queryBuilder->query('@age:{20 | 34}'); @@ -35,8 +43,8 @@ public function testCustomQueryOrBadRequest() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + + $repository = $this->objectManager->getRepository(DummyHash::class); $queryBuilder = $repository->createQueryBuilder(); $queryBuilder->query('@age:{99 | 98}'); @@ -50,8 +58,8 @@ public function testCustomQueryStartWith() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + + $repository = $this->objectManager->getRepository(DummyHash::class); $queryBuilder = $repository->createQueryBuilder(); $queryBuilder->query('@name:{Oli*}'); diff --git a/tests/Functionnal/Om/Repository/HashModel/SearchByDateTest.php b/tests/Functionnal/Om/Repository/HashModel/SearchByDateTest.php index ce7f99f..59e206b 100644 --- a/tests/Functionnal/Om/Repository/HashModel/SearchByDateTest.php +++ b/tests/Functionnal/Om/Repository/HashModel/SearchByDateTest.php @@ -10,14 +10,20 @@ final class SearchByDateTest extends RedisAbstractTestCase { + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testFindOneBy() { static::emptyRedis(); static::generateIndex(); $collection = static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); // An existing date $createdAt = new \DateTime('2022-01-01 00:00:00'); @@ -52,8 +58,7 @@ public function testFindBy() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); // An existing date $createdAt = new \DateTime('2022-01-01 00:00:00'); @@ -74,6 +79,7 @@ public function testFindBy() // A date that does not exist $createdAt = new \DateTime('2000-05-01'); $collection = $repository->findBy(['createdAt' => $createdAt]); + $this->assertEmpty($collection); } @@ -83,12 +89,12 @@ public function testFindOneByWithString() static::generateIndex(); $collection = static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); // An existing date $createdAt = new \DateTime('2022-01-01 00:00:00'); $object = $repository->findOneBy(['createdAt' => '2022-01-01 00:00:00']); + $this->assertInstanceOf(DummyHash::class, $object); $this->assertEquals($object->createdAt, $createdAt); $this->assertEquals($object, $collection[0]); @@ -111,8 +117,7 @@ public function testFindByString() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyHash::class); + $repository = $this->objectManager->getRepository(DummyHash::class); // An existing date $createdAt = new \DateTime('2022-01-01 00:00:00'); diff --git a/tests/Functionnal/Om/Repository/JsonModel/JsonBooleanRepositoryTest.php b/tests/Functionnal/Om/Repository/JsonModel/JsonBooleanRepositoryTest.php index 05cff06..559cf8f 100644 --- a/tests/Functionnal/Om/Repository/JsonModel/JsonBooleanRepositoryTest.php +++ b/tests/Functionnal/Om/Repository/JsonModel/JsonBooleanRepositoryTest.php @@ -10,14 +10,20 @@ final class JsonBooleanRepositoryTest extends RedisAbstractTestCase { + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testFindByEnabled() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $collection = $repository->findBy(['enabled' => true]); foreach ($collection as $dummy) { @@ -32,8 +38,7 @@ public function testFindOneByEnabled() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $objet = $repository->findOneBy(['enabled' => true]); $this->assertTrue($objet->enabled); @@ -45,8 +50,7 @@ public function testFindByDisabled() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $collection = $repository->findBy(['enabled' => false]); foreach ($collection as $dummy) { @@ -61,8 +65,7 @@ public function testFindOneByDisabled() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $objet = $repository->findOneBy(['enabled' => false]); $this->assertFalse($objet->enabled); diff --git a/tests/Functionnal/Om/Repository/JsonModel/JsonFindLikeTest.php b/tests/Functionnal/Om/Repository/JsonModel/JsonFindLikeTest.php index 7016063..3041aed 100644 --- a/tests/Functionnal/Om/Repository/JsonModel/JsonFindLikeTest.php +++ b/tests/Functionnal/Om/Repository/JsonModel/JsonFindLikeTest.php @@ -11,14 +11,21 @@ final class JsonFindLikeTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testFindByLike() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $collection = $repository->findByLike(['name' => 'Oli']); foreach ($collection as $dummy) { @@ -33,8 +40,7 @@ public function testFindOneByLike() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $object = $repository->findOneByLike(['name' => 'Oli']); $this->assertTrue(str_contains($object->name, 'Oli')); @@ -46,8 +52,7 @@ public function testFindByLikeEndWith() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $collection = $repository->findByLike(['name' => 'vier']); foreach ($collection as $dummy) { @@ -62,8 +67,7 @@ public function testFindOneByLikeEndWith() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $object = $repository->findOneByLike(['name' => 'vier']); $this->assertTrue(str_contains($object->name, 'vier')); diff --git a/tests/Functionnal/Om/Repository/JsonModel/JsonIdentifierRepositoryTest.php b/tests/Functionnal/Om/Repository/JsonModel/JsonIdentifierRepositoryTest.php index 160bb9d..fc4ef4f 100644 --- a/tests/Functionnal/Om/Repository/JsonModel/JsonIdentifierRepositoryTest.php +++ b/tests/Functionnal/Om/Repository/JsonModel/JsonIdentifierRepositoryTest.php @@ -10,14 +10,21 @@ final class JsonIdentifierRepositoryTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testFindById() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $collection = $repository->findBy(['id' => 1]); foreach ($collection as $dummy) { @@ -32,8 +39,7 @@ public function testFindOneById() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $objet = $repository->findOneBy(['id' => 2]); $this->assertEquals($objet->id, 2); diff --git a/tests/Functionnal/Om/Repository/JsonModel/JsonNullableRepositoryTest.php b/tests/Functionnal/Om/Repository/JsonModel/JsonNullableRepositoryTest.php index fa93a8c..9ddfa79 100644 --- a/tests/Functionnal/Om/Repository/JsonModel/JsonNullableRepositoryTest.php +++ b/tests/Functionnal/Om/Repository/JsonModel/JsonNullableRepositoryTest.php @@ -10,21 +10,28 @@ final class JsonNullableRepositoryTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testFindByNull() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(DummyJsonWithNullProperties::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJsonWithNullProperties::class); + $repository = $this->objectManager->getRepository(DummyJsonWithNullProperties::class); // Update 1 object to set unknown to not null /** @var DummyJsonWithNullProperties $object */ $object = $repository->findOneBy(['name' => 'Kevin']); $object->unknown = 'Not null'; - $objectManager->persist($object); - $objectManager->flush(); + $this->objectManager->persist($object); + $this->objectManager->flush(); $collection = $repository->findBy(['unknown' => null]); foreach ($collection as $dummy) { @@ -39,15 +46,14 @@ public function testFindByNotNull() static::generateIndex(); static::loadRedisFixtures(DummyJsonWithNullProperties::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJsonWithNullProperties::class); + $repository = $this->objectManager->getRepository(DummyJsonWithNullProperties::class); // Update 1 object to set unknown to not null /** @var DummyJsonWithNullProperties $object */ $object = $repository->findOneBy(['name' => 'Kevin']); $object->unknown = 'Notnull'; - $objectManager->persist($object); - $objectManager->flush(); + $this->objectManager->persist($object); + $this->objectManager->flush(); $collection = $repository->findBy(['unknown' => 'Notnull']); foreach ($collection as $dummy) { diff --git a/tests/Functionnal/Om/Repository/JsonModel/JsonRepositoryTest.php b/tests/Functionnal/Om/Repository/JsonModel/JsonRepositoryTest.php index 6328655..df24916 100644 --- a/tests/Functionnal/Om/Repository/JsonModel/JsonRepositoryTest.php +++ b/tests/Functionnal/Om/Repository/JsonModel/JsonRepositoryTest.php @@ -11,14 +11,21 @@ final class JsonRepositoryTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testFindAll() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $collection = $repository->findAll(); $this->assertCount(3, $collection); @@ -33,8 +40,7 @@ public function testFindBy() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $collection = $repository->findBy(['name' => 'Olivier']); @@ -51,8 +57,7 @@ public function testFindByTypo() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $collection = $repository->findBy(['name' => 'Lolivier']); $this->assertEmpty($collection); @@ -64,8 +69,7 @@ public function testFindByOrder() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $collection = $repository->findBy(['name' => 'Olivier'], ['age' => 'ASC']); $this->assertCount(2, $collection); @@ -96,8 +100,7 @@ public function testFindByMultiCriterias() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $collection = $repository->findBy(['name' => 'Olivier', 'age' => 34]); @@ -115,8 +118,7 @@ public function testFindOneBy() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $object = $repository->findOneBy(['age' => 34]); $this->assertInstanceOf(DummyJson::class, $object); @@ -129,8 +131,7 @@ public function testFindLikeJson() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $collection = $repository->findLike('olivier'); @@ -147,8 +148,7 @@ public function testGetPropertyValue() static::generateIndex(); $collection = static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $value = $repository->getPropertyValue(1, 'createdAt'); $value1 = $repository->getPropertyValue(2, 'createdAt'); @@ -179,8 +179,7 @@ public function testGetPropertyValuePropertyDoesNotExist() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $this->expectException(BadPropertyException::class); $repository->getPropertyValue(1, 'test'); @@ -192,8 +191,7 @@ public function testFindByNestedObjectJsonProperty() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $collection = $repository->findBy(['bar_title' => 'Hello']); foreach ($collection as $dummy) { @@ -208,8 +206,7 @@ public function testFindByNestedObjectJsonId() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $collection = $repository->findBy(['bar_id' => 1]); foreach ($collection as $dummy) { @@ -224,8 +221,7 @@ public function testFindOneByNestedObjectJsonId() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + $repository = $this->objectManager->getRepository(DummyJson::class); $object = $repository->findOneBy(['bar_id' => 1]); $this->assertEquals($object->bar->id, 1); diff --git a/tests/Functionnal/Om/Repository/JsonModel/JsonSpaceCharsRepositoryTest.php b/tests/Functionnal/Om/Repository/JsonModel/JsonSpaceCharsRepositoryTest.php index 53c064a..a084123 100644 --- a/tests/Functionnal/Om/Repository/JsonModel/JsonSpaceCharsRepositoryTest.php +++ b/tests/Functionnal/Om/Repository/JsonModel/JsonSpaceCharsRepositoryTest.php @@ -10,14 +10,20 @@ final class JsonSpaceCharsRepositoryTest extends RedisAbstractTestCase { + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testFindOneBySpace() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(DummyJsonWithSpaceChars::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJsonWithSpaceChars::class); + $repository = $this->objectManager->getRepository(DummyJsonWithSpaceChars::class); // Update 1 object to set unknown to not null /** @var DummyJsonWithSpaceChars $object */ @@ -31,8 +37,7 @@ public function testFindBySpace() static::generateIndex(); static::loadRedisFixtures(DummyJsonWithSpaceChars::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJsonWithSpaceChars::class); + $repository = $this->objectManager->getRepository(DummyJsonWithSpaceChars::class); // Update 1 object to set unknown to not null /** @var DummyJsonWithSpaceChars[] $collection */ diff --git a/tests/Functionnal/Om/Repository/JsonModel/QueryBuilderTest.php b/tests/Functionnal/Om/Repository/JsonModel/QueryBuilderTest.php index 5c11cd2..ee90876 100644 --- a/tests/Functionnal/Om/Repository/JsonModel/QueryBuilderTest.php +++ b/tests/Functionnal/Om/Repository/JsonModel/QueryBuilderTest.php @@ -10,14 +10,21 @@ final class QueryBuilderTest extends RedisAbstractTestCase { + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testCustomQueryOr() { static::emptyRedis(); static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + + $repository = $this->objectManager->getRepository(DummyJson::class); $queryBuilder = $repository->createQueryBuilder(); $queryBuilder->query('@age:{20|34}'); @@ -35,8 +42,8 @@ public function testCustomQueryOrBadRequest() static::generateIndex(); static::loadRedisFixtures(); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + + $repository = $this->objectManager->getRepository(DummyJson::class); $queryBuilder = $repository->createQueryBuilder(); $queryBuilder->query('@age:{99 | 98}'); @@ -51,8 +58,8 @@ public function testCustomQueryStartWith() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + + $repository = $this->objectManager->getRepository(DummyJson::class); $queryBuilder = $repository->createQueryBuilder(); $queryBuilder->query('@name:{Oli*}'); diff --git a/tests/Functionnal/Om/Repository/JsonModel/SearchByDateTest.php b/tests/Functionnal/Om/Repository/JsonModel/SearchByDateTest.php index 0dc3524..90fb0dc 100644 --- a/tests/Functionnal/Om/Repository/JsonModel/SearchByDateTest.php +++ b/tests/Functionnal/Om/Repository/JsonModel/SearchByDateTest.php @@ -10,14 +10,21 @@ final class SearchByDateTest extends RedisAbstractTestCase { + private RedisObjectManager $objectManager; + protected function setUp(): void + { + parent::setUp(); + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + } + public function testFindOneBy() { static::emptyRedis(); static::generateIndex(); $collection = static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + + $repository = $this->objectManager->getRepository(DummyJson::class); // An existing date $createdAt = new \DateTime('2022-01-01 00:00:00'); @@ -51,8 +58,8 @@ public function testFindBy() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + + $repository = $this->objectManager->getRepository(DummyJson::class); // An existing date $createdAt = new \DateTime('2022-01-01 00:00:00'); @@ -82,8 +89,8 @@ public function testFindOneByWithString() static::generateIndex(); $collection = static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + + $repository = $this->objectManager->getRepository(DummyJson::class); // An existing date $createdAt = new \DateTime('2022-01-01 00:00:00'); @@ -110,8 +117,8 @@ public function testFindByString() static::generateIndex(); static::loadRedisFixtures(DummyJson::class); - $objectManager = new RedisObjectManager(); - $repository = $objectManager->getRepository(DummyJson::class); + + $repository = $this->objectManager->getRepository(DummyJson::class); // An existing date $createdAt = new \DateTime('2022-01-01 00:00:00'); diff --git a/tests/Metadata/MetadataTest.php b/tests/Metadata/MetadataTest.php index 95478c6..ccef098 100644 --- a/tests/Metadata/MetadataTest.php +++ b/tests/Metadata/MetadataTest.php @@ -10,10 +10,18 @@ class MetadataTest extends RedisAbstractTestCase { + + private RedisObjectManager $objectManager; + protected function setUp(): void + { + $this->objectManager = new RedisObjectManager(RedisAbstractTestCase::createClient()); + parent::setUp(); + } + public function testGetClassMetadataHasAssociation() { - $objectManager = new RedisObjectManager(); - $classMetadata = $objectManager->getClassMetadata(DummyHash::class); + + $classMetadata = $this->objectManager->getClassMetadata(DummyHash::class); $this->assertEquals(['id'], $classMetadata->getIdentifier()); $this->assertCount(11, $classMetadata->fieldsMapping); diff --git a/tests/RedisAbstractTestCase.php b/tests/RedisAbstractTestCase.php index 7e351bc..6642bda 100644 --- a/tests/RedisAbstractTestCase.php +++ b/tests/RedisAbstractTestCase.php @@ -31,7 +31,7 @@ public static function generateIndex(): void public static function loadRedisFixtures(?string $dummyClass = DummyHash::class, ?bool $flush = true): array { - $objectManager = new RedisObjectManager(); + $objectManager = new RedisObjectManager(self::createClient()); $dummies = FixturesGenerator::generateDummies($dummyClass); foreach ($dummies as $dummy) { $objectManager->persist($dummy);