Skip to content

Commit

Permalink
feat: add to extender
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Jun 28, 2023
1 parent cf70865 commit 7c5bfb0
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
10 changes: 10 additions & 0 deletions framework/core/src/Extend/Conditional.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public function whenExtensionEnabled(string $extensionId, array $extenders): sel
}, $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[] = [
Expand Down
115 changes: 115 additions & 0 deletions framework/core/tests/integration/extenders/ConditionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,119 @@ public function conditional_injects_dependencies_to_condition_callable()

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

0 comments on commit 7c5bfb0

Please sign in to comment.