Skip to content

Commit

Permalink
Move tests from Hyde to Framework
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Apr 28, 2022
1 parent 5064680 commit 22ca673
Show file tree
Hide file tree
Showing 47 changed files with 2,994 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/Feature/Actions/GeneratesTableOfContentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Tests\Feature\Actions;

use Hyde\Framework\Actions\GeneratesTableOfContents;
use Tests\TestCase;

/**
* @covers \Hyde\Framework\Actions\GeneratesTableOfContents
*/
class GeneratesTableOfContentsTest extends TestCase
{
public function testCanGenerateTableOfContents()
{
$markdown = "# Level 1\n## Level 2\n## Level 2B\n### Level 3\n";
$result = (new GeneratesTableOfContents($markdown))->execute();

$this->assertIsString($result);
$this->assertStringContainsString('<ul>', $result);
$this->assertStringContainsString('<a href="#level-2">Level 2</a>', $result);
$this->assertStringNotContainsString('[[END_TOC]]', $result);
}

public function testReturnStringContainsExpectedContent()
{
$markdown = "# Level 1\n## Level 2\n### Level 3\n";
$result = (new GeneratesTableOfContents($markdown))->execute();

$this->assertEquals('<ul class="table-of-contents"><li><a href="#level-2">Level 2</a><ul><li><a href="#level-3">Level 3</a></li></ul></li></ul>', str_replace("\n", '', $result));
}
}
10 changes: 10 additions & 0 deletions tests/Feature/Actions/PublishesHomepageViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tests\Feature\Actions;

use Tests\TestCase;

class PublishesHomepageViewTest extends TestCase
{
//
}
183 changes: 183 additions & 0 deletions tests/Feature/AuthorPostsIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php

namespace Tests\Feature;

use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;
use Hyde\Framework\Hyde;
use Tests\TestCase;

/**
* Test that the config/authors.yml feature works in
* conjunction with the static Post generator.
*
* @see AuthorServiceTest
* @see StaticSiteBuilderPostModuleTest
*/
class AuthorPostsIntegrationTest extends TestCase
{
/**
* Set up the test environment.
*
* @return void
*/
public function test_setup_integration_test_environment()
{
// If an authors.yml file exists, back it up.
if (file_exists(Hyde::path('config/authors.yml')) && ! file_exists(Hyde::path('config/authors.yml.bak'))) {
copy(Hyde::path('config/authors.yml'), Hyde::path('config/authors.yml.bak'));
}

// Create a new authors.yml file.
file_put_contents(Hyde::path('config/authors.yml'), "authors:\n");

$this->assertTrue(true);
}

/**
* 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.
*/
public function test_create_post_with_undefined_author()
{
// Create a new post
(new CreatesNewMarkdownPostFile(
title: 'test-2dcbb2c-post-with-undefined-author',
description: '',
category: '',
author: 'test_undefined_author'
))->save(true);

// 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'));

// 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'))
);

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

/**
* Test that a defined author has its name injected into the DOM.
*/
public function test_create_post_with_defined_author_with_name()
{
// 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'));

// Add the author to the authors.yml file
file_put_contents(
Hyde::path('config/authors.yml'),
'authors:
test_named_author:
name: Test Author'
);

// 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'));

// 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'))
);

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

/**
* Test that a defined author with website has its site linked.
*/
public function test_create_post_with_defined_author_with_website()
{
// 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'));

// Add the author to the authors.yml file
file_put_contents(
Hyde::path('config/authors.yml'),
'authors:
test_author_with_website:
name: Test Author
website: 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'));

// 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'))
);

// 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'))
);

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

/**
* Tear down the test environment.
*
* @return void
*/
public function test_teardown_integration_test_environment()
{
// Remove the test authors.yml file.
unlink(Hyde::path('config/authors.yml'));

// If an authors.yml backup exists, restore it.
if (file_exists(Hyde::path('config/authors.yml.bak'))) {
copy(Hyde::path('config/authors.yml.bak'), Hyde::path('config/authors.yml'));
unlink(Hyde::path('config/authors.yml.bak'));
}

$this->assertTrue(true);
}
}
102 changes: 102 additions & 0 deletions tests/Feature/AuthorServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Tests\Feature;

use Hyde\Framework\Models\Author;
use Hyde\Framework\Services\AuthorService;
use Illuminate\Support\Collection;
use Tests\TestCase;

class AuthorServiceTest extends TestCase
{
public function test_setup()
{
// If an authors.yml file exists, back it up and remove it
$service = new AuthorService();
$path = $service->filepath;

backup($path);
unlinkIfExists($path);

$this->assertTrue(true);
}

public function test_publish_file_creates_file()
{
$service = new AuthorService();
$path = $service->filepath;

if (file_exists($path)) {
unlink($path);
}

$service->publishFile();
$this->assertFileExists($path);
}

public function test_get_authors_returns_author_collection()
{
$service = new AuthorService();
$service->publishFile();

$collection = $service->authors;

$this->assertInstanceOf(Collection::class, $collection);

$author = $collection->first();

$this->assertInstanceOf(Author::class, $author);

$this->assertEquals('mr_hyde', $author->username);
$this->assertEquals('Mr Hyde', $author->name);
$this->assertEquals('https://github.com/hydephp/hyde', $author->website);
}

public function test_can_find_author_by_username()
{
$service = new AuthorService();
$service->publishFile();

$author = AuthorService::find('mr_hyde');

$this->assertInstanceOf(Author::class, $author);

$this->assertEquals('mr_hyde', $author->username);
$this->assertEquals('Mr Hyde', $author->name);
$this->assertEquals('https://github.com/hydephp/hyde', $author->website);
}

public function test_get_yaml_can_parse_file()
{
$service = new AuthorService();

$service->publishFile();

$array = $service->getYaml();

$this->assertIsArray($array);

$this->assertEquals([
'authors' => [
'mr_hyde' => [
'name' => 'Mr Hyde',
'website' => 'https://github.com/hydephp/hyde',
],
],
], $array);
}

public function test_teardown()
{
// Clean up any test files
$service = new AuthorService();
$path = $service->filepath;
unlinkIfExists($path);

// Restore the original authors.yml file

restore($path);

$this->assertTrue(true);
}
}
Loading

0 comments on commit 22ca673

Please sign in to comment.