mirror of
https://github.com/flarum/core.git
synced 2025-07-24 02:01:19 +02:00
TestCase Config method (#13)
Similarly to `settings`, this allows setting/overriding config prior to application boot.
This commit is contained in:
committed by
GitHub
parent
b2ecb8f020
commit
8fd1cc4f0d
@@ -17,6 +17,7 @@ use Flarum\Testing\integration\Extend\BeginTransactionAndSetDatabase;
|
|||||||
use Flarum\Testing\integration\Extend\OverrideExtensionManagerForTests;
|
use Flarum\Testing\integration\Extend\OverrideExtensionManagerForTests;
|
||||||
use Flarum\Testing\integration\Extend\SetSettingsBeforeBoot;
|
use Flarum\Testing\integration\Extend\SetSettingsBeforeBoot;
|
||||||
use Illuminate\Database\ConnectionInterface;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
use Laminas\Diactoros\ServerRequest;
|
use Laminas\Diactoros\ServerRequest;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
@@ -50,6 +51,12 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
|
|||||||
if (is_null($this->app)) {
|
if (is_null($this->app)) {
|
||||||
$tmp = $this->tmpDir();
|
$tmp = $this->tmpDir();
|
||||||
|
|
||||||
|
$config = include "$tmp/config.php";
|
||||||
|
|
||||||
|
foreach ($this->config as $key => $value) {
|
||||||
|
Arr::set($config, $key, $value);
|
||||||
|
}
|
||||||
|
|
||||||
$site = new InstalledSite(
|
$site = new InstalledSite(
|
||||||
new Paths([
|
new Paths([
|
||||||
'base' => $tmp,
|
'base' => $tmp,
|
||||||
@@ -57,7 +64,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
|
|||||||
'storage' => "$tmp/storage",
|
'storage' => "$tmp/storage",
|
||||||
'vendor' => getcwd().'/vendor',
|
'vendor' => getcwd().'/vendor',
|
||||||
]),
|
]),
|
||||||
new Config(include "$tmp/config.php")
|
new Config($config)
|
||||||
);
|
);
|
||||||
|
|
||||||
$extenders = array_merge([
|
$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 = [];
|
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
|
* Note that this method will have no effect if called after the
|
||||||
* application is booted.
|
* application is booted.
|
||||||
*/
|
*/
|
||||||
protected function setting(string $key, string $value)
|
protected function setting(string $key, $value)
|
||||||
{
|
{
|
||||||
$this->settings[$key] = $value;
|
$this->settings[$key] = $value;
|
||||||
}
|
}
|
||||||
|
@@ -10,6 +10,7 @@
|
|||||||
namespace Flarum\Testing\Tests\integration;
|
namespace Flarum\Testing\Tests\integration;
|
||||||
|
|
||||||
use Flarum\Extend;
|
use Flarum\Extend;
|
||||||
|
use Flarum\Foundation\Config;
|
||||||
use Flarum\Settings\SettingsRepositoryInterface;
|
use Flarum\Settings\SettingsRepositoryInterface;
|
||||||
use Flarum\Testing\integration\TestCase;
|
use Flarum\Testing\integration\TestCase;
|
||||||
use Flarum\User\User;
|
use Flarum\User\User;
|
||||||
@@ -57,6 +58,34 @@ class TestCaseTest extends TestCase
|
|||||||
$this->assertEquals(null, $settings->get('display_name_driver'));
|
$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
|
* @test
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user