diff --git a/php-packages/testing/.github/workflows/test.yml b/php-packages/testing/.github/workflows/test.yml new file mode 100644 index 000000000..b7ba28c30 --- /dev/null +++ b/php-packages/testing/.github/workflows/test.yml @@ -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 diff --git a/php-packages/testing/composer.json b/php-packages/testing/composer.json index eb29a6161..44ea9e07f 100644 --- a/php-packages/testing/composer.json +++ b/php-packages/testing/composer.json @@ -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" diff --git a/php-packages/testing/src/integration/Extend/SetSettingsBeforeBoot.php b/php-packages/testing/src/integration/Extend/SetSettingsBeforeBoot.php new file mode 100644 index 000000000..51957b19a --- /dev/null +++ b/php-packages/testing/src/integration/Extend/SetSettingsBeforeBoot.php @@ -0,0 +1,32 @@ +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); + } + } + } +} diff --git a/php-packages/testing/src/integration/TestCase.php b/php-packages/testing/src/integration/TestCase.php index bba228879..d54a90a37 100644 --- a/php-packages/testing/src/integration/TestCase.php +++ b/php-packages/testing/src/integration/TestCase.php @@ -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 */ diff --git a/php-packages/testing/tests/.gitignore b/php-packages/testing/tests/.gitignore new file mode 100644 index 000000000..208a59913 --- /dev/null +++ b/php-packages/testing/tests/.gitignore @@ -0,0 +1 @@ +vendor/* \ No newline at end of file diff --git a/php-packages/testing/tests/README.md b/php-packages/testing/tests/README.md new file mode 100644 index 000000000..ec93c967f --- /dev/null +++ b/php-packages/testing/tests/README.md @@ -0,0 +1 @@ +A minimal extension skeleton to test the flarum/testing package. \ No newline at end of file diff --git a/php-packages/testing/tests/composer.json b/php-packages/testing/tests/composer.json new file mode 100644 index 000000000..58baed651 --- /dev/null +++ b/php-packages/testing/tests/composer.json @@ -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": "../" + }] +} diff --git a/php-packages/testing/tests/extend.php b/php-packages/testing/tests/extend.php new file mode 100644 index 000000000..8ece8c036 --- /dev/null +++ b/php-packages/testing/tests/extend.php @@ -0,0 +1,20 @@ +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')); + } +} \ No newline at end of file diff --git a/php-packages/testing/tests/tests/integration/setup.php b/php-packages/testing/tests/tests/integration/setup.php new file mode 100644 index 000000000..67039c083 --- /dev/null +++ b/php-packages/testing/tests/tests/integration/setup.php @@ -0,0 +1,16 @@ +run(); diff --git a/php-packages/testing/tests/tests/phpunit.integration.xml b/php-packages/testing/tests/tests/phpunit.integration.xml new file mode 100644 index 000000000..23afc237d --- /dev/null +++ b/php-packages/testing/tests/tests/phpunit.integration.xml @@ -0,0 +1,24 @@ + + + + + ../src/ + + + + + ./integration + + + diff --git a/php-packages/testing/tests/tests/phpunit.unit.xml b/php-packages/testing/tests/tests/phpunit.unit.xml new file mode 100644 index 000000000..d3a4a3e3d --- /dev/null +++ b/php-packages/testing/tests/tests/phpunit.unit.xml @@ -0,0 +1,27 @@ + + + + + ../src/ + + + + + ./unit + + + + + + diff --git a/php-packages/testing/tests/tests/unit/.gitkeep b/php-packages/testing/tests/tests/unit/.gitkeep new file mode 100644 index 000000000..e69de29bb