diff --git a/src/Driver/MemoryArray.php b/src/Driver/MemoryArray.php index 2771914..75a6e7b 100644 --- a/src/Driver/MemoryArray.php +++ b/src/Driver/MemoryArray.php @@ -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 diff --git a/src/Internal/Driver/RedisArray.php b/src/Internal/Driver/RedisArray.php index 24508ba..5390dea 100644 --- a/src/Internal/Driver/RedisArray.php +++ b/src/Internal/Driver/RedisArray.php @@ -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 $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)); } } diff --git a/tests/OrmTest.php b/tests/OrmTest.php index a02a65e..083dd6a 100644 --- a/tests/OrmTest.php +++ b/tests/OrmTest.php @@ -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,