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: Clean up and refactor test code #1777

Merged
merged 24 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
cc90bb8
Remove newlines from code comments
caendesilva Jul 3, 2024
e9a2832
Reword commends to be more fluent
caendesilva Jul 3, 2024
dab8512
Remove unnecessary identifier from test file name
caendesilva Jul 3, 2024
edea953
Remove unnecessary prefixes from test data
caendesilva Jul 3, 2024
c9f25d9
Remove unnecessary force parameter usage
caendesilva Jul 3, 2024
71a95c4
Remove unnecessary test code comments
caendesilva Jul 3, 2024
123b592
Extract testing helper method
caendesilva Jul 3, 2024
4e30713
Automatically handle test file cleanup
caendesilva Jul 3, 2024
f73bb76
Convert concatenation to string interpolation
caendesilva Jul 3, 2024
7feca1e
Remove unnecessary named arguments
caendesilva Jul 3, 2024
1596490
Remove already made assertions
caendesilva Jul 3, 2024
7d6c3d7
Add 'void' as the function's return type
caendesilva Jul 3, 2024
1198b12
Move down helper method in test
caendesilva Jul 3, 2024
7395ad5
Add 'void' as the function's return type
caendesilva Jul 3, 2024
4532d7a
Move down helper method in test
caendesilva Jul 3, 2024
cede6a0
Use fault tolerant file memory helpers
caendesilva Jul 3, 2024
d8017ac
Normalize whitespace formatting
caendesilva Jul 3, 2024
468c8ec
Replace substring check with faking the Carbon time
caendesilva Jul 3, 2024
07ae03d
Assert against the entire contents of the file
caendesilva Jul 3, 2024
8117c24
Improve and update mock data
caendesilva Jul 3, 2024
45a5175
Normalize file formatting
caendesilva Jul 3, 2024
d661a68
Update year used in file
caendesilva Jul 3, 2024
71ef79b
Revert "Update year used in file"
caendesilva Jul 3, 2024
02393f3
Use fault tolerant file memory helper
caendesilva Jul 3, 2024
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
72 changes: 38 additions & 34 deletions packages/framework/tests/Feature/Commands/MakePostCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Hyde\Framework\Testing\Feature\Commands;

use Hyde\Facades\Filesystem;
use Hyde\Hyde;
use Hyde\Testing\TestCase;
use Illuminate\Support\Carbon;

