Skip to content

Commit

Permalink
Merge pull request #1777 from hydephp/test-code-cleanup
Browse files Browse the repository at this point in the history
Internal: Clean up and refactor test code
  • Loading branch information
caendesilva authored Jul 3, 2024
2 parents cd55238 + 02393f3 commit 0408032
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 133 deletions.
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

0 comments on commit 0408032

Please sign in to comment.