Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add boolean converter + tests #62

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Client/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ public function search(string $prefixKey, array $search, array $orderBy, ?string
$criteria .= sprintf('@%s:%s', $property, $value);
}
}

$arguments[] = $criteria;
}

Expand Down
43 changes: 43 additions & 0 deletions src/Om/Converters/BooleanConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Talleu\RedisOm\Om\Converters;

class BooleanConverter implements ConverterInterface
{
/**
* @param boolean|null $data
*/
public function convert($data): ?string
{
if ($data === false) {
return 'false';
} elseif ($data === true) {
return 'true';
} else {
return null;
}
}

public function revert($data, string $type): ?bool
{
if ($data === 'false') {
return false;
} elseif ($data === 'true') {
return true;
} else {
return null;
}
}

public function supportsConversion(string $type, mixed $data): bool
{
return in_array($type, ['boolean', 'bool']);
}

public function supportsReversion(string $type, mixed $value): bool
{
return $type === 'bool';
}
}
2 changes: 2 additions & 0 deletions src/Om/Converters/HashModel/ConverterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Talleu\RedisOm\Om\Converters\HashModel;

use Talleu\RedisOm\Om\Converters\AbstractConverterFactory;
use Talleu\RedisOm\Om\Converters\BooleanConverter;
use Talleu\RedisOm\Om\Converters\ConverterInterface;
use Talleu\RedisOm\Om\Converters\ScalarConverter;

Expand All @@ -20,6 +21,7 @@ protected static function getConvertersCollection(): array
new ArrayConverter(),
new StandardClassConverter(),
new ScalarConverter(),
new BooleanConverter(),
new NullConverter(),
new DateTimeConverter(),
new DateTimeImmutableConverter(),
Expand Down
2 changes: 2 additions & 0 deletions src/Om/Converters/JsonModel/ConverterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Talleu\RedisOm\Om\Converters\JsonModel;

use Talleu\RedisOm\Om\Converters\AbstractConverterFactory;
use Talleu\RedisOm\Om\Converters\BooleanConverter;
use Talleu\RedisOm\Om\Converters\ConverterInterface;
use Talleu\RedisOm\Om\Converters\ScalarConverter;

