AsyncOrm/tests/OrmTest.php

729 lines
23 KiB
PHP
Raw Normal View History

2024-03-28 14:57:59 +01:00
<?php declare(strict_types=1);
/**
2024-03-31 18:28:23 +02:00
* Copyright 2024 Daniil Gentili.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2024-03-28 14:57:59 +01:00
*
* @author Daniil Gentili <daniil@daniil.it>
* @copyright 2016-2024 Daniil Gentili <daniil@daniil.it>
2024-03-31 18:28:23 +02:00
* @license https://opensource.org/license/apache-2-0 Apache 2.0
2024-03-31 19:37:48 +02:00
* @link https://github.com/danog/AsyncOrm AsyncOrm documentation
2024-03-28 14:57:59 +01:00
*/
2024-03-31 17:13:03 +02:00
namespace danog\TestAsyncOrm;
2024-03-28 14:57:59 +01:00
use Amp\ByteStream\ReadableStream;
use Amp\Mysql\MysqlConfig;
use Amp\Postgres\PostgresConfig;
use Amp\Process\Process;
use Amp\Redis\RedisConfig;
use AssertionError;
2024-03-31 19:38:58 +02:00
use danog\AsyncOrm\DbArrayBuilder;
2024-03-28 20:55:45 +01:00
use danog\AsyncOrm\DbObject;
2024-03-28 19:00:38 +01:00
use danog\AsyncOrm\Driver\MemoryArray;
2024-03-31 20:56:18 +02:00
use danog\AsyncOrm\Internal\Containers\CacheContainer;
2024-03-31 21:36:36 +02:00
use danog\AsyncOrm\Internal\Containers\ObjectContainer;
2024-03-28 23:04:40 +01:00
use danog\AsyncOrm\Internal\Driver\CachedArray;
2024-03-28 20:16:31 +01:00
use danog\AsyncOrm\Internal\Driver\ObjectArray;
2024-03-28 14:57:59 +01:00
use danog\AsyncOrm\KeyType;
use danog\AsyncOrm\Serializer\Igbinary;
use danog\AsyncOrm\Serializer\Json;
use danog\AsyncOrm\Serializer\Native;
use danog\AsyncOrm\Settings;
2024-03-28 20:32:20 +01:00
use danog\AsyncOrm\Settings\DriverSettings;
2024-03-31 17:13:03 +02:00
use danog\AsyncOrm\Settings\MemorySettings;
use danog\AsyncOrm\Settings\MysqlSettings;
use danog\AsyncOrm\Settings\PostgresSettings;
use danog\AsyncOrm\Settings\RedisSettings;
2024-03-28 14:57:59 +01:00
use danog\AsyncOrm\ValueType;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
2024-03-28 20:32:20 +01:00
use ReflectionProperty;
2024-04-06 19:45:35 +02:00
use Revolt\EventLoop;
2024-03-28 21:08:03 +01:00
use WeakReference;
2024-03-28 14:57:59 +01:00
use function Amp\async;
use function Amp\ByteStream\buffer;
use function Amp\ByteStream\getStderr;
use function Amp\ByteStream\getStdout;
use function Amp\ByteStream\pipe;
use function Amp\ByteStream\splitLines;
2024-03-31 17:05:51 +02:00
use function Amp\delay;
2024-03-28 14:57:59 +01:00
use function Amp\Future\await;
use function Amp\Future\awaitAny;
final class OrmTest extends TestCase
{
/** @var array<string, Process> */
private static array $processes = [];
private static function shellExec(string $cmd): void
{
$process = Process::start($cmd);
async(pipe(...), $process->getStderr(), getStderr());
async(pipe(...), $process->getStdout(), getStdout());
$process->join();
}
2024-03-28 22:40:41 +01:00
private static bool $configured = false;
2024-03-28 14:57:59 +01:00
public static function setUpBeforeClass(): void
{
2024-03-28 22:45:03 +01:00
\touch('/tmp/async-orm-test');
$lockFile = \fopen('/tmp/async-orm-test', 'r+');
\flock($lockFile, LOCK_EX);
if (\fgets($lockFile) === 'done') {
\flock($lockFile, LOCK_UN);
2024-03-28 22:40:41 +01:00
return;
}
self::$configured = true;
2024-03-28 22:45:03 +01:00
\fwrite($lockFile, "done\n");
2024-03-28 22:40:41 +01:00
2024-03-28 14:57:59 +01:00
$f = [];
foreach (['redis' => 6379, 'mariadb' => 3306, 'postgres' => 5432] as $image => $port) {
$f []= async(function () use ($image, $port) {
2024-03-28 18:24:41 +01:00
self::shellExec("docker rm -f test_$image 2>/dev/null");
2024-03-28 14:57:59 +01:00
$args = match ($image) {
'postgres' => '-e POSTGRES_HOST_AUTH_METHOD=trust',
'mariadb' => '-e MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=1',
default => ''
};
$process = Process::start(
"docker run --rm -p $port:$port $args --name test_$image $image"
);
self::$processes[$image] = $process;
});
}
await($f);
if (!self::$processes) {
throw new AssertionError("No processes!");
}
foreach (self::$processes as $name => $process) {
$ok = awaitAny([
async(self::waitForStartup(...), $process->getStdout()),
async(self::waitForStartup(...), $process->getStderr()),
]);
if (!$ok) {
throw new AssertionError("Could not start $name!");
}
}
2024-03-28 22:40:41 +01:00
2024-03-28 22:45:03 +01:00
\flock($lockFile, LOCK_UN);
2024-03-28 22:40:41 +01:00
}
public static function tearDownAfterClass(): void
{
if (self::$configured) {
2024-03-28 22:45:03 +01:00
\unlink('/tmp/async-orm-test');
2024-03-28 22:40:41 +01:00
}
2024-03-28 14:57:59 +01:00
}
private static function waitForStartup(ReadableStream $f): bool
{
foreach (splitLines($f) as $line) {
if (\stripos($line, 'ready to ') !== false
|| \stripos($line, "socket: '/run/mysqld/mysqld.sock' port: 3306") !== false
) {
async(buffer(...), $f);
return true;
}
}
return false;
}
2024-03-28 20:55:45 +01:00
public function assertSameNotObject(mixed $a, mixed $b): void
{
if ($b instanceof DbObject) {
$this->assertSame($a::class, $b::class);
} else {
$this->assertSame($a, $b);
}
}
2024-03-28 19:53:01 +01:00
2024-03-28 19:43:21 +01:00
#[DataProvider('provideSettingsKeysValues')]
2024-03-28 22:40:41 +01:00
public function testBasic(int $tablePostfix, Settings $settings, KeyType $keyType, string|int $key, ValueType $valueType, mixed $value): void
2024-03-28 14:57:59 +01:00
{
2024-03-31 19:11:42 +02:00
$field = new DbArrayBuilder(
2024-03-28 22:40:41 +01:00
"testBasic_$tablePostfix",
2024-03-28 14:57:59 +01:00
$settings,
$keyType,
2024-03-28 19:43:21 +01:00
$valueType
2024-03-28 14:57:59 +01:00
);
$orm = $field->build();
2024-03-28 19:43:21 +01:00
$orm[$key] = $value;
2024-03-28 14:57:59 +01:00
2024-03-28 23:21:13 +01:00
[$a, $b] = await([
async($orm->get(...), $key),
async($orm->get(...), $key),
]);
$this->assertSameNotObject($value, $a);
$this->assertSameNotObject($value, $b);
2024-03-28 20:55:45 +01:00
$this->assertSameNotObject($value, $orm[$key]);
2024-03-28 14:57:59 +01:00
$this->assertTrue(isset($orm[$key]));
2024-03-28 20:55:45 +01:00
if (!$value instanceof DbObject) {
$this->assertSameNotObject([$key => $value], $orm->getArrayCopy());
}
2024-03-28 14:57:59 +01:00
unset($orm[$key]);
$this->assertNull($orm[$key]);
$this->assertFalse(isset($orm[$key]));
2024-03-28 18:34:35 +01:00
2024-03-28 19:00:38 +01:00
if ($orm instanceof CachedArray) {
$orm->flushCache();
}
$this->assertCount(0, $orm);
$this->assertNull($orm[$key]);
$this->assertFalse(isset($orm[$key]));
2024-03-28 20:55:45 +01:00
if (!$value instanceof DbObject) {
$this->assertSameNotObject([], $orm->getArrayCopy());
}
2024-03-28 19:00:38 +01:00
if ($orm instanceof MemoryArray) {
2024-03-28 19:30:19 +01:00
$orm->clear();
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
}
$this->assertEquals(0, $cnt);
$this->assertCount(0, $orm);
2024-03-28 19:00:38 +01:00
return;
}
2024-03-28 18:34:35 +01:00
$orm = $field->build();
2024-03-28 19:43:21 +01:00
$orm[$key] = $value;
2024-03-28 18:34:35 +01:00
2024-03-28 19:25:49 +01:00
$this->assertCount(1, $orm);
2024-03-28 20:55:45 +01:00
$this->assertSameNotObject($value, $orm[$key]);
2024-03-28 18:34:35 +01:00
$this->assertTrue(isset($orm[$key]));
2024-03-28 19:00:38 +01:00
if ($orm instanceof CachedArray) {
$orm->flushCache();
}
2024-03-28 18:34:35 +01:00
unset($orm);
while (\gc_collect_cycles());
$orm = $field->build();
2024-03-28 20:55:45 +01:00
$this->assertSameNotObject($value, $orm[$key]);
2024-03-28 18:34:35 +01:00
$this->assertTrue(isset($orm[$key]));
unset($orm[$key]);
$this->assertNull($orm[$key]);
$this->assertFalse(isset($orm[$key]));
2024-03-28 19:00:38 +01:00
if ($orm instanceof CachedArray) {
$orm->flushCache();
}
$this->assertCount(0, $orm);
2024-03-28 19:43:21 +01:00
$orm[$key] = $value;
2024-03-28 19:00:38 +01:00
$this->assertCount(1, $orm);
2024-03-28 23:21:13 +01:00
$orm[$key] = $value;
$this->assertCount(1, $orm);
2024-03-28 19:00:38 +01:00
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
2024-03-28 20:55:45 +01:00
$this->assertSameNotObject($key, $kk);
$this->assertSameNotObject($value, $vv);
2024-03-28 19:00:38 +01:00
}
$this->assertEquals(1, $cnt);
$orm->clear();
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
}
$this->assertEquals(0, $cnt);
$this->assertCount(0, $orm);
2024-03-29 19:39:04 +01:00
// Test that db is flushed on __destruct
$orm = $field->build();
$orm[$key] = $value;
unset($orm);
2024-03-31 20:57:51 +02:00
delay(0.1);
2024-03-29 19:39:04 +01:00
$orm = $field->build();
$this->assertCount(1, $orm);
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
$this->assertSameNotObject($key, $kk);
$this->assertSameNotObject($value, $vv);
}
$this->assertEquals(1, $cnt);
$orm->clear();
2024-03-28 18:34:35 +01:00
}
#[DataProvider('provideSettings')]
2024-03-28 22:40:41 +01:00
public function testKeyMigration(int $tablePostfix, Settings $settings): void
2024-03-28 18:34:35 +01:00
{
2024-03-31 19:11:42 +02:00
$field = new DbArrayBuilder(
2024-03-28 22:40:41 +01:00
$table = 'testKeyMigration_'.$tablePostfix,
2024-03-28 18:34:35 +01:00
$settings,
KeyType::STRING_OR_INT,
ValueType::INT
);
$orm = $field->build();
$orm[321] = 123;
2024-03-28 19:00:38 +01:00
$this->assertSame(123, $orm[321]);
2024-03-28 18:34:35 +01:00
$this->assertTrue(isset($orm[321]));
2024-03-28 19:25:49 +01:00
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
$this->assertSame($orm instanceof MemoryArray ? 321 : "321", $kk);
$this->assertSame(123, $vv);
}
$this->assertEquals(1, $cnt);
2024-03-28 18:34:35 +01:00
2024-03-28 19:00:38 +01:00
if ($orm instanceof MemoryArray) {
return;
}
2024-03-31 19:11:42 +02:00
$field = new DbArrayBuilder(
2024-03-28 22:40:41 +01:00
$table,
2024-03-28 18:34:35 +01:00
$settings,
KeyType::INT,
ValueType::INT
);
$orm = $field->build();
2024-03-28 19:00:38 +01:00
$this->assertSame(123, $orm[321]);
2024-03-28 18:34:35 +01:00
$this->assertTrue(isset($orm[321]));
2024-03-28 19:25:49 +01:00
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
$this->assertSame(321, $kk);
$this->assertSame(123, $vv);
}
$this->assertEquals(1, $cnt);
2024-03-28 18:34:35 +01:00
2024-03-31 19:11:42 +02:00
$field = new DbArrayBuilder(
2024-03-28 22:40:41 +01:00
$table,
2024-03-28 18:34:35 +01:00
$settings,
KeyType::STRING,
ValueType::INT
);
$orm = $field->build();
2024-03-28 19:00:38 +01:00
$this->assertSame(123, $orm[321]);
2024-03-28 18:34:35 +01:00
$this->assertTrue(isset($orm[321]));
2024-03-28 19:25:49 +01:00
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
$this->assertSame('321', $kk);
$this->assertSame(123, $vv);
}
$this->assertEquals(1, $cnt);
2024-03-31 19:11:42 +02:00
$field = new DbArrayBuilder(
2024-03-28 22:40:41 +01:00
$table,
2024-03-28 19:25:49 +01:00
$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);
2024-03-28 23:21:13 +01:00
2024-03-31 19:11:42 +02:00
$field = new DbArrayBuilder(
2024-03-28 23:21:13 +01:00
$table.'_new',
$settings,
KeyType::INT,
ValueType::INT
);
$orm = $field->build($orm);
$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);
2024-03-31 17:05:51 +02:00
2024-03-31 19:11:42 +02:00
$field = new DbArrayBuilder(
2024-03-31 17:05:51 +02:00
$table.'_new',
2024-03-31 17:13:03 +02:00
new MemorySettings,
2024-03-31 17:05:51 +02:00
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(0, $old);
2024-03-31 20:56:18 +02:00
$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);
2024-03-28 18:34:35 +01:00
}
2024-03-28 20:16:31 +01:00
#[DataProvider('provideSettings')]
2024-03-28 22:40:41 +01:00
public function testObject(int $tablePostfix, Settings $settings): void
2024-03-28 20:16:31 +01:00
{
2024-03-28 20:32:20 +01:00
if (!$settings instanceof DriverSettings) {
$this->expectExceptionMessage("Objects can only be saved to a database backend!");
}
if ($settings->serializer instanceof Json) {
$this->expectExceptionMessage("The JSON backend cannot be used when serializing objects!");
}
2024-03-31 19:11:42 +02:00
$field = new DbArrayBuilder(
2024-03-28 22:40:41 +01:00
'testObject_'.$tablePostfix,
2024-03-28 20:16:31 +01:00
$settings,
KeyType::STRING_OR_INT,
ValueType::OBJECT
);
$orm = $field->build();
$this->assertSame(ObjectArray::class, $orm::class);
$obj = new TestObject;
$this->assertSame(0, $obj->loadedCnt);
$this->assertSame(0, $obj->saveAfterCnt);
$this->assertSame(0, $obj->saveBeforeCnt);
$orm[321] = $obj;
$this->assertSame(1, $obj->loadedCnt);
$this->assertSame(1, $obj->saveAfterCnt);
$this->assertSame(1, $obj->saveBeforeCnt);
2024-03-29 20:24:38 +01:00
$obj->arr[12345] = 54321;
2024-03-29 20:39:44 +01:00
$obj->arr2[123456] = 654321;
2024-03-29 20:24:38 +01:00
$this->assertSame(54321, $obj->arr[12345]);
2024-03-29 20:39:44 +01:00
$this->assertSame(654321, $obj->arr2[123456]);
$this->assertCount(1, $obj->arr);
$this->assertCount(1, $obj->arr2);
2024-03-29 20:24:38 +01:00
2024-03-28 20:16:31 +01:00
$obj = $orm[321];
$this->assertSame(1, $obj->loadedCnt);
$this->assertSame(1, $obj->saveAfterCnt);
$this->assertSame(1, $obj->saveBeforeCnt);
2024-03-29 20:24:38 +01:00
$this->assertSame(54321, $obj->arr[12345]);
2024-03-29 20:39:44 +01:00
$this->assertSame(654321, $obj->arr2[123456]);
$this->assertCount(1, $obj->arr);
$this->assertCount(1, $obj->arr2);
2024-03-28 20:16:31 +01:00
unset($obj);
$orm = $field->build();
$obj = $orm[321];
2024-03-28 20:32:20 +01:00
$this->assertSame(1, $obj->loadedCnt);
2024-03-28 20:16:31 +01:00
$this->assertSame(0, $obj->saveAfterCnt);
$this->assertSame(0, $obj->saveBeforeCnt);
2024-03-29 20:24:38 +01:00
$this->assertSame(54321, $obj->arr[12345]);
2024-03-29 20:39:44 +01:00
$this->assertSame(654321, $obj->arr2[123456]);
$this->assertCount(1, $obj->arr);
$this->assertCount(1, $obj->arr2);
2024-03-28 20:16:31 +01:00
$orm[321] = $obj;
$this->assertSame(1, $obj->loadedCnt);
2024-03-28 20:32:20 +01:00
$this->assertSame(0, $obj->saveAfterCnt);
$this->assertSame(0, $obj->saveBeforeCnt);
2024-03-29 20:24:38 +01:00
$this->assertSame(54321, $obj->arr[12345]);
2024-03-29 20:39:44 +01:00
$this->assertSame(654321, $obj->arr2[123456]);
$this->assertCount(1, $obj->arr);
$this->assertCount(1, $obj->arr2);
2024-03-28 20:32:20 +01:00
$f = new ReflectionProperty(ObjectArray::class, 'cache');
$f->getValue($orm)->flushCache();
while (\gc_collect_cycles());
$this->assertSame($obj, $orm[321]);
2024-03-28 20:34:54 +01:00
$orm->clear();
2024-03-28 21:08:03 +01:00
unset($obj);
$obj = new TestObject;
$ref = WeakReference::create($obj);
$orm[123] = $obj;
unset($obj, $orm[123]);
$this->assertNull($ref->get());
$obj = new TestObject;
$ref = WeakReference::create($obj);
$orm = $field->build();
$orm[123] = $obj;
unset($obj, $orm);
while (\gc_collect_cycles());
$this->assertNull($ref->get());
$obj = $field->build()[123];
$obj->savedProp = 123;
$obj->save();
$this->assertSame($obj->savedProp, 123);
unset($obj);
$this->assertSame($field->build()[123]->savedProp, 123);
unset($obj, $orm);
2024-03-31 17:05:51 +02:00
$field->build()->clear();
}
2024-03-31 20:56:18 +02:00
public function testException(): void
{
$this->expectExceptionMessage("Cannot save an uninitialized object!");
(new TestObject)->save();
}
2024-03-31 21:36:36 +02:00
#[DataProvider('provideKeyValues')]
public function testCache(int $tablePostfix, KeyType $keyType, string|int $key, ValueType $valueType, mixed $value): void
2024-03-31 17:05:51 +02:00
{
2024-03-31 21:36:36 +02:00
if ($value instanceof TestObject) {
$value = new TestObject;
2024-03-31 21:52:02 +02:00
} elseif (!\is_int($value) || !\is_int($key)) {
$this->assertTrue(true);
return;
2024-03-31 21:36:36 +02:00
}
$field = new DbArrayBuilder("testCache_{$tablePostfix}", new RedisSettings(
2024-03-31 17:05:51 +02:00
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 1
2024-03-31 21:36:36 +02:00
), $keyType, $valueType);
$fieldNoCache = new DbArrayBuilder("testCache_{$tablePostfix}", new RedisSettings(
2024-03-31 17:05:51 +02:00
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 0
2024-03-31 21:36:36 +02:00
), $keyType, $valueType);
2024-03-31 17:05:51 +02:00
$orm = $field->build();
$ormUnCached = $fieldNoCache->build();
2024-03-31 21:36:36 +02:00
$orm->set($key, $value);
2024-03-31 21:37:53 +02:00
if ($valueType === ValueType::OBJECT) {
2024-03-31 21:36:36 +02:00
$this->assertCount(1, $ormUnCached);
} else {
$this->assertCount(0, $ormUnCached);
delay(0.1);
$this->assertCount(0, $ormUnCached);
delay(0.9);
}
2024-03-31 20:56:18 +02:00
delay(1.0);
2024-03-31 21:36:36 +02:00
if ($value instanceof TestObject) {
unset($value);
$c = (new ReflectionProperty(ObjectArray::class, 'cache'))->getValue($orm);
$c->flushCache();
$this->assertCount(0, (new ReflectionProperty(ObjectContainer::class, 'cache'))->getValue($c));
$f1 = async($orm->get(...), $key);
$f2 = async($orm->get(...), $key);
$value = $f1->await();
$this->assertSame($value, $f1->await());
$this->assertSame($value, $f2->await());
} else {
/** @var CacheContainer */
$c = (new ReflectionProperty(CachedArray::class, 'cache'))->getValue($orm);
$this->assertCount(0, (new ReflectionProperty(CacheContainer::class, 'cache'))->getValue($c));
$f1 = async($orm->get(...), $key);
$f2 = async($orm->get(...), $key);
$this->assertSame($value, $f1->await());
$this->assertSame($value, $f2->await());
2024-04-06 19:45:35 +02:00
$orm[$key] = PHP_INT_MAX;
$this->assertSame(PHP_INT_MAX, $orm[$key]);
EventLoop::queue($orm->set(...), $key, $value);
$c->flushCache();
$this->assertSame($value, $orm[$key]);
2024-03-31 21:36:36 +02:00
}
2024-03-31 20:56:18 +02:00
2024-03-31 17:05:51 +02:00
$orm->clear();
2024-03-31 21:36:36 +02:00
}
public function testCacheStandalone(): void
{
2024-03-31 20:56:18 +02:00
$obj = new TestObject;
$obj->initDbProperties(new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 1
2024-03-31 21:36:36 +02:00
), "testCacheStandalone_");
2024-03-31 20:56:18 +02:00
2024-03-31 21:36:36 +02:00
$fieldNoCache2 = new DbArrayBuilder("testCacheStandalone_arr2", new RedisSettings(
2024-03-31 20:56:18 +02:00
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 0
), KeyType::INT, ValueType::INT);
$orm2Uncached = $fieldNoCache2->build();
2024-03-31 21:36:36 +02:00
$fieldNoCache4 = new DbArrayBuilder("testCacheStandalone_arr4", new RedisSettings(
2024-03-31 20:56:18 +02:00
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);
2024-04-06 20:00:44 +02:00
$fieldNoCache2 = new DbArrayBuilder("testCacheStandalone_arr2", new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 100
), KeyType::INT, ValueType::INT);
$orm2Uncached = $fieldNoCache2->build($orm2Uncached);
$this->assertCount(1, $orm2Uncached);
2024-03-31 20:56:18 +02:00
$orm2Uncached->clear();
$obj->arr4->set(0, 1);
$this->assertCount(1, $orm4Uncached);
2024-04-06 20:00:44 +02:00
$fieldNoCache4 = new DbArrayBuilder("testCacheStandalone_arr4", new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 100
), KeyType::INT, ValueType::INT);
$orm4Uncached = $fieldNoCache4->build($orm4Uncached);
$this->assertCount(1, $orm4Uncached);
2024-03-31 20:56:18 +02:00
$orm4Uncached->clear();
2024-03-28 20:16:31 +01:00
}
2024-03-28 19:43:21 +01:00
public static function provideSettingsKeysValues(): \Generator
2024-03-28 18:34:35 +01:00
{
2024-03-31 21:37:13 +02:00
$k = 0;
2024-03-28 22:40:41 +01:00
foreach (self::provideSettings() as [, $settings]) {
2024-03-31 21:36:36 +02:00
foreach (self::provideKeyValues() as [, $keyType, $key, $valueType, $value]) {
2024-03-28 20:55:45 +01:00
if ($valueType === ValueType::OBJECT && (
2024-03-31 17:17:04 +02:00
$settings instanceof MemorySettings
2024-03-28 20:55:45 +01:00
|| $settings->serializer instanceof Json
)) {
continue;
}
2024-03-28 19:43:21 +01:00
yield [
2024-03-31 21:37:13 +02:00
$k++,
2024-03-28 19:43:21 +01:00
$settings,
2024-03-31 21:36:36 +02:00
$keyType,
$key,
2024-03-28 19:43:21 +01:00
$valueType,
$value
];
}
2024-03-28 18:34:35 +01:00
}
2024-03-28 14:57:59 +01:00
}
2024-03-31 21:36:36 +02:00
public static function provideKeyValues(): \Generator
{
$key = 0;
foreach ([
[ValueType::INT, 123],
[ValueType::STRING, '123'],
[ValueType::STRING, 'test'],
[ValueType::FLOAT, 123.321],
[ValueType::BOOL, true],
[ValueType::BOOL, false],
// Uncomment when segfaults are fixed
[ValueType::OBJECT, new TestObject],
[ValueType::SCALAR, 'test'],
[ValueType::SCALAR, 123],
[ValueType::SCALAR, ['test' => 123]],
[ValueType::SCALAR, 123.321],
] as [$valueType, $value]) {
yield [
$key++,
KeyType::INT,
1234,
$valueType,
$value
];
yield [
$key++,
KeyType::STRING,
'test',
$valueType,
$value
];
yield [
$key++,
KeyType::STRING,
'4321',
$valueType,
$value
];
yield [
$key++,
KeyType::STRING_OR_INT,
'test_2',
$valueType,
$value
];
}
}
2024-03-28 14:57:59 +01:00
public static function provideSettings(): \Generator
{
2024-03-28 22:40:41 +01:00
$key = 0;
2024-03-31 17:13:03 +02:00
yield [$key++, new MemorySettings()];
2024-03-28 14:57:59 +01:00
foreach ([new Native, new Igbinary, new Json] as $serializer) {
2024-03-28 19:00:38 +01:00
foreach ([0, 100] as $ttl) {
yield from [
2024-03-31 17:13:03 +02:00
[$key++, new RedisSettings(
2024-03-28 19:00:38 +01:00
RedisConfig::fromUri('redis://127.0.0.1'),
$serializer,
$ttl,
)],
2024-03-31 17:13:03 +02:00
[$key++, new PostgresSettings(
2024-03-28 19:00:38 +01:00
PostgresConfig::fromString('host=127.0.0.1:5432 user=postgres db=test'),
$serializer,
$ttl,
)],
2024-03-31 17:13:03 +02:00
[$key++, new MysqlSettings(
2024-03-28 19:00:38 +01:00
MysqlConfig::fromString('host=127.0.0.1:3306 user=root db=test'),
$serializer,
$ttl,
)],
2024-03-31 17:13:03 +02:00
[$key++, new MysqlSettings(
2024-03-29 19:56:29 +01:00
MysqlConfig::fromString('host=127.0.0.1:3306 user=root db=test'),
$serializer,
$ttl,
optimizeIfWastedMb: 0,
)],
2024-03-28 19:00:38 +01:00
];
}
2024-03-28 14:57:59 +01:00
}
}
}