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

Refactor the CollectionService to reduce boilerplate #123

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ This serves two purposes:
- Deprecate Hyde::docsIndexPath()
- Deprecate Hyde::getDocumentationOutputDirectory()
- Deprecate RegistersDefaultDirectories.php pending rename
- Deprecated CollectionService::getBladePageList, is renamed to getBladePageFiles
- Deprecated CollectionService::getMarkdownPageList, is renamed to getMarkdownPageFiles
- Deprecated CollectionService::getMarkdownPostList, is renamed to getMarkdownPostFiles
- Deprecated CollectionService::getDocumentationPageList, is renamed to getDocumentationPageFiles

### Removed
- Remove unused `$withoutNavigation` variable from the app layout
Expand Down
105 changes: 53 additions & 52 deletions packages/framework/src/Services/CollectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hyde\Framework\Services;

use Hyde\Framework\Contracts\AbstractPage;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\BladePage;
use Hyde\Framework\Models\DocumentationPage;
Expand All @@ -25,95 +26,95 @@ class CollectionService
*/
public static function getSourceFileListForModel(string $model): array|false
{
if ($model == BladePage::class) {
return self::getBladePageList();
if (! class_exists($model) || ! is_subclass_of($model, AbstractPage::class)) {
return false;
}

if ($model == MarkdownPage::class) {
return self::getMarkdownPageList();
}
return array_map(function ($filepath) use ($model) {
if (! str_starts_with(basename($filepath), '_')) {
return basename($filepath, $model::getFileExtension());
}
}, glob(Hyde::path($model::qualifyBasename('*'))));
}

if ($model == MarkdownPost::class) {
return self::getMarkdownPostList();
}
/**
* @deprecated v0.44.x Is renamed to getBladePageFiles
*/
public static function getBladePageList(): array
{
return static::getBladePageFiles();
}

if ($model == DocumentationPage::class) {
return self::getDocumentationPageList();
}
/**
* @deprecated v0.44.x Is renamed to getMarkdownPageFiles
*/
public static function getMarkdownPageList(): array
{
return static::getMarkdownPageFiles();
}

return false;
/**
* @deprecated v0.44.x Is renamed to getMarkdownPostFiles
*/
public static function getMarkdownPostList(): array
{
return static::getMarkdownPostFiles();
}

/**
* @deprecated v0.44.x Is renamed to getDocumentationPageFiles
*/
public static function getDocumentationPageList(): array
{
return static::getDocumentationPageFiles();
}

/**
* Get all the Blade files in the resources/views/vendor/hyde/pages directory.
*
* @since v0.44.x replaces getBladePageList
*
* @return array
*/
public static function getBladePageList(): array
public static function getBladePageFiles(): array
{
$array = [];

foreach (glob(Hyde::path(BladePage::qualifyBasename('*'))) as $filepath) {
if (! str_starts_with(basename($filepath), '_')) {
$array[] = basename($filepath, '.blade.php');
}
}

return $array;
return static::getSourceFileListForModel(BladePage::class);
}

/**
* Get all the Markdown files in the _pages directory.
*
* @since v0.44.x replaces getMarkdownPageList
*
* @return array
*/
public static function getMarkdownPageList(): array
public static function getMarkdownPageFiles(): array
{
$array = [];

foreach (glob(Hyde::path(MarkdownPage::qualifyBasename('*'))) as $filepath) {
if (! str_starts_with(basename($filepath), '_')) {
$array[] = basename($filepath, '.md');
}
}

return $array;
return static::getSourceFileListForModel(MarkdownPage::class);
}

/**
* Get all the Markdown files in the _posts directory.
*
* @since v0.44.x replaces getMarkdownPostList
*
* @return array
*/
public static function getMarkdownPostList(): array
public static function getMarkdownPostFiles(): array
{
$array = [];

foreach (glob(Hyde::path(MarkdownPost::qualifyBasename('*'))) as $filepath) {
if (! str_starts_with(basename($filepath), '_')) {
$array[] = basename($filepath, '.md');
}
}

return $array;
return static::getSourceFileListForModel(MarkdownPost::class);
}

/**
* Get all the Markdown files in the _docs directory.
*
* @since v0.44.x replaces getDocumentationPageList
*
* @return array
*/
public static function getDocumentationPageList(): array
public static function getDocumentationPageFiles(): array
{
$array = [];

foreach (glob(Hyde::path(DocumentationPage::qualifyBasename('*'))) as $filepath) {
if (! str_starts_with(basename($filepath), '_')) {
$array[] = basename($filepath, '.md');
}
}

return $array;
return static::getSourceFileListForModel(DocumentationPage::class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public function __construct()
{
parent::__construct();

$this->markdownPath = Hyde::path('_pages/8450de2-test-page.md');
$this->bladePath = Hyde::path('_pages/8450de2-test-page.blade.php');
$this->markdownPath = Hyde::path('_pages/foo-test-page.md');
$this->bladePath = Hyde::path('_pages/foo-test-page.blade.php');
}

protected function tearDown(): void
Expand All @@ -39,29 +39,29 @@ protected function tearDown(): void
// Assert the command can run
public function test_command_can_run()
{
$this->artisan('make:page "8450de2 test page"')->assertExitCode(0);
$this->artisan('make:page "foo test page"')->assertExitCode(0);
}

// Assert the command contains expected output
public function test_command_output()
{
$this->artisan('make:page "8450de2 test page"')
$this->artisan('make:page "foo test page"')
->expectsOutputToContain('Creating a new page!')
->expectsOutputToContain('Created file '.$this->markdownPath);
}

// Assert the command allows the user to specify a page type
public function test_command_allows_user_to_specify_page_type()
{
$this->artisan('make:page "8450de2 test page" --type=markdown')->assertExitCode(0);
$this->artisan('make:page "8450de2 test page" --type=blade')->assertExitCode(0);
$this->artisan('make:page "foo test page" --type=markdown')->assertExitCode(0);
$this->artisan('make:page "foo test page" --type=blade')->assertExitCode(0);
}

// Assert the type option is case-insensitive
public function test_type_option_is_case_insensitive()
{
$this->artisan('make:page "8450de2 test page" --type=Markdown')->assertExitCode(0);
$this->artisan('make:page "8450de2 test page" --type=Blade')->assertExitCode(0);
$this->artisan('make:page "foo test page" --type=Markdown')->assertExitCode(0);
$this->artisan('make:page "foo test page" --type=Blade')->assertExitCode(0);
}

// Assert that the command fails if the user specifies an invalid page type
Expand All @@ -70,32 +70,32 @@ public function test_command_fails_if_user_specifies_invalid_page_type()
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid page type: invalid');
$this->expectExceptionCode(400);
$this->artisan('make:page "8450de2 test page" --type=invalid')->assertExitCode(400);
$this->artisan('make:page "foo test page" --type=invalid')->assertExitCode(400);
}

// Assert the command creates the markdown file
public function test_command_creates_markdown_file()
{
$this->artisan('make:page "8450de2 test page"')->assertExitCode(0);
$this->artisan('make:page "foo test page"')->assertExitCode(0);

$this->assertFileExists($this->markdownPath);
}

// Assert the command creates the blade file
public function test_command_creates_blade_file()
{
$this->artisan('make:page "8450de2 test page" --type="blade"')->assertExitCode(0);
$this->artisan('make:page "foo test page" --type="blade"')->assertExitCode(0);

$this->assertFileExists($this->bladePath);
}

// Assert the command creates the documentation file
public function test_command_creates_documentation_file()
{
$this->artisan('make:page "8450de2 test page" --type="documentation"')->assertExitCode(0);
$this->artisan('make:page "foo test page" --type="documentation"')->assertExitCode(0);

$this->assertFileExists(Hyde::path('_docs/8450de2-test-page.md'));
unlink(Hyde::path('_docs/8450de2-test-page.md'));
$this->assertFileExists(Hyde::path('_docs/foo-test-page.md'));
unlink(Hyde::path('_docs/foo-test-page.md'));
}

// Assert the command fails if the file already exists
Expand All @@ -106,7 +106,7 @@ public function test_command_fails_if_file_already_exists()
$this->expectException(FileConflictException::class);
$this->expectExceptionMessage("File already exists: $this->markdownPath");
$this->expectExceptionCode(409);
$this->artisan('make:page "8450de2 test page"')->assertExitCode(409);
$this->artisan('make:page "foo test page"')->assertExitCode(409);

$this->assertEquals('This should not be overwritten', file_get_contents($this->markdownPath));
}
Expand All @@ -116,7 +116,7 @@ public function test_command_overwrites_existing_files_when_force_option_is_used
{
file_put_contents($this->markdownPath, 'This should be overwritten');

$this->artisan('make:page "8450de2 test page" --force')->assertExitCode(0);
$this->artisan('make:page "foo test page" --force')->assertExitCode(0);

$this->assertNotEquals('This should be overwritten', file_get_contents($this->markdownPath));
}
Expand All @@ -128,6 +128,8 @@ public function test_command_prompts_for_title_if_it_was_not_specified()
->expectsQuestion('What is the title of the page?', 'Test Page')
->expectsOutput("Creating page with title: Test Page\n")
->assertExitCode(0);

unlink(Hyde::path('_pages/test-page.md'));
}

// Assert the command falls back to default title if the user enters nothing
Expand All @@ -137,5 +139,7 @@ public function test_command_falls_back_to_default_title_if_user_enters_nothing(
->expectsQuestion('What is the title of the page?', null)
->expectsOutput("Creating page with title: My New Page\n")
->assertExitCode(0);

unlink(Hyde::path('_pages/my-new-page.md'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,10 @@ public function test_created_model_contains_expected_data(DocumentationPage $obj
$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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Hyde\Framework\Testing\Feature\Services;

use Hyde\Framework\Contracts\AbstractBuildTask;
use Hyde\Framework\Hyde;
use Hyde\Framework\Services\BuildHookService;
use Hyde\Testing\TestCase;

Expand All @@ -24,6 +25,8 @@ public function test_build_command_can_run_post_build_tasks()
->expectsOutputToContain('Generating sitemap')
->expectsOutputToContain('Created sitemap.xml')
->assertExitCode(0);

unlink(Hyde::path('_site/sitemap.xml'));
}

/**
Expand Down
Loading