feat: add whenExtensionDisabled to Conditional extender ()

* feat: add  to  extender

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
This commit is contained in:
IanM 2023-07-27 11:30:05 +01:00 committed by GitHub
parent e014aa0105
commit 76004ed844
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 125 additions and 0 deletions
framework/core
src/Extend
tests/integration/extenders

@ -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[] = [

@ -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']);
}
}