mirror of
https://github.com/flarum/framework.git
synced 2025-03-31 04:15:15 +08:00
feat: add whenExtensionDisabled
to Conditional
extender (#3847)
* feat: add to extender * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
This commit is contained in:
parent
e014aa0105
commit
76004ed844
framework/core
@ -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']);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user