/**
* @covers \Hyde\Console\Commands\MakePostCommand
Expand All @@ -18,106 +18,110 @@ public function testCommandHasExpectedOutputAndCreatesValidFile()
{
// Assert that no old file exists which would cause issues
$this->assertFileDoesNotExist(Hyde::path('_posts/test-post.md'));
$this->cleanUpWhenDone('_posts/test-post.md');

Carbon::setTestNow(Carbon::create(2024, hour: 12));

$this->artisan('make:post')
->expectsQuestion('What is the title of the post?', 'Test Post')
->expectsQuestion('Write a short post excerpt/description', 'A short description')
->expectsQuestion('What is your (the author\'s) name?', 'PHPUnit')
->expectsQuestion('What is your (the author\'s) name?', 'Mr Hyde')
->expectsQuestion('What is the primary category of the post?', 'general')
->expectsOutput('Creating a post with the following details:')

->expectsOutput('Title: Test Post')
->expectsOutput('Description: A short description')
->expectsOutput('Category: general')
->expectsOutput('Author: PHPUnit')
->expectsOutputToContain('Date: '.date('Y-m-d')) // Don't check min/sec to avoid flaky tests
->expectsOutput('Author: Mr Hyde')
->expectsOutput('Date: 2024-01-01 12:00')
->expectsOutput('Identifier: test-post')

->expectsConfirmation('Do you wish to continue?', 'yes')

->assertExitCode(0);

$this->assertFileExists(Hyde::path('_posts/test-post.md'));
$this->assertStringContainsString(
"title: 'Test Post'",
file_get_contents(Hyde::path('_posts/test-post.md'))
);

Filesystem::unlink('_posts/test-post.md');
$this->assertFileEqualsString(<<<'MARKDOWN'
---
title: 'Test Post'
description: 'A short description'
category: general
author: 'Mr Hyde'
date: '2024-01-01 12:00'
---

## Write something awesome.

MARKDOWN,
Hyde::path('_posts/test-post.md')
);
}

public function testThatFilesAreNotOverwrittenWhenForceFlagIsNotSet()
{
file_put_contents(Hyde::path('_posts/test-post.md'), 'This should not be overwritten');
$this->file('_posts/test-post.md', 'This should not be overwritten');

$this->artisan('make:post')
->expectsQuestion('What is the title of the post?', 'Test Post')
->expectsQuestion('Write a short post excerpt/description', 'A short description')
->expectsQuestion('What is your (the author\'s) name?', 'PHPUnit')
->expectsQuestion('What is your (the author\'s) name?', 'Mr Hyde')
->expectsQuestion('What is the primary category of the post?', 'general')
->expectsOutput('Creating a post with the following details:')

->expectsConfirmation('Do you wish to continue?', 'yes')
->expectsOutput('If you want to overwrite the file supply the --force flag.')

->assertExitCode(409);

$this->assertStringContainsString(
'This should not be overwritten',
file_get_contents(Hyde::path('_posts/test-post.md'))
);

Filesystem::unlink('_posts/test-post.md');
}

public function testThatFilesAreOverwrittenWhenForceFlagIsSet()
{
file_put_contents(Hyde::path('_posts/test-post.md'), 'This should be overwritten');
$this->file('_posts/test-post.md', 'This should be overwritten');

$this->artisan('make:post --force')
->expectsQuestion('What is the title of the post?', 'Test Post')
->expectsQuestion('Write a short post excerpt/description', 'A short description')
->expectsQuestion('What is your (the author\'s) name?', 'PHPUnit')
->expectsQuestion('What is your (the author\'s) name?', 'Mr Hyde')
->expectsQuestion('What is the primary category of the post?', 'general')
->expectsOutput('Creating a post with the following details:')
->expectsConfirmation('Do you wish to continue?', 'yes')

->assertExitCode(0);

$this->assertStringNotContainsString(
'This should be overwritten',
file_get_contents(Hyde::path('_posts/test-post.md'))
);

$this->assertStringContainsString(
"title: 'Test Post'",
file_get_contents(Hyde::path('_posts/test-post.md'))
);

Filesystem::unlink('_posts/test-post.md');
}

public function testThatTitleCanBeSpecifiedInCommandSignature()
{
$this->cleanUpWhenDone('_posts/test-post.md');

$this->artisan('make:post "Test Post"')
->expectsOutputToContain('Selected title: Test Post')
->expectsQuestion('Write a short post excerpt/description', 'A short description')
->expectsQuestion('What is your (the author\'s) name?', 'PHPUnit')
->expectsQuestion('What is your (the author\'s) name?', 'Mr Hyde')
->expectsQuestion('What is the primary category of the post?', 'general')
->expectsConfirmation('Do you wish to continue?', 'yes')

->assertExitCode(0);

Filesystem::unlink('_posts/test-post.md');
}

public function testThatCommandCanBeCanceled()
{
$this->artisan('make:post "Test Post"')
->expectsOutputToContain('Selected title: Test Post')
->expectsQuestion('Write a short post excerpt/description', 'A short description')
->expectsQuestion('What is your (the author\'s) name?', 'PHPUnit')
->expectsQuestion('What is the primary category of the post?', 'general')
->expectsConfirmation('Do you wish to continue?')
->expectsOutput('Aborting.')
->assertExitCode(130);
->expectsOutputToContain('Selected title: Test Post')
->expectsQuestion('Write a short post excerpt/description', 'A short description')
->expectsQuestion('What is your (the author\'s) name?', 'Mr Hyde')
->expectsQuestion('What is the primary category of the post?', 'general')
->expectsConfirmation('Do you wish to continue?')
->expectsOutput('Aborting.')
->assertExitCode(130);

$this->assertFileDoesNotExist(Hyde::path('_posts/test-post.md'));
}
Expand Down
98 changes: 27 additions & 71 deletions packages/framework/tests/Feature/PostsAuthorIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
namespace Hyde\Framework\Testing\Feature;

use Hyde\Facades\Author;
use Hyde\Facades\Filesystem;
use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;
use Hyde\Hyde;
use Hyde\Testing\TestCase;
use Illuminate\Support\Facades\Config;

/**
* Test that the Author feature works in
* conjunction with the static Post generator.
* Test that the Author feature works in conjunction with the static Post generator.
*
* @see StaticSiteBuilderPostModuleTest
*/
Expand All @@ -27,121 +25,79 @@ protected function setUp(): void
}

