diff --git a/packages/framework/src/Actions/FindsTitleForDocument.php b/packages/framework/src/Actions/FindsTitleForDocument.php new file mode 100644 index 00000000000..aec60605aef --- /dev/null +++ b/packages/framework/src/Actions/FindsTitleForDocument.php @@ -0,0 +1,35 @@ +get(); + $page = DocumentationPage::parse($slug); return (object) [ 'slug' => $page->slug, diff --git a/packages/framework/src/Actions/SourceFileParser.php b/packages/framework/src/Actions/SourceFileParser.php new file mode 100644 index 00000000000..c47008af10d --- /dev/null +++ b/packages/framework/src/Actions/SourceFileParser.php @@ -0,0 +1,120 @@ +validateExistence($pageClass, $slug); + + $this->slug = $slug; + + $this->page = match ($pageClass) { + BladePage::class => $this->parseBladePage(), + MarkdownPage::class => $this->parseMarkdownPage(), + MarkdownPost::class => $this->parseMarkdownPost(), + DocumentationPage::class => $this->parseDocumentationPage(), + }; + } + + protected function parseBladePage(): BladePage + { + return new BladePage($this->slug); + } + + protected function parseMarkdownPage(): MarkdownPage + { + $document = (new MarkdownFileParser( + Hyde::getMarkdownPagePath($this->slug.MarkdownPage::$fileExtension) + ))->get(); + + $matter = $document->matter; + $body = $document->body; + + return new MarkdownPage( + matter: $matter, + body: $body, + title: FindsTitleForDocument::get($this->slug, $matter, $body), + slug: $this->slug + ); + } + + protected function parseMarkdownPost(): MarkdownPost + { + $document = (new MarkdownFileParser( + Hyde::getMarkdownPostPath($this->slug.MarkdownPost::$fileExtension) + ))->get(); + + $matter = $document->matter; + $body = $document->body; + + return new MarkdownPost( + matter: $matter, + body: $body, + title: FindsTitleForDocument::get($this->slug, $matter, $body), + slug: $this->slug + ); + } + + protected function parseDocumentationPage(): DocumentationPage + { + $document = (new MarkdownFileParser( + Hyde::getDocumentationPagePath($this->slug.DocumentationPage::$fileExtension) + ))->get(); + + $matter = array_merge($document->matter, [ + 'slug' => $this->slug, + ]); + + $body = $document->body; + + return new DocumentationPage( + matter: $matter, + body: $body, + title: FindsTitleForDocument::get($this->slug, $matter, $body), + slug: basename($this->slug), + category: $this->getDocumentationPageCategory($matter), + localPath: $this->slug + ); + } + + protected function getDocumentationPageCategory(array $matter): ?string + { + if (str_contains($this->slug, '/')) { + return Str::before($this->slug, '/'); + } + + return $matter['category'] ?? null; + } + + public function get(): PageContract + { + return $this->page; + } +} diff --git a/packages/framework/src/Concerns/HasDynamicTitle.php b/packages/framework/src/Concerns/HasDynamicTitle.php index 7afeb7e105c..ed576ddb5b6 100644 --- a/packages/framework/src/Concerns/HasDynamicTitle.php +++ b/packages/framework/src/Concerns/HasDynamicTitle.php @@ -10,6 +10,8 @@ * First check the front matter for a title. If one is not found, * it searches the Markdown for a level one heading. Falls back to * generating a title from the slug if no other title could be found. + * + * @deprecated Move to action to generate when constructing a page. */ trait HasDynamicTitle { diff --git a/packages/framework/src/Contracts/AbstractMarkdownPage.php b/packages/framework/src/Contracts/AbstractMarkdownPage.php index 068c0ad9746..12cf1419e41 100644 --- a/packages/framework/src/Contracts/AbstractMarkdownPage.php +++ b/packages/framework/src/Contracts/AbstractMarkdownPage.php @@ -20,7 +20,7 @@ * * @test \Hyde\Framework\Testing\Feature\AbstractPageTest */ -abstract class AbstractMarkdownPage extends AbstractPage implements MarkdownPageContract +abstract class AbstractMarkdownPage extends AbstractPage { use HasDynamicTitle; diff --git a/packages/framework/src/Contracts/AbstractPage.php b/packages/framework/src/Contracts/AbstractPage.php index b7432a594cb..40d42b04581 100644 --- a/packages/framework/src/Contracts/AbstractPage.php +++ b/packages/framework/src/Contracts/AbstractPage.php @@ -2,6 +2,7 @@ namespace Hyde\Framework\Contracts; +use Hyde\Framework\Actions\SourceFileParser; use Hyde\Framework\Concerns\CanBeInNavigation; use Hyde\Framework\Concerns\HasPageMetadata; use Hyde\Framework\Models\Route; @@ -27,7 +28,7 @@ abstract class AbstractPage implements PageContract, CompilableContract public static string $sourceDirectory; public static string $outputDirectory; public static string $fileExtension; - public static string $parserClass; + public static string $template; /** @inheritDoc */ @@ -49,21 +50,9 @@ final public static function getFileExtension(): string } /** @inheritDoc */ - final public static function getParserClass(): string - { - return static::$parserClass; - } - - /** @inheritDoc */ - public static function getParser(string $slug): PageParserContract - { - return new static::$parserClass($slug); - } - - /** @inheritDoc */ - public static function parse(string $slug): static + public static function parse(string $slug): PageContract { - return (new static::$parserClass($slug))->get(); + return (new SourceFileParser(static::class, $slug))->get(); } /** @inheritDoc */ diff --git a/packages/framework/src/Contracts/AbstractPageParser.php b/packages/framework/src/Contracts/AbstractPageParser.php deleted file mode 100644 index 8530ff6740f..00000000000 --- a/packages/framework/src/Contracts/AbstractPageParser.php +++ /dev/null @@ -1,43 +0,0 @@ -slug = $slug; - $this->validateExistence($this->pageModel, $slug); - $this->execute(); - } -} diff --git a/packages/framework/src/Contracts/MarkdownPageContract.php b/packages/framework/src/Contracts/MarkdownPageContract.php deleted file mode 100644 index d5cf16e60ea..00000000000 --- a/packages/framework/src/Contracts/MarkdownPageContract.php +++ /dev/null @@ -1,15 +0,0 @@ - - */ - public static function getParserClass(): string; - - /** - * Create and return a new PageParser instance for this model, - * with the given slug passed to the constructor. - */ - public static function getParser(string $slug): PageParserContract; - /** * Parse a source file slug into a page model. * @@ -48,7 +35,7 @@ public static function getParser(string $slug): PageParserContract; * * @see \Hyde\Framework\Testing\Unit\PageModelParseHelperTest */ - public static function parse(string $slug): static; + public static function parse(string $slug): PageContract; /** * Get an array of all the source file slugs for the model. diff --git a/packages/framework/src/Contracts/PageParserContract.php b/packages/framework/src/Contracts/PageParserContract.php deleted file mode 100644 index 4e2b7bc2103..00000000000 --- a/packages/framework/src/Contracts/PageParserContract.php +++ /dev/null @@ -1,20 +0,0 @@ -slug.md") - ))->get(); - - $this->body = $document->body; - $this->matter = $document->matter; - } - - public function get(): DocumentationPage - { - return new DocumentationPage( - matter: $this->matter, - body: $this->body, - title: $this->title, - slug: basename($this->slug), - category: $this->getCategory(), - localPath: $this->slug - ); - } - - public function getCategory(): ?string - { - if (str_contains($this->slug, '/')) { - return Str::before($this->slug, '/'); - } - - return $this->matter['category'] ?? null; - } -} diff --git a/packages/framework/src/Models/Parsers/MarkdownPageParser.php b/packages/framework/src/Models/Parsers/MarkdownPageParser.php deleted file mode 100644 index 10206a30479..00000000000 --- a/packages/framework/src/Models/Parsers/MarkdownPageParser.php +++ /dev/null @@ -1,44 +0,0 @@ -slug.md") - ))->get(); - - $this->matter = $document->matter; - $this->body = $document->body; - } - - public function get(): MarkdownPage - { - return new MarkdownPage( - matter: $this->matter, - body: $this->body, - title: $this->title, - slug: $this->slug - ); - } -} diff --git a/packages/framework/src/Models/Parsers/MarkdownPostParser.php b/packages/framework/src/Models/Parsers/MarkdownPostParser.php deleted file mode 100644 index 9ed92c12fbf..00000000000 --- a/packages/framework/src/Models/Parsers/MarkdownPostParser.php +++ /dev/null @@ -1,42 +0,0 @@ -slug.md") - ))->get(); - - $this->matter = array_merge($document->matter, [ - 'slug' => $this->slug, - ]); - - $this->body = $document->body; - } - - public function get(): MarkdownPost - { - return new MarkdownPost( - matter: $this->matter, - body: $this->body, - title: $this->title, - slug: $this->slug - ); - } -} diff --git a/packages/framework/src/Modules/Markdown/MarkdownFileParser.php b/packages/framework/src/Modules/Markdown/MarkdownFileParser.php index 75b2693b2b2..e8602ca22c1 100644 --- a/packages/framework/src/Modules/Markdown/MarkdownFileParser.php +++ b/packages/framework/src/Modules/Markdown/MarkdownFileParser.php @@ -36,6 +36,9 @@ public function __construct(string $filepath) if ($object->matter()) { $this->matter = $object->matter(); + + // Unset the slug from the matter, as it can cause problems if it exists. + unset($this->matter['slug']); } if ($object->body()) { diff --git a/packages/framework/src/Services/DiscoveryService.php b/packages/framework/src/Services/DiscoveryService.php index 2aab6ed482a..053243b3c44 100644 --- a/packages/framework/src/Services/DiscoveryService.php +++ b/packages/framework/src/Services/DiscoveryService.php @@ -3,7 +3,6 @@ namespace Hyde\Framework\Services; use Hyde\Framework\Contracts\AbstractPage; -use Hyde\Framework\Contracts\PageParserContract; use Hyde\Framework\Exceptions\UnsupportedPageTypeException; use Hyde\Framework\Hyde; use Hyde\Framework\Models\Pages\BladePage; @@ -22,28 +21,6 @@ */ class DiscoveryService { - public static function getParserClassForModel(string $model): string - { - /** @var AbstractPage $model */ - return $model::getParserClass(); - } - - /** - * Create and get a constructed instance of a Model's Parser class. - * - * @param string $model Class constant of the Model to get the Parser for. - * @param string $slug The slug of the source file to parse. - * - * @example getParserForModel(MarkdownPost::class, 'hello-world') - * - * @return PageParserContract The constructed Parser instance. - */ - public static function getParserInstanceForModel(string $model, string $slug): PageParserContract - { - /** @var AbstractPage $model */ - return new $model::$parserClass($slug); - } - /** * Supply a model::class constant and get a list of all the existing source file base names. * diff --git a/packages/framework/tests/Feature/AbstractPageTest.php b/packages/framework/tests/Feature/AbstractPageTest.php index 8769665112b..53c98deb568 100644 --- a/packages/framework/tests/Feature/AbstractPageTest.php +++ b/packages/framework/tests/Feature/AbstractPageTest.php @@ -12,8 +12,6 @@ use Hyde\Framework\Models\Pages\DocumentationPage; use Hyde\Framework\Models\Pages\MarkdownPage; use Hyde\Framework\Models\Pages\MarkdownPost; -use Hyde\Framework\Models\Parsers\MarkdownPageParser; -use Hyde\Framework\Models\Parsers\MarkdownPostParser; use Hyde\Framework\Models\Route; use Hyde\Testing\TestCase; @@ -67,32 +65,6 @@ public function test_get_file_extension_forces_leading_period() $this->assertEquals('.foo', MarkdownPage::getFileExtension()); } - public function test_get_parser_class_returns_static_property() - { - MarkdownPage::$parserClass = 'foo'; - $this->assertEquals('foo', MarkdownPage::getParserClass()); - } - - public function test_get_parser_returns_the_configured_parser_class() - { - Hyde::touch(('_posts/foo.md')); - - MarkdownPage::$parserClass = MarkdownPostParser::class; - $this->assertInstanceOf(MarkdownPostParser::class, MarkdownPage::getParser('foo')); - - unlink(Hyde::path('_posts/foo.md')); - } - - public function test_get_parser_returns_instantiated_parser_for_the_supplied_slug() - { - Hyde::touch(('_pages/foo.md')); - - $this->assertInstanceOf(MarkdownPageParser::class, $parser = MarkdownPage::getParser('foo')); - $this->assertEquals('foo', $parser->get()->slug); - - unlink(Hyde::path('_pages/foo.md')); - } - public function test_parse_parses_supplied_slug_into_a_page_model() { Hyde::touch(('_pages/foo.md')); @@ -255,20 +227,6 @@ public function test_all_page_models_have_configured_file_extension() } } - public function test_all_page_models_have_configured_parser_class() - { - $pages = [ - BladePage::class => 'Hyde\Framework\Models\Pages\BladePage', - MarkdownPage::class => 'Hyde\Framework\Models\Parsers\MarkdownPageParser', - MarkdownPost::class => 'Hyde\Framework\Models\Parsers\MarkdownPostParser', - DocumentationPage::class => 'Hyde\Framework\Models\Parsers\DocumentationPageParser', - ]; - - foreach ($pages as $page => $expected) { - $this->assertEquals($expected, $page::$parserClass); - } - } - public function test_abstract_markdown_page_extends_abstract_page() { $this->assertInstanceOf(AbstractPage::class, new class extends AbstractMarkdownPage {}); diff --git a/packages/framework/tests/Feature/DiscoveryServiceTest.php b/packages/framework/tests/Feature/DiscoveryServiceTest.php index dbee2965d76..8f4a5959b3f 100644 --- a/packages/framework/tests/Feature/DiscoveryServiceTest.php +++ b/packages/framework/tests/Feature/DiscoveryServiceTest.php @@ -8,9 +8,6 @@ use Hyde\Framework\Models\Pages\DocumentationPage; use Hyde\Framework\Models\Pages\MarkdownPage; use Hyde\Framework\Models\Pages\MarkdownPost; -use Hyde\Framework\Models\Parsers\DocumentationPageParser; -use Hyde\Framework\Models\Parsers\MarkdownPageParser; -use Hyde\Framework\Models\Parsers\MarkdownPostParser; use Hyde\Framework\Services\DiscoveryService; use Hyde\Testing\TestCase; use Illuminate\Support\Facades\File; @@ -33,26 +30,6 @@ public function deleteContentSourceTestFiles() unlink(Hyde::path(DiscoveryService::getModelSourceDirectory(BladePage::class).'/test.blade.php')); } - public function test_get_parser_class_for_model() - { - $this->assertEquals(MarkdownPageParser::class, DiscoveryService::getParserClassForModel(MarkdownPage::class)); - $this->assertEquals(MarkdownPostParser::class, DiscoveryService::getParserClassForModel(MarkdownPost::class)); - $this->assertEquals(DocumentationPageParser::class, DiscoveryService::getParserClassForModel(DocumentationPage::class)); - $this->assertEquals(BladePage::class, DiscoveryService::getParserClassForModel(BladePage::class)); - } - - public function test_get_parser_instance_for_model() - { - $this->createContentSourceTestFiles(); - - $this->assertInstanceOf(MarkdownPageParser::class, DiscoveryService::getParserInstanceForModel(MarkdownPage::class, 'test')); - $this->assertInstanceOf(MarkdownPostParser::class, DiscoveryService::getParserInstanceForModel(MarkdownPost::class, 'test')); - $this->assertInstanceOf(DocumentationPageParser::class, DiscoveryService::getParserInstanceForModel(DocumentationPage::class, 'test')); - $this->assertInstanceOf(BladePage::class, DiscoveryService::getParserInstanceForModel(BladePage::class, 'test')); - - $this->deleteContentSourceTestFiles(); - } - public function test_get_file_extension_for_model_files() { $this->assertEquals('.md', DiscoveryService::getModelFileExtension(MarkdownPage::class)); diff --git a/packages/framework/tests/Feature/DocumentationPageParserTest.php b/packages/framework/tests/Feature/DocumentationPageParserTest.php deleted file mode 100644 index 387aa53c9a1..00000000000 --- a/packages/framework/tests/Feature/DocumentationPageParserTest.php +++ /dev/null @@ -1,106 +0,0 @@ -get(); - $this->assertInstanceOf(DocumentationPage::class, $page); - unlink(Hyde::path('_docs/test.md')); - } - - public function test_can_get_collection_of_slugs() - { - $this->resetDocs(); - - file_put_contents(Hyde::path('_docs/phpunit-test.md'), "# PHPUnit Test File \n Hello World!"); - - $array = DiscoveryService::getDocumentationPageFiles(); - - $this->assertIsArray($array); - $this->assertCount(1, $array); - $this->assertArrayHasKey('phpunit-test', array_flip($array)); - } - - public function test_exception_is_thrown_for_missing_slug() - { - $this->expectException(Exception::class); - $this->expectExceptionMessage('File _docs/invalid-file.md not found.'); - new DocumentationPageParser('invalid-file'); - } - - public function test_can_parse_documentation_page() - { - $parser = new DocumentationPageParser('phpunit-test'); - $this->assertInstanceOf(DocumentationPageParser::class, $parser); - } - - public function test_title_was_inferred_from_heading() - { - $parser = new DocumentationPageParser('phpunit-test'); - $object = $parser->get(); - $this->assertIsString($object->title); - $this->assertEquals('PHPUnit Test File', $object->title); - } - - public function test_parser_contains_body_text() - { - $parser = new DocumentationPageParser('phpunit-test'); - $this->assertIsString($parser->body); - $this->assertEquals("# PHPUnit Test File \n Hello World!", $parser->body); - } - - public function test_can_get_page_model_object() - { - $parser = new DocumentationPageParser('phpunit-test'); - $object = $parser->get(); - $this->assertInstanceOf(DocumentationPage::class, $object); - } - - public function test_created_model_contains_expected_data() - { - $parser = new DocumentationPageParser('phpunit-test'); - $object = $parser->get(); - $this->assertEquals('PHPUnit Test File', $object->title); - $this->assertEquals("# PHPUnit Test File \n Hello World!", $object->body); - $this->assertEquals('phpunit-test', $object->slug); - } - - public function test_cleanup() - { - unlink(Hyde::path('_docs/phpunit-test.md')); - $this->assertTrue(true); - } - - public function test_can_get_category_from_front_matter() - { - file_put_contents(Hyde::path('_docs/foo.md'), "---\ncategory: foo\n---\n"); - $parser = new DocumentationPageParser('foo'); - $this->assertEquals('foo', $parser->getCategory()); - unlink(Hyde::path('_docs/foo.md')); - } - - public function test_can_get_category_automatically_from_nested_page() - { - mkdir(Hyde::path('_docs/foo')); - touch(Hyde::path('_docs/foo/bar.md')); - $parser = new DocumentationPageParser('foo/bar'); - $this->assertEquals('foo', $parser->getCategory()); - - unlink(Hyde::path('_docs/foo/bar.md')); - rmdir(Hyde::path('_docs/foo')); - } -} diff --git a/packages/framework/tests/Feature/MarkdownPageTest.php b/packages/framework/tests/Feature/MarkdownPageTest.php index 857cd74d350..f3b85d95188 100644 --- a/packages/framework/tests/Feature/MarkdownPageTest.php +++ b/packages/framework/tests/Feature/MarkdownPageTest.php @@ -2,10 +2,8 @@ namespace Hyde\Framework\Testing\Feature; -use Exception; use Hyde\Framework\Hyde; use Hyde\Framework\Models\Pages\MarkdownPage; -use Hyde\Framework\Models\Parsers\MarkdownPageParser; use Hyde\Framework\Services\DiscoveryService; use Hyde\Testing\TestCase; use Illuminate\Support\Facades\File; @@ -33,9 +31,6 @@ protected function tearDown(): void parent::tearDown(); } - /** - * Test the Parser. - */ public function test_can_get_collection_of_slugs() { $array = DiscoveryService::getMarkdownPageFiles(); @@ -45,53 +40,12 @@ public function test_can_get_collection_of_slugs() $this->assertArrayHasKey('test-post', array_flip($array)); } - public function test_exception_is_thrown_for_missing_slug() + public function test_created_model_contains_expected_data() { - $this->expectException(Exception::class); - $this->expectExceptionMessage('File _pages/invalid-file.md not found.'); - new MarkdownPageParser('invalid-file'); - } - - public function test_can_parse_documentation_page() - { - $parser = new MarkdownPageParser('test-post'); - $this->assertInstanceOf(MarkdownPageParser::class, $parser); - } + $page = MarkdownPage::parse('test-post'); - public function test_title_was_inferred_from_heading() - { - $parser = new MarkdownPageParser('test-post'); - $object = $parser->get(); - $this->assertIsString($object->title); - $this->assertEquals('PHPUnit Test File', $object->title); - } - - public function test_parser_contains_body_text() - { - $parser = new MarkdownPageParser('test-post'); - $this->assertIsString($parser->body); - $this->assertEquals("# PHPUnit Test File \n Hello World!", $parser->body); - } - - /** - * Test the Model. - */ - public function test_can_get_page_model_object(): MarkdownPage - { - $parser = new MarkdownPageParser('test-post'); - $object = $parser->get(); - $this->assertInstanceOf(MarkdownPage::class, $object); - - return $object; - } - - /** - * @depends test_can_get_page_model_object - */ - public function test_created_model_contains_expected_data(MarkdownPage $object) - { - $this->assertEquals('PHPUnit Test File', $object->title); - $this->assertEquals("# PHPUnit Test File \n Hello World!", $object->body); - $this->assertEquals('test-post', $object->slug); + $this->assertEquals('PHPUnit Test File', $page->title); + $this->assertEquals("# PHPUnit Test File \n Hello World!", $page->body); + $this->assertEquals('test-post', $page->slug); } } diff --git a/packages/framework/tests/Feature/SourceFileParserTest.php b/packages/framework/tests/Feature/SourceFileParserTest.php new file mode 100644 index 00000000000..5709f58d3eb --- /dev/null +++ b/packages/framework/tests/Feature/SourceFileParserTest.php @@ -0,0 +1,89 @@ +file('_pages/foo.blade.php'); + + $parser = new SourceFileParser(BladePage::class, 'foo'); + $page = $parser->get(); + $this->assertInstanceOf(BladePage::class, $page); + $this->assertEquals('foo', $page->slug); + } + + public function test_markdown_page_parser() + { + $this->markdown('_pages/foo.md', '# Foo Bar', ['title' => 'Foo Bar Baz']); + + $parser = new SourceFileParser(MarkdownPage::class, 'foo'); + $page = $parser->get(); + $this->assertInstanceOf(MarkdownPage::class, $page); + $this->assertEquals('foo', $page->slug); + $this->assertEquals('# Foo Bar', $page->body); + $this->assertEquals('Foo Bar Baz', $page->title); + } + + public function test_markdown_post_parser() + { + $this->markdown('_posts/foo.md', '# Foo Bar', ['title' => 'Foo Bar Baz']); + + $parser = new SourceFileParser(MarkdownPost::class, 'foo'); + $page = $parser->get(); + $this->assertInstanceOf(MarkdownPost::class, $page); + $this->assertEquals('foo', $page->slug); + $this->assertEquals('# Foo Bar', $page->body); + $this->assertEquals('Foo Bar Baz', $page->title); + } + + public function test_documentation_page_parser() + { + $this->markdown('_docs/foo.md', '# Foo Bar', ['title' => 'Foo Bar Baz']); + + $parser = new SourceFileParser(DocumentationPage::class, 'foo'); + $page = $parser->get(); + $this->assertInstanceOf(DocumentationPage::class, $page); + $this->assertEquals('foo', $page->slug); + $this->assertEquals('# Foo Bar', $page->body); + $this->assertEquals('Foo Bar Baz', $page->title); + } + + public function test_documentation_page_parser_can_get_category_from_front_matter() + { + $this->markdown('_docs/foo.md', '# Foo Bar', ['category' => 'foo']); + + $parser = new SourceFileParser(DocumentationPage::class, 'foo'); + + /** @var DocumentationPage $page */ + $page = $parser->get(); + $this->assertEquals('foo', $page->category); + } + + public function test_documentation_page_parser_can_get_category_automatically_from_nested_page() + { + mkdir(Hyde::path('_docs/foo')); + touch(Hyde::path('_docs/foo/bar.md')); + + $parser = new SourceFileParser(DocumentationPage::class, 'foo/bar'); + + /** @var DocumentationPage $page */ + $page = $parser->get(); + $this->assertEquals('foo', $page->category); + + unlink(Hyde::path('_docs/foo/bar.md')); + rmdir(Hyde::path('_docs/foo')); + } +} diff --git a/packages/framework/tests/Unit/InteractsWithDirectoriesConcernTest.php b/packages/framework/tests/Unit/InteractsWithDirectoriesConcernTest.php index 544becac2e1..cd304cb59cc 100644 --- a/packages/framework/tests/Unit/InteractsWithDirectoriesConcernTest.php +++ b/packages/framework/tests/Unit/InteractsWithDirectoriesConcernTest.php @@ -60,5 +60,7 @@ public function test_needs_directories_creates_multiple_directories() $this->needsDirectories([Hyde::path('foo'), Hyde::path('bar')]); $this->assertDirectoryExists(Hyde::path('foo')); $this->assertDirectoryExists(Hyde::path('bar')); + + File::deleteDirectory(Hyde::path('bar')); } } diff --git a/packages/framework/tests/Unit/MarkdownPostParserTest.php b/packages/framework/tests/Unit/MarkdownPostParserTest.php index 3b0d95bd964..dc1e1a51a94 100644 --- a/packages/framework/tests/Unit/MarkdownPostParserTest.php +++ b/packages/framework/tests/Unit/MarkdownPostParserTest.php @@ -4,7 +4,6 @@ use Hyde\Framework\Hyde; use Hyde\Framework\Models\Pages\MarkdownPost; -use Hyde\Framework\Models\Parsers\MarkdownPostParser; use Hyde\Testing\TestCase; /** @@ -28,9 +27,9 @@ protected function tearDown(): void public function test_can_parse_markdown_file() { - $post = (new MarkdownPostParser('test-post'))->get(); + $post = MarkdownPost::parse('test-post'); $this->assertInstanceOf(MarkdownPost::class, $post); - $this->assertCount(4, ($post->matter)); + $this->assertCount(3, ($post->matter)); $this->assertIsArray($post->matter); $this->assertIsString($post->body); $this->assertIsString($post->slug); @@ -40,10 +39,9 @@ public function test_can_parse_markdown_file() public function test_parsed_markdown_post_contains_valid_front_matter() { - $post = (new MarkdownPostParser('test-post'))->get(); + $post = MarkdownPost::parse('test-post'); $this->assertEquals('My New Post', $post->matter['title']); $this->assertEquals('Mr. Hyde', $post->matter['author']); $this->assertEquals('blog', $post->matter['category']); - $this->assertEquals('test-post', $post->matter['slug']); } } diff --git a/packages/framework/tests/Unit/PageModelParseHelperTest.php b/packages/framework/tests/Unit/PageModelParseHelperTest.php index 08b86d2e647..b8dab4a4b4b 100644 --- a/packages/framework/tests/Unit/PageModelParseHelperTest.php +++ b/packages/framework/tests/Unit/PageModelParseHelperTest.php @@ -16,8 +16,12 @@ class PageModelParseHelperTest extends TestCase { public function test_blade_page_get_helper_returns_blade_page_object() { + Hyde::touch(('_pages/foo.blade.php')); + $object = BladePage::parse('foo'); $this->assertInstanceOf(BladePage::class, $object); + + unlink(Hyde::path('_pages/foo.blade.php')); } public function test_markdown_page_get_helper_returns_markdown_page_object() diff --git a/packages/framework/tests/Unit/SourceFilesInCustomDirectoriesCanBeCompiledTest.php b/packages/framework/tests/Unit/SourceFilesInCustomDirectoriesCanBeCompiledTest.php index 09e0a3fe201..74055157c6c 100644 --- a/packages/framework/tests/Unit/SourceFilesInCustomDirectoriesCanBeCompiledTest.php +++ b/packages/framework/tests/Unit/SourceFilesInCustomDirectoriesCanBeCompiledTest.php @@ -7,7 +7,6 @@ use Hyde\Framework\Models\Pages\DocumentationPage; use Hyde\Framework\Models\Pages\MarkdownPage; use Hyde\Framework\Models\Pages\MarkdownPost; -use Hyde\Framework\Services\DiscoveryService; use Hyde\Framework\StaticPageBuilder; use Hyde\Testing\TestCase; use Illuminate\Support\Facades\Config; @@ -39,12 +38,8 @@ public function test_markdown_posts_in_changed_directory_can_be_compiled() MarkdownPost::$sourceDirectory = 'testSourceDir/blog'; - // Uses the same logic as the BuildActionRunner for an accurate test. new StaticPageBuilder( - DiscoveryService::getParserInstanceForModel( - MarkdownPost::class, - 'test' - )->get(), + MarkdownPost::parse('test'), true ); @@ -59,12 +54,8 @@ public function test_markdown_pages_in_changed_directory_can_be_compiled() MarkdownPage::$sourceDirectory = 'testSourceDir/pages'; - // Uses the same logic as the BuildActionRunner for an accurate test. new StaticPageBuilder( - DiscoveryService::getParserInstanceForModel( - MarkdownPage::class, - 'test' - )->get(), + MarkdownPage::parse('test'), true ); @@ -79,12 +70,8 @@ public function test_documentation_pages_in_changed_directory_can_be_compiled() DocumentationPage::$sourceDirectory = 'testSourceDir/documentation'; - // Uses the same logic as the BuildActionRunner for an accurate test. new StaticPageBuilder( - DiscoveryService::getParserInstanceForModel( - DocumentationPage::class, - 'test' - )->get(), + DocumentationPage::parse('test'), true ); @@ -100,12 +87,8 @@ public function test_blade_pages_in_changed_directory_can_be_compiled() BladePage::$sourceDirectory = 'testSourceDir/blade'; Config::set('view.paths', ['testSourceDir/blade']); - // Uses the same logic as the BuildActionRunner for an accurate test. new StaticPageBuilder( - DiscoveryService::getParserInstanceForModel( - BladePage::class, - 'test' - )->get(), + BladePage::parse('test'), true ); diff --git a/packages/testing/src/TestCase.php b/packages/testing/src/TestCase.php index 97eaac70141..d716746d522 100644 --- a/packages/testing/src/TestCase.php +++ b/packages/testing/src/TestCase.php @@ -2,7 +2,9 @@ namespace Hyde\Testing; +use Hyde\Framework\Actions\ConvertsArrayToFrontMatter; use Hyde\Framework\Contracts\PageContract; +use Hyde\Framework\Hyde; use Hyde\Framework\Models\Pages\MarkdownPage; use Hyde\Framework\Models\Route; use LaravelZero\Framework\Testing\TestCase as BaseTestCase; @@ -16,6 +18,8 @@ abstract class TestCase extends BaseTestCase protected static bool $booted = false; + protected array $fileMemory = []; + /** * Setup the test environment. * @@ -39,6 +43,11 @@ protected function setUp(): void */ protected function tearDown(): void { + if (sizeof($this->fileMemory) > 0) { + Hyde::unlink($this->fileMemory); + $this->fileMemory = []; + } + parent::tearDown(); } @@ -60,4 +69,27 @@ protected function mockCurrentPage(string $currentPage) { view()->share('currentPage', $currentPage); } + + /** + * Create a temporary file in the project directory. + * The TestCase will automatically remove the file when the test is completed. + */ + protected function file(string $path, ?string $contents = null): void + { + if ($contents) { + file_put_contents(Hyde::path($path), $contents); + } else { + Hyde::touch($path); + } + + $this->fileMemory[] = $path; + } + + /** + * Create a temporary Markdown+FrontMatter file in the project directory. + */ + protected function markdown(string $path, string $contents = '', array $matter = []): void + { + $this->file($path, (new ConvertsArrayToFrontMatter())->execute($matter).$contents); + } }