From 9957277b374e124264b4e0180ad43b93d5d7479a Mon Sep 17 00:00:00 2001 From: github-actions Date: Tue, 10 Sep 2024 10:05:03 +0000 Subject: [PATCH] Merge pull request #1960 from hydephp/test-cleanup Internal: Test code refactors and cleanup https://github.com/hydephp/develop/commit/1eb450241434972784a1db3073d7059b6156a6ed --- src/Facades/Filesystem.php | 4 +- src/Framework/Actions/BladeMatterParser.php | 5 +- .../DocumentationPageTest.php | 2 +- tests/Feature/FilesystemFacadeTest.php | 361 +--------------- tests/Feature/Foundation/FilesystemTest.php | 4 - tests/Feature/HydeKernelTest.php | 6 + tests/Feature/MarkdownFileParserTest.php | 54 ++- tests/Feature/MarkdownPostTest.php | 34 ++ tests/Feature/ReadingTimeTest.php | 7 +- tests/Feature/StaticSiteServiceTest.php | 64 +++ tests/Unit/BaseFoundationCollectionTest.php | 2 +- .../BlogPostFrontMatterIsOptionalTest.php | 45 -- tests/Unit/BreadcrumbsComponentTest.php | 2 +- .../BuildOutputDirectoryCanBeChangedTest.php | 83 ---- tests/Unit/BuildTaskServiceUnitTest.php | 55 ++- tests/Unit/BuildWarningsTest.php | 2 - tests/Unit/CreatesNewMarkdownPostFileTest.php | 9 +- tests/Unit/Facades/AssetFacadeTest.php | 12 +- .../HydeFacadesAreAliasedInAppConfigTest.php | 11 +- tests/Unit/FilesystemFacadeUnitTest.php | 398 ++++++++++++++++++ .../HyperlinksUrlPathHelpersTest.php | 26 +- tests/Unit/HydeFileHelpersTest.php | 17 +- tests/Unit/HydeHelperFacadeTest.php | 20 +- tests/Unit/HydePageDataFactoryTest.php | 2 - .../InteractsWithDirectoriesConcernTest.php | 67 +-- tests/Unit/Pages/PageModelParseHelperTest.php | 47 ++- tests/Unit/RenderHelperTest.php | 2 - tests/Unit/ServeCommandOptionsUnitTest.php | 6 +- tests/Unit/TestingSupportHelpersMetaTest.php | 2 + tests/Unit/ValidatesExistenceTest.php | 6 +- ...nentTest.php => LinkComponentViewTest.php} | 2 +- 31 files changed, 696 insertions(+), 661 deletions(-) rename tests/{Unit => Feature}/DocumentationPageTest.php (99%) delete mode 100644 tests/Unit/BlogPostFrontMatterIsOptionalTest.php delete mode 100644 tests/Unit/BuildOutputDirectoryCanBeChangedTest.php create mode 100644 tests/Unit/FilesystemFacadeUnitTest.php rename tests/Unit/Views/{LinkComponentTest.php => LinkComponentViewTest.php} (95%) diff --git a/src/Facades/Filesystem.php b/src/Facades/Filesystem.php index 8d1d86ac..2cbcf58b 100644 --- a/src/Facades/Filesystem.php +++ b/src/Facades/Filesystem.php @@ -128,7 +128,7 @@ public static function unlinkIfExists(string $path): bool */ public static function getContents(string $path, bool $lock = false): string { - return self::get($path, $lock); + return self::get(...func_get_args()); } /** @@ -141,7 +141,7 @@ public static function getContents(string $path, bool $lock = false): string */ public static function putContents(string $path, string $contents, bool $lock = false): bool|int { - return self::put($path, $contents, $lock); + return self::put(...func_get_args()); } protected static function filesystem(): \Illuminate\Filesystem\Filesystem diff --git a/src/Framework/Actions/BladeMatterParser.php b/src/Framework/Actions/BladeMatterParser.php index 2f2dcc6e..df3c402d 100644 --- a/src/Framework/Actions/BladeMatterParser.php +++ b/src/Framework/Actions/BladeMatterParser.php @@ -4,10 +4,9 @@ namespace Hyde\Framework\Actions; -use Hyde\Hyde; use RuntimeException; +use Hyde\Facades\Filesystem; -use function file_get_contents; use function str_ends_with; use function str_starts_with; use function substr_count; @@ -53,7 +52,7 @@ class BladeMatterParser public static function parseFile(string $path): array { - return static::parseString(file_get_contents(Hyde::path($path))); + return static::parseString(Filesystem::getContents($path)); } public static function parseString(string $contents): array diff --git a/tests/Unit/DocumentationPageTest.php b/tests/Feature/DocumentationPageTest.php similarity index 99% rename from tests/Unit/DocumentationPageTest.php rename to tests/Feature/DocumentationPageTest.php index 48a2af77..f110ea54 100644 --- a/tests/Unit/DocumentationPageTest.php +++ b/tests/Feature/DocumentationPageTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Hyde\Framework\Testing\Unit; +namespace Hyde\Framework\Testing\Feature; use Hyde\Facades\Filesystem; use Hyde\Foundation\Facades\Routes; diff --git a/tests/Feature/FilesystemFacadeTest.php b/tests/Feature/FilesystemFacadeTest.php index db00aefc..1c4216c4 100644 --- a/tests/Feature/FilesystemFacadeTest.php +++ b/tests/Feature/FilesystemFacadeTest.php @@ -7,14 +7,13 @@ use Hyde\Facades\Filesystem; use Hyde\Hyde; use Hyde\Testing\TestCase; -use Illuminate\Support\Collection; -use Illuminate\Support\Facades\File; -use Illuminate\Support\LazyCollection; /** * @covers \Hyde\Facades\Filesystem * @covers \Hyde\Foundation\Kernel\Filesystem * @covers \Hyde\Framework\Concerns\Internal\ForwardsIlluminateFilesystem + * + * @see \Hyde\Framework\Testing\Unit\FilesystemFacadeUnitTest */ class FilesystemFacadeTest extends TestCase { @@ -36,21 +35,6 @@ public function testRelativePath() $this->assertSame('foo', Filesystem::relativePath('foo')); } - public function testSmartGlob() - { - $this->createExpectation('glob', [ - Hyde::path('foo'), - Hyde::path('bar'), - Hyde::path('baz'), - ], Hyde::path('pattern/*.md'), 0); - - $expected = Collection::make(['foo', 'bar', 'baz']); - $actual = Filesystem::smartGlob('pattern/*.md'); - - $this->assertEquals($expected, $actual); - $this->assertSame($expected->all(), $actual->all()); - } - public function testTouch() { Filesystem::touch('foo'); @@ -78,342 +62,6 @@ public function testUnlinkIfExists() $this->assertFileDoesNotExist(Hyde::path('foo')); } - public function testGetContents() - { - $this->createExpectation('get', 'string', Hyde::path('path'), false); - - Filesystem::getContents('path'); - } - - public function testPutContents() - { - $this->createExpectation('put', true, Hyde::path('path'), 'string', false); - - Filesystem::putContents('path', 'string'); - } - - public function testExists() - { - $this->createExpectation('exists', true, Hyde::path('path')); - - Filesystem::exists('path'); - } - - public function testMissing() - { - $this->createExpectation('missing', true, Hyde::path('path')); - - Filesystem::missing('path'); - } - - public function testGet() - { - $this->createExpectation('get', 'string', Hyde::path('path')); - - Filesystem::get('path'); - } - - public function testSharedGet() - { - $this->createExpectation('sharedGet', 'string', Hyde::path('path')); - - Filesystem::sharedGet('path'); - } - - public function testGetRequire() - { - $this->createExpectation('getRequire', 'string', Hyde::path('path')); - - Filesystem::getRequire('path'); - } - - public function testRequireOnce() - { - $this->createExpectation('requireOnce', 'string', Hyde::path('path')); - - Filesystem::requireOnce('path'); - } - - public function testLines() - { - $this->createExpectation('lines', new LazyCollection(), Hyde::path('path')); - - Filesystem::lines('path'); - } - - public function testHash() - { - $this->createExpectation('hash', 'string', Hyde::path('path')); - - Filesystem::hash('path'); - } - - public function testPut() - { - $this->createExpectation('put', 10, Hyde::path('path'), 'contents'); - - Filesystem::put('path', 'contents'); - } - - public function testReplace() - { - $this->createExpectation('replace', null, Hyde::path('path'), 'content'); - - Filesystem::replace('path', 'content'); - } - - public function testReplaceInFile() - { - $this->createExpectation('replaceInFile', null, 'search', 'replace', Hyde::path('path')); - - Filesystem::replaceInFile('search', 'replace', 'path'); - } - - public function testPrepend() - { - $this->createExpectation('prepend', 10, Hyde::path('path'), 'data'); - - Filesystem::prepend('path', 'data'); - } - - public function testAppend() - { - $this->createExpectation('append', 10, Hyde::path('path'), 'data'); - - Filesystem::append('path', 'data'); - } - - public function testChmod() - { - $this->createExpectation('chmod', null, Hyde::path('path'), 0755); - - Filesystem::chmod('path', 0755); - } - - public function testDelete() - { - $this->createExpectation('delete', true, Hyde::path('path')); - - Filesystem::delete('path'); - } - - public function testDeleteWithArray() - { - $this->createExpectation('delete', true, [Hyde::path('path'), Hyde::path('another')]); - - Filesystem::delete(['path', 'another']); - } - - public function testMove() - { - $this->createExpectation('move', true, Hyde::path('path'), Hyde::path('target')); - - Filesystem::move('path', 'target'); - } - - public function testCopy() - { - $this->createExpectation('copy', true, Hyde::path('path'), Hyde::path('target')); - - Filesystem::copy('path', 'target'); - } - - public function testLink() - { - $this->createExpectation('link', true, Hyde::path('target'), Hyde::path('link')); - - Filesystem::link('target', 'link'); - } - - public function testRelativeLink() - { - $this->createExpectation('relativeLink', true, Hyde::path('target'), Hyde::path('link')); - - Filesystem::relativeLink('target', 'link'); - } - - public function testName() - { - $this->createExpectation('name', 'string', Hyde::path('path')); - - Filesystem::name('path'); - } - - public function testBasename() - { - $this->createExpectation('basename', 'string', Hyde::path('path')); - - Filesystem::basename('path'); - } - - public function testDirname() - { - $this->createExpectation('dirname', 'string', Hyde::path('path')); - - Filesystem::dirname('path'); - } - - public function testExtension() - { - $this->createExpectation('extension', 'string', Hyde::path('path')); - - Filesystem::extension('path'); - } - - public function testGuessExtension() - { - $this->createExpectation('guessExtension', 'string', Hyde::path('path')); - - Filesystem::guessExtension('path'); - } - - public function testType() - { - $this->createExpectation('type', 'string', Hyde::path('path')); - - Filesystem::type('path'); - } - - public function testMimeType() - { - $this->createExpectation('mimeType', 'string', Hyde::path('path')); - - Filesystem::mimeType('path'); - } - - public function testSize() - { - $this->createExpectation('size', 10, Hyde::path('path')); - - Filesystem::size('path'); - } - - public function testLastModified() - { - $this->createExpectation('lastModified', 10, Hyde::path('path')); - - Filesystem::lastModified('path'); - } - - public function testIsDirectory() - { - $this->createExpectation('isDirectory', true, Hyde::path('directory')); - - Filesystem::isDirectory('directory'); - } - - public function testIsEmptyDirectory() - { - $this->createExpectation('isEmptyDirectory', true, Hyde::path('directory')); - - Filesystem::isEmptyDirectory('directory'); - } - - public function testIsReadable() - { - $this->createExpectation('isReadable', true, Hyde::path('path')); - - Filesystem::isReadable('path'); - } - - public function testIsWritable() - { - $this->createExpectation('isWritable', true, Hyde::path('path')); - - Filesystem::isWritable('path'); - } - - public function testHasSameHash() - { - $this->createExpectation('hasSameHash', true, Hyde::path('firstFile'), Hyde::path('secondFile')); - - Filesystem::hasSameHash('firstFile', 'secondFile'); - } - - public function testIsFile() - { - $this->createExpectation('isFile', true, Hyde::path('file')); - - Filesystem::isFile('file'); - } - - public function testGlob() - { - $this->createExpectation('glob', [], Hyde::path('pattern')); - - Filesystem::glob('pattern'); - } - - public function testFiles() - { - $this->createExpectation('files', [], Hyde::path('directory')); - - Filesystem::files('directory'); - } - - public function testAllFiles() - { - $this->createExpectation('allFiles', [], Hyde::path('directory')); - - Filesystem::allFiles('directory'); - } - - public function testDirectories() - { - $this->createExpectation('directories', [], Hyde::path('directory')); - - Filesystem::directories('directory'); - } - - public function testEnsureDirectoryExists() - { - $this->createExpectation('ensureDirectoryExists', null, Hyde::path('path')); - - Filesystem::ensureDirectoryExists('path'); - } - - public function testMakeDirectory() - { - $this->createExpectation('makeDirectory', true, Hyde::path('path')); - - Filesystem::makeDirectory('path'); - } - - public function testMoveDirectory() - { - $this->createExpectation('moveDirectory', true, Hyde::path('from'), Hyde::path('to')); - - Filesystem::moveDirectory('from', 'to'); - } - - public function testCopyDirectory() - { - $this->createExpectation('copyDirectory', true, Hyde::path('directory'), Hyde::path('destination')); - - Filesystem::copyDirectory('directory', 'destination'); - } - - public function testDeleteDirectory() - { - $this->createExpectation('deleteDirectory', true, Hyde::path('directory')); - - Filesystem::deleteDirectory('directory'); - } - - public function testDeleteDirectories() - { - $this->createExpectation('deleteDirectories', true, Hyde::path('directory')); - - Filesystem::deleteDirectories('directory'); - } - - public function testCleanDirectory() - { - $this->createExpectation('cleanDirectory', true, Hyde::path('directory')); - - Filesystem::cleanDirectory('directory'); - } - public function testMethodWithoutMocking() { $this->assertSame(3, Filesystem::put('foo', 'bar')); @@ -446,9 +94,4 @@ public function testMethodWithMixedSequentialAndNamedArgumentsSkippingMiddleOne( rmdir(Hyde::path('foo')); } - - protected function createExpectation(string $method, mixed $returns, ...$args): void - { - File::shouldReceive($method)->withArgs($args)->once()->andReturn($returns); - } } diff --git a/tests/Feature/Foundation/FilesystemTest.php b/tests/Feature/Foundation/FilesystemTest.php index e442a034..25bf1082 100644 --- a/tests/Feature/Foundation/FilesystemTest.php +++ b/tests/Feature/Foundation/FilesystemTest.php @@ -37,8 +37,6 @@ class FilesystemTest extends UnitTestCase protected function setUp(): void { - parent::setUp(); - $this->originalBasePath = Hyde::getBasePath(); $this->filesystem = new Filesystem(Hyde::getInstance()); } @@ -46,8 +44,6 @@ protected function setUp(): void protected function tearDown(): void { Hyde::getInstance()->setBasePath($this->originalBasePath); - - parent::tearDown(); } public function testGetBasePathReturnsKernelsBasePath() diff --git a/tests/Feature/HydeKernelTest.php b/tests/Feature/HydeKernelTest.php index 117d455f..0b2afa5e 100644 --- a/tests/Feature/HydeKernelTest.php +++ b/tests/Feature/HydeKernelTest.php @@ -359,6 +359,12 @@ public function testSetOutputDirectoryTrimsTrailingSlashes() $this->assertSame('/foo', Hyde::getOutputDirectory()); } + public function testSiteOutputDirectoryPathIsNormalizedToTrimTrailingSlashes() + { + Hyde::setOutputDirectory('foo/bar/'); + $this->assertSame('foo/bar', Hyde::kernel()->getOutputDirectory()); + } + public function testCanGetMediaDirectory() { $this->assertSame('_media', Hyde::getMediaDirectory()); diff --git a/tests/Feature/MarkdownFileParserTest.php b/tests/Feature/MarkdownFileParserTest.php index 09e5b1dc..75ba8afc 100644 --- a/tests/Feature/MarkdownFileParserTest.php +++ b/tests/Feature/MarkdownFileParserTest.php @@ -4,8 +4,6 @@ namespace Hyde\Framework\Testing\Feature; -use Hyde\Hyde; -use Hyde\Facades\Filesystem; use Hyde\Framework\Actions\MarkdownFileParser; use Hyde\Markdown\Models\FrontMatter; use Hyde\Markdown\Models\MarkdownDocument; @@ -15,33 +13,9 @@ class MarkdownFileParserTest extends UnitTestCase { protected static bool $needsKernel = true; - protected function makeTestPost(): void - { - Filesystem::putContents('_posts/test-post.md', <<<'MD' - --- - title: My New Post - category: blog - author: Mr. Hyde - --- - - # My New Post - - This is a post stub used in the automated tests - - MD - ); - } - - protected function tearDown(): void - { - Filesystem::unlink('_posts/test-post.md'); - - parent::tearDown(); - } - public function testCanParseMarkdownFile() { - file_put_contents(Hyde::path('_posts/test-post.md'), 'Foo bar'); + $this->mockFilesystem(['get' => 'Foo bar']); $document = MarkdownFileParser::parse('_posts/test-post.md'); @@ -52,7 +26,17 @@ public function testCanParseMarkdownFile() public function testCanParseMarkdownFileWithFrontMatter() { - $this->makeTestPost(); + $this->mockFilesystem(['get' => <<<'MD' + --- + title: My New Post + category: blog + author: Mr. Hyde + --- + + # My New Post + + This is a post stub used in the automated tests + MD]); $document = MarkdownFileParser::parse('_posts/test-post.md'); @@ -76,7 +60,17 @@ public function testCanParseMarkdownFileWithFrontMatter() public function testParsedMarkdownPostContainsValidFrontMatter() { - $this->makeTestPost(); + $this->mockFilesystem(['get' => <<<'MD' + --- + title: My New Post + category: blog + author: Mr. Hyde + --- + + # My New Post + + This is a post stub used in the automated tests + MD]); $post = MarkdownFileParser::parse('_posts/test-post.md'); @@ -87,7 +81,7 @@ public function testParsedMarkdownPostContainsValidFrontMatter() public function testCanParseMarkdownFileWithFrontMatterAndNoMarkdownBody() { - file_put_contents(Hyde::path('_posts/test-post.md'), "---\nfoo: bar\n---"); + $this->mockFilesystem(['get' => "---\nfoo: bar\n---"]); $document = MarkdownFileParser::parse('_posts/test-post.md'); diff --git a/tests/Feature/MarkdownPostTest.php b/tests/Feature/MarkdownPostTest.php index 66c76b3a..ef8c319d 100644 --- a/tests/Feature/MarkdownPostTest.php +++ b/tests/Feature/MarkdownPostTest.php @@ -4,6 +4,10 @@ namespace Hyde\Framework\Testing\Feature; +use Hyde\Hyde; +use Hyde\Pages\BladePage; +use Hyde\Facades\Filesystem; +use Hyde\Framework\Actions\StaticPageBuilder; use Hyde\Framework\Features\Blogging\Models\FeaturedImage; use Hyde\Framework\Features\Blogging\Models\PostAuthor; use Hyde\Markdown\Models\FrontMatter; @@ -107,4 +111,34 @@ public function testFeaturedImageCanBeConstructedReturnsImageObjectWithSuppliedD $this->assertSame('media/foo.png', $image->getSource()); $this->assertSame('bar', $image->getTitleText()); } + + public function testBlogPostCanBeCreatedWithoutFrontMatter() + { + file_put_contents(Hyde::path('_posts/test-post.md'), '# My New Post'); + + StaticPageBuilder::handle(MarkdownPost::get('test-post')); + + $this->assertFileExists(Hyde::path('_site/posts/test-post.html')); + + Filesystem::unlink('_posts/test-post.md'); + Filesystem::unlink('_site/posts/test-post.html'); + } + + public function testBlogPostFeedCanBeRenderedWhenPostHasNoFrontMatter() + { + file_put_contents(Hyde::path('_posts/test-post.md'), '# My New Post'); + + // Create a temporary page to test the feed + copy(Hyde::vendorPath('resources/views/components/blog-post-feed.blade.php'), + Hyde::path('_pages/feed-test.blade.php') + ); + + StaticPageBuilder::handle(BladePage::get('feed-test')); + + $this->assertFileExists(Hyde::path('_site/feed-test.html')); + + Filesystem::unlink('_posts/test-post.md'); + Filesystem::unlink('_pages/feed-test.blade.php'); + Filesystem::unlink('_site/feed-test.html'); + } } diff --git a/tests/Feature/ReadingTimeTest.php b/tests/Feature/ReadingTimeTest.php index 93dbe654..c55014d6 100644 --- a/tests/Feature/ReadingTimeTest.php +++ b/tests/Feature/ReadingTimeTest.php @@ -4,11 +4,9 @@ namespace Hyde\Framework\Testing\Feature; -use Mockery; use Hyde\Hyde; use Hyde\Support\ReadingTime; use Hyde\Testing\UnitTestCase; -use Illuminate\Filesystem\Filesystem; /** * @covers \Hyde\Support\ReadingTime @@ -111,13 +109,12 @@ public function testFromString() public function testFromFile() { - app()->instance(Filesystem::class, Mockery::mock(Filesystem::class)->shouldReceive('get')->with(Hyde::path('foo.md'), false)->andReturn('Hello world')->getMock()); + $this->mockFilesystemStrict()->shouldReceive('get')->twice()->with(Hyde::path('foo.md'))->andReturn('Hello world'); $this->assertInstanceOf(ReadingTime::class, ReadingTime::fromFile('foo.md')); $this->assertEquals(new ReadingTime('Hello world'), ReadingTime::fromFile('foo.md')); - Mockery::close(); - app()->forgetInstance(Filesystem::class); + $this->verifyMockeryExpectations(); } protected function words(int $words): string diff --git a/tests/Feature/StaticSiteServiceTest.php b/tests/Feature/StaticSiteServiceTest.php index 4e26d8d3..77c75506 100644 --- a/tests/Feature/StaticSiteServiceTest.php +++ b/tests/Feature/StaticSiteServiceTest.php @@ -8,8 +8,11 @@ use Hyde\Hyde; use Hyde\Support\BuildWarnings; use Hyde\Testing\TestCase; +use Hyde\Foundation\Facades\Pages; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Process; +use Hyde\Framework\HydeServiceProvider; +use Hyde\Framework\Actions\StaticPageBuilder; /** * @covers \Hyde\Console\Commands\BuildSiteCommand @@ -305,4 +308,65 @@ public function testWithWarningsConvertedToExceptions() ->doesntExpectOutput(' 1. This is a warning') ->assertExitCode(2); } + + public function testSiteOutputDirectoryCanBeChangedForSiteBuilds() + { + $this->file('_posts/test-post.md'); + + Hyde::setOutputDirectory('_site/build'); + + $this->withoutMockingConsoleOutput(); + $this->artisan('build'); + + $this->assertFileExists(Hyde::path('_site/build/posts/test-post.html')); + $this->assertFileExists(Hyde::path('_site/build/media/app.css')); + $this->assertFileExists(Hyde::path('_site/build/index.html')); + + File::deleteDirectory(Hyde::path('_site/build')); + } + + public function testSiteOutputDirectoryCanBeChangedInStaticPageBuilder() + { + $this->file('_posts/test-post.md'); + + Hyde::setOutputDirectory('_site/build'); + + StaticPageBuilder::handle(Pages::getPage('_posts/test-post.md')); + + $this->assertFileExists(Hyde::path('_site/build/posts/test-post.html')); + + File::deleteDirectory(Hyde::path('_site/build')); + } + + public function testOutputDirectoryIsCreatedIfItDoesNotExistInStaticPageBuilder() + { + $this->file('_posts/test-post.md'); + + File::deleteDirectory(Hyde::path('_site/build/foo')); + Hyde::setOutputDirectory('_site/build/foo'); + + StaticPageBuilder::handle(Pages::getPage('_posts/test-post.md')); + + $this->assertFileExists(Hyde::path('_site/build/foo/posts/test-post.html')); + + File::deleteDirectory(Hyde::path('_site/build/foo')); + } + + public function testSiteOutputDirectoryCanBeChangedInConfiguration() + { + $this->assertSame('_site', Hyde::kernel()->getOutputDirectory()); + + config(['hyde.output_directory' => '_site/build']); + (new HydeServiceProvider($this->app))->register(); + + $this->assertSame('_site/build', Hyde::kernel()->getOutputDirectory()); + + $this->file('_posts/test-post.md'); + + StaticPageBuilder::handle(Pages::getPage('_posts/test-post.md')); + + $this->assertFileExists(Hyde::path('_site/build/posts/test-post.html')); + + File::deleteDirectory(Hyde::path('_site/build')); + } } diff --git a/tests/Unit/BaseFoundationCollectionTest.php b/tests/Unit/BaseFoundationCollectionTest.php index 0f11bb3b..cdcc215d 100644 --- a/tests/Unit/BaseFoundationCollectionTest.php +++ b/tests/Unit/BaseFoundationCollectionTest.php @@ -15,7 +15,7 @@ */ class BaseFoundationCollectionTest extends UnitTestCase { - public function testInit() + public function testBaseFoundationCollectionInitialization() { $this->setupKernel(); diff --git a/tests/Unit/BlogPostFrontMatterIsOptionalTest.php b/tests/Unit/BlogPostFrontMatterIsOptionalTest.php deleted file mode 100644 index b90fe0a0..00000000 --- a/tests/Unit/BlogPostFrontMatterIsOptionalTest.php +++ /dev/null @@ -1,45 +0,0 @@ -assertFileExists(Hyde::path('_site/posts/test-post.html')); - - Filesystem::unlink('_posts/test-post.md'); - Filesystem::unlink('_site/posts/test-post.html'); - } - - public function testBlogPostFeedCanBeRenderedWhenPostHasNoFrontMatter() - { - file_put_contents(Hyde::path('_posts/test-post.md'), '# My New Post'); - - // Create a temporary page to test the feed - copy(Hyde::vendorPath('resources/views/components/blog-post-feed.blade.php'), - Hyde::path('_pages/feed-test.blade.php') - ); - - StaticPageBuilder::handle(BladePage::get('feed-test')); - - $this->assertFileExists(Hyde::path('_site/feed-test.html')); - - Filesystem::unlink('_posts/test-post.md'); - Filesystem::unlink('_pages/feed-test.blade.php'); - Filesystem::unlink('_site/feed-test.html'); - } -} diff --git a/tests/Unit/BreadcrumbsComponentTest.php b/tests/Unit/BreadcrumbsComponentTest.php index e2553930..ffd7c29f 100644 --- a/tests/Unit/BreadcrumbsComponentTest.php +++ b/tests/Unit/BreadcrumbsComponentTest.php @@ -52,7 +52,7 @@ public function testCanRender() $this->assertSame($view, (new BreadcrumbsComponent())->render()); - Mockery::close(); + $this->verifyMockeryExpectations(); } public function testCanGenerateBreadcrumbs() diff --git a/tests/Unit/BuildOutputDirectoryCanBeChangedTest.php b/tests/Unit/BuildOutputDirectoryCanBeChangedTest.php deleted file mode 100644 index 8205f17d..00000000 --- a/tests/Unit/BuildOutputDirectoryCanBeChangedTest.php +++ /dev/null @@ -1,83 +0,0 @@ -file('_posts/test-post.md'); - - Hyde::setOutputDirectory('_site/build'); - - $this->withoutMockingConsoleOutput(); - $this->artisan('build'); - - $this->assertFileExists(Hyde::path('_site/build/posts/test-post.html')); - $this->assertFileExists(Hyde::path('_site/build/media/app.css')); - $this->assertFileExists(Hyde::path('_site/build/index.html')); - - File::deleteDirectory(Hyde::path('_site/build')); - } - - public function testSiteOutputDirectoryCanBeChangedInStaticPageBuilder() - { - $this->file('_posts/test-post.md'); - - Hyde::setOutputDirectory('_site/build'); - - StaticPageBuilder::handle(Pages::getPage('_posts/test-post.md')); - - $this->assertFileExists(Hyde::path('_site/build/posts/test-post.html')); - - File::deleteDirectory(Hyde::path('_site/build')); - } - - public function testOutputDirectoryIsCreatedIfItDoesNotExistInStaticPageBuilder() - { - $this->file('_posts/test-post.md'); - - File::deleteDirectory(Hyde::path('_site/build/foo')); - Hyde::setOutputDirectory('_site/build/foo'); - - StaticPageBuilder::handle(Pages::getPage('_posts/test-post.md')); - - $this->assertFileExists(Hyde::path('_site/build/foo/posts/test-post.html')); - - File::deleteDirectory(Hyde::path('_site/build/foo')); - } - - public function testSiteOutputDirectoryCanBeChangedInConfiguration() - { - $this->assertSame('_site', Hyde::kernel()->getOutputDirectory()); - - config(['hyde.output_directory' => '_site/build']); - (new HydeServiceProvider($this->app))->register(); - - $this->assertSame('_site/build', Hyde::kernel()->getOutputDirectory()); - - $this->file('_posts/test-post.md'); - - StaticPageBuilder::handle(Pages::getPage('_posts/test-post.md')); - - $this->assertFileExists(Hyde::path('_site/build/posts/test-post.html')); - - File::deleteDirectory(Hyde::path('_site/build')); - } - - public function testSiteOutputDirectoryPathIsNormalizedToTrimTrailingSlashes() - { - Hyde::setOutputDirectory('foo/bar/'); - - $this->assertSame('foo/bar', Hyde::kernel()->getOutputDirectory()); - } -} diff --git a/tests/Unit/BuildTaskServiceUnitTest.php b/tests/Unit/BuildTaskServiceUnitTest.php index 1581584a..a18d88e0 100644 --- a/tests/Unit/BuildTaskServiceUnitTest.php +++ b/tests/Unit/BuildTaskServiceUnitTest.php @@ -45,8 +45,6 @@ protected function setUp(): void protected function tearDown(): void { - parent::tearDown(); - $this->verifyMockeryExpectations(); } @@ -153,16 +151,12 @@ public function testCanOverloadFrameworkTasks() public function testCanSetOutputWithNull() { - $this->can(function () { - $this->service->setOutput(null); - }); + $this->can(fn () => $this->service->setOutput(null)); } public function testCanSetOutputWithOutputStyle() { - $this->can(function () { - $this->service->setOutput(Mockery::mock(OutputStyle::class)); - }); + $this->can(fn () => $this->service->setOutput($this->mockOutput())); } public function testGenerateBuildManifestExtendsPostBuildTask() @@ -187,12 +181,12 @@ public function testGenerateSitemapExtendsPostBuildTask() public function testCanRunPreBuildTasks() { - $this->can($this->service->runPreBuildTasks(...)); + $this->can(fn () => $this->service->runPreBuildTasks(...)); } public function testCanRunPostBuildTasks() { - $this->can($this->service->runPostBuildTasks(...)); + $this->can(fn () => $this->service->runPostBuildTasks(...)); } public function testCanRunPreBuildTasksWithTasks() @@ -213,7 +207,7 @@ public function testCanRunPostBuildTasksWithTasks() public function testRunPreBuildTasksCallsHandleMethods() { - $task = Mockery::mock(TestPreBuildTask::class)->makePartial()->shouldReceive('handle')->once()->getMock(); + $task = $this->setupMock(TestPreBuildTask::class, 'handle')->getMock(); $this->service->registerTask($task); $this->service->runPreBuildTasks(); @@ -221,7 +215,7 @@ public function testRunPreBuildTasksCallsHandleMethods() public function testRunPostBuildTasksCallsHandleMethods() { - $task = Mockery::mock(TestPostBuildTask::class)->makePartial()->shouldReceive('handle')->once()->getMock(); + $task = $this->setupMock(TestPostBuildTask::class, 'handle')->getMock(); $this->service->registerTask($task); $this->service->runPostBuildTasks(); @@ -229,7 +223,7 @@ public function testRunPostBuildTasksCallsHandleMethods() public function testRunPreBuildTasksCallsRunMethods() { - $task = Mockery::mock(TestPreBuildTask::class)->makePartial()->shouldReceive('run')->once()->getMock(); + $task = $this->setupMock(TestPreBuildTask::class, 'run')->getMock(); $this->service->registerTask($task); $this->service->runPreBuildTasks(); @@ -237,7 +231,7 @@ public function testRunPreBuildTasksCallsRunMethods() public function testRunPostBuildTasksCallsRunMethods() { - $task = Mockery::mock(TestPostBuildTask::class)->makePartial()->shouldReceive('run')->once()->getMock(); + $task = $this->setupMock(TestPostBuildTask::class, 'run')->getMock(); $this->service->registerTask($task); $this->service->runPostBuildTasks(); @@ -245,7 +239,7 @@ public function testRunPostBuildTasksCallsRunMethods() public function testRunPreBuildTasksCallsRunMethodsWithNullWhenServiceHasNoOutput() { - $task = Mockery::mock(TestPreBuildTask::class)->makePartial()->shouldReceive('run')->with(null)->once()->getMock(); + $task = $this->setupMock(TestPreBuildTask::class, 'run')->with(null)->once()->getMock(); $this->service->registerTask($task); $this->service->runPreBuildTasks(); @@ -253,7 +247,7 @@ public function testRunPreBuildTasksCallsRunMethodsWithNullWhenServiceHasNoOutpu public function testRunPostBuildTasksCallsRunMethodsWithNullWhenServiceHasNoOutput() { - $task = Mockery::mock(TestPostBuildTask::class)->makePartial()->shouldReceive('run')->with(null)->once()->getMock(); + $task = $this->setupMock(TestPostBuildTask::class, 'run')->with(null)->once()->getMock(); $this->service->registerTask($task); $this->service->runPostBuildTasks(); @@ -261,8 +255,8 @@ public function testRunPostBuildTasksCallsRunMethodsWithNullWhenServiceHasNoOutp public function testRunPreBuildTasksCallsRunMethodsWithOutputWhenServiceHasOutput() { - $output = Mockery::mock(OutputStyle::class)->makePartial(); - $task = Mockery::mock(TestPreBuildTask::class)->makePartial()->shouldReceive('run')->with($output)->once()->getMock(); + $output = $this->mockOutput(); + $task = $this->setupMock(TestPreBuildTask::class, 'run')->with($output)->once()->getMock(); $this->service->setOutput($output); $this->service->registerTask($task); @@ -271,8 +265,8 @@ public function testRunPreBuildTasksCallsRunMethodsWithOutputWhenServiceHasOutpu public function testRunPostBuildTasksCallsRunMethodsWithOutputWhenServiceHasOutput() { - $output = Mockery::mock(OutputStyle::class)->makePartial(); - $task = Mockery::mock(TestPostBuildTask::class)->makePartial()->shouldReceive('run')->with($output)->once()->getMock(); + $output = $this->mockOutput(); + $task = $this->setupMock(TestPostBuildTask::class, 'run')->with($output)->once()->getMock(); $this->service->setOutput($output); $this->service->registerTask($task); @@ -297,6 +291,7 @@ public function testServiceFindsTasksInAppDirectory() 'app/Actions/GenerateRssFeedBuildTask.php' => GenerateRssFeed::class, 'app/Actions/GenerateSearchBuildTask.php' => GenerateSearch::class, ]; + $this->mockKernelFilesystem($files); $this->can($this->createService(...)); @@ -323,17 +318,9 @@ protected function createService(): BuildTaskService return tap(new BuildTaskService(), fn (BuildTaskService $service) => $this->service = $service); } - protected function verifyMockeryExpectations(): void - { - $this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount()); - - Mockery::close(); - } - protected function mockKernelFilesystem(array $files = []): void { - $filesystem = Mockery::mock(Filesystem::class, [HydeKernel::getInstance()]) - ->makePartial()->shouldReceive('smartGlob')->once() + $filesystem = $this->setupMock(Filesystem::class, 'smartGlob') ->with('app/Actions/*BuildTask.php', 0) ->andReturn(collect($files))->getMock(); @@ -345,6 +332,16 @@ protected function resetKernelInstance(): void { HydeKernel::setInstance(new HydeKernel()); } + + protected function setupMock(string $class, string $method): Mockery\ExpectationInterface|Mockery\Expectation|Mockery\HigherOrderMessage + { + return Mockery::mock($class)->makePartial()->shouldReceive($method)->once(); + } + + protected function mockOutput(): Mockery\LegacyMockInterface|Mockery\MockInterface|OutputStyle + { + return Mockery::mock(OutputStyle::class)->makePartial(); + } } class InstantiableTestBuildTask extends BuildTask diff --git a/tests/Unit/BuildWarningsTest.php b/tests/Unit/BuildWarningsTest.php index 5de6645c..0d420711 100644 --- a/tests/Unit/BuildWarningsTest.php +++ b/tests/Unit/BuildWarningsTest.php @@ -21,8 +21,6 @@ class BuildWarningsTest extends UnitTestCase protected function tearDown(): void { app()->forgetInstance(BuildWarnings::class); - - parent::tearDown(); } public function testGetInstance() diff --git a/tests/Unit/CreatesNewMarkdownPostFileTest.php b/tests/Unit/CreatesNewMarkdownPostFileTest.php index 47b81290..338db38c 100644 --- a/tests/Unit/CreatesNewMarkdownPostFileTest.php +++ b/tests/Unit/CreatesNewMarkdownPostFileTest.php @@ -5,7 +5,7 @@ namespace Hyde\Framework\Testing\Unit; use Hyde\Hyde; -use Hyde\Testing\TestCase; +use Hyde\Testing\UnitTestCase; use Illuminate\Support\Carbon; use Hyde\Framework\Actions\CreatesNewMarkdownPostFile; @@ -14,12 +14,13 @@ * * @see \Hyde\Framework\Testing\Feature\Commands\MakePostCommandTest */ -class CreatesNewMarkdownPostFileTest extends TestCase +class CreatesNewMarkdownPostFileTest extends UnitTestCase { + protected static bool $needsKernel = true; + protected static bool $needsConfig = true; + protected function setUp(): void { - parent::setUp(); - Carbon::setTestNow(Carbon::create(2024)); } diff --git a/tests/Unit/Facades/AssetFacadeTest.php b/tests/Unit/Facades/AssetFacadeTest.php index df8344a3..a3e504c2 100644 --- a/tests/Unit/Facades/AssetFacadeTest.php +++ b/tests/Unit/Facades/AssetFacadeTest.php @@ -5,14 +5,22 @@ namespace Hyde\Framework\Testing\Unit\Facades; use Hyde\Facades\Asset; +use Hyde\Testing\UnitTestCase; +use Hyde\Testing\CreatesApplication; use Hyde\Framework\Services\AssetService; -use Hyde\Testing\TestCase; /** * @covers \Hyde\Facades\Asset */ -class AssetFacadeTest extends TestCase +class AssetFacadeTest extends UnitTestCase { + use CreatesApplication; + + protected function setUp(): void + { + $this->createApplication(); + } + public function testAssetFacadeReturnsTheAssetService() { $this->assertInstanceOf(AssetService::class, Asset::getFacadeRoot()); diff --git a/tests/Unit/Facades/HydeFacadesAreAliasedInAppConfigTest.php b/tests/Unit/Facades/HydeFacadesAreAliasedInAppConfigTest.php index 132e7bda..07efeb0a 100644 --- a/tests/Unit/Facades/HydeFacadesAreAliasedInAppConfigTest.php +++ b/tests/Unit/Facades/HydeFacadesAreAliasedInAppConfigTest.php @@ -5,10 +5,17 @@ namespace Hyde\Framework\Testing\Unit\Facades; use Hyde\Hyde; -use Hyde\Testing\TestCase; +use Hyde\Testing\UnitTestCase; -class HydeFacadesAreAliasedInAppConfigTest extends TestCase +class HydeFacadesAreAliasedInAppConfigTest extends UnitTestCase { + protected static bool $needsKernel = true; + + protected function setUp(): void + { + self::mockConfig(['app' => require Hyde::path('app/config.php')]); + } + public function testAllFacadesAreAliasedInAppConfig() { $this->assertArrayHasKey('Hyde', config('app.aliases')); diff --git a/tests/Unit/FilesystemFacadeUnitTest.php b/tests/Unit/FilesystemFacadeUnitTest.php new file mode 100644 index 00000000..8aa29c7b --- /dev/null +++ b/tests/Unit/FilesystemFacadeUnitTest.php @@ -0,0 +1,398 @@ +verifyMockeryExpectations(); + } + + public function testGetContents() + { + $this->createExpectation('get', 'string', Hyde::path('path')); + + Filesystem::getContents('path'); + } + + public function testGetContentsWithLock() + { + $this->createExpectation('get', 'string', Hyde::path('path'), true); + + Filesystem::getContents('path', true); + } + + public function testPutContents() + { + $this->createExpectation('put', true, Hyde::path('path'), 'string'); + + Filesystem::putContents('path', 'string'); + } + + public function testPutContentsWithLock() + { + $this->createExpectation('put', true, Hyde::path('path'), 'string', true); + + Filesystem::putContents('path', 'string', true); + } + + public function testExists() + { + $this->createExpectation('exists', true, Hyde::path('path')); + + Filesystem::exists('path'); + } + + public function testMissing() + { + $this->createExpectation('missing', true, Hyde::path('path')); + + Filesystem::missing('path'); + } + + public function testGet() + { + $this->createExpectation('get', 'string', Hyde::path('path')); + + Filesystem::get('path'); + } + + public function testSharedGet() + { + $this->createExpectation('sharedGet', 'string', Hyde::path('path')); + + Filesystem::sharedGet('path'); + } + + public function testGetRequire() + { + $this->createExpectation('getRequire', 'string', Hyde::path('path')); + + Filesystem::getRequire('path'); + } + + public function testRequireOnce() + { + $this->createExpectation('requireOnce', 'string', Hyde::path('path')); + + Filesystem::requireOnce('path'); + } + + public function testLines() + { + $this->createExpectation('lines', new LazyCollection(), Hyde::path('path')); + + Filesystem::lines('path'); + } + + public function testHash() + { + $this->createExpectation('hash', 'string', Hyde::path('path')); + + Filesystem::hash('path'); + } + + public function testPut() + { + $this->createExpectation('put', 10, Hyde::path('path'), 'contents'); + + Filesystem::put('path', 'contents'); + } + + public function testReplace() + { + $this->createExpectation('replace', null, Hyde::path('path'), 'content'); + + Filesystem::replace('path', 'content'); + } + + public function testReplaceInFile() + { + $this->createExpectation('replaceInFile', null, 'search', 'replace', Hyde::path('path')); + + Filesystem::replaceInFile('search', 'replace', 'path'); + } + + public function testPrepend() + { + $this->createExpectation('prepend', 10, Hyde::path('path'), 'data'); + + Filesystem::prepend('path', 'data'); + } + + public function testAppend() + { + $this->createExpectation('append', 10, Hyde::path('path'), 'data'); + + Filesystem::append('path', 'data'); + } + + public function testChmod() + { + $this->createExpectation('chmod', null, Hyde::path('path'), 0755); + + Filesystem::chmod('path', 0755); + } + + public function testDelete() + { + $this->createExpectation('delete', true, Hyde::path('path')); + + Filesystem::delete('path'); + } + + public function testDeleteWithArray() + { + $this->createExpectation('delete', true, [Hyde::path('path'), Hyde::path('another')]); + + Filesystem::delete(['path', 'another']); + } + + public function testMove() + { + $this->createExpectation('move', true, Hyde::path('path'), Hyde::path('target')); + + Filesystem::move('path', 'target'); + } + + public function testCopy() + { + $this->createExpectation('copy', true, Hyde::path('path'), Hyde::path('target')); + + Filesystem::copy('path', 'target'); + } + + public function testLink() + { + $this->createExpectation('link', true, Hyde::path('target'), Hyde::path('link')); + + Filesystem::link('target', 'link'); + } + + public function testRelativeLink() + { + $this->createExpectation('relativeLink', true, Hyde::path('target'), Hyde::path('link')); + + Filesystem::relativeLink('target', 'link'); + } + + public function testName() + { + $this->createExpectation('name', 'string', Hyde::path('path')); + + Filesystem::name('path'); + } + + public function testBasename() + { + $this->createExpectation('basename', 'string', Hyde::path('path')); + + Filesystem::basename('path'); + } + + public function testDirname() + { + $this->createExpectation('dirname', 'string', Hyde::path('path')); + + Filesystem::dirname('path'); + } + + public function testExtension() + { + $this->createExpectation('extension', 'string', Hyde::path('path')); + + Filesystem::extension('path'); + } + + public function testGuessExtension() + { + $this->createExpectation('guessExtension', 'string', Hyde::path('path')); + + Filesystem::guessExtension('path'); + } + + public function testType() + { + $this->createExpectation('type', 'string', Hyde::path('path')); + + Filesystem::type('path'); + } + + public function testMimeType() + { + $this->createExpectation('mimeType', 'string', Hyde::path('path')); + + Filesystem::mimeType('path'); + } + + public function testSize() + { + $this->createExpectation('size', 10, Hyde::path('path')); + + Filesystem::size('path'); + } + + public function testLastModified() + { + $this->createExpectation('lastModified', 10, Hyde::path('path')); + + Filesystem::lastModified('path'); + } + + public function testIsDirectory() + { + $this->createExpectation('isDirectory', true, Hyde::path('directory')); + + Filesystem::isDirectory('directory'); + } + + public function testIsEmptyDirectory() + { + $this->createExpectation('isEmptyDirectory', true, Hyde::path('directory')); + + Filesystem::isEmptyDirectory('directory'); + } + + public function testIsReadable() + { + $this->createExpectation('isReadable', true, Hyde::path('path')); + + Filesystem::isReadable('path'); + } + + public function testIsWritable() + { + $this->createExpectation('isWritable', true, Hyde::path('path')); + + Filesystem::isWritable('path'); + } + + public function testHasSameHash() + { + $this->createExpectation('hasSameHash', true, Hyde::path('firstFile'), Hyde::path('secondFile')); + + Filesystem::hasSameHash('firstFile', 'secondFile'); + } + + public function testIsFile() + { + $this->createExpectation('isFile', true, Hyde::path('file')); + + Filesystem::isFile('file'); + } + + public function testGlob() + { + $this->createExpectation('glob', [], Hyde::path('pattern')); + + Filesystem::glob('pattern'); + } + + public function testFiles() + { + $this->createExpectation('files', [], Hyde::path('directory')); + + Filesystem::files('directory'); + } + + public function testAllFiles() + { + $this->createExpectation('allFiles', [], Hyde::path('directory')); + + Filesystem::allFiles('directory'); + } + + public function testDirectories() + { + $this->createExpectation('directories', [], Hyde::path('directory')); + + Filesystem::directories('directory'); + } + + public function testEnsureDirectoryExists() + { + $this->createExpectation('ensureDirectoryExists', null, Hyde::path('path')); + + Filesystem::ensureDirectoryExists('path'); + } + + public function testMakeDirectory() + { + $this->createExpectation('makeDirectory', true, Hyde::path('path')); + + Filesystem::makeDirectory('path'); + } + + public function testMoveDirectory() + { + $this->createExpectation('moveDirectory', true, Hyde::path('from'), Hyde::path('to')); + + Filesystem::moveDirectory('from', 'to'); + } + + public function testCopyDirectory() + { + $this->createExpectation('copyDirectory', true, Hyde::path('directory'), Hyde::path('destination')); + + Filesystem::copyDirectory('directory', 'destination'); + } + + public function testDeleteDirectory() + { + $this->createExpectation('deleteDirectory', true, Hyde::path('directory')); + + Filesystem::deleteDirectory('directory'); + } + + public function testDeleteDirectories() + { + $this->createExpectation('deleteDirectories', true, Hyde::path('directory')); + + Filesystem::deleteDirectories('directory'); + } + + public function testCleanDirectory() + { + $this->createExpectation('cleanDirectory', true, Hyde::path('directory')); + + Filesystem::cleanDirectory('directory'); + } + + public function testSmartGlob() + { + $this->createExpectation('glob', [ + Hyde::path('foo'), + Hyde::path('bar'), + Hyde::path('baz'), + ], Hyde::path('pattern/*.md'), 0); + + $expected = Collection::make(['foo', 'bar', 'baz']); + $actual = Filesystem::smartGlob('pattern/*.md'); + + $this->assertEquals($expected, $actual); + $this->assertSame($expected->all(), $actual->all()); + } + + protected function createExpectation(string $method, mixed $returns, ...$args): void + { + $this->mockFilesystem()->shouldReceive($method)->withArgs($args)->once()->andReturn($returns); + } +} diff --git a/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php b/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php index d228f3ae..9d2a394b 100644 --- a/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php +++ b/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php @@ -6,21 +6,25 @@ use Hyde\Foundation\HydeKernel; use Hyde\Foundation\Kernel\Hyperlinks; +use Hyde\Testing\FluentTestingHelpers; use Hyde\Framework\Exceptions\BaseUrlNotSetException; -use Hyde\Testing\TestCase; +use Hyde\Testing\UnitTestCase; /** * @covers \Hyde\Foundation\Kernel\Hyperlinks * @covers \Hyde\Framework\Exceptions\BaseUrlNotSetException */ -class HyperlinksUrlPathHelpersTest extends TestCase +class HyperlinksUrlPathHelpersTest extends UnitTestCase { + use FluentTestingHelpers; + + protected static bool $needsKernel = true; + protected static bool $needsConfig = true; + protected Hyperlinks $class; protected function setUp(): void { - parent::setUp(); - $this->class = new Hyperlinks(HydeKernel::getInstance()); } @@ -118,7 +122,7 @@ public function testQualifiedUrlHelperWithAlreadyQualifiedUrl() public function testQualifiedUrlHelperWithAlreadyQualifiedUrlWhenSiteUrlIsSet() { - $this->app['config']->set(['hyde.url' => 'https://example.com']); + self::mockConfig(['hyde.url' => 'https://example.com']); $this->assertSame('https://example.com/foo', $this->class->url('https://example.com/foo')); $this->assertSame('http://localhost/foo', $this->class->url('http://localhost/foo')); @@ -126,7 +130,7 @@ public function testQualifiedUrlHelperWithAlreadyQualifiedUrlWhenSiteUrlIsSet() public function testQualifiedUrlHelperWithAlreadyQualifiedUrlWhenSiteUrlIsSetToSomethingElse() { - $this->app['config']->set(['hyde.url' => 'my-site.com']); + self::mockConfig(['hyde.url' => 'my-site.com']); $this->assertSame('https://example.com/foo', $this->class->url('https://example.com/foo')); $this->assertSame('http://localhost/foo', $this->class->url('http://localhost/foo')); @@ -141,7 +145,7 @@ public function testQualifiedUrlHelperWithAlreadyQualifiedUrlStillFormatsPath() public function testQualifiedUrlHelperWithAlreadyQualifiedUrlStillFormatsPathWhenSiteUrlIsSet() { - $this->app['config']->set(['hyde.url' => 'https://example.com']); + self::mockConfig(['hyde.url' => 'https://example.com']); $this->assertSame('https://example.com/foo/bar.html', $this->class->url('https://example.com/foo/bar.html')); $this->assertSame('http://localhost/foo/bar.html', $this->class->url('http://localhost/foo/bar.html')); $this->assertSame('http://localhost/foo/bar', $this->class->url('http://localhost/foo/bar/')); @@ -149,7 +153,7 @@ public function testQualifiedUrlHelperWithAlreadyQualifiedUrlStillFormatsPathWhe public function testQualifiedUrlHelperWithAlreadyQualifiedUrlStillFormatsPathWithPrettyUrls() { - $this->app['config']->set(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]); + self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]); $this->assertSame('https://example.com/foo/bar', $this->class->url('https://example.com/foo/bar.html')); $this->assertSame('http://localhost/foo/bar', $this->class->url('http://localhost/foo/bar.html')); $this->assertSame('http://localhost/foo/bar', $this->class->url('http://localhost/foo/bar/')); @@ -177,7 +181,7 @@ public function testHelperFallsBackToRelativeLinksWhenNoSiteUrlIsSet() public function testHelperFallsBackToPrettyRelativeLinksWhenNoSiteUrlIsSetAndPrettyUrlsAreEnabled() { - config(['hyde.url' => '', 'hyde.pretty_urls' => true]); + self::mockConfig(['hyde.url' => '', 'hyde.pretty_urls' => true]); $this->assertSame('/', $this->class->url('index.html')); $this->assertSame('foo', $this->class->url('foo.html')); @@ -197,7 +201,7 @@ public function testHelperFallsBackToRelativeLinksWhenSiteUrlIsSetToLocalhost() public function testHelperFallsBackToPrettyRelativeLinksWhenSiteUrlIsSetToLocalhostAndPrettyUrlsAreEnabled() { - config(['hyde.url' => 'http://localhost', 'hyde.pretty_urls' => true]); + self::mockConfig(['hyde.url' => 'http://localhost', 'hyde.pretty_urls' => true]); $this->assertSame('/', $this->class->url('index.html')); $this->assertSame('foo', $this->class->url('foo.html')); @@ -214,7 +218,7 @@ public function testHelperReturnsExpectedStringWhenSiteUrlIsSet() public function testHelperReturnsExpectedStringWhenPrettyUrlsAreEnabled() { - config(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]); + self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]); $this->assertSame('https://example.com', $this->class->url('index.html')); $this->assertSame('https://example.com/foo', $this->class->url('foo.html')); diff --git a/tests/Unit/HydeFileHelpersTest.php b/tests/Unit/HydeFileHelpersTest.php index 7bb1974b..d373c9b2 100644 --- a/tests/Unit/HydeFileHelpersTest.php +++ b/tests/Unit/HydeFileHelpersTest.php @@ -4,16 +4,29 @@ namespace Hyde\Framework\Testing\Unit; +use Mockery; +use Illuminate\View\Factory; +use Hyde\Testing\UnitTestCase; use Hyde\Foundation\Facades\Routes; use Hyde\Hyde; use Hyde\Support\Facades\Render; -use Hyde\Testing\TestCase; +use Illuminate\Support\Facades\View; /** * @covers \Hyde\Foundation\HydeKernel */ -class HydeFileHelpersTest extends TestCase +class HydeFileHelpersTest extends UnitTestCase { + protected static bool $needsKernel = true; + protected static bool $needsConfig = true; + + protected function setUp(): void + { + self::mockRender(); + + View::swap(Mockery::mock(Factory::class)->makePartial()); + } + public function testCurrentPageReturnsCurrentPageViewProperty() { Render::share('routeKey', 'foo'); diff --git a/tests/Unit/HydeHelperFacadeTest.php b/tests/Unit/HydeHelperFacadeTest.php index d6a3aef3..f8b5d2df 100644 --- a/tests/Unit/HydeHelperFacadeTest.php +++ b/tests/Unit/HydeHelperFacadeTest.php @@ -7,32 +7,28 @@ use Hyde\Enums\Feature; use Hyde\Facades\Features; use Hyde\Hyde; -use Hyde\Testing\TestCase; +use Hyde\Testing\UnitTestCase; /** * @covers \Hyde\Foundation\HydeKernel */ -class HydeHelperFacadeTest extends TestCase +class HydeHelperFacadeTest extends UnitTestCase { + protected static bool $needsKernel = true; + protected static bool $needsConfig = true; + public function testFeaturesFacadeReturnsInstanceOfFeaturesClass() { - $this->assertInstanceOf( - Features::class, - Hyde::features() - ); + $this->assertInstanceOf(Features::class, Hyde::features()); } public function testFeaturesFacadeCanBeUsedToCallStaticMethodsOnFeaturesClass() { - $this->assertTrue( - Hyde::features()->hasMarkdownPosts() - ); + $this->assertTrue(Hyde::features()->hasMarkdownPosts()); } public function testHydeHasFeatureShorthandCallsStaticMethodOnFeaturesClass() { - $this->assertTrue( - Hyde::hasFeature(Feature::MarkdownPosts) - ); + $this->assertTrue(Hyde::hasFeature(Feature::MarkdownPosts)); } } diff --git a/tests/Unit/HydePageDataFactoryTest.php b/tests/Unit/HydePageDataFactoryTest.php index 8c8f2a86..4590a54a 100644 --- a/tests/Unit/HydePageDataFactoryTest.php +++ b/tests/Unit/HydePageDataFactoryTest.php @@ -22,8 +22,6 @@ class HydePageDataFactoryTest extends UnitTestCase protected function tearDown(): void { self::mockConfig(); - - parent::tearDown(); } public function testCanConstruct() diff --git a/tests/Unit/InteractsWithDirectoriesConcernTest.php b/tests/Unit/InteractsWithDirectoriesConcernTest.php index d788f101..9d23ff0b 100644 --- a/tests/Unit/InteractsWithDirectoriesConcernTest.php +++ b/tests/Unit/InteractsWithDirectoriesConcernTest.php @@ -4,80 +4,89 @@ namespace Hyde\Framework\Testing\Unit; +use Hyde\Testing\UnitTestCase; use Hyde\Framework\Concerns\InteractsWithDirectories; use Hyde\Hyde; -use Hyde\Testing\TestCase; -use Illuminate\Support\Facades\File; /** * @covers \Hyde\Framework\Concerns\InteractsWithDirectories */ -class InteractsWithDirectoriesConcernTest extends TestCase +class InteractsWithDirectoriesConcernTest extends UnitTestCase { use InteractsWithDirectories; - protected function setUp(): void - { - parent::setUp(); + protected static bool $needsKernel = true; - File::deleteDirectory(Hyde::path('foo')); - } + /** @var \Illuminate\Filesystem\Filesystem&\Mockery\MockInterface */ + protected $filesystem; - protected function tearDown(): void + protected function setUp(): void { - File::deleteDirectory(Hyde::path('foo')); - - parent::tearDown(); + $this->filesystem = $this->mockFilesystemStrict(); } - public function testMethodsCanBeCalledStatically() + protected function tearDown(): void { - static::needsDirectory('foo'); - $this->assertDirectoryExists(Hyde::path('foo')); - - static::needsDirectories(['foo']); - $this->assertDirectoryExists(Hyde::path('foo')); + $this->verifyMockeryExpectations(); } public function testNeedsDirectoryCreatesTheDirectory() { + $this->filesystem->expects('exists')->with(Hyde::path('foo'))->andReturnFalse(); + $this->filesystem->expects('makeDirectory')->with(Hyde::path('foo'), 0755, true); + $this->needsDirectory('foo'); - $this->assertDirectoryExists(Hyde::path('foo')); } public function testNeedsDirectoryCreatesTheDirectoryRecursively() { + $this->filesystem->expects('exists')->with(Hyde::path('foo/bar/baz'))->andReturnFalse(); + $this->filesystem->expects('makeDirectory')->with(Hyde::path('foo/bar/baz'), 0755, true); + $this->needsDirectory('foo/bar/baz'); - $this->assertDirectoryExists(Hyde::path('foo/bar/baz')); } public function testNeedsDirectoryHandlesExistingDirectory() { + $this->filesystem->expects('exists')->with(Hyde::path('foo'))->andReturnTrue()->twice(); + $this->filesystem->expects('makeDirectory')->never(); + $this->needsDirectory('foo'); $this->needsDirectory('foo'); - - $this->assertDirectoryExists(Hyde::path('foo')); } public function testNeedsDirectoriesCreatesSingleDirectory() { + $this->filesystem->expects('exists')->with(Hyde::path('foo'))->andReturnFalse(); + $this->filesystem->expects('makeDirectory')->with(Hyde::path('foo'), 0755, true); + $this->needsDirectories(['foo']); - $this->assertDirectoryExists(Hyde::path('foo')); } public function testNeedsDirectoriesCreatesMultipleDirectories() { - $this->needsDirectories(['foo', 'bar']); - - $this->assertDirectoryExists(Hyde::path('foo')); - $this->assertDirectoryExists(Hyde::path('bar')); + $this->filesystem->expects('exists')->with(Hyde::path('foo'))->andReturnFalse(); + $this->filesystem->expects('exists')->with(Hyde::path('bar'))->andReturnFalse(); + $this->filesystem->expects('makeDirectory')->with(Hyde::path('foo'), 0755, true); + $this->filesystem->expects('makeDirectory')->with(Hyde::path('bar'), 0755, true); - File::deleteDirectory(Hyde::path('bar')); + $this->needsDirectories(['foo', 'bar']); } public function testNeedsParentDirectoryCreatesDirectoryForTheParentFile() { + $this->filesystem->expects('exists')->with(Hyde::path('foo/bar'))->andReturnFalse(); + $this->filesystem->expects('makeDirectory')->with(Hyde::path('foo/bar'), 0755, true); + $this->needsParentDirectory(Hyde::path('foo/bar/baz.txt')); - $this->assertDirectoryExists(Hyde::path('foo/bar')); + } + + public function testMethodsCanBeCalledStatically() + { + $this->filesystem->expects('exists')->with(Hyde::path('foo'))->andReturnFalse()->twice(); + $this->filesystem->expects('makeDirectory')->with(Hyde::path('foo'), 0755, true)->twice(); + + static::needsDirectory('foo'); + static::needsDirectories(['foo']); } } diff --git a/tests/Unit/Pages/PageModelParseHelperTest.php b/tests/Unit/Pages/PageModelParseHelperTest.php index 8e4c55fe..885a35a8 100644 --- a/tests/Unit/Pages/PageModelParseHelperTest.php +++ b/tests/Unit/Pages/PageModelParseHelperTest.php @@ -4,55 +4,58 @@ namespace Hyde\Framework\Testing\Unit\Pages; -use Hyde\Facades\Filesystem; +use Hyde\Hyde; use Hyde\Pages\BladePage; use Hyde\Pages\DocumentationPage; use Hyde\Pages\MarkdownPage; use Hyde\Pages\MarkdownPost; -use Hyde\Testing\TestCase; +use Hyde\Testing\UnitTestCase; /** * @covers \Hyde\Pages\Concerns\HydePage::parse */ -class PageModelParseHelperTest extends TestCase +class PageModelParseHelperTest extends UnitTestCase { - public function testBladePageGetHelperReturnsBladePageObject() + protected static bool $needsKernel = true; + protected static bool $needsConfig = true; + + protected function tearDown(): void { - Filesystem::touch('_pages/foo.blade.php'); + $this->verifyMockeryExpectations(); + } - $object = BladePage::parse('foo'); - $this->assertInstanceOf(BladePage::class, $object); + public function testBladePageGetHelperReturnsBladePageObject() + { + $this->mockFilesystemCalls('_pages/foo.blade.php'); - Filesystem::unlink('_pages/foo.blade.php'); + $this->assertInstanceOf(BladePage::class, BladePage::parse('foo')); } public function testMarkdownPageGetHelperReturnsMarkdownPageObject() { - Filesystem::touch('_pages/foo.md'); + $this->mockFilesystemCalls('_pages/foo.md'); - $object = MarkdownPage::parse('foo'); - $this->assertInstanceOf(MarkdownPage::class, $object); - - Filesystem::unlink('_pages/foo.md'); + $this->assertInstanceOf(MarkdownPage::class, MarkdownPage::parse('foo')); } public function testMarkdownPostGetHelperReturnsMarkdownPostObject() { - Filesystem::touch('_posts/foo.md'); - - $object = MarkdownPost::parse('foo'); - $this->assertInstanceOf(MarkdownPost::class, $object); + $this->mockFilesystemCalls('_posts/foo.md'); - Filesystem::unlink('_posts/foo.md'); + $this->assertInstanceOf(MarkdownPost::class, MarkdownPost::parse('foo')); } public function testDocumentationPageGetHelperReturnsDocumentationPageObject() { - Filesystem::touch('_docs/foo.md'); + $this->mockFilesystemCalls('_docs/foo.md'); - $object = DocumentationPage::parse('foo'); - $this->assertInstanceOf(DocumentationPage::class, $object); + $this->assertInstanceOf(DocumentationPage::class, DocumentationPage::parse('foo')); + } - Filesystem::unlink('_docs/foo.md'); + protected function mockFilesystemCalls(string $path): void + { + $this->mockFilesystemStrict() + ->shouldReceive('missing')->once()->with(Hyde::path($path))->andReturn(false) + ->shouldReceive('get')->once()->with(Hyde::path($path))->andReturn('foo'); } } diff --git a/tests/Unit/RenderHelperTest.php b/tests/Unit/RenderHelperTest.php index ee0cc280..e9919524 100644 --- a/tests/Unit/RenderHelperTest.php +++ b/tests/Unit/RenderHelperTest.php @@ -26,8 +26,6 @@ class RenderHelperTest extends UnitTestCase protected function setUp(): void { - parent::setUp(); - Render::swap(new RenderData()); View::swap(Mockery::mock(Factory::class)->makePartial()); } diff --git a/tests/Unit/ServeCommandOptionsUnitTest.php b/tests/Unit/ServeCommandOptionsUnitTest.php index 073734e9..3c9c0a96 100644 --- a/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/tests/Unit/ServeCommandOptionsUnitTest.php @@ -34,11 +34,7 @@ protected function setUp(): void protected function tearDown(): void { - $this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount()); - - Mockery::close(); - - parent::tearDown(); + $this->verifyMockeryExpectations(); } public function testGetHostSelection() diff --git a/tests/Unit/TestingSupportHelpersMetaTest.php b/tests/Unit/TestingSupportHelpersMetaTest.php index b27bff28..f4ca64f3 100644 --- a/tests/Unit/TestingSupportHelpersMetaTest.php +++ b/tests/Unit/TestingSupportHelpersMetaTest.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Testing\Unit; +use AllowDynamicProperties; use Hyde\Pages\InMemoryPage; use Hyde\Testing\UnitTestCase; use Hyde\Testing\MocksKernelFeatures; @@ -16,6 +17,7 @@ * * @coversNothing */ +#[AllowDynamicProperties] class TestingSupportHelpersMetaTest extends UnitTestCase { use MocksKernelFeatures; diff --git a/tests/Unit/ValidatesExistenceTest.php b/tests/Unit/ValidatesExistenceTest.php index 51414ea4..efcc3b88 100644 --- a/tests/Unit/ValidatesExistenceTest.php +++ b/tests/Unit/ValidatesExistenceTest.php @@ -7,14 +7,16 @@ use Hyde\Framework\Concerns\ValidatesExistence; use Hyde\Framework\Exceptions\FileNotFoundException; use Hyde\Pages\BladePage; -use Hyde\Testing\TestCase; +use Hyde\Testing\UnitTestCase; /** * @covers \Hyde\Framework\Concerns\ValidatesExistence * @covers \Hyde\Framework\Exceptions\FileNotFoundException */ -class ValidatesExistenceTest extends TestCase +class ValidatesExistenceTest extends UnitTestCase { + protected static bool $needsKernel = true; + public function testValidateExistenceDoesNothingIfFileExists() { $class = new ValidatesExistenceTestClass(); diff --git a/tests/Unit/Views/LinkComponentTest.php b/tests/Unit/Views/LinkComponentViewTest.php similarity index 95% rename from tests/Unit/Views/LinkComponentTest.php rename to tests/Unit/Views/LinkComponentViewTest.php index edcb4c15..5fef3bb8 100644 --- a/tests/Unit/Views/LinkComponentTest.php +++ b/tests/Unit/Views/LinkComponentViewTest.php @@ -12,7 +12,7 @@ /** * @covers \Hyde\Framework\Views\Components\LinkComponent */ -class LinkComponentTest extends TestCase +class LinkComponentViewTest extends TestCase { public function testLinkComponentCanBeRendered() {