Files
XC_VM/tests/Unit/StreamsCronConnectionsTest.php
T
AbdoAhmedElbanaaandDivarion_D 202ad85d6c fix(cron): resolve TypeError in StreamsCronJob when parsing Redis connections
Port of Rosmi720/XC_VM@b44abddf. The rogue-connection sweep assumed
ConnectionTracker::getConnections() always returned a MySQL map grouped by
user id and iterated it with a flat double-loop. In Redis mode it returns a
[keys, data] pair, so the inner loop hit string keys and threw a TypeError,
aborting the cron.

Parse the result defensively via a small recursive helper (collectUuidRows)
that flattens any of the shapes — the Redis [keys, data] pair, the MySQL
user-id map and a single connection row — to the rows carrying a uuid, then
filter to the current stream. Both helpers are pure and unit-tested.

Adapted to this tree: the ar.ini/en.ini label tweaks target keys not present
here, so only the cron fix was ported; the added guards were split into
covered helpers to keep loadCron within the complexity ceiling.
2026-09-21 19:17:33 +03:00

55 lines
1.7 KiB
PHP

<?php
use PHPUnit\Framework\TestCase;
use XcVm\Cli\CronJobs\StreamsCronJob;
/**
* Coverage for StreamsCronJob connection parsing — the fix for the TypeError
* raised when getConnections() returns the Redis [keys, data] pair instead of
* the MySQL user-id map. Both shapes (and a single connection row) must yield
* the stream's UUIDs, filtered to that stream.
*/
final class StreamsCronConnectionsTest extends TestCase {
private function uuids($rConnections, $rStreamID): array {
$rM = new ReflectionMethod(StreamsCronJob::class, 'connectionUuidsForStream');
$rM->setAccessible(true);
return $rM->invoke(null, $rConnections, $rStreamID);
}
private function collect($rNode): array {
$rM = new ReflectionMethod(StreamsCronJob::class, 'collectUuidRows');
$rM->setAccessible(true);
return $rM->invoke(null, $rNode);
}
public function testRedisPairShapeFiltersByStream(): void {
$rConnections = [
['redis-key-1', 'redis-key-2'],
[
['uuid' => 'a', 'stream_id' => 5],
['uuid' => 'b', 'stream_id' => 9],
],
];
$this->assertSame(['a'], $this->uuids($rConnections, 5));
}
public function testMysqlGroupedShapeKeepsRowsWithoutStreamId(): void {
$rConnections = [
10 => [['uuid' => 'c', 'stream_id' => 5]],
11 => [['uuid' => 'd']], // no stream_id → kept
];
$this->assertSame(['c', 'd'], $this->uuids($rConnections, 5));
}
public function testSingleConnectionRow(): void {
$this->assertSame(['e'], $this->uuids(['uuid' => 'e', 'stream_id' => 5], 5));
}
public function testNonArrayAndEmptyYieldNothing(): void {
$this->assertSame([], $this->uuids([], 5));
$this->assertSame([], $this->uuids(null, 5));
$this->assertSame([], $this->collect('a-redis-key-string'));
}
}