Skip to content

Commit

Permalink
Utalize the $sourceDirectory property in build services
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed May 15, 2022
1 parent ff9b112 commit 9d9cbff
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/DocumentationPageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DocumentationPageParser extends AbstractPageParser
public function execute(): void
{
$document = (new MarkdownFileService(
Hyde::path("_docs/$this->slug.md")
Hyde::path(DocumentationPage::$sourceDirectory."/$this->slug.md")
))->get();

$this->title = $document->findTitleForDocument();
Expand Down
2 changes: 1 addition & 1 deletion src/MarkdownPageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MarkdownPageParser extends AbstractPageParser
public function execute(): void
{
$document = (new MarkdownFileService(
Hyde::path("_pages/$this->slug.md")
Hyde::path(MarkdownPage::$sourceDirectory."/$this->slug.md")
))->get();

$this->title = $document->findTitleForDocument();
Expand Down
2 changes: 1 addition & 1 deletion src/MarkdownPostParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MarkdownPostParser extends AbstractPageParser
public function execute(): void
{
$document = (new MarkdownFileService(
Hyde::path("_posts/$this->slug.md")
Hyde::path(MarkdownPost::$sourceDirectory."/$this->slug.md")
))->get();

$this->matter = array_merge($document->matter, [
Expand Down
2 changes: 1 addition & 1 deletion src/Models/DocumentationSidebarItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function isHidden(): bool
public static function parseFromFile(string $documentationPageSlug): static
{
$matter = YamlFrontMatter::markdownCompatibleParse(
file_get_contents(Hyde::path('_docs/'.$documentationPageSlug.'.md'))
file_get_contents(Hyde::path(DocumentationPage::$sourceDirectory.'/'.$documentationPageSlug.'.md'))
)->matter();

return new static(
Expand Down
16 changes: 16 additions & 0 deletions tests/Feature/HydeBasePathCanBeChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Tests\Feature;

use Tests\TestCase;

/**
* Class HydeBasePathCanBeChanged.
*/
class HydeBasePathCanBeChanged extends TestCase
{
public function test_example()
{
// TODO: Implement class
}
}
102 changes: 102 additions & 0 deletions tests/Feature/SourceDirectoriesCanBeChangedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,36 @@
namespace Tests\Feature;

use Hyde\Framework\Hyde;
use Hyde\Framework\Models\BladePage;
use Hyde\Framework\Models\DocumentationPage;
use Hyde\Framework\Models\MarkdownPage;
use Hyde\Framework\Models\MarkdownPost;
use Hyde\Framework\Services\BuildService;
use Hyde\Framework\Services\CollectionService;
use Hyde\Framework\StaticPageBuilder;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;
use Tests\TestCase;

/**
* Class SourceDirectoriesCanBeChangedTest.
*/
class SourceDirectoriesCanBeChangedTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

is_dir(Hyde::path('testSourceDir')) || mkdir(Hyde::path('testSourceDir'));
}

protected function tearDown(): void
{
File::deleteDirectory(Hyde::path('testSourceDir'));

parent::tearDown();
}

public function test_post_directory_baseline()
{
$this->assertEquals('_posts', MarkdownPost::$sourceDirectory);
Expand Down Expand Up @@ -50,4 +70,86 @@ public function test_autodiscovery_discovers_posts_in_changed_directory()
unlink(Hyde::path('_posts/test/test.md'));
rmdir(Hyde::path('_posts/test'));
}

public function test_markdown_posts_in_changed_directory_can_be_compiled()
{
mkdir(Hyde::path('testSourceDir/blog'));
touch(Hyde::path('testSourceDir/blog/test.md'));

MarkdownPost::$sourceDirectory = 'testSourceDir/blog';

// Uses the same logic as the BuildActionRunner for an accurate test.
new StaticPageBuilder(
BuildService::getParserInstanceForModel(
MarkdownPost::class,
'test'
)->get(),
true
);

$this->assertFileExists(Hyde::path('_site/posts/test.html'));
unlink(Hyde::path('_site/posts/test.html'));
}

public function test_markdown_pages_in_changed_directory_can_be_compiled()
{
mkdir(Hyde::path('testSourceDir/pages'));
touch(Hyde::path('testSourceDir/pages/test.md'));

MarkdownPage::$sourceDirectory = 'testSourceDir/pages';

// Uses the same logic as the BuildActionRunner for an accurate test.
new StaticPageBuilder(
BuildService::getParserInstanceForModel(
MarkdownPage::class,
'test'
)->get(),
true
);

$this->assertFileExists(Hyde::path('_site/test.html'));
unlink(Hyde::path('_site/test.html'));
}

public function test_documentation_pages_in_changed_directory_can_be_compiled()
{
mkdir(Hyde::path('testSourceDir/documentation'));
touch(Hyde::path('testSourceDir/documentation/test.md'));

DocumentationPage::$sourceDirectory = 'testSourceDir/documentation';

// Uses the same logic as the BuildActionRunner for an accurate test.
new StaticPageBuilder(
BuildService::getParserInstanceForModel(
DocumentationPage::class,
'test'
)->get(),
true
);

$this->assertFileExists(Hyde::path('_site/docs/test.html'));
unlink(Hyde::path('_site/docs/test.html'));
}


public function test_blade_pages_in_changed_directory_can_be_compiled()
{
mkdir(Hyde::path('testSourceDir/blade'));
touch(Hyde::path('testSourceDir/blade/test.blade.php'));

BladePage::$sourceDirectory = 'testSourceDir/blade';
Config::set('view.paths', ['testSourceDir/blade']);

// Uses the same logic as the BuildActionRunner for an accurate test.
new StaticPageBuilder(
BuildService::getParserInstanceForModel(
BladePage::class,
'test'
)->get(),
true
);

$this->assertFileExists(Hyde::path('_site/test.html'));
unlink(Hyde::path('_site/test.html'));
}
}

0 comments on commit 9d9cbff

Please sign in to comment.