Extension Tests (#9)

This commit is contained in:
Sami Mazouz 2021-11-08 20:22:07 +01:00 committed by GitHub
parent 5cef9ad52c
commit a9ff884231
17 changed files with 429 additions and 1003 deletions

View File

@ -0,0 +1,5 @@
js/src export-ignore
.git* export-ignore
js/dist/*.js -diff
tests/*

View File

@ -71,6 +71,7 @@ jobs:
DB_PORT: 13306
DB_PASSWORD: root
DB_PREFIX: ${{ matrix.prefix }}
FLARUM_TEST_TMP_DIR_LOCAL: tests/tmp
- name: Run Composer tests
run: composer test

View File

@ -22,7 +22,8 @@
"composer/composer": "^2.0"
},
"require-dev": {
"flarum/testing": "^1.0.0"
"flarum/testing": "^1.0.0",
"flarum/tags": "*"
},
"extra": {
"flarum-extension": {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -13,7 +13,7 @@ use Flarum\Foundation\ErrorHandling\HandledError;
class ComposerCommandFailedExceptionHandler
{
protected const INCOMPATIBLE_REGEX = '/ +- {PACKAGE_NAME} v[0-9.]+ requires flarum\/core/m';
protected const INCOMPATIBLE_REGEX = '/(?:(?: +- {PACKAGE_NAME} v[0-9A-z.-]+ requires flarum\/core)|(?:Could not find a version of package {PACKAGE_NAME} matching your minim))/m';
public function handle(ComposerCommandFailedException $e): HandledError
{

View File

View File

@ -0,0 +1,18 @@
<?php
namespace Flarum\PackageManager\Tests\integration;
trait RefreshComposerSetup
{
public function tearDown(): void
{
$composerSetup = new SetupComposer();
@unlink($this->tmpDir().'/composer.lock');
$composerSetup->run();
$this->composer('install');
parent::tearDown();
}
}

View File

@ -0,0 +1,34 @@
<?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\PackageManager\Tests\integration;
use Flarum\Testing\integration\UsesTmpDir;
class SetupComposer
{
use UsesTmpDir;
public function run()
{
$filePath = $this->tmpDir().'/composer.json';
file_put_contents($filePath, json_encode([
'require' => [
'flarum/core' => '1.0.0',
'flarum/tags' => '1.0.3',
'flarum/lang-english' => '*',
],
'config' => [
'preferred-install' => 'dist',
'sort-packages' => true,
],
], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}
}

View File

@ -0,0 +1,96 @@
<?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\PackageManager\Tests\integration;
use Composer\Config;
use Composer\Console\Application;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\Paths;
use Flarum\PackageManager\Extension\ExtensionUtils;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\NullOutput;
class TestCase extends \Flarum\Testing\integration\TestCase
{
use RetrievesAuthorizedUsers;
protected function setUp(): void
{
parent::setUp();
$this->extension('flarum-package-manager', 'flarum-tags');
$tmp = $this->tmpDir();
$this->app()->getContainer()->instance('flarum.paths', new Paths([
'base' => $tmp,
'public' => "$tmp/public",
'storage' => "$tmp/storage",
'vendor' => "$tmp/vendor",
]));
}
protected function assertExtension(string $id, bool $exists)
{
$installed = json_decode(file_get_contents($this->app()->getContainer()->make(Paths::class)->vendor.'/composer/installed.json'), true);
$installedExtensions = Arr::where($installed['packages'] ?? $installed, function (array $package) {
return $package['type'] === 'flarum-extension';
});
$installedExtensionIds = array_map(function (string $name) {
return ExtensionUtils::nameToId($name);
}, Arr::pluck($installedExtensions, 'name'));
if ($exists) {
$this->assertTrue(in_array($id, $installedExtensionIds), "Failed asserting that extension $id is installed");
} else {
$this->assertFalse(in_array($id, $installedExtensionIds), "Failed asserting that extension $id is not installed");
}
}
protected function assertExtensionExists(string $id)
{
$this->assertExtension($id, true);
}
protected function assertExtensionNotExists(string $id)
{
$this->assertExtension($id, false);
}
protected function requireExtension(string $package)
{
$this->composer("require $package");
}
protected function removeExtension(string $package)
{
$this->composer("remove $package");
}
protected function composer(string $command): void
{
/** @var Application $composer */
$composer = $this->app()->getContainer()->make(Application::class);
$output = new NullOutput();
$composer->run(new StringInput($command), $output);
}
protected function guessedCause(ResponseInterface $response): ?string
{
$json = json_decode($response->getBody()->getContents(), true);
return $json['errors'] ? ($json['errors'][0]['guessed_cause'] ?? null) : null;
}
}