/**
* Baseline test to create a post without a defined author,
* and assert that the username is displayed as is.
* Baseline test to create a post without a defined author, and assert that the username is displayed as is.
*
* Check that the author was not defined.
* We do this by building the static site and inspecting the DOM.
* Checks that the author was not defined, we do this by building the static site and inspecting the DOM.
*/
public function testCreatePostWithUndefinedAuthor()
{
// Create a new post
(new CreatesNewMarkdownPostFile(
title: 'test-2dcbb2c-post-with-undefined-author',
description: '',
category: '',
author: 'test_undefined_author'
))->save(true);
$this->createPostFile('post-with-undefined-author', 'test_undefined_author');

// Check that the post was created
$this->assertFileExists(Hyde::path('_posts/test-2dcbb2c-post-with-undefined-author.md'));

// Build the static page
$this->artisan('rebuild _posts/test-2dcbb2c-post-with-undefined-author.md')->assertExitCode(0);

// Check that the file was created
$this->assertFileExists(Hyde::path('_site/posts/test-2dcbb2c-post-with-undefined-author.html'));
$this->artisan('rebuild _posts/post-with-undefined-author.md')->assertExitCode(0);
$this->assertFileExists(Hyde::path('_site/posts/post-with-undefined-author.html'));

// Check that the author is rendered as is in the DOM
$this->assertStringContainsString(
'>test_undefined_author</span>',
file_get_contents(Hyde::path('_site/posts/test-2dcbb2c-post-with-undefined-author.html'))
file_get_contents(Hyde::path('_site/posts/post-with-undefined-author.html'))
);

// Remove the test files
Filesystem::unlink('_posts/test-2dcbb2c-post-with-undefined-author.md');
Filesystem::unlink('_site/posts/test-2dcbb2c-post-with-undefined-author.html');
}

/**
* Test that a defined author has its name injected into the DOM.
*/
public function testCreatePostWithDefinedAuthorWithName()
{
// Create a new post
(new CreatesNewMarkdownPostFile(
title: 'test-2dcbb2c-post-with-defined-author-with-name',
description: '',
category: '',
author: 'test_named_author'
))->save(true);

// Check that the post was created
$this->assertFileExists(Hyde::path('_posts/test-2dcbb2c-post-with-defined-author-with-name.md'));
$this->createPostFile('post-with-defined-author-with-name', 'named_author');

Config::set('hyde.authors', [
Author::create('test_named_author', 'Test Author', null),
Author::create('named_author', 'Test Author', null),
]);

// Check that the post was created
$this->assertFileExists(Hyde::path('_posts/test-2dcbb2c-post-with-defined-author-with-name.md'));
// Build the static page
$this->artisan('rebuild _posts/test-2dcbb2c-post-with-defined-author-with-name.md')->assertExitCode(0);
// Check that the file was created
$this->assertFileExists(Hyde::path('_site/posts/test-2dcbb2c-post-with-defined-author-with-name.html'));
$this->artisan('rebuild _posts/post-with-defined-author-with-name.md')->assertExitCode(0);
$this->assertFileExists(Hyde::path('_site/posts/post-with-defined-author-with-name.html'));

// Check that the author is contains the set name in the DOM
$this->assertStringContainsString(
'<span itemprop="name" aria-label="The author\'s name" title=@test_named_author>Test Author</span>',
file_get_contents(Hyde::path('_site/posts/test-2dcbb2c-post-with-defined-author-with-name.html'))
'<span itemprop="name" aria-label="The author\'s name" title=@named_author>Test Author</span>',
file_get_contents(Hyde::path('_site/posts/post-with-defined-author-with-name.html'))
);

// Remove the test files
Filesystem::unlink('_posts/test-2dcbb2c-post-with-defined-author-with-name.md');
Filesystem::unlink('_site/posts/test-2dcbb2c-post-with-defined-author-with-name.html');
}

