165 lines
5.2 KiB
PHP
165 lines
5.2 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace Orbit\Tests;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Monolog\Logger;
|
|
use Orbit\Console;
|
|
use Orbit\Config;
|
|
|
|
final class ConsoleTest extends TestCase
|
|
{
|
|
public function setUp(): void
|
|
{
|
|
Console::$under_test = true;
|
|
}
|
|
|
|
public function makeTestLogger(): Logger
|
|
{
|
|
$logger = new Logger('test-orbit');
|
|
$logger->pushHandler(new \Monolog\Handler\TestHandler());
|
|
|
|
return $logger;
|
|
}
|
|
|
|
public function getTestLogRecords($logger): array
|
|
{
|
|
return $logger->getHandlers()[0]->getRecords();
|
|
}
|
|
|
|
public function getTestLogMessages($logger): array
|
|
{
|
|
$messages = [];
|
|
foreach ($this->getTestLogRecords($logger) as $message) {
|
|
$messages[] = $message['level_name'] . ": " . $message['message'];
|
|
}
|
|
|
|
return $messages;
|
|
}
|
|
|
|
public function makeConsole($argv = []): Console
|
|
{
|
|
$args = new \Qi_Console_ArgV(
|
|
$argv,
|
|
[
|
|
'config|c:' => 'Use specified config file (.ini) for configuration',
|
|
'host:' => 'Set host/ip address to listen on (default 0.0.0.0)',
|
|
'port|p:' => 'Set port to listen on (default 1965)',
|
|
'hostname:' => 'Set hostname of server (default localhost)',
|
|
'tls-cert:' => 'Set cert PEM file to use (default null)',
|
|
'tls-key:' => 'Set private key PEM file to use (default null)',
|
|
'root-dir:' => 'Set the file root directory',
|
|
'log:' => 'Set log filename (default orbit.log)',
|
|
'dev' => 'Allow developer server functions (default false)',
|
|
'help|h' => 'Show help',
|
|
'verbose|v' => 'Include more verbose output',
|
|
'quiet|q' => 'Print less messages',
|
|
'no-color' => 'Don\'t use color output',
|
|
'version' => 'Show version and exit',
|
|
]
|
|
);
|
|
$terminal = new \Qi_Console_Terminal();
|
|
return new Console($args, $terminal);
|
|
}
|
|
|
|
public function testConstruct(): void
|
|
{
|
|
$args = new \Qi_Console_ArgV([]);
|
|
$terminal = new \Qi_Console_Terminal();
|
|
$console = new Console($args, $terminal);
|
|
$this->assertInstanceOf(Console::class, $console);
|
|
}
|
|
|
|
public function testExecuteNocolor(): void
|
|
{
|
|
$console = $this->makeConsole(['p', '--no-color', '--dev']);
|
|
$logger = $this->makeTestLogger();
|
|
ob_start();
|
|
$status = $console->execute($logger);
|
|
$output = ob_get_contents();
|
|
ob_get_clean();
|
|
|
|
$this->assertStringContainsString('Orbit // Gemini', $output);
|
|
}
|
|
|
|
public function testExecuteVersion(): void
|
|
{
|
|
$console = $this->makeConsole(['p', '--version', '--dev']);
|
|
$logger = $this->makeTestLogger();
|
|
ob_start();
|
|
$status = $console->execute($logger);
|
|
$output = ob_get_contents();
|
|
ob_get_clean();
|
|
|
|
$this->assertStringContainsString('Orbit ', $output);
|
|
}
|
|
|
|
public function testExecuteHelp(): void
|
|
{
|
|
$console = $this->makeConsole(['p', '--help', '--dev']);
|
|
$logger = $this->makeTestLogger();
|
|
ob_start();
|
|
$status = $console->execute($logger);
|
|
$output = ob_get_contents();
|
|
ob_get_clean();
|
|
|
|
$this->assertStringContainsString('Orbit ', $output);
|
|
}
|
|
|
|
public function testMakeConfigWithConfig(): void
|
|
{
|
|
$data = 'host=rainbow.muffin';
|
|
file_put_contents('test.ini', $data);
|
|
|
|
$console = $this->makeConsole(['p', '--config', 'test.ini', '--dev']);
|
|
$config = $console->makeConfig();
|
|
$this->assertSame('rainbow.muffin', $config->host);
|
|
@unlink('test.ini');
|
|
}
|
|
|
|
public function testMakeConfigSetValues(): void
|
|
{
|
|
$args = [
|
|
'p', '--host=a', '--port=b', '--hostname=c', '--log=d',
|
|
'--verbose', '--root-dir=e', '--tls-cert=f', '--tls-key=g'
|
|
];
|
|
$console = $this->makeConsole($args);
|
|
$config = $console->makeConfig();
|
|
$this->assertSame('a', $config->host);
|
|
$this->assertSame('b', $config->port);
|
|
$this->assertSame('c', $config->hostname);
|
|
$this->assertSame('d', $config->log_file);
|
|
$this->assertsame(100, $config->log_level);
|
|
$this->assertSame('e', $config->root_dir);
|
|
$this->assertSame('f', $config->tls_certfile);
|
|
$this->assertSame('g', $config->tls_keyfile);
|
|
}
|
|
|
|
public function testMakeLogger(): void
|
|
{
|
|
$config = new Config('dev');
|
|
$console = $this->makeConsole([]);
|
|
$logger = $console->makeLogger($config);
|
|
$this->assertInstanceOf(Logger::class, $logger);
|
|
// There should be two handlers attached
|
|
$this->assertEquals(2, count($logger->getHandlers()));
|
|
}
|
|
|
|
public function testMakeLoggerQuiet(): void
|
|
{
|
|
$config = new Config('dev');
|
|
$console = $this->makeConsole([]);
|
|
$logger = $console->makeLogger($config, true);
|
|
$this->assertInstanceOf(Logger::class, $logger);
|
|
// There should be one handler attached
|
|
$this->assertEquals(1, count($logger->getHandlers()));
|
|
}
|
|
|
|
public function tearDown(): void
|
|
{
|
|
@unlink('certs/localhost.cert.pem');
|
|
@unlink('certs/localhost.key.pem');
|
|
}
|
|
}
|