From 6eafce06604c719d68d36767eacb6b1799c69675 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com> Date: Sun, 24 Jan 2021 12:13:27 -0500 Subject: [PATCH] If current package is an extension, add it to the extension manager (#1) Core's ExtensionManager only looks for extensions in the vendor directory, which makes sense for a Flarum instance, but is problematic if used in the context of a test suite for an extension. This PR: - Adds a class extending ExtensionManager to include the current package - Adds an extender that replaces ExtensionManager with this new class in container bindings Effectively, this package can now be used to test extensions. --- .../OverrideExtensionManagerForTests.php | 35 ++++++++++++++++ .../ExtensionManagerIncludeCurrent.php | 42 +++++++++++++++++++ .../testing/src/integration/TestCase.php | 19 ++++++++- 3 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 php-packages/testing/src/integration/Extend/OverrideExtensionManagerForTests.php create mode 100644 php-packages/testing/src/integration/Extension/ExtensionManagerIncludeCurrent.php diff --git a/php-packages/testing/src/integration/Extend/OverrideExtensionManagerForTests.php b/php-packages/testing/src/integration/Extend/OverrideExtensionManagerForTests.php new file mode 100644 index 000000000..a975cdab7 --- /dev/null +++ b/php-packages/testing/src/integration/Extend/OverrideExtensionManagerForTests.php @@ -0,0 +1,35 @@ +extensions = $extensions; + } + + public function extend(Container $container, Extension $extension = null) + { + if (count($this->extensions)) { + $container->bind(ExtensionManager::class, ExtensionManagerIncludeCurrent::class); + $extensionManager = $container->make(ExtensionManager::class); + + foreach ($this->extensions as $extension) { + $extensionManager->enable($extension); + } + + $extensionManager->extend($container); + } + } +} diff --git a/php-packages/testing/src/integration/Extension/ExtensionManagerIncludeCurrent.php b/php-packages/testing/src/integration/Extension/ExtensionManagerIncludeCurrent.php new file mode 100644 index 000000000..0d869b8d8 --- /dev/null +++ b/php-packages/testing/src/integration/Extension/ExtensionManagerIncludeCurrent.php @@ -0,0 +1,42 @@ +filesystem->get($this->paths->vendor . '/../composer.json'), true); + + if (Arr::get($package, 'type') === 'flarum-extension') { + $current = new Extension($this->paths->vendor . '/../', $package); + $current->setInstalled(true); + $current->setVersion(Arr::get($package, 'version')); + $current->calculateDependencies([]); + + $extensions->put($current->getId(), $current); + + $this->extensions = $extensions->sortBy(function ($extension, $name) { + return $extension->composerJsonAttribute('extra.flarum-extension.title'); + }); + } + + return $this->extensions; + } +} diff --git a/php-packages/testing/src/integration/TestCase.php b/php-packages/testing/src/integration/TestCase.php index ad6986a83..7b385b173 100644 --- a/php-packages/testing/src/integration/TestCase.php +++ b/php-packages/testing/src/integration/TestCase.php @@ -13,6 +13,7 @@ use Flarum\Extend\ExtenderInterface; use Flarum\Foundation\Config; use Flarum\Foundation\InstalledSite; use Flarum\Foundation\Paths; +use Flarum\Testing\integration\Extend\OverrideExtensionManagerForTests; use Illuminate\Database\ConnectionInterface; use Laminas\Diactoros\ServerRequest; use Psr\Http\Message\ResponseInterface; @@ -51,10 +52,14 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase 'public' => __DIR__.'/tmp/public', 'storage' => __DIR__.'/tmp/storage', ]), - new Config(include __DIR__.'/tmp/config.php') + new Config(include __DIR__ . '/tmp/config.php') ); - $site->extendWith($this->extenders); + $extenders = array_merge([ + new OverrideExtensionManagerForTests($this->extensions) + ], $this->extenders); + + $site->extendWith($extenders); $this->app = $site->bootApp(); @@ -76,6 +81,16 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase $this->extenders = array_merge($this->extenders, $extenders); } + /** + * @var string[] + */ + protected $extensions = []; + + protected function extension(string ...$extensions) + { + $this->extensions = array_merge($this->extensions, $extensions); + } + /** * @var RequestHandlerInterface */