View File

@ -0,0 +1,55 @@
<?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\PackageManager\Tests\integration\api\extensions;
use Flarum\PackageManager\Tests\integration\RefreshComposerSetup;
use Flarum\PackageManager\Tests\integration\TestCase;
class RemoveExtensionTest extends TestCase
{
use RefreshComposerSetup;
/**
* @test
*/
public function extension_installed_by_default()
{
$this->assertExtensionExists('flarum-tags');
}
/**
* @test
*/
public function removing_an_extension_works()
{
$response = $this->send(
$this->request('DELETE', '/api/package-manager/extensions/flarum-tags', [
'authenticatedAs' => 1
])
);
$this->assertEquals(204, $response->getStatusCode());
$this->assertExtensionNotExists('flarum-tags');
}
/**
* @test
*/
public function removing_a_non_existant_extension_fails()
{
$response = $this->send(
$this->request('DELETE', '/api/package-manager/extensions/flarum-potato', [
'authenticatedAs' => 1
])
);
$this->assertEquals(409, $response->getStatusCode());
}
}

View File

@ -0,0 +1,85 @@
<?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\PackageManager\Tests\integration\api\extensions;
use Flarum\PackageManager\Tests\integration\RefreshComposerSetup;
use Flarum\PackageManager\Tests\integration\TestCase;
class RequireExtensionTest extends TestCase
{
use RefreshComposerSetup;
/**
* @test
*/
public function extension_uninstalled_by_default()
{
$this->assertExtensionNotExists('v17development-blog');
}
/**
* @test
*/
public function requiring_an_existing_extension_fails()
{
$response = $this->send(
$this->request('POST', '/api/package-manager/extensions', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'package' => 'flarum/tags'
]
]
])
);
$this->assertEquals(409, $response->getStatusCode());
}
/**
* @test
*/
public function requiring_a_compatible_extension_works()
{
$response = $this->send(
$this->request('POST', '/api/package-manager/extensions', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'package' => 'v17development/flarum-blog'
]
]
])
);
$this->assertEquals(200, $response->getStatusCode());
$this->assertExtensionExists('v17development-blog');
}
/**
* @test
*/
public function requiring_an_uncompatible_extension_fails()
{
$response = $this->send(
$this->request('POST', '/api/package-manager/extensions', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'package' => 'flarum/auth-github'
]
]
])
);
$this->assertEquals(409, $response->getStatusCode());
$this->assertEquals('extension_incompatible_with_instance', $this->guessedCause($response));
}
}

View File

@ -0,0 +1,55 @@
<?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\PackageManager\Tests\integration\api\extensions;
use Flarum\PackageManager\Tests\integration\RefreshComposerSetup;
use Flarum\PackageManager\Tests\integration\TestCase;
class UpdateExtensionTest extends TestCase
{
use RefreshComposerSetup;
/**
* @test
*/
public function extension_installed_by_default()
{
$this->assertExtensionExists('flarum-tags');
}
/**
* @test
*/
public function updating_an_existing_extension_works()
{
$response = $this->send(
$this->request('PATCH', '/api/package-manager/extensions/flarum-tags', [
'authenticatedAs' => 1,
])
);
$this->assertEquals(204, $response->getStatusCode());
$this->assertExtensionExists('flarum-tags');
}
/**
* @test
*/
public function updating_a_non_existing_extension_fails()
{
$response = $this->send(
$this->request('PATCH', '/api/package-manager/extensions/flarum-potato', [
'authenticatedAs' => 1,
])
);
$this->assertEquals(409, $response->getStatusCode());
}
}

View File

@ -0,0 +1,21 @@
<?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;
use Flarum\PackageManager\Tests\integration\SetupComposer;
require __DIR__.'/../../vendor/autoload.php';
$setup = new SetupScript();
$setup->run();
$setupComposer = new SetupComposer();
$setupComposer->run();

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="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>
<php>
<env name="FLARUM_TEST_TMP_DIR_LOCAL" value="tests/tmp" force="true" />
</php>
</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>