orbit/tests/src/Orbit/ConfigTest.php

100 lines
2.7 KiB
PHP

<?php declare(strict_types=1);
namespace Orbit\Tests;
use PHPUnit\Framework\TestCase;
use Orbit\Config;
final class ConfigTest extends TestCase
{
public function testConstruct(): void
{
$config = new Config();
$this->assertInstanceOf(Config::class, $config);
}
public function testConstructDev(): void
{
$config = new Config(true);
$this->assertInstanceOf(Config::class, $config);
$this->assertTrue($config->getIsDevelopmentServer());
}
public function testConstructDevFromTruthyValue(): void
{
$config = new Config("yes");
$this->assertInstanceOf(Config::class, $config);
$this->assertTrue($config->getIsDevelopmentServer());
}
public function testReadFromIniFile(): void
{
$data = "hostname=fabricate.dev";
file_put_contents('test.ini', $data);
$config = new Config();
$config->readFromIniFile('test.ini');
$this->assertSame('fabricate.dev', $config->hostname);
@unlink('test.ini');
}
public function testReadFromIniNonFile(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Cannot read config file');
$config = new Config();
$config->readFromIniFile('nonfile.ini');
}
public function testReadFromIniAllPossibleKeys(): void
{
$data = <<<EOF
host=1.1.1.1
port=1988
hostname=beatles.org
tls_certfile=1212
tls_keyfile=3434
keypassphrase=strawberry
log_file=xyz.log
log_level=cherry
root_dir=blueberry
index_file=led.gmi
enable_directory_index=0
EOF;
file_put_contents('test.ini', $data);
$config = new Config();
$config->readFromIniFile('test.ini');
$this->assertSame('1.1.1.1', $config->host);
$this->assertSame('1988', $config->port);
$this->assertSame('beatles.org', $config->hostname);
$this->assertSame('1212', $config->tls_certfile);
$this->assertSame('3434', $config->tls_keyfile);
$this->assertSame('strawberry', $config->keypassphrase);
$this->assertSame('xyz.log', $config->log_file);
$this->assertSame('cherry', $config->log_level);
$this->assertSame('blueberry', $config->root_dir);
$this->assertSame('led.gmi', $config->index_file);
$this->assertSame('0', $config->enable_directory_index);
@unlink('test.ini');
}
public function testReadFromIniWithInvalidKey(): void
{
$data = <<<EOF
invalid_key=true
EOF;
file_put_contents('test.ini', $data);
$config = new Config();
$config->readFromIniFile('test.ini');
$this->assertFalse(property_exists($config, 'invalid_key'));
@unlink('test.ini');
}
}