From 76004ed8442c2c285b6c2dae8c090a614e7c1a7f Mon Sep 17 00:00:00 2001 From: IanM <16573496+imorland@users.noreply.github.com> Date: Thu, 27 Jul 2023 11:30:05 +0100 Subject: [PATCH] feat: add `whenExtensionDisabled` to `Conditional` extender (#3847) * feat: add to extender * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot --- framework/core/src/Extend/Conditional.php | 10 ++ .../integration/extenders/ConditionalTest.php | 115 ++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/framework/core/src/Extend/Conditional.php b/framework/core/src/Extend/Conditional.php index 3630063ec..e2c2a6af7 100644 --- a/framework/core/src/Extend/Conditional.php +++ b/framework/core/src/Extend/Conditional.php @@ -30,6 +30,16 @@ class Conditional implements ExtenderInterface }, $extenders); } + /** + * @param ExtenderInterface[] $extenders + */ + public function whenExtensionDisabled(string $extensionId, array $extenders): self + { + return $this->when(function (ExtensionManager $extensions) use ($extensionId) { + return ! $extensions->isEnabled($extensionId); + }, $extenders); + } + public function when(callable|bool $condition, array $extenders): self { $this->conditions[] = [ diff --git a/framework/core/tests/integration/extenders/ConditionalTest.php b/framework/core/tests/integration/extenders/ConditionalTest.php index ec96423e1..c632ba918 100644 --- a/framework/core/tests/integration/extenders/ConditionalTest.php +++ b/framework/core/tests/integration/extenders/ConditionalTest.php @@ -159,4 +159,119 @@ class ConditionalTest extends TestCase $this->app(); } + + /** @test */ + public function conditional_disabled_extension_not_enabled_applies_extender_array() + { + $this->extend( + (new Extend\Conditional()) + ->whenExtensionDisabled('flarum-dummy-extension', [ + (new Extend\ApiSerializer(ForumSerializer::class)) + ->attributes(function () { + return [ + 'customConditionalAttribute' => true + ]; + }) + ]) + ); + + $this->app(); + + $response = $this->send( + $this->request('GET', '/api', [ + 'authenticatedAs' => 1, + ]) + ); + + $payload = json_decode($response->getBody()->getContents(), true); + + $this->assertArrayHasKey('customConditionalAttribute', $payload['data']['attributes']); + } + + /** @test */ + public function conditional_disabled_extension_enabled_does_not_apply_extender_array() + { + $this->extension('flarum-tags'); + + $this->extend( + (new Extend\Conditional()) + ->whenExtensionDisabled('flarum-tags', [ + (new Extend\ApiSerializer(ForumSerializer::class)) + ->attributes(function () { + return [ + 'customConditionalAttribute' => true + ]; + }) + ]) + ); + + $this->app(); + + $response = $this->send( + $this->request('GET', '/api', [ + 'authenticatedAs' => 1, + ]) + ); + + $payload = json_decode($response->getBody()->getContents(), true); + + $this->assertArrayNotHasKey('customConditionalAttribute', $payload['data']['attributes']); + } + + /** @test */ + public function conditional_enabled_extension_disabled_does_not_apply_extender_array() + { + $this->extend( + (new Extend\Conditional()) + ->whenExtensionEnabled('flarum-dummy-extension', [ + (new Extend\ApiSerializer(ForumSerializer::class)) + ->attributes(function () { + return [ + 'customConditionalAttribute' => true + ]; + }) + ]) + ); + + $this->app(); + + $response = $this->send( + $this->request('GET', '/api', [ + 'authenticatedAs' => 1, + ]) + ); + + $payload = json_decode($response->getBody()->getContents(), true); + + $this->assertArrayNotHasKey('customConditionalAttribute', $payload['data']['attributes']); + } + + /** @test */ + public function conditional_enabled_extension_enabled_applies_extender_array() + { + $this->extension('flarum-tags'); + $this->extend( + (new Extend\Conditional()) + ->whenExtensionEnabled('flarum-tags', [ + (new Extend\ApiSerializer(ForumSerializer::class)) + ->attributes(function () { + return [ + 'customConditionalAttribute' => true + ]; + }) + ]) + ); + + $this->app(); + + $response = $this->send( + $this->request('GET', '/api', [ + 'authenticatedAs' => 1, + ]) + ); + + $payload = json_decode($response->getBody()->getContents(), true); + + $this->assertArrayHasKey('customConditionalAttribute', $payload['data']['attributes']); + } }