1
0
mirror of https://github.com/flarum/core.git synced 2025-07-25 18:51:40 +02:00

Merge pull request #10 from flarum/as/allow-specifying-settings-before-boot

Allow configuring settings before app boot
This commit is contained in:
Sami Mazouz
2021-04-07 22:59:20 +01:00
committed by GitHub
15 changed files with 314 additions and 1 deletions

View File

@@ -0,0 +1,83 @@
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
working-directory: tests
strategy:
matrix:
php: [7.3, 7.4, '8.0']
service: ['mysql:5.7', mariadb]
prefix: ['', flarum_]
include:
- service: 'mysql:5.7'
db: MySQL
- service: mariadb
db: MariaDB
- prefix: flarum_
prefixStr: (prefix)
exclude:
- php: 7.3
service: 'mysql:5.7'
prefix: flarum_
- php: 7.3
service: mariadb
prefix: flarum_
- php: 8.0
service: 'mysql:5.7'
prefix: flarum_
- php: 8.0
service: mariadb
prefix: flarum_
services:
mysql:
image: ${{ matrix.service }}
ports:
- 13306:3306
name: 'PHP ${{ matrix.php }} / ${{ matrix.db }} ${{ matrix.prefixStr }}'
steps:
- uses: actions/checkout@master
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: xdebug
extensions: curl, dom, gd, json, mbstring, openssl, pdo_mysql, tokenizer, zip
tools: phpunit, composer:v2
# The authentication alter is necessary because newer mysql versions use the `caching_sha2_password` driver,
# which isn't supported prior to PHP7.4
# When we drop support for PHP7.3, we should remove this from the setup.
- name: Create MySQL Database
run: |
sudo systemctl start mysql
mysql -uroot -proot -e 'CREATE DATABASE flarum_test;' --port 13306
mysql -uroot -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';" --port 13306
- name: Install Composer dependencies
run: composer install
- name: Setup Composer tests
run: composer test:setup
env:
DB_PORT: 13306
DB_PASSWORD: root
DB_PREFIX: ${{ matrix.prefix }}
- name: Run Composer tests
run: composer test
env:
COMPOSER_PROCESS_TIMEOUT: 600

View File

@@ -20,6 +20,11 @@
"Flarum\\Testing\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Flarum\\Testing\\Tests\\": "src/tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "0.1.x-dev"

View File

@@ -0,0 +1,32 @@
<?php
namespace Flarum\Testing\integration\Extend;
use Flarum\Extend\ExtenderInterface;
use Flarum\Extension\Extension;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
class SetSettingsBeforeBoot implements ExtenderInterface
{
/**
* IDs of extensions to boot
*/
protected $settings;
public function __construct($settings)
{
$this->settings = $settings;
}
public function extend(Container $container, Extension $extension = null)
{
if (count($this->settings)) {
$settings = $container->make(SettingsRepositoryInterface::class);
foreach ($this->settings as $key => $value) {
$settings->set($key, $value);
}
}
}
}

View File

