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

Internal: Extract testing helpers and refactor tests #639

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
1 change: 1 addition & 0 deletions src/Console/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Hyde\RealtimeCompiler\ConsoleOutput;
use Illuminate\Support\Facades\Process;

use function rtrim;
use function sprintf;
use function in_array;
use function str_replace;
Expand Down
1 change: 1 addition & 0 deletions src/Support/DataCollections.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use function json_decode;
use function sprintf;
use function unslash;
use function str_starts_with;

/**
* Automatically generates Laravel Collections from static data files,
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Commands/BuildRssFeedCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BuildRssFeedCommandTest extends TestCase
{
public function testRssFeedIsGeneratedWhenConditionsAreMet()
{
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();
config(['hyde.rss.enabled' => true]);
$this->file('_posts/foo.md');

Expand All @@ -29,7 +29,7 @@ public function testRssFeedIsGeneratedWhenConditionsAreMet()

public function testRssFilenameCanBeChanged()
{
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();
config(['hyde.rss.enabled' => true]);
config(['hyde.rss.filename' => 'blog.xml']);
$this->file('_posts/foo.md');
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Commands/BuildSitemapCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BuildSitemapCommandTest extends TestCase
{
public function testSitemapIsGeneratedWhenConditionsAreMet()
{
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();
config(['hyde.generate_sitemap' => true]);

$this->assertFileDoesNotExist(Hyde::path('_site/sitemap.xml'));
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/ConfigurableFeaturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ public function testHasFeatureReturnsTrueWhenFeatureIsEnabled()

public function testCanGenerateSitemapHelperReturnsTrueIfHydeHasBaseUrl()
{
config(['hyde.url' => 'foo']);
$this->withSiteUrl();
$this->assertTrue(Features::sitemap());
}

public function testCanGenerateSitemapHelperReturnsFalseIfHydeDoesNotHaveBaseUrl()
{
config(['hyde.url' => '']);
$this->withoutSiteUrl();
$this->assertFalse(Features::sitemap());
}

public function testCanGenerateSitemapHelperReturnsFalseIfSitemapsAreDisabledInConfig()
{
config(['hyde.url' => 'foo']);
$this->withSiteUrl();
config(['hyde.generate_sitemap' => false]);
$this->assertFalse(Features::sitemap());
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Foundation/HyperlinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testAssetHelperReturnsQualifiedAbsoluteUriWhenRequestedAndSiteHa

public function testAssetHelperReturnsDefaultRelativePathWhenQualifiedAbsoluteUriIsRequestedButSiteHasNoBaseUrl()
{
config(['hyde.url' => null]);
$this->withoutSiteUrl();
$this->assertSame('media/test.jpg', $this->class->asset('test.jpg', true));
}

Expand Down
58 changes: 19 additions & 39 deletions tests/Feature/GlobalMetadataBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
*/
class GlobalMetadataBagTest extends TestCase
{
public function testSiteMetadataAddsConfigDefinedMetadata()
protected function setUp(): void
{
parent::setUp();

$this->withEmptyConfig();
}

public function testSiteMetadataAddsConfigDefinedMetadata()
{
config(['hyde.meta' => [
Meta::link('foo', 'bar'),
Meta::name('foo', 'bar'),
Expand All @@ -43,53 +48,37 @@ public function testSiteMetadataAddsConfigDefinedMetadata()

public function testSiteMetadataAutomaticallyAddsSitemapWhenEnabled()
{
$this->withEmptyConfig();

config(['hyde.url' => 'foo']);
config(['hyde.generate_sitemap' => true]);
config(['hyde.url' => 'foo', 'hyde.generate_sitemap' => true]);

$this->assertSame('<link rel="sitemap" href="foo/sitemap.xml" type="application/xml" title="Sitemap">', GlobalMetadataBag::make()->render());
}

public function testSiteMetadataSitemapUsesConfiguredSiteUrl()
{
$this->withEmptyConfig();

config(['hyde.url' => 'bar']);
config(['hyde.generate_sitemap' => true]);
config(['hyde.url' => 'bar', 'hyde.generate_sitemap' => true]);

$this->assertSame('<link rel="sitemap" href="bar/sitemap.xml" type="application/xml" title="Sitemap">', GlobalMetadataBag::make()->render());
}

public function testSiteMetadataAutomaticallyAddsRssFeedWhenEnabled()
{
$this->withEmptyConfig();

config(['hyde.url' => 'foo']);
config(['hyde.rss.enabled' => true]);
config(['hyde.url' => 'foo', 'hyde.rss.enabled' => true]);
$this->file('_posts/foo.md');

$this->assertSame('<link rel="alternate" href="foo/feed.xml" type="application/rss+xml" title="HydePHP RSS Feed">', GlobalMetadataBag::make()->render());
}

public function testSiteMetadataRssFeedUsesConfiguredSiteUrl()
{
$this->withEmptyConfig();

config(['hyde.url' => 'bar']);
config(['hyde.rss.enabled' => true]);
config(['hyde.url' => 'bar', 'hyde.rss.enabled' => true]);
$this->file('_posts/foo.md');

$this->assertSame('<link rel="alternate" href="bar/feed.xml" type="application/rss+xml" title="HydePHP RSS Feed">', GlobalMetadataBag::make()->render());
}

public function testSiteMetadataRssFeedUsesConfiguredSiteName()
{
$this->withEmptyConfig();

config(['hyde.url' => 'foo']);
config(['hyde.name' => 'Site']);
config(['hyde.rss.enabled' => true]);
config(['hyde.url' => 'foo', 'hyde.name' => 'Site', 'hyde.rss.enabled' => true]);
$config = config('hyde');
unset($config['rss']['description']);
config(['hyde' => $config]);
Expand All @@ -100,11 +89,7 @@ public function testSiteMetadataRssFeedUsesConfiguredSiteName()

public function testSiteMetadataRssFeedUsesConfiguredRssFileName()
{
$this->withEmptyConfig();

config(['hyde.url' => 'foo']);
config(['hyde.rss.filename' => 'posts.rss']);
config(['hyde.rss.enabled' => true]);
config(['hyde.url' => 'foo', 'hyde.rss.filename' => 'posts.rss', 'hyde.rss.enabled' => true]);
$this->file('_posts/foo.md');

$this->assertStringContainsString(
Expand All @@ -115,15 +100,10 @@ public function testSiteMetadataRssFeedUsesConfiguredRssFileName()

public function testMetadataExistingInTheCurrentPageIsNotAdded()
{
$this->withEmptyConfig();

$duplicate = Meta::name('remove', 'me');
$keep = Meta::name('keep', 'this');

config(['hyde.meta' => [
$duplicate,
$keep,
]]);
config(['hyde.meta' => [$duplicate, $keep]]);

$page = new MarkdownPage('foo');
$page->metadata->add($duplicate);
Expand All @@ -136,8 +116,6 @@ public function testMetadataExistingInTheCurrentPageIsNotAdded()

public function testMetadataExistingInTheCurrentPageIsNotAddedRegardlessOfItsValue()
{
$this->withEmptyConfig();

config(['hyde.meta' => [Meta::name('foo', 'bar')]]);

$page = new MarkdownPage('foo');
Expand All @@ -151,9 +129,11 @@ public function testMetadataExistingInTheCurrentPageIsNotAddedRegardlessOfItsVal

protected function withEmptyConfig(): void
{
config(['hyde.url' => null]);
config(['hyde.meta' => []]);
config(['hyde.rss.enabled' => false]);
config(['hyde.generate_sitemap' => false]);
config([
'hyde.url' => null,
'hyde.meta' => [],
'hyde.rss.enabled' => false,
'hyde.generate_sitemap' => false,
]);
}
}
14 changes: 7 additions & 7 deletions tests/Feature/HydePageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,15 +822,15 @@ public function testDocumentationPageCanBeHiddenFromNavigationUsingConfig()

public function testGetCanonicalUrlReturnsUrlForTopLevelPage()
{
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();

$page = new MarkdownPage('foo');
$this->assertSame('https://example.com/foo.html', $page->getCanonicalUrl());
}

public function testGetCanonicalUrlReturnsPrettyUrlForTopLevelPage()
{
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();
config(['hyde.pretty_urls' => true]);

$page = new MarkdownPage('foo');
Expand All @@ -840,7 +840,7 @@ public function testGetCanonicalUrlReturnsPrettyUrlForTopLevelPage()

public function testGetCanonicalUrlReturnsUrlForNestedPage()
{
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();

$page = new MarkdownPage('foo/bar');

Expand All @@ -849,7 +849,7 @@ public function testGetCanonicalUrlReturnsUrlForNestedPage()

public function testGetCanonicalUrlReturnsUrlForDeeplyNestedPage()
{
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();

$page = new MarkdownPage('foo/bar/baz');

Expand All @@ -858,7 +858,7 @@ public function testGetCanonicalUrlReturnsUrlForDeeplyNestedPage()

public function testCanonicalUrlIsNotSetWhenIdentifierIsNull()
{
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();

$page = new MarkdownPage();

Expand All @@ -869,7 +869,7 @@ public function testCanonicalUrlIsNotSetWhenIdentifierIsNull()

public function testCanonicalUrlIsNotSetWhenSiteUrlIsNull()
{
config(['hyde.url' => null]);
$this->withoutSiteUrl();

$page = new MarkdownPage('foo');

Expand All @@ -880,7 +880,7 @@ public function testCanonicalUrlIsNotSetWhenSiteUrlIsNull()

public function testCustomCanonicalLinkCanBeSetInFrontMatter()
{
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();

$page = MarkdownPage::make(matter: ['canonicalUrl' => 'foo/bar']);

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/MetadataHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected function setUp(): void
parent::setUp();

config(['hyde.meta' => []]);
config(['hyde.url' => null]);
$this->withoutSiteUrl();
}

public function testNameMethodReturnsAValidHtmlMetaString()
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/MetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function setUp(): void
{
parent::setUp();

config(['hyde.url' => null]);
$this->withoutSiteUrl();
config(['hyde.meta' => []]);
config(['hyde.rss.enabled' => false]);
config(['hyde.generate_sitemap' => false]);
Expand Down Expand Up @@ -205,7 +205,7 @@ public function testDynamicMetadataOverridesConfigDefinedMetadata()

public function testDoesNotAddCanonicalLinkWhenBaseUrlIsNotSet()
{
config(['hyde.url' => null]);
$this->withoutSiteUrl();

$page = MarkdownPage::make('bar');

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Services/BuildTaskServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BuildTaskServiceTest extends TestCase
*/
public function testBuildCommandCanRunBuildTasks()
{
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();

$this->artisan('build')
->expectsOutputToContain('Removing all files from build directory')
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/Services/RssFeedServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testXmlElementHasChannelElement()
public function testXmlChannelElementHasRequiredElements()
{
config(['hyde.name' => 'Test Blog']);
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();
config(['hyde.rss.description' => 'Test Blog RSS Feed']);

$service = new RssFeedGenerator();
Expand All @@ -57,7 +57,7 @@ public function testXmlChannelElementHasRequiredElements()

public function testXmlChannelElementHasAdditionalElements()
{
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();

$service = new RssFeedGenerator();

Expand Down Expand Up @@ -102,7 +102,7 @@ public function testMarkdownBlogPostsAreAddedToRssFeedThroughAutodiscovery()
MD
);

config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();

file_put_contents(Hyde::path('_media/rss-test.jpg'), 'statData'); // 8 bytes to test stat gets file length

Expand Down Expand Up @@ -150,7 +150,7 @@ public function testCanGenerateFeedHelperReturnsTrueIfHydeHasBaseUrl()

public function testCanGenerateFeedHelperReturnsFalseIfHydeDoesNotHaveBaseUrl()
{
config(['hyde.url' => '']);
$this->withoutSiteUrl();
$this->file('_posts/foo.md');

$this->assertFalse(Features::rss());
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/Services/SitemapServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function testGenerateSitemapShorthandMethodReturnsXmlString()
public function testUrlItemIsGeneratedCorrectly()
{
config(['hyde.pretty_urls' => false]);
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();

Filesystem::touch('_pages/0-test.blade.php');

Expand All @@ -122,7 +122,7 @@ public function testUrlItemIsGeneratedCorrectly()
public function testUrlItemIsGeneratedWithPrettyUrlsIfEnabled()
{
config(['hyde.pretty_urls' => true]);
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();

Filesystem::touch('_pages/0-test.blade.php');

Expand Down Expand Up @@ -170,7 +170,7 @@ public function testAllRouteTypesAreDiscovered()

public function testLinksFallbackToRelativeLinksWhenASiteUrlIsNotSet()
{
config(['hyde.url' => null]);
$this->withoutSiteUrl();

$service = new SitemapGenerator();
$service->generate();
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Services/ValidationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ public function testCheckSiteHasAnAppCssStylesheetCanFail()

public function testCheckSiteHasABaseUrlSetCanPass()
{
config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();

$this->testMethod('check_site_has_a_base_url_set', 0);
}

public function testCheckSiteHasABaseUrlSetCanFail()
{
config(['hyde.url' => null]);
$this->withoutSiteUrl();

$this->testMethod('check_site_has_a_base_url_set', 2);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/SiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public function testUrl()
{
$this->assertSame('http://localhost', Site::url());

config(['hyde.url' => null]);
$this->withoutSiteUrl();
$this->assertNull(Site::url());

config(['hyde.url' => 'https://example.com']);
$this->withSiteUrl();
$this->assertSame('https://example.com', Site::url());
}

Expand Down
Loading
Loading