From 4ac4ff9aa4fee9a3de1f26201ce8ee420a6a07aa Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 31 Mar 2024 20:56:18 +0200 Subject: [PATCH] More coverage --- src/DbAutoProperties.php | 24 +++++++--- src/Driver/DriverArray.php | 2 + src/Internal/Driver/PostgresArray.php | 2 + tests/OrmTest.php | 68 +++++++++++++++++++++++++++ tests/TestObject.php | 15 ++++++ 5 files changed, 105 insertions(+), 6 deletions(-) diff --git a/src/DbAutoProperties.php b/src/DbAutoProperties.php index 0c5f62b..24284f0 100644 --- a/src/DbAutoProperties.php +++ b/src/DbAutoProperties.php @@ -44,6 +44,20 @@ trait DbAutoProperties /** @var list */ private array $properties = []; + /** @return list<\ReflectionProperty> */ + private function getDbAutoProperties(): array + { + $res = []; + foreach ((new ReflectionClass(static::class))->getProperties() as $property) { + $attr = $property->getAttributes(OrmMappedArray::class); + if (!$attr) { + continue; + } + $res []= $property; + } + return $res; + } + /** * Initialize database properties. */ @@ -51,17 +65,15 @@ trait DbAutoProperties { $this->properties = []; $promises = []; - foreach ((new ReflectionClass(static::class))->getProperties() as $property) { + foreach ($this->getDbAutoProperties() as $property) { $attr = $property->getAttributes(OrmMappedArray::class); - if (!$attr) { - continue; - } + \assert(\count($attr) !== 0); $attr = $attr[0]->newInstance(); if ($settings instanceof DriverSettings) { $ttl = $attr->cacheTtl ?? $settings->cacheTtl; if ($ttl !== $settings->cacheTtl) { - $settings = new $settings(\array_merge( + $settings = new $settings(...\array_merge( (array) $settings, ['cacheTtl' => $ttl] )); @@ -70,7 +82,7 @@ trait DbAutoProperties $optimize = $attr->optimizeIfWastedMb ?? $settings->optimizeIfWastedMb; if ($optimize !== $settings->optimizeIfWastedMb) { - $settings = new $settings(\array_merge( + $settings = new $settings(...\array_merge( (array) $settings, ['optimizeIfWastedMb' => $optimize] )); diff --git a/src/Driver/DriverArray.php b/src/Driver/DriverArray.php index bbdc1f2..cac1bb0 100644 --- a/src/Driver/DriverArray.php +++ b/src/Driver/DriverArray.php @@ -98,8 +98,10 @@ abstract class DriverArray extends DbArray $promises []= async($previous->unset(...), $key) ->map(static fn () => $instance->set($key, $value)); if (\count($promises) % 500 === 0) { + // @codeCoverageIgnoreStart await($promises); $promises = []; + // @codeCoverageIgnoreEnd } } if ($promises) { diff --git a/src/Internal/Driver/PostgresArray.php b/src/Internal/Driver/PostgresArray.php index 689d826..645ca28 100644 --- a/src/Internal/Driver/PostgresArray.php +++ b/src/Internal/Driver/PostgresArray.php @@ -130,8 +130,10 @@ class PostgresArray extends SqlArray } elseif ($key === 'value') { $expected = $valueType; } else { + // @codeCoverageIgnoreStart $connection->query("ALTER TABLE \"bytea_{$config->table}\" DROP \"$key\""); continue; + // @codeCoverageIgnoreEnd } if ($expected !== $type) { if ($expected === 'BIGINT') { diff --git a/tests/OrmTest.php b/tests/OrmTest.php index 730fd6b..6d3814b 100644 --- a/tests/OrmTest.php +++ b/tests/OrmTest.php @@ -33,6 +33,7 @@ use AssertionError; use danog\AsyncOrm\DbArrayBuilder; use danog\AsyncOrm\DbObject; use danog\AsyncOrm\Driver\MemoryArray; +use danog\AsyncOrm\Internal\Containers\CacheContainer; use danog\AsyncOrm\Internal\Driver\CachedArray; use danog\AsyncOrm\Internal\Driver\ObjectArray; use danog\AsyncOrm\KeyType; @@ -379,6 +380,27 @@ final class OrmTest extends TestCase $this->assertEquals(1, $cnt); $this->assertCount(0, $old); + + $field = new DbArrayBuilder( + $table.'_new', + new MemorySettings, + KeyType::INT, + ValueType::INT + ); + $old = $orm; + $orm = $field->build($old); + $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); + + $this->assertCount(1, $old); } #[DataProvider('provideSettings')] @@ -486,6 +508,12 @@ final class OrmTest extends TestCase $field->build()->clear(); } + public function testException(): void + { + $this->expectExceptionMessage("Cannot save an uninitialized object!"); + (new TestObject)->save(); + } + public function testCache(): void { $field = new DbArrayBuilder("testCache", new RedisSettings( @@ -505,7 +533,47 @@ final class OrmTest extends TestCase $this->assertCount(0, $ormUnCached); delay(0.9); $this->assertCount(1, $ormUnCached); + delay(1.0); + /** @var CacheContainer */ + $c = (new ReflectionProperty(CachedArray::class, 'cache'))->getValue($orm); + $this->assertCount(0, (new ReflectionProperty(CacheContainer::class, 'cache'))->getValue($c)); + + $f1 = async($orm->get(...), 0); + $f2 = async($orm->get(...), 0); + $this->assertSame(1, $f1->await()); + $this->assertSame(1, $f2->await()); + $orm->clear(); + + $obj = new TestObject; + $obj->initDbProperties(new RedisSettings( + RedisConfig::fromUri("redis://127.0.0.1"), + cacheTtl: 1 + ), 'testCacheMore_'); + + $fieldNoCache2 = new DbArrayBuilder("testCacheMore_arr2", new RedisSettings( + RedisConfig::fromUri("redis://127.0.0.1"), + cacheTtl: 0 + ), KeyType::INT, ValueType::INT); + $orm2Uncached = $fieldNoCache2->build(); + + $fieldNoCache4 = new DbArrayBuilder("testCacheMore_arr4", new RedisSettings( + RedisConfig::fromUri("redis://127.0.0.1"), + cacheTtl: 0 + ), KeyType::INT, ValueType::INT); + $orm4Uncached = $fieldNoCache4->build(); + + $obj->arr2->set(0, 1); + $this->assertCount(0, $orm2Uncached); + delay(0.1); + $this->assertCount(0, $orm2Uncached); + delay(0.9); + $this->assertCount(1, $orm2Uncached); + $orm2Uncached->clear(); + + $obj->arr4->set(0, 1); + $this->assertCount(1, $orm4Uncached); + $orm4Uncached->clear(); } public static function provideSettingsKeysValues(): \Generator diff --git a/tests/TestObject.php b/tests/TestObject.php index a1c3800..acd2440 100644 --- a/tests/TestObject.php +++ b/tests/TestObject.php @@ -52,6 +52,21 @@ final class TestObject extends DbObject )] public DbArray $arr2; + #[OrmMappedArray( + KeyType::INT, + ValueType::INT, + cacheTtl: 0, + )] + public DbArray $arr3; + + #[OrmMappedArray( + KeyType::INT, + ValueType::INT, + cacheTtl: 0, + optimizeIfWastedMb: 0 + )] + public DbArray $arr4; + public function __sleep() { return ['savedProp', 'arr'];