Files
XC_VM/tests/Unit/RadioServiceProcessTest.php
T
Divarion_D 7aa7b0d29d refactor(radio): decompose RadioService::process into tested helpers + flatten
process() was a 250-line, ~10-deep method riddled with the empty-if/else
anti-pattern — the exact shape Rector mis-transformed (which is why RadioService
was reverted from the Rector pass). Rebuild it safely, test-first:

Extracted 7 helpers, each covered by tests (RadioServiceTest, 18 tests):
- buildAutoRestart()        — auto_restart schedule from days/time
- resolveSelectedIds()      — created-name or numeric id (merged the duplicated
                              bouquet/category resolution)
- createMissingBouquets()   — insert bouquets from bouquet_create_list
- createMissingCategories() — insert radio categories from category_create_list
- saveStreamOptions()       — clear + re-insert streams_options (6 option types)
- syncServerTree()          — reconcile streams_servers against the server tree
- syncBouquets()            — attach to selected bouquets, detach on edit

Flattened the orchestrator: 250 -> 87 lines, nesting ~10 -> 4. Guard clauses for
validate / auth / no-source, ternaries for the flag fields, and the vestigial
single-element $rImportStreams loop removed (mutating $rArray directly is
equivalent to its per-iteration merge). exit() on auth failure is unchanged
(returning instead would be a semantic change). Behaviour preserved throughout.

RadioServiceProcessTest characterizes the outer guard flow (INVALID_INPUT, and
NO_SOURCES via the edit path) — the branches the flattening restructures. The
success path can't run under the SQLite harness (verifyPostTable hits MySQL
information_schema) so its internals are covered by the extracted-helper tests.

PHPStan level 5 clean; suite 711 tests / 0 errors (+20).
2026-09-13 18:33:02 +03:00

67 lines
2.5 KiB
PHP

<?php
use XcVm\Domain\Stream\RadioService;
use XcVm\Infrastructure\Database\DatabaseFactory;
use PHPUnit\Framework\TestCase;
// STATUS_* live in bootstrap.php (not loaded here); define the ones process() returns.
if (!defined('STATUS_FAILURE')) {
define('STATUS_FAILURE', 0);
}
if (!defined('STATUS_SUCCESS')) {
define('STATUS_SUCCESS', 1);
}
if (!defined('STATUS_NO_SOURCES')) {
define('STATUS_NO_SOURCES', 4);
}
if (!defined('STATUS_INVALID_INPUT')) {
define('STATUS_INVALID_INPUT', 34);
}
/**
* RadioService::process() — characterization of the OUTER GUARD control flow,
* the part being flattened into guard clauses.
*
* The success path can't run under the SQLite TestDb (QueryHelper::verifyPostTable
* queries MySQL information_schema) and the auth-fail path exit()s, so those are
* out of scope here; the success-path internals are covered by the extracted
* helpers in RadioServiceTest. These two branches — invalid input, and no source
* — are exactly the guards the flattening restructures, so they guard the change.
*/
final class RadioServiceProcessTest extends TestCase {
private TestDb $db;
protected function setUp(): void {
$this->db = new TestDb();
$this->db->exec('CREATE TABLE streams (id INTEGER PRIMARY KEY, stream_display_name TEXT, stream_icon TEXT);');
$this->db->query('INSERT INTO streams (id, stream_display_name, stream_icon) VALUES (5, "old", "");');
// Resolve every DatabaseAware class (RadioService, StreamRepository, ...) to the test db.
DatabaseFactory::set($this->db);
// Authorization::check() reads these globals + global $db.
$GLOBALS['db'] = $this->db;
$GLOBALS['rUserInfo'] = ['id' => 1, 'member_group_id' => 1];
$GLOBALS['rPermissions'] = ['is_admin' => 1, 'advanced' => []];
}
protected function tearDown(): void {
unset($GLOBALS['db'], $GLOBALS['rUserInfo'], $GLOBALS['rPermissions']);
}
public function testInvalidInputWhenValidationFails(): void {
// Empty display name, no review, no upload → processRadio validation fails.
$rResult = RadioService::process(['stream_display_name' => '']);
$this->assertSame(STATUS_INVALID_INPUT, $rResult['status']);
}
public function testNoSourcesWhenStreamSourceEmpty(): void {
// Edit path (avoids verifyPostTable): valid + authorized, but no source.
$rResult = RadioService::process([
'edit' => 5,
'stream_display_name' => 'My Radio',
'stream_source' => [''],
]);
$this->assertSame(STATUS_NO_SOURCES, $rResult['status']);
}
}