This commit is contained in:
Daniil Gentili 2024-03-28 19:25:49 +01:00
parent 9784a2e1cf
commit cc61cec062
3 changed files with 64 additions and 16 deletions

View File

@ -18,7 +18,7 @@
namespace danog\AsyncOrm\Driver;
use ArrayObject;
use ArrayIterator;
use danog\AsyncOrm\DbArray;
use danog\AsyncOrm\FieldConfig;
use danog\AsyncOrm\Settings\Database\Memory;
@ -78,7 +78,7 @@ final class MemoryArray extends DbArray
public function getIterator(): \Traversable
{
return new ArrayObject($this);
return new ArrayIterator($this->data);
}
public function getArrayCopy(): array

View File

@ -25,6 +25,7 @@ use danog\AsyncOrm\Driver\DriverArray;
use danog\AsyncOrm\FieldConfig;
use danog\AsyncOrm\Internal\Serializer\IntString;
use danog\AsyncOrm\Internal\Serializer\Passthrough;
use danog\AsyncOrm\KeyType;
use danog\AsyncOrm\Serializer;
use danog\AsyncOrm\Settings\Redis;
use danog\AsyncOrm\ValueType;
@ -48,6 +49,7 @@ final class RedisArray extends DriverArray
private static ?LocalKeyedMutex $mutex = null;
private readonly RedisClient $db;
private readonly bool $castToInt;
/**
* @param Serializer<TValue> $serializer
@ -60,6 +62,7 @@ final class RedisArray extends DriverArray
ValueType::STRING => new Passthrough,
default => $serializer
};
$this->castToInt = $config->keyType === KeyType::INT;
parent::__construct($config, $serializer);
self::$mutex ??= new LocalKeyedMutex;
@ -147,7 +150,11 @@ final class RedisArray extends DriverArray
$len = \strlen($this->rKey(''));
foreach ($request as $key) {
yield \substr($key, $len) => $this->serializer->deserialize($this->db->get($key));
$sub = \substr($key, $len);
if ($this->castToInt) {
$sub = (int) $sub;
}
yield $sub => $this->serializer->deserialize($this->db->get($key));
}
}

View File

@ -146,7 +146,7 @@ final class OrmTest extends TestCase
public function testBasic(Settings $settings, KeyType $keyType, string|int $key): void
{
$field = new FieldConfig(
__METHOD__,
'testBasic',
$settings,
$keyType,
ValueType::INT
@ -176,6 +176,7 @@ final class OrmTest extends TestCase
$orm = $field->build();
$orm[$key] = 124;
$this->assertCount(1, $orm);
$this->assertCount(1, $orm);
$this->assertSame(124, $orm[$key]);
$this->assertTrue(isset($orm[$key]));
@ -222,7 +223,7 @@ final class OrmTest extends TestCase
public function testMigration(Settings $settings): void
{
$field = new FieldConfig(
__METHOD__,
'testMigration',
$settings,
KeyType::STRING_OR_INT,
ValueType::INT
@ -232,13 +233,55 @@ final class OrmTest extends TestCase
$this->assertSame(123, $orm[321]);
$this->assertTrue(isset($orm[321]));
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
$this->assertSame($orm instanceof MemoryArray ? 321 : "321", $kk);
$this->assertSame(123, $vv);
}
$this->assertEquals(1, $cnt);
if ($orm instanceof MemoryArray) {
return;
}
$field = new FieldConfig(
__METHOD__,
'testMigration',
$settings,
KeyType::INT,
ValueType::INT
);
$orm = $field->build();
$this->assertSame(123, $orm[321]);
$this->assertTrue(isset($orm[321]));
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
$this->assertSame(321, $kk);
$this->assertSame(123, $vv);
}
$this->assertEquals(1, $cnt);
$field = new FieldConfig(
'testMigration',
$settings,
KeyType::STRING,
ValueType::INT
);
$orm = $field->build();
$this->assertSame(123, $orm[321]);
$this->assertTrue(isset($orm[321]));
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
$this->assertSame('321', $kk);
$this->assertSame(123, $vv);
}
$this->assertEquals(1, $cnt);
$field = new FieldConfig(
'testMigration',
$settings,
KeyType::INT,
ValueType::INT
@ -247,15 +290,13 @@ final class OrmTest extends TestCase
$this->assertSame(123, $orm[321]);
$this->assertTrue(isset($orm[321]));
$field = new FieldConfig(
__METHOD__,
$settings,
KeyType::STRING,
ValueType::INT
);
$orm = $field->build();
$this->assertSame(123, $orm[321]);
$this->assertTrue(isset($orm[321]));
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
$this->assertSame(321, $kk);
$this->assertSame(123, $vv);
}
$this->assertEquals(1, $cnt);
}
public static function provideSettingsKeys(): \Generator
@ -274,7 +315,7 @@ final class OrmTest extends TestCase
yield [
$settings,
KeyType::STRING,
4321,
'4321',
];
yield [
$settings,