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

Internal build service refactor tie in #65

Merged
merged 15 commits into from
Apr 8, 2022
Next Next commit
Update tests for Build Service refactor
  • Loading branch information
caendesilva committed Apr 8, 2022
commit 06c80480a6a5fc2221e4fd3d457c06a3748cbedb
88 changes: 1 addition & 87 deletions tests/Feature/BuildServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,96 +2,10 @@

namespace Tests\Feature;

use Exception;
use Hyde\Framework\Hyde;
use Hyde\Framework\Services\BuildService;
use Hyde\Framework\StaticPageBuilder;
use Tests\TestCase;

/**
* Note that we don't actually test if the files were created,
* since the service is just a proxy for the actual builders,
* which have their own tests that include this feature.
*/
class BuildServiceTest extends TestCase
{
/** @throws Exception */
public function testDetermineModelCanFindMarkdownPost()
{
$service = new BuildService('_posts/test.md');
$result = $service->determineModel();
$this->assertEquals('Hyde\Framework\Models\MarkdownPost', $result);
}

/** @throws Exception */
public function testDetermineModelCanFindMarkdownPage()
{
$service = new BuildService('_pages/test.md');
$result = $service->determineModel();
$this->assertEquals('Hyde\Framework\Models\MarkdownPage', $result);
}

/** @throws Exception */
public function testDetermineModelCanFindDocumentationPage()
{
$service = new BuildService('_docs/test.md');
$result = $service->determineModel();
$this->assertEquals('Hyde\Framework\Models\DocumentationPage', $result);
}

/** @throws Exception */
public function testDetermineModelCanFindBladePage()
{
$service = new BuildService('resources/views/pages/test.md');
$result = $service->determineModel();
$this->assertEquals('Hyde\Framework\Models\BladePage', $result);
}

public function testDetermineModelThrowsExceptionWhenSuppliedWithUnknownPath()
{
$service = new BuildService('foo/bar/test.md');
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid source path.');
$this->expectExceptionCode(400);
$result = $service->determineModel();
$this->assertNull($result);
}

/** @throws Exception */
public function testExecute()
{
$path = '_posts/test-f01cae99-29ca-481e-b977-6acf9ee364d3.md';
copy(
Hyde::path('vendor/hyde/framework/tests/stubs/_posts/my-new-post.md'),
Hyde::path($path)
);
$service = new BuildService('_posts/test-f01cae99-29ca-481e-b977-6acf9ee364d3.md');
$service->execute();
$this->assertNotNull($service->model);
unlink(Hyde::path($path));
}

/** @throws Exception */
public function testHandle()
{
$this->runHandleTest('_posts');
$this->runHandleTest('_pages');
$this->runHandleTest('_docs');
$this->runHandleTest('resources/views/pages', '.blade.php');
}

/** @throws Exception */
private function runHandleTest(string $prefix, string $suffix = '.md')
{
$path = $prefix.'/test-f01cae99-29ca-481e-b977-6acf9ee364d3'.$suffix;
copy(
Hyde::path('vendor/hyde/framework/tests/stubs/_posts/my-new-post.md'),
Hyde::path($path)
);
$service = new BuildService($path);
$service->determineModel();
$result = $service->handle();
$this->assertInstanceOf(StaticPageBuilder::class, $result);
unlink(Hyde::path($path));
}

}
2 changes: 1 addition & 1 deletion tests/Feature/Commands/BuildStaticSiteCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function test_build_command_contains_expected_output()
->expectsOutput('Transferring Media Assets...')
->expectsOutput('Creating Markdown Posts...')
->expectsOutput('No Documentation Pages found. Skipping...')
->expectsOutput('Creating Custom Blade Pages...')
->expectsOutput('Creating Blade Pages...')
->expectsOutputToContain('All done! Finished in')
->expectsOutput('Congratulations! 🎉 Your static site has been built!')
->expectsOutput('Your new homepage is stored here -> file://'.str_replace(
Expand Down
50 changes: 50 additions & 0 deletions tests/Feature/RebuildServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Tests\Feature;

use Hyde\Framework\Hyde;
use Hyde\Framework\Services\RebuildService;
use Hyde\Framework\StaticPageBuilder;
use Tests\TestCase;

/**
* Note that we don't actually test if the files were created,
* since the service is just a proxy for the actual builders,
* which have their own tests that include this feature.
*/
class RebuildServiceTest extends TestCase
{
public function testService()
{
$path = '_posts/test-f01cae99-29ca-481e-b977-6acf9ee364d3.md';
copy(
Hyde::path('vendor/hyde/framework/tests/stubs/_posts/my-new-post.md'),
Hyde::path($path)
);
$service = new RebuildService('_posts/test-f01cae99-29ca-481e-b977-6acf9ee364d3.md');
$service->execute();
$this->assertNotNull($service->model);
unlink(Hyde::path($path));
}

public function testExecute()
{
$this->runExecuteTest('_posts');
$this->runExecuteTest('_pages');
$this->runExecuteTest('_docs');
$this->runExecuteTest('resources/views/pages', '.blade.php');
}

private function runExecuteTest(string $prefix, string $suffix = '.md')
{
$path = $prefix.'/test-f01cae99-29ca-481e-b977-6acf9ee364d3'.$suffix;
copy(
Hyde::path('vendor/hyde/framework/tests/stubs/_posts/my-new-post.md'),
Hyde::path($path)
);
$service = new RebuildService($path);
$result = $service->execute();
$this->assertInstanceOf(StaticPageBuilder::class, $result);
unlink(Hyde::path($path));
}
}
62 changes: 62 additions & 0 deletions tests/Feature/Services/CollectionServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Tests\Feature\Services;

use Tests\TestCase;
use Hyde\Framework\Hyde;
use Hyde\Framework\Services\CollectionService;
use Hyde\Framework\Models\BladePage;
use Hyde\Framework\Models\MarkdownPage;
use Hyde\Framework\Models\MarkdownPost;
use Hyde\Framework\Models\DocumentationPage;
use Illuminate\Support\Facades\File;
use App\Commands\TestWithBackup;

class CollectionServiceTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

// TestWithBackup::backupDirectory(Hyde::path('_docs'));
// File::deleteDirectory(Hyde::path('_docs'));
}

public function testClassExists()
{
$this->assertTrue(class_exists(CollectionService::class));
}

public function testGetSourceFileListForModelMethod()
{
$this->testListUnit(BladePage::class, 'resources/views/pages/a8a7b7ce.blade.php');
$this->testListUnit(MarkdownPage::class, '_pages/a8a7b7ce.md');
$this->testListUnit(MarkdownPost::class, '_posts/a8a7b7ce.md');
$this->testListUnit(DocumentationPage::class, '_docs/a8a7b7ce.md');

$this->assertFalse(CollectionService::getSourceFileListForModel('NonExistentModel'));
}

public function testGetMediaAssetFiles()
{
$this->assertTrue(is_array(CollectionService::getMediaAssetFiles()));
}

private function testListUnit(string $model, string $path)
{
touch(Hyde::path($path));

$expected = str_replace(['.md', '.blade.php'], '', basename($path));

$this->assertContains($expected, CollectionService::getSourceFileListForModel($model));

unlink(Hyde::path($path));
}

public function tearDown(): void
{
// TestWithBackup::restoreDirectory(Hyde::path('_docs'));

parent::tearDown();
}
}