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

87 lines
2.9 KiB
PHP
Raw Normal View History

2024-05-18 13:09:01 +04:00
<?php
namespace nur\sery\db\sqlite;
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() {
$capacitor = new class(__DIR__.'/capacitor.db') extends SqliteCapacitor {
protected function getKeyDefinitions(?string $channel): ?array {
if ($channel === "arrays") {
return ["id" => "integer"];
}
return parent::getKeyDefinitions($channel);
}
protected function getKeyValues($item, ?string $channel): ?array {
if ($channel === "arrays") {
return ["id" => $item["id"] ?? null];
}
return parent::getKeyValues($item, $channel);
}
};
$this->_testChargeStrings($capacitor, "strings");
$this->_testChargeArrays($capacitor, "arrays");
$capacitor->close();
}
2024-05-19 11:19:00 +04:00
function testEach() {
$capacitor = new class(__DIR__.'/capacitor.db') extends SqliteCapacitor {
protected function getKeyDefinitions(?string $channel): ?array {
return [
"age" => "integer",
"done" => "integer default 0",
];
}
protected function getKeyValues($item, ?string $channel): ?array {
return [
"age" => $item["age"],
];
}
};
$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
}