/**
* Test that a defined author with website has its site linked.
*/
public function testCreatePostWithDefinedAuthorWithWebsite()
{
// Create a new post
(new CreatesNewMarkdownPostFile(
title: 'test-2dcbb2c-post-with-defined-author-with-name',
description: '',
category: '',
author: 'test_author_with_website'
))->save(true);

// Check that the post was created
$this->assertFileExists(Hyde::path('_posts/test-2dcbb2c-post-with-defined-author-with-name.md'));
$this->createPostFile('post-with-defined-author-with-name', 'test_author_with_website');

Config::set('hyde.authors', [
Author::create('test_author_with_website', 'Test Author', 'https://example.org'),
]);

// Check that the post was created
$this->assertFileExists(Hyde::path('_posts/test-2dcbb2c-post-with-defined-author-with-name.md'));
// Build the static page
$this->artisan('rebuild _posts/test-2dcbb2c-post-with-defined-author-with-name.md')->assertExitCode(0);
// Check that the file was created
$this->assertFileExists(Hyde::path('_site/posts/test-2dcbb2c-post-with-defined-author-with-name.html'));
$this->artisan('rebuild _posts/post-with-defined-author-with-name.md')->assertExitCode(0);
$this->assertFileExists(Hyde::path('_site/posts/post-with-defined-author-with-name.html'));

// Check that the author is contains the set name in the DOM
$this->assertStringContainsString(
'<span itemprop="name" aria-label="The author\'s name" title=@test_author_with_website>Test Author</span>',
file_get_contents(Hyde::path('_site/posts/test-2dcbb2c-post-with-defined-author-with-name.html'))
file_get_contents(Hyde::path('_site/posts/post-with-defined-author-with-name.html'))
);

// Check that the author is contains the set website in the DOM
$this->assertStringContainsString(
'<a href="https://example.org" rel="author" itemprop="url" aria-label="The author\'s website">',
file_get_contents(Hyde::path('_site/posts/test-2dcbb2c-post-with-defined-author-with-name.html'))
file_get_contents(Hyde::path('_site/posts/post-with-defined-author-with-name.html'))
);
}

protected function createPostFile(string $title, string $author): void
{
(new CreatesNewMarkdownPostFile($title, '', '', $author))->save();

$this->assertFileExists(Hyde::path("_posts/$title.md"));

// Remove the test files
Filesystem::unlink('_posts/test-2dcbb2c-post-with-defined-author-with-name.md');
Filesystem::unlink('_site/posts/test-2dcbb2c-post-with-defined-author-with-name.html');
$this->cleanUpWhenDone("_posts/$title.md");
$this->cleanUpWhenDone("_site/posts/$title.html");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,6 @@ protected function setUp(): void
);
}

protected function inspectHtml(array $expectedStrings, string $path = null)
{
StaticPageBuilder::handle($this->page);
$stream = file_get_contents(Hyde::path($path ?? '_site/docs/test-page.html'));
$this->cleanUpWhenDone($path ?? '_site/docs/test-page.html');

foreach ($expectedStrings as $expectedString) {
$this->assertStringContainsString($expectedString, $stream);
}
}

public function testCanCreatePage()
{
StaticPageBuilder::handle($this->page);
Expand Down Expand Up @@ -70,4 +59,15 @@ public function testCanCompilePageToRootOutputDirectory()
'<p>So she was considering in her own mind, as well as she could',
], '_site/test-page.html');
}

protected function inspectHtml(array $expectedStrings, string $path = null): void
{
StaticPageBuilder::handle($this->page);
$stream = file_get_contents(Hyde::path($path ?? '_site/docs/test-page.html'));
$this->cleanUpWhenDone($path ?? '_site/docs/test-page.html');

foreach ($expectedStrings as $expectedString) {
$this->assertStringContainsString($expectedString, $stream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ protected function tearDown(): void
parent::tearDown();
}

protected function inspectHtml(array $expectedStrings)
{
StaticPageBuilder::handle($this->post);
$stream = file_get_contents(Hyde::path('_site/posts/test-post.html'));

foreach ($expectedStrings as $expectedString) {
$this->assertStringContainsString($expectedString, $stream);
}
}

public function testCanCreatePost()
{
StaticPageBuilder::handle($this->post);
Expand Down Expand Up @@ -134,4 +124,14 @@ public function testPostImageIsResolvedRelatively()
'<meta itemprop="contentUrl" content="../media/image.png">',
]);
}

protected function inspectHtml(array $expectedStrings): void
{
StaticPageBuilder::handle($this->post);
$stream = file_get_contents(Hyde::path('_site/posts/test-post.html'));

foreach ($expectedStrings as $expectedString) {
$this->assertStringContainsString($expectedString, $stream);
}
}
}
Loading
Loading