This commit is contained in:
Daniil Gentili 2024-03-28 22:40:41 +01:00
parent aa50698197
commit 831241da55
2 changed files with 41 additions and 23 deletions

View File

@ -35,7 +35,8 @@
"phpunit/phpunit": "^11.0.6", "phpunit/phpunit": "^11.0.6",
"amphp/php-cs-fixer-config": "^2.0.1", "amphp/php-cs-fixer-config": "^2.0.1",
"friendsofphp/php-cs-fixer": "^3.51", "friendsofphp/php-cs-fixer": "^3.51",
"amphp/process": "^2.0" "amphp/process": "^2.0",
"brianium/paratest": "^7.4"
}, },
"scripts": { "scripts": {
"cs-fix": "PHP_CS_FIXER_IGNORE_ENV=1 php -d pcre.jit=0 vendor/bin/php-cs-fixer fix -v" "cs-fix": "PHP_CS_FIXER_IGNORE_ENV=1 php -d pcre.jit=0 vendor/bin/php-cs-fixer fix -v"

View File

@ -63,8 +63,19 @@ final class OrmTest extends TestCase
async(pipe(...), $process->getStdout(), getStdout()); async(pipe(...), $process->getStdout(), getStdout());
$process->join(); $process->join();
} }
private static bool $configured = false;
public static function setUpBeforeClass(): void public static function setUpBeforeClass(): void
{ {
touch('/tmp/async-orm-test');
$lockFile = fopen('/tmp/async-orm-test', 'r+');
flock($lockFile, LOCK_EX);
if (fgets($lockFile) === 'done') {
flock($lockFile, LOCK_UN);
return;
}
self::$configured = true;
fwrite($lockFile, "done\n");
$f = []; $f = [];
foreach (['redis' => 6379, 'mariadb' => 3306, 'postgres' => 5432] as $image => $port) { foreach (['redis' => 6379, 'mariadb' => 3306, 'postgres' => 5432] as $image => $port) {
$f []= async(function () use ($image, $port) { $f []= async(function () use ($image, $port) {
@ -94,6 +105,14 @@ final class OrmTest extends TestCase
throw new AssertionError("Could not start $name!"); throw new AssertionError("Could not start $name!");
} }
} }
flock($lockFile, LOCK_UN);
}
public static function tearDownAfterClass(): void
{
if (self::$configured) {
unlink('/tmp/async-orm-test');
}
} }
private static function waitForStartup(ReadableStream $f): bool private static function waitForStartup(ReadableStream $f): bool
{ {
@ -107,12 +126,6 @@ final class OrmTest extends TestCase
} }
return false; return false;
} }
public static function tearDownAfterClass(): void
{
foreach (self::$processes as $process) {
$process->signal(15);
}
}
public function assertSameNotObject(mixed $a, mixed $b): void public function assertSameNotObject(mixed $a, mixed $b): void
{ {
@ -122,14 +135,12 @@ final class OrmTest extends TestCase
$this->assertSame($a, $b); $this->assertSame($a, $b);
} }
} }
private static int $cnt = 0;
#[DataProvider('provideSettingsKeysValues')] #[DataProvider('provideSettingsKeysValues')]
public function testBasic(Settings $settings, KeyType $keyType, string|int $key, ValueType $valueType, mixed $value): void public function testBasic(int $tablePostfix, Settings $settings, KeyType $keyType, string|int $key, ValueType $valueType, mixed $value): void
{ {
$cnt = self::$cnt++;
$field = new FieldConfig( $field = new FieldConfig(
"testBasic_$cnt", "testBasic_$tablePostfix",
$settings, $settings,
$keyType, $keyType,
$valueType $valueType
@ -215,10 +226,10 @@ final class OrmTest extends TestCase
} }
#[DataProvider('provideSettings')] #[DataProvider('provideSettings')]
public function testKeyMigration(Settings $settings): void public function testKeyMigration(int $tablePostfix, Settings $settings): void
{ {
$field = new FieldConfig( $field = new FieldConfig(
'testKeyMigration', $table = 'testKeyMigration_'.$tablePostfix,
$settings, $settings,
KeyType::STRING_OR_INT, KeyType::STRING_OR_INT,
ValueType::INT ValueType::INT
@ -241,7 +252,7 @@ final class OrmTest extends TestCase
} }
$field = new FieldConfig( $field = new FieldConfig(
'testKeyMigration', $table,
$settings, $settings,
KeyType::INT, KeyType::INT,
ValueType::INT ValueType::INT
@ -258,7 +269,7 @@ final class OrmTest extends TestCase
$this->assertEquals(1, $cnt); $this->assertEquals(1, $cnt);
$field = new FieldConfig( $field = new FieldConfig(
'testKeyMigration', $table,
$settings, $settings,
KeyType::STRING, KeyType::STRING,
ValueType::INT ValueType::INT
@ -276,7 +287,7 @@ final class OrmTest extends TestCase
$this->assertEquals(1, $cnt); $this->assertEquals(1, $cnt);
$field = new FieldConfig( $field = new FieldConfig(
'testKeyMigration', $table,
$settings, $settings,
KeyType::INT, KeyType::INT,
ValueType::INT ValueType::INT
@ -295,7 +306,7 @@ final class OrmTest extends TestCase
} }
#[DataProvider('provideSettings')] #[DataProvider('provideSettings')]
public function testObject(Settings $settings): void public function testObject(int $tablePostfix, Settings $settings): void
{ {
if (!$settings instanceof DriverSettings) { if (!$settings instanceof DriverSettings) {
$this->expectExceptionMessage("Objects can only be saved to a database backend!"); $this->expectExceptionMessage("Objects can only be saved to a database backend!");
@ -304,7 +315,7 @@ final class OrmTest extends TestCase
$this->expectExceptionMessage("The JSON backend cannot be used when serializing objects!"); $this->expectExceptionMessage("The JSON backend cannot be used when serializing objects!");
} }
$field = new FieldConfig( $field = new FieldConfig(
'testObject', 'testObject_'.$tablePostfix,
$settings, $settings,
KeyType::STRING_OR_INT, KeyType::STRING_OR_INT,
ValueType::OBJECT ValueType::OBJECT
@ -380,7 +391,8 @@ final class OrmTest extends TestCase
public static function provideSettingsKeysValues(): \Generator public static function provideSettingsKeysValues(): \Generator
{ {
foreach (self::provideSettings() as [$settings]) { $key = 0;
foreach (self::provideSettings() as [, $settings]) {
foreach ([ foreach ([
[ValueType::INT, 123], [ValueType::INT, 123],
[ValueType::STRING, '123'], [ValueType::STRING, '123'],
@ -404,6 +416,7 @@ final class OrmTest extends TestCase
continue; continue;
} }
yield [ yield [
$key++,
$settings, $settings,
KeyType::INT, KeyType::INT,
1234, 1234,
@ -411,6 +424,7 @@ final class OrmTest extends TestCase
$value $value
]; ];
yield [ yield [
$key++,
$settings, $settings,
KeyType::STRING, KeyType::STRING,
'test', 'test',
@ -418,6 +432,7 @@ final class OrmTest extends TestCase
$value $value
]; ];
yield [ yield [
$key++,
$settings, $settings,
KeyType::STRING, KeyType::STRING,
'4321', '4321',
@ -425,6 +440,7 @@ final class OrmTest extends TestCase
$value $value
]; ];
yield [ yield [
$key++,
$settings, $settings,
KeyType::STRING_OR_INT, KeyType::STRING_OR_INT,
'test_2', 'test_2',
@ -437,21 +453,22 @@ final class OrmTest extends TestCase
public static function provideSettings(): \Generator public static function provideSettings(): \Generator
{ {
yield [new Memory()]; $key = 0;
yield [$key++, new Memory()];
foreach ([new Native, new Igbinary, new Json] as $serializer) { foreach ([new Native, new Igbinary, new Json] as $serializer) {
foreach ([0, 100] as $ttl) { foreach ([0, 100] as $ttl) {
yield from [ yield from [
[new Redis( [$key++, new Redis(
RedisConfig::fromUri('redis://127.0.0.1'), RedisConfig::fromUri('redis://127.0.0.1'),
$serializer, $serializer,
$ttl, $ttl,
)], )],
[new Postgres( [$key++, new Postgres(
PostgresConfig::fromString('host=127.0.0.1:5432 user=postgres db=test'), PostgresConfig::fromString('host=127.0.0.1:5432 user=postgres db=test'),
$serializer, $serializer,
$ttl, $ttl,
)], )],
[new Mysql( [$key++, new Mysql(
MysqlConfig::fromString('host=127.0.0.1:3306 user=root db=test'), MysqlConfig::fromString('host=127.0.0.1:3306 user=root db=test'),
$serializer, $serializer,
$ttl, $ttl,