@@ -14,6 +14,7 @@ use Flarum\Foundation\Config;
use Flarum\Foundation\InstalledSite;
use Flarum\Foundation\Paths;
use Flarum\Testing\integration\Extend\OverrideExtensionManagerForTests;
use Flarum\Testing\integration\Extend\SetSettingsBeforeBoot;
use Illuminate\Database\ConnectionInterface;
use Laminas\Diactoros\ServerRequest;
use Psr\Http\Message\ResponseInterface;
@@ -59,7 +60,8 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
);
$extenders = array_merge([
new OverrideExtensionManagerForTests($this->extensions)
new OverrideExtensionManagerForTests($this->extensions),
new SetSettingsBeforeBoot($this->settings),
], $this->extenders);
$site->extendWith($extenders);
@@ -79,6 +81,13 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
*/
protected $extenders = [];
/**
* Each argument should be an instance of an extender that should
* be applied at application boot.
*
* Note that this method will have no effect if called after the
* application is booted.
*/
protected function extend(ExtenderInterface ...$extenders)
{
$this->extenders = array_merge($this->extenders, $extenders);
@@ -89,11 +98,40 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
*/
protected $extensions = [];
/**
* Each argument should be an ID of an extension to be enabled.
* Extensions other than the one currently being tested must be
* listed in this extension's `composer.json` under `require` or
* `require-dev`.
*
* Note that this method will have no effect if called after the
* application is booted.
*/
protected function extension(string ...$extensions)
{
$this->extensions = array_merge($this->extensions, $extensions);
}
/**
* @var string[]
*/
protected $settings = [];
/**
* Some settings are used during application boot, so setting
* them via `prepareDatabase` will be too late for the desired
* effect. For instance, in core the active display name driver
* is configured based on the `display_name_driver` setting.
* That setting should be registered using this method.
*
* Note that this method will have no effect if called after the
* application is booted.
*/
protected function setting(string $key, string $value)
{
$this->settings[$key] = $value;
}
/**
* @var RequestHandlerInterface
*/

1
php-packages/testing/tests/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
vendor/*

View File

@@ -0,0 +1 @@
A minimal extension skeleton to test the flarum/testing package.

View File

@@ -0,0 +1,35 @@
{
"name": "flarum/testing-tests",
"description": "Minimal extension to test the flarum/testing package",
"type": "flarum-extension",
"require": {
"flarum/core": "^0.1.0-beta.16"
},
"require-dev": {
"flarum/testing": "*@dev"
},
"autoload-dev": {
"psr-4": {
"Flarum\\Testing\\Tests\\": "tests/"
}
},
"scripts": {
"test": [
"@test:unit",
"@test:integration"
],
"test:unit": "phpunit -c tests/phpunit.unit.xml",
"test:integration": "phpunit -c tests/phpunit.integration.xml",
"test:setup": "@php tests/integration/setup.php"
},
"scripts-descriptions": {
"test": "Runs all tests.",
"test:unit": "Runs all unit tests.",
"test:integration": "Runs all integration tests.",
"test:setup": "Sets up a database for use with integration tests. Execute this only once."
},
"repositories": [{
"type": "path",
"url": "../"
}]
}

View File

@@ -0,0 +1,20 @@
<?php
/*
* This file is part of flarum/testing-tests.
*
* Copyright (c) 2021 .
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Flarum\Testing;
use Flarum\Extend;
return [
];

View File

@@ -0,0 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":219:{a:2:{s:7:"defects";a:1:{s:74:"Flarum\Testing\Tests\integration\TestCaseTest::can_add_settings_via_method";i:4;}s:5:"times";a:1:{s:74:"Flarum\Testing\Tests\integration\TestCaseTest::can_add_settings_via_method";d:0.09;}}}

View File

View File

@@ -0,0 +1,30 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Testing\Tests\integration;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Testing\integration\TestCase;
class TestCaseTest extends TestCase
{
/**
* @test
*/
public function can_add_settings_via_method()
{
$this->setting('hello', 'world');
$this->setting('display_name_driver', 'something_other_than_username');
$settings = $this->app()->getContainer()->make(SettingsRepositoryInterface::class);
$this->assertEquals('world', $settings->get('hello'));
$this->assertEquals('something_other_than_username', $settings->get('display_name_driver'));
}
}

View File

@@ -0,0 +1,16 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
use Flarum\Testing\integration\Setup\SetupScript;
require __DIR__.'/../../vendor/autoload.php';
$setup = new SetupScript();
$setup->run();

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="true"
stopOnFailure="false"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">../src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Flarum Integration Tests">
<directory suffix="Test.php">./integration</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">../src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Flarum Unit Tests">
<directory suffix="Test.php">./unit</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="\Mockery\Adapter\Phpunit\TestListener" />
</listeners>
</phpunit>