Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the Features class mockable so it's easier to test #630

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/framework/src/Concerns/Internal/MockableFeatures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Concerns\Internal;

/**
* @internal
*/
trait MockableFeatures
{
protected static array $mockedInstances = [];

public static function mock(string|array $feature, ?bool $enabled = null): void
{
if (is_array($feature)) {
foreach ($feature as $key => $value) {
static::mock($key, $value);
}

return;
}

static::$mockedInstances[$feature] = $enabled;
}

public static function resolveMockedInstance(string $feature): ?bool
{
return static::$mockedInstances[$feature] ?? null;
}

public static function clearMockedInstances(): void
{
static::$mockedInstances = [];
}
}
8 changes: 5 additions & 3 deletions packages/framework/src/Helpers/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Helpers;

use Hyde\Framework\Concerns\Internal\MockableFeatures;
use Hyde\Framework\Concerns\JsonSerializesArrayable;
use Hyde\Framework\Hyde;
use Hyde\Framework\Services\DiscoveryService;
Expand All @@ -22,6 +23,7 @@
class Features implements Arrayable, JsonSerializable
{
use JsonSerializesArrayable;
use MockableFeatures;

/**
* Determine if the given specified is enabled.
Expand All @@ -31,7 +33,7 @@ class Features implements Arrayable, JsonSerializable
*/
public static function enabled(string $feature): bool
{
return in_array($feature, config('hyde.features', [
return static::resolveMockedInstance($feature) ?? in_array($feature, config('hyde.features', [
// Page Modules
static::htmlPages(),
static::markdownPosts(),
Expand Down Expand Up @@ -162,15 +164,15 @@ public static function torchlight(): string
/** Can a sitemap be generated? */
public static function sitemap(): bool
{
return Hyde::hasSiteUrl()
return static::resolveMockedInstance('sitemap') ?? Hyde::hasSiteUrl()
&& config('site.generate_sitemap', true)
&& extension_loaded('simplexml');
}

/** Can an RSS feed be generated? */
public static function rss(): bool
{
return Hyde::hasSiteUrl()
return static::resolveMockedInstance('rss') ?? Hyde::hasSiteUrl()
&& static::hasMarkdownPosts()
&& config('hyde.generate_rss_feed', true)
&& extension_loaded('simplexml')
Expand Down
37 changes: 37 additions & 0 deletions packages/framework/tests/Feature/ConfigurableFeaturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,41 @@ public function test_to_array_method_returns_method_array()
$this->assertStringStartsNotWith('has', $feature);
}
}

public function test_features_can_be_mocked()
{
Features::mock('darkmode', true);
$this->assertTrue(Features::hasDarkmode());

Features::mock('darkmode', false);
$this->assertFalse(Features::hasDarkmode());
}

public function test_dynamic_features_can_be_mocked()
{
Features::mock('rss', true);
$this->assertTrue(Features::rss());

Features::mock('rss', false);
$this->assertFalse(Features::rss());
}

public function test_multiple_features_can_be_mocked()
{
Features::mock([
'rss' => true,
'darkmode' => true,
]);

$this->assertTrue(Features::rss());
$this->assertTrue(Features::hasDarkmode());

Features::mock([
'rss' => false,
'darkmode' => false,
]);

$this->assertFalse(Features::rss());
$this->assertFalse(Features::hasDarkmode());
}
}
3 changes: 3 additions & 0 deletions packages/testing/src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Hyde\Framework\Actions\ConvertsArrayToFrontMatter;
use Hyde\Framework\Concerns\HydePage;
use Hyde\Framework\Helpers\Features;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Framework\Models\Support\Route;
Expand Down Expand Up @@ -56,6 +57,8 @@ protected function tearDown(): void
Component::forgetFactory();
}

Features::clearMockedInstances();

parent::tearDown();
}

Expand Down