Skip to content

Commit 76004ed

Browse files
imorlandStyleCIBot
andauthored
feat: add whenExtensionDisabled to Conditional extender (#3847)
* feat: add to extender * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent e014aa0 commit 76004ed

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

framework/core/src/Extend/Conditional.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ public function whenExtensionEnabled(string $extensionId, array $extenders): sel
3030
}, $extenders);
3131
}
3232

33+
/**
34+
* @param ExtenderInterface[] $extenders
35+
*/
36+
public function whenExtensionDisabled(string $extensionId, array $extenders): self
37+
{
38+
return $this->when(function (ExtensionManager $extensions) use ($extensionId) {
39+
return ! $extensions->isEnabled($extensionId);
40+
}, $extenders);
41+
}
42+
3343
public function when(callable|bool $condition, array $extenders): self
3444
{
3545
$this->conditions[] = [

framework/core/tests/integration/extenders/ConditionalTest.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,119 @@ public function conditional_injects_dependencies_to_condition_callable()
159159

160160
$this->app();
161161
}
162+
163+
/** @test */
164+
public function conditional_disabled_extension_not_enabled_applies_extender_array()
165+
{
166+
$this->extend(
167+
(new Extend\Conditional())
168+
->whenExtensionDisabled('flarum-dummy-extension', [
169+
(new Extend\ApiSerializer(ForumSerializer::class))
170+
->attributes(function () {
171+
return [
172+
'customConditionalAttribute' => true
173+
];
174+
})
175+
])
176+
);
177+
178+
$this->app();
179+
180+
$response = $this->send(
181+
$this->request('GET', '/api', [
182+
'authenticatedAs' => 1,
183+
])
184+
);
185+
186+
$payload = json_decode($response->getBody()->getContents(), true);
187+
188+
$this->assertArrayHasKey('customConditionalAttribute', $payload['data']['attributes']);
189+
}
190+
191+
/** @test */
192+
public function conditional_disabled_extension_enabled_does_not_apply_extender_array()
193+
{
194+
$this->extension('flarum-tags');
195+
196+
$this->extend(
197+
(new Extend\Conditional())
198+
->whenExtensionDisabled('flarum-tags', [
199+
(new Extend\ApiSerializer(ForumSerializer::class))
200+
->attributes(function () {
201+
return [
202+
'customConditionalAttribute' => true
203+
];
204+
})
205+
])
206+
);
207+
208+
$this->app();
209+
210+
$response = $this->send(
211+
$this->request('GET', '/api', [
212+
'authenticatedAs' => 1,
213+
])
214+
);
215+
216+
$payload = json_decode($response->getBody()->getContents(), true);
217+
218+
$this->assertArrayNotHasKey('customConditionalAttribute', $payload['data']['attributes']);
219+
}
220+
221+
/** @test */
222+
public function conditional_enabled_extension_disabled_does_not_apply_extender_array()
223+
{
224+
$this->extend(
225+
(new Extend\Conditional())
226+
->whenExtensionEnabled('flarum-dummy-extension', [
227+
(new Extend\ApiSerializer(ForumSerializer::class))
228+
->attributes(function () {
229+
return [
230+
'customConditionalAttribute' => true
231+
];
232+
})
233+
])
234+
);
235+
236+
$this->app();
237+
238+
$response = $this->send(
239+
$this->request('GET', '/api', [
240+
'authenticatedAs' => 1,
241+
])
242+
);
243+
244+
$payload = json_decode($response->getBody()->getContents(), true);
245+
246+
$this->assertArrayNotHasKey('customConditionalAttribute', $payload['data']['attributes']);
247+
}
248+
249+
/** @test */
250+
public function conditional_enabled_extension_enabled_applies_extender_array()
251+
{
252+
$this->extension('flarum-tags');
253+
$this->extend(
254+
(new Extend\Conditional())
255+
->whenExtensionEnabled('flarum-tags', [
256+
(new Extend\ApiSerializer(ForumSerializer::class))
257+
->attributes(function () {
258+
return [
259+
'customConditionalAttribute' => true
260+
];
261+
})
262+
])
263+
);
264+
265+
$this->app();
266+
267+
$response = $this->send(
268+
$this->request('GET', '/api', [
269+
'authenticatedAs' => 1,
270+
])
271+
);
272+
273+
$payload = json_decode($response->getBody()->getContents(), true);
274+
275+
$this->assertArrayHasKey('customConditionalAttribute', $payload['data']['attributes']);
276+
}
162277
}

0 commit comments

Comments
 (0)