TestCase Config method (#13)

Similarly to `settings`, this allows setting/overriding config prior to application boot.
This commit is contained in:
Alexander Skvortsov 2021-04-14 16:27:59 -04:00 committed by GitHub
parent b2ecb8f020
commit 8fd1cc4f0d
2 changed files with 69 additions and 3 deletions

View File

@ -17,6 +17,7 @@ use Flarum\Testing\integration\Extend\BeginTransactionAndSetDatabase;
use Flarum\Testing\integration\Extend\OverrideExtensionManagerForTests;
use Flarum\Testing\integration\Extend\SetSettingsBeforeBoot;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Arr;
use Laminas\Diactoros\ServerRequest;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
@ -50,6 +51,12 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
if (is_null($this->app)) {
$tmp = $this->tmpDir();
$config = include "$tmp/config.php";
foreach ($this->config as $key => $value) {
Arr::set($config, $key, $value);
}
$site = new InstalledSite(
new Paths([
'base' => $tmp,
@ -57,7 +64,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
'storage' => "$tmp/storage",
'vendor' => getcwd().'/vendor',
]),
new Config(include "$tmp/config.php")
new Config($config)
);
$extenders = array_merge([
@ -117,7 +124,37 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
}
/**
* @var string[]
* @var array
*/
protected $config = [];
/**
* Some Flarum code depends on config.php values. Flarum doesn't
* offer a way to set them at runtime, so this method lets you
* add/override them before boot.
*
* You can use dot-separated syntax to assign values to subarrays.
*
* For example:
*
* `$this->config('a.b.c', 'value');` will result in the following:
*
* [
* 'a' => [
* 'b' => ['c' => 'value']
* ]
* ]
*
* Note that this method will have no effect if called after the
* application is booted.
*/
protected function config(string $key, $value)
{
$this->config[$key] = $value;
}
/**
* @var array
*/
protected $settings = [];
@ -131,7 +168,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
* Note that this method will have no effect if called after the
* application is booted.
*/
protected function setting(string $key, string $value)
protected function setting(string $key, $value)
{
$this->settings[$key] = $value;
}

View File

@ -10,6 +10,7 @@
namespace Flarum\Testing\Tests\integration;
use Flarum\Extend;
use Flarum\Foundation\Config;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Testing\integration\TestCase;
use Flarum\User\User;
@ -57,6 +58,34 @@ class TestCaseTest extends TestCase
$this->assertEquals(null, $settings->get('display_name_driver'));
}
/**
* @test
*/
public function can_add_config_via_method()
{
$this->config('hello', 'world');
$this->config('url', 'https://flarum.org');
$this->config('level1.level2', 'value');
$config = $this->app()->getContainer()->make(Config::class);
$this->assertEquals('world', $config['hello']);
$this->assertEquals('https://flarum.org', $config['url']);
$this->assertEquals('value', $config['level1']['level2']);
}
/**
* @test
*/
public function config_cleaned_up_from_previous_method()
{
$config = $this->app()->getContainer()->make(Config::class);
$this->assertEquals(null, $config['hello']);
$this->assertEquals('http://localhost', $config['url']);
$this->assertFalse(isset($config['level1']['level2']));
}
/**
* @test
*/