Expand All @@ -19,6 +20,7 @@ protected static function getConvertersCollection(): array
new JsonObjectConverter(),
new ArrayConverter(),
new ScalarConverter(),
new BooleanConverter(),
new StandardClassConverter(),
new NullConverter(),
new DateTimeConverter(),
Expand Down
8 changes: 3 additions & 5 deletions src/Om/Converters/ScalarConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ScalarConverter implements ConverterInterface
/**
* @param scalar $data
*/
public function convert($data): string
public function convert($data)
{
return (string) $data;
}
Expand All @@ -18,8 +18,6 @@ public function revert($data, string $type)
{
if ($type === 'int') {
return (int) $data;
} elseif ($type === 'bool') {
return (bool) $data;
} elseif ($type === 'float') {
return (float) $data;
}
Expand All @@ -29,11 +27,11 @@ public function revert($data, string $type)

public function supportsConversion(string $type, mixed $data): bool
{
return in_array($type, ['int', 'string', 'double', 'boolean', 'integer', 'float', 'bool', 'null']);
return in_array($type, ['int', 'string', 'double', 'integer', 'float', 'null']);
}

public function supportsReversion(string $type, mixed $value): bool
{
return in_array($type, ['int', 'string', 'float', 'bool']);
return in_array($type, ['int', 'string', 'float']);
}
}
16 changes: 11 additions & 5 deletions src/Om/Repository/AbstractObjectRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ abstract public function getPropertyValue($identifier, string $property): mixed;
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
{
$this->convertDates($criteria);
$this->escapeSpecialCharsInCriterias($criteria);
$this->convertSpecial($criteria);
$data = $this->redisClient->search($this->prefix, $criteria, $orderBy ?? [], $this->format, $limit);

$collection = [];
Expand All @@ -55,7 +55,7 @@ public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = nu
public function findByLike(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
{
$this->convertDates($criteria);
$this->escapeSpecialCharsInCriterias($criteria);
$this->convertSpecial($criteria);
foreach ($criteria as $property => $value) {
$criteria[$property.'_text'] = "*$value*";
unset($criteria[$property]);
Expand Down Expand Up @@ -100,7 +100,7 @@ public function findAll(): array
public function findOneBy(array $criteria, ?array $orderBy = null): ?object
{
$this->convertDates($criteria);
$this->escapeSpecialCharsInCriterias($criteria);
$this->convertSpecial($criteria);
$data = $this->redisClient->search($this->prefix, $criteria, $orderBy ?? [], $this->format, 1);

if ($data === []) {
Expand All @@ -116,7 +116,7 @@ public function findOneBy(array $criteria, ?array $orderBy = null): ?object
public function findOneByLike(array $criteria, ?array $orderBy = null): ?object
{
$this->convertDates($criteria);
$this->escapeSpecialCharsInCriterias($criteria);
$this->convertSpecial($criteria);
foreach ($criteria as $property => $value) {
$criteria[$property.'_text'] = "*$value*";
unset($criteria[$property]);
Expand Down Expand Up @@ -184,9 +184,15 @@ public function setFormat(?string $format = null): void
$this->format = $format ?? RedisFormat::HASH->value;
}

protected function escapeSpecialCharsInCriterias(array|string &$criteria): void
protected function convertSpecial(array|string &$criteria): void
{
foreach ($criteria as $property => $value) {

if (is_bool($value)) {
$criteria[$property] = $value ? 'true' : 'false';
continue;
}

if (!is_string($value)) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/AbstractDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class AbstractDummy
#[RedisOm\Property]
public ?array $infos = [];

#[RedisOm\Property]
#[RedisOm\Property(index: true)]
public ?bool $enabled = null;

#[RedisOm\Property]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Talleu\RedisOm\Tests\Functionnal\Om\Repository\HashModel;

use Talleu\RedisOm\Om\RedisObjectManager;
use Talleu\RedisOm\Tests\Fixtures\Hash\DummyHash;
use Talleu\RedisOm\Tests\RedisAbstractTestCase;

final class HashBooleanRepositoryTest extends RedisAbstractTestCase
{
public function testFindByEnabled()
{
static::emptyRedis();
static::generateIndex();
static::loadRedisFixtures();

$objectManager = new RedisObjectManager();
$repository = $objectManager->getRepository(DummyHash::class);

$collection = $repository->findBy(['enabled' => true]);
foreach ($collection as $dummy) {
$this->assertInstanceOf(DummyHash::class, $dummy);
$this->assertTrue($dummy->enabled);
}
}

public function testFindOneByEnabled()
{
static::emptyRedis();
static::generateIndex();
static::loadRedisFixtures();

$objectManager = new RedisObjectManager();
$repository = $objectManager->getRepository(DummyHash::class);

$objet = $repository->findOneBy(['enabled' => true]);
$this->assertTrue($objet->enabled);
}

public function testFindByDisabled()
{
static::emptyRedis();
static::generateIndex();
static::loadRedisFixtures();

$objectManager = new RedisObjectManager();
$repository = $objectManager->getRepository(DummyHash::class);

$collection = $repository->findBy(['enabled' => false]);
foreach ($collection as $dummy) {
$this->assertInstanceOf(DummyHash::class, $dummy);
$this->assertFalse($dummy->enabled);
}
}

public function testFindOneByDisabled()
{
static::emptyRedis();
static::generateIndex();
static::loadRedisFixtures();

$objectManager = new RedisObjectManager();
$repository = $objectManager->getRepository(DummyHash::class);

$objet = $repository->findOneBy(['enabled' => false]);
$this->assertFalse($objet->enabled);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Talleu\RedisOm\Tests\Functionnal\Om\Repository\JsonModel;

use Talleu\RedisOm\Om\RedisObjectManager;
use Talleu\RedisOm\Tests\Fixtures\Json\DummyJson;
use Talleu\RedisOm\Tests\RedisAbstractTestCase;

final class JsonBooleanRepositoryTest extends RedisAbstractTestCase
{
public function testFindByEnabled()
{
static::emptyRedis();
static::generateIndex();
static::loadRedisFixtures(DummyJson::class);

$objectManager = new RedisObjectManager();
$repository = $objectManager->getRepository(DummyJson::class);

$collection = $repository->findBy(['enabled' => true]);
foreach ($collection as $dummy) {
$this->assertInstanceOf(DummyJson::class, $dummy);
$this->assertTrue($dummy->enabled);
}
}

public function testFindOneByEnabled()
{
static::emptyRedis();
static::generateIndex();
static::loadRedisFixtures(DummyJson::class);

$objectManager = new RedisObjectManager();
$repository = $objectManager->getRepository(DummyJson::class);

$objet = $repository->findOneBy(['enabled' => true]);
$this->assertTrue($objet->enabled);
}

public function testFindByDisabled()
{
static::emptyRedis();
static::generateIndex();
static::loadRedisFixtures(DummyJson::class);

$objectManager = new RedisObjectManager();
$repository = $objectManager->getRepository(DummyJson::class);

$collection = $repository->findBy(['enabled' => false]);
foreach ($collection as $dummy) {
$this->assertInstanceOf(DummyJson::class, $dummy);
$this->assertFalse($dummy->enabled);
}
}

public function testFindOneByDisabled()
{
static::emptyRedis();
static::generateIndex();
static::loadRedisFixtures(DummyJson::class);

$objectManager = new RedisObjectManager();
$repository = $objectManager->getRepository(DummyJson::class);

$objet = $repository->findOneBy(['enabled' => false]);
$this->assertFalse($objet->enabled);
}
}