Files
XC_VM/tests/Unit/RadioServiceProcessTest.php
T
Divarion_D 9e118da975 test: run the unit suite on MariaDB only, and on the panel's own stack
The suite ran on in-memory SQLite, an engine production never uses: it hid
column lengths, MySQL-only syntax and type coercion (the identity bug fixed in
the previous commit passed CI that way). SQLite is gone:

- TestDb connects only to MariaDB/MySQL (XCVM_TEST_DB_DSN/_USER/_PASS; root over
  the local socket, else 127.0.0.1:3306). Each instance gets a throwaway
  schema, dropped with it, in the installer's sql_mode, with an explicit
  connect timeout (a boot under test can zero default_socket_timeout).
- InstallSchema and the tests take the production DDL as is: the SQLite
  reducers, the keys they re-added, the faked information_schema and the
  sqliteCreateFunction() stand-ins are removed; test DDL is MySQL DDL.
- ReplicaBootTest, DbConnectRefusalTest and LazyDatabaseHandlerTest reach the
  test server instead of SQLite files; RecordingFinalizer no longer swallows
  an information_schema failure "for tests on SQLite".
- `make test-db` starts a throwaway mariadb:11.4 (tmpfs) for a machine without
  a server; the CI PHPUnit job gets the same as a service.

The suite also runs on a panel host with the bundled PHP and its own php.ini:
tests reach the code through MAIN_HOME, not `<repo>/src`; repository checks
and process-isolated tests (ionCube Loader + OPcache CLI segfault on a script
read from stdin) are in the skip-on-panel group; xcvm_test_child_php() starts
a child that fakes \XC_VM without the real extension; Redis-down tests use
RedisManager::useConnector() instead of an eval'd \XC_VM.

Stale opt-in tests fixed on the way: the real-agent provisioning test answers
the agent-started check, the extension integration test sends the typed starts
and reads the binary signature raw, a fake producer starts as PHP_BINARY, and
the certbot test waits for the line its background command writes.
2026-10-04 16:12:23 +03:00

65 lines
2.4 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 and the auth-fail path (which exit()s) 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 AUTO_INCREMENT, 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']);
}
}