nur-sery/tests/db/sqlite/SqliteCapacitorTest.php

86 lines
2.8 KiB
PHP
Raw Normal View History

2024-05-18 13:09:01 +04:00
<?php
namespace nur\sery\db\sqlite;
2024-05-20 09:24:47 +04:00
use nur\sery\db\CapacitorChannel;
2024-05-18 13:09:01 +04:00
use PHPUnit\Framework\TestCase;
class SqliteCapacitorTest extends TestCase {
function _testChargeStrings(SqliteCapacitor $capacitor, ?string $channel) {
$capacitor->reset($channel);
$capacitor->charge("first", $channel);
$capacitor->charge("second", $channel);
$capacitor->charge("third", $channel);
2024-05-19 11:19:00 +04:00
$items = iterator_to_array($capacitor->discharge(null, $channel, false));
2024-05-18 13:09:01 +04:00
self::assertSame(["first", "second", "third"], $items);
}
function _testChargeArrays(SqliteCapacitor $capacitor, ?string $channel) {
$capacitor->reset($channel);
$capacitor->charge(["id" => 10, "name" => "first"], $channel);
$capacitor->charge(["name" => "second", "id" => 20], $channel);
$capacitor->charge(["name" => "third", "id" => "30"], $channel);
}
function testChargeStrings() {
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
$this->_testChargeStrings($capacitor, null);
$capacitor->close();
}
function testChargeArrays() {
2024-05-20 09:24:47 +04:00
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
$capacitor->addChannel(new class extends CapacitorChannel {
const NAME = "arrays";
function getKeyDefinitions(): ?array {
return ["id" => "integer"];
2024-05-18 13:09:01 +04:00
}
2024-05-20 09:24:47 +04:00
function getKeyValues($item): ?array {
return ["id" => $item["id"] ?? null];
2024-05-18 13:09:01 +04:00
}
2024-05-20 09:24:47 +04:00
});
2024-05-18 13:09:01 +04:00
$this->_testChargeStrings($capacitor, "strings");
$this->_testChargeArrays($capacitor, "arrays");
$capacitor->close();
}
2024-05-19 11:19:00 +04:00
function testEach() {
2024-05-20 09:24:47 +04:00
$capacitor = new SqliteCapacitor(__DIR__.'/capacitor.db');
$capacitor->addChannel(new class extends CapacitorChannel {
const NAME = "each";
function getKeyDefinitions(): ?array {
2024-05-19 11:19:00 +04:00
return [
"age" => "integer",
"done" => "integer default 0",
];
}
2024-05-20 09:24:47 +04:00
function getKeyValues($item): ?array {
2024-05-19 11:19:00 +04:00
return [
"age" => $item["age"],
];
}
2024-05-20 09:24:47 +04:00
});
2024-05-19 11:19:00 +04:00
$channel = "each";
$capacitor->reset($channel);
$capacitor->charge(["name" => "first", "age" => 5], $channel);
$capacitor->charge(["name" => "second", "age" => 10], $channel);
$capacitor->charge(["name" => "third", "age" => 15], $channel);
$capacitor->charge(["name" => "fourth", "age" => 20], $channel);
$setDone = function ($item, $row, $suffix=null) {
$updates = ["done" => 1];
if ($suffix !== null) {
$item["name"] .= $suffix;
$updates["_item"] = $item;
}
return $updates;
};
$capacitor->each(["age" => [">", 10]], $setDone, ["++"], $channel);
$capacitor->each(["done" => 0], $setDone, null, $channel);
Txx(iterator_to_array($capacitor->discharge(null, $channel, false)));
$capacitor->close();
self::assertTrue(true);
}
2024-05-18 13:09:01 +04:00
}