From 542f05ce0c3c558a578b789cc830917eb4c4e27b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 20:17:18 +0200 Subject: [PATCH 01/72] Remove unnecessary setup logic --- packages/framework/tests/Unit/NavItemTest.php | 2 -- packages/testing/src/UnitTestCase.php | 7 +------ 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/framework/tests/Unit/NavItemTest.php b/packages/framework/tests/Unit/NavItemTest.php index 379fbf581eb..6409ef27668 100644 --- a/packages/framework/tests/Unit/NavItemTest.php +++ b/packages/framework/tests/Unit/NavItemTest.php @@ -27,8 +27,6 @@ class NavItemTest extends UnitTestCase { public static function setUpBeforeClass(): void { - self::$hasSetUpKernel = false; - self::needsKernel(); self::mockConfig(); } diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index 764334bec74..14f32803e74 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -13,17 +13,13 @@ abstract class UnitTestCase extends BaseTestCase { - protected static bool $hasSetUpKernel = false; - protected static bool $needsKernel = false; protected static bool $needsConfig = false; protected static bool $needsRender = false; protected static function needsKernel(): void { - if (! self::$hasSetUpKernel) { - self::setupKernel(); - } + self::setupKernel(); } public static function setUpBeforeClass(): void @@ -44,7 +40,6 @@ public static function setUpBeforeClass(): void protected static function setupKernel(): void { HydeKernel::setInstance(new HydeKernel()); - self::$hasSetUpKernel = true; } protected static function resetKernel(): void From e17c37f32363d1bdcd760246571aa868a5a4462f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 20:21:16 +0200 Subject: [PATCH 02/72] Inline simplified testing helper method alias --- packages/framework/tests/Unit/AssetServiceUnitTest.php | 2 +- .../framework/tests/Unit/BaseFoundationCollectionTest.php | 2 +- packages/framework/tests/Unit/BreadcrumbsComponentTest.php | 2 +- packages/framework/tests/Unit/BuildTaskUnitTest.php | 4 ++-- .../framework/tests/Unit/HtmlTestingSupportMetaTest.php | 2 +- packages/framework/tests/Unit/NavItemTest.php | 2 +- .../framework/tests/Unit/NavigationDataFactoryUnitTest.php | 2 +- packages/framework/tests/Unit/UnixsumTest.php | 2 +- packages/hyde/tests/DefaultContentTest.php | 2 +- packages/testing/src/UnitTestCase.php | 7 +------ 10 files changed, 11 insertions(+), 16 deletions(-) diff --git a/packages/framework/tests/Unit/AssetServiceUnitTest.php b/packages/framework/tests/Unit/AssetServiceUnitTest.php index 9267b4715a6..4554ec53df0 100644 --- a/packages/framework/tests/Unit/AssetServiceUnitTest.php +++ b/packages/framework/tests/Unit/AssetServiceUnitTest.php @@ -17,7 +17,7 @@ class AssetServiceUnitTest extends UnitTestCase { protected function setUp(): void { - self::needsKernel(); + self::resetKernel(); self::mockConfig(); } diff --git a/packages/framework/tests/Unit/BaseFoundationCollectionTest.php b/packages/framework/tests/Unit/BaseFoundationCollectionTest.php index 9fb8edb7d5e..8dee25e871d 100644 --- a/packages/framework/tests/Unit/BaseFoundationCollectionTest.php +++ b/packages/framework/tests/Unit/BaseFoundationCollectionTest.php @@ -17,7 +17,7 @@ class BaseFoundationCollectionTest extends UnitTestCase { public function testInit() { - $this->needsKernel(); + $this->resetKernel(); $booted = BaseFoundationCollectionTestClass::init(HydeKernel::getInstance())->boot(); diff --git a/packages/framework/tests/Unit/BreadcrumbsComponentTest.php b/packages/framework/tests/Unit/BreadcrumbsComponentTest.php index 154fddbfb00..c94d6627cc9 100644 --- a/packages/framework/tests/Unit/BreadcrumbsComponentTest.php +++ b/packages/framework/tests/Unit/BreadcrumbsComponentTest.php @@ -23,7 +23,7 @@ class BreadcrumbsComponentTest extends UnitTestCase { protected function setUp(): void { - self::needsKernel(); + self::resetKernel(); self::mockConfig(); } diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 4b484cf27c6..45127d4d337 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -153,7 +153,7 @@ public function testCanWriteToOutput() public function testCreatedSiteFile() { - self::needsKernel(); + self::resetKernel(); $task = new BufferedTestBuildTask(); @@ -164,7 +164,7 @@ public function testCreatedSiteFile() public function testCreatedSiteFileWithAbsolutePath() { - self::needsKernel(); + self::resetKernel(); $task = new BufferedTestBuildTask(); diff --git a/packages/framework/tests/Unit/HtmlTestingSupportMetaTest.php b/packages/framework/tests/Unit/HtmlTestingSupportMetaTest.php index 5b6ec6f42da..e17ad1e510b 100644 --- a/packages/framework/tests/Unit/HtmlTestingSupportMetaTest.php +++ b/packages/framework/tests/Unit/HtmlTestingSupportMetaTest.php @@ -33,7 +33,7 @@ protected function setUp(): void { parent::setUp(); - self::needsKernel(); + self::resetKernel(); $this->html ??= file_get_contents(Hyde::vendorPath('resources/views/homepages/welcome.blade.php')); } diff --git a/packages/framework/tests/Unit/NavItemTest.php b/packages/framework/tests/Unit/NavItemTest.php index 6409ef27668..e4dbb2f99a9 100644 --- a/packages/framework/tests/Unit/NavItemTest.php +++ b/packages/framework/tests/Unit/NavItemTest.php @@ -27,7 +27,7 @@ class NavItemTest extends UnitTestCase { public static function setUpBeforeClass(): void { - self::needsKernel(); + self::resetKernel(); self::mockConfig(); } diff --git a/packages/framework/tests/Unit/NavigationDataFactoryUnitTest.php b/packages/framework/tests/Unit/NavigationDataFactoryUnitTest.php index 8074f2efff7..c5f3524f263 100644 --- a/packages/framework/tests/Unit/NavigationDataFactoryUnitTest.php +++ b/packages/framework/tests/Unit/NavigationDataFactoryUnitTest.php @@ -19,7 +19,7 @@ class NavigationDataFactoryUnitTest extends UnitTestCase { protected function setUp(): void { - self::needsKernel(); + self::resetKernel(); self::mockConfig(); } diff --git a/packages/framework/tests/Unit/UnixsumTest.php b/packages/framework/tests/Unit/UnixsumTest.php index 968c1d0d5f1..81b800eda6c 100644 --- a/packages/framework/tests/Unit/UnixsumTest.php +++ b/packages/framework/tests/Unit/UnixsumTest.php @@ -63,7 +63,7 @@ public function testMethodReturnsSameValueForStringWithMixedEndOfLineSequences() public function testMethodReturnsSameValueWhenLoadedFromFileUsingShorthand() { - self::needsKernel(); + self::resetKernel(); $string = "foo\nbar\r\nbaz\r\n"; diff --git a/packages/hyde/tests/DefaultContentTest.php b/packages/hyde/tests/DefaultContentTest.php index f632c9c433a..77c8d349d18 100644 --- a/packages/hyde/tests/DefaultContentTest.php +++ b/packages/hyde/tests/DefaultContentTest.php @@ -10,7 +10,7 @@ class DefaultContentTest extends UnitTestCase { public static function setUpBeforeClass(): void { - self::needsKernel(); + self::resetKernel(); } public function testDefaultPagesArePresent() diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index 14f32803e74..7a743b9d4be 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -17,15 +17,10 @@ abstract class UnitTestCase extends BaseTestCase protected static bool $needsConfig = false; protected static bool $needsRender = false; - protected static function needsKernel(): void - { - self::setupKernel(); - } - public static function setUpBeforeClass(): void { if (static::$needsKernel) { - self::needsKernel(); + self::resetKernel(); } if (static::$needsConfig) { From 059ddafcfc5a61687e0259e23347bb8042dd26eb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 20:21:37 +0200 Subject: [PATCH 03/72] Normalize static method access --- packages/framework/tests/Unit/BaseFoundationCollectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/BaseFoundationCollectionTest.php b/packages/framework/tests/Unit/BaseFoundationCollectionTest.php index 8dee25e871d..4f0192fec98 100644 --- a/packages/framework/tests/Unit/BaseFoundationCollectionTest.php +++ b/packages/framework/tests/Unit/BaseFoundationCollectionTest.php @@ -17,7 +17,7 @@ class BaseFoundationCollectionTest extends UnitTestCase { public function testInit() { - $this->resetKernel(); + self::resetKernel(); $booted = BaseFoundationCollectionTestClass::init(HydeKernel::getInstance())->boot(); From 330f08927765869206e30af6d4644853d4feb79d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 20:22:19 +0200 Subject: [PATCH 04/72] Use a better test method name --- packages/framework/tests/Unit/BaseFoundationCollectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/BaseFoundationCollectionTest.php b/packages/framework/tests/Unit/BaseFoundationCollectionTest.php index 4f0192fec98..95725ec5751 100644 --- a/packages/framework/tests/Unit/BaseFoundationCollectionTest.php +++ b/packages/framework/tests/Unit/BaseFoundationCollectionTest.php @@ -15,7 +15,7 @@ */ class BaseFoundationCollectionTest extends UnitTestCase { - public function testInit() + public function testBaseFoundationCollectionInitialization() { self::resetKernel(); From 40e3dfe7b4f9ebfc7cd318156b58d97c10bd9a09 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 20:26:19 +0200 Subject: [PATCH 05/72] Add linting rule against using useless parent calls in unit tests --- monorepo/HydeStan/HydeStan.php | 22 ++++++++++++++++++++++ monorepo/HydeStan/includes/helpers.php | 9 ++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/monorepo/HydeStan/HydeStan.php b/monorepo/HydeStan/HydeStan.php index 05aa7eafe55..ea49692f419 100644 --- a/monorepo/HydeStan/HydeStan.php +++ b/monorepo/HydeStan/HydeStan.php @@ -20,6 +20,7 @@ final class HydeStan private const TEST_FILE_ANALYSERS = [ NoFixMeAnalyser::class, NoUsingAssertEqualsForScalarTypesTestAnalyser::class, + NoParentSetUpTearDownInUnitTestCaseAnalyser::class, ]; private const LINE_ANALYSERS = [ @@ -382,3 +383,24 @@ public function run(string $file, int $lineNumber, string $line): void } } } + +class NoParentSetUpTearDownInUnitTestCaseAnalyser extends FileAnalyser +{ + public function run(string $file, string $contents): void + { + if (! str_contains($contents, 'extends UnitTestCase')) { + return; + } + + $methods = ['setUp', 'tearDown']; + + foreach ($methods as $method) { + AnalysisStatisticsContainer::analysedExpression(); + if (str_contains($contents, "parent::$method()")) { + $lineNumber = substr_count(substr($contents, 0, strpos($contents, $method)), "\n") + 1; + $this->fail(sprintf("Found '%s' method in UnitTestCase at %s", "parent::$method()", fileLink($file, $lineNumber, false))); + HydeStan::addActionsMessage('error', $file, $lineNumber, "HydeStan: UnnecessaryParent{$method}MethodError", "{$method} method in UnitTestCase performs no operation and should be removed."); + } + } + } +} diff --git a/monorepo/HydeStan/includes/helpers.php b/monorepo/HydeStan/includes/helpers.php index f3a9c8dcb31..7e542f683d3 100644 --- a/monorepo/HydeStan/includes/helpers.php +++ b/monorepo/HydeStan/includes/helpers.php @@ -51,11 +51,14 @@ function check_str_contains_any(array $searches, string $line): bool return $strContainsAny; } -function fileLink(string $file, ?int $line = null): string +function fileLink(string $file, ?int $line = null, bool $substr = true): string { $path = (realpath(__DIR__.'/../../packages/framework/'.$file) ?: $file).($line ? ':'.$line : ''); - $trim = strlen(getcwd()) + 2; - $path = substr($path, $trim); + + if ($substr) { + $trim = strlen(getcwd()) + 2; + $path = substr($path, $trim); + } return str_replace('\\', '/', $path); } From 2095b8ce97a1da2374749de6018ff04579263587 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 20:37:48 +0200 Subject: [PATCH 06/72] Remove useless parent calls in unit tests --- .../framework/tests/Feature/Foundation/FilesystemTest.php | 4 ---- packages/framework/tests/Feature/MarkdownFileParserTest.php | 2 -- packages/framework/tests/Unit/BuildTaskServiceUnitTest.php | 2 -- packages/framework/tests/Unit/BuildWarningsTest.php | 2 -- packages/framework/tests/Unit/HtmlTestingSupportMetaTest.php | 2 -- packages/framework/tests/Unit/HydePageDataFactoryTest.php | 2 -- packages/framework/tests/Unit/RenderHelperTest.php | 2 -- packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php | 2 -- 8 files changed, 18 deletions(-) diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index 6c2c2811cd8..5527f9ce127 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -31,8 +31,6 @@ class FilesystemTest extends UnitTestCase protected function setUp(): void { - parent::setUp(); - $this->originalBasePath = Hyde::getBasePath(); $this->filesystem = new Filesystem(Hyde::getInstance()); } @@ -40,8 +38,6 @@ protected function setUp(): void protected function tearDown(): void { Hyde::getInstance()->setBasePath($this->originalBasePath); - - parent::tearDown(); } public function testGetBasePathReturnsKernelsBasePath() diff --git a/packages/framework/tests/Feature/MarkdownFileParserTest.php b/packages/framework/tests/Feature/MarkdownFileParserTest.php index 09e5b1dc06e..511e3afa970 100644 --- a/packages/framework/tests/Feature/MarkdownFileParserTest.php +++ b/packages/framework/tests/Feature/MarkdownFileParserTest.php @@ -35,8 +35,6 @@ protected function makeTestPost(): void protected function tearDown(): void { Filesystem::unlink('_posts/test-post.md'); - - parent::tearDown(); } public function testCanParseMarkdownFile() diff --git a/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php index 1581584a008..0b5b89908e5 100644 --- a/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php @@ -45,8 +45,6 @@ protected function setUp(): void protected function tearDown(): void { - parent::tearDown(); - $this->verifyMockeryExpectations(); } diff --git a/packages/framework/tests/Unit/BuildWarningsTest.php b/packages/framework/tests/Unit/BuildWarningsTest.php index 5de6645c419..0d4207112a5 100644 --- a/packages/framework/tests/Unit/BuildWarningsTest.php +++ b/packages/framework/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/packages/framework/tests/Unit/HtmlTestingSupportMetaTest.php b/packages/framework/tests/Unit/HtmlTestingSupportMetaTest.php index e17ad1e510b..3c109d7473b 100644 --- a/packages/framework/tests/Unit/HtmlTestingSupportMetaTest.php +++ b/packages/framework/tests/Unit/HtmlTestingSupportMetaTest.php @@ -31,8 +31,6 @@ class HtmlTestingSupportMetaTest extends UnitTestCase protected function setUp(): void { - parent::setUp(); - self::resetKernel(); $this->html ??= file_get_contents(Hyde::vendorPath('resources/views/homepages/welcome.blade.php')); diff --git a/packages/framework/tests/Unit/HydePageDataFactoryTest.php b/packages/framework/tests/Unit/HydePageDataFactoryTest.php index 8c8f2a8676a..4590a54a64d 100644 --- a/packages/framework/tests/Unit/HydePageDataFactoryTest.php +++ b/packages/framework/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/packages/framework/tests/Unit/RenderHelperTest.php b/packages/framework/tests/Unit/RenderHelperTest.php index ee0cc280ecb..e99195248f1 100644 --- a/packages/framework/tests/Unit/RenderHelperTest.php +++ b/packages/framework/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/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index ea003f8feaf..ad04c70ea41 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -37,8 +37,6 @@ protected function tearDown(): void $this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount()); Mockery::close(); - - parent::tearDown(); } public function testGetHostSelection() From c6787fa30f5ba64e692c4f32c5fcbd828bc44382 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 21:02:47 +0200 Subject: [PATCH 07/72] Refactor unit test to mock the filesystem --- .../tests/Feature/MarkdownFileParserTest.php | 53 ++++++++++--------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/packages/framework/tests/Feature/MarkdownFileParserTest.php b/packages/framework/tests/Feature/MarkdownFileParserTest.php index 511e3afa970..9072defa793 100644 --- a/packages/framework/tests/Feature/MarkdownFileParserTest.php +++ b/packages/framework/tests/Feature/MarkdownFileParserTest.php @@ -4,42 +4,27 @@ namespace Hyde\Framework\Testing\Feature; -use Hyde\Hyde; -use Hyde\Facades\Filesystem; +use Illuminate\Filesystem\Filesystem; use Hyde\Framework\Actions\MarkdownFileParser; use Hyde\Markdown\Models\FrontMatter; use Hyde\Markdown\Models\MarkdownDocument; use Hyde\Testing\UnitTestCase; +use Mockery; class MarkdownFileParserTest extends UnitTestCase { protected static bool $needsKernel = true; - protected function makeTestPost(): void + protected function mockFilesystem(array $methods): 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 - ); - } + $filesystem = Mockery::mock(Filesystem::class, $methods)->makePartial(); - protected function tearDown(): void - { - Filesystem::unlink('_posts/test-post.md'); + app()->instance(Filesystem::class, $filesystem); } 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'); @@ -50,7 +35,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'); @@ -74,7 +69,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'); @@ -85,7 +90,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'); From 0a40852df9d4683ba279e8f993bcee1c3b53de3c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 21:13:52 +0200 Subject: [PATCH 08/72] Extract helper method to mock the filesystem --- .../framework/tests/Feature/MarkdownFileParserTest.php | 9 --------- packages/testing/src/UnitTestCase.php | 9 +++++++++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/framework/tests/Feature/MarkdownFileParserTest.php b/packages/framework/tests/Feature/MarkdownFileParserTest.php index 9072defa793..75ba8afcb1c 100644 --- a/packages/framework/tests/Feature/MarkdownFileParserTest.php +++ b/packages/framework/tests/Feature/MarkdownFileParserTest.php @@ -4,24 +4,15 @@ namespace Hyde\Framework\Testing\Feature; -use Illuminate\Filesystem\Filesystem; use Hyde\Framework\Actions\MarkdownFileParser; use Hyde\Markdown\Models\FrontMatter; use Hyde\Markdown\Models\MarkdownDocument; use Hyde\Testing\UnitTestCase; -use Mockery; class MarkdownFileParserTest extends UnitTestCase { protected static bool $needsKernel = true; - protected function mockFilesystem(array $methods): void - { - $filesystem = Mockery::mock(Filesystem::class, $methods)->makePartial(); - - app()->instance(Filesystem::class, $filesystem); - } - public function testCanParseMarkdownFile() { $this->mockFilesystem(['get' => 'Foo bar']); diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index 7a743b9d4be..40a9340fc42 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -4,10 +4,12 @@ namespace Hyde\Testing; +use Mockery; use Hyde\Foundation\HydeKernel; use Hyde\Support\Facades\Render; use Illuminate\Config\Repository; use Hyde\Support\Models\RenderData; +use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\Config; use PHPUnit\Framework\TestCase as BaseTestCase; @@ -60,4 +62,11 @@ protected static function mockConfig(array $items = []): void Config::swap(app('config')); } + + protected function mockFilesystem(array $methods): void + { + $filesystem = Mockery::mock(Filesystem::class, $methods)->makePartial(); + + app()->instance(Filesystem::class, $filesystem); + } } From b5dd609ebd65be9f48e1731d5f11687cce3362f2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 21:14:16 +0200 Subject: [PATCH 09/72] Return the mocked filesystem --- packages/testing/src/UnitTestCase.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index 40a9340fc42..5747094edfd 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -63,10 +63,12 @@ protected static function mockConfig(array $items = []): void Config::swap(app('config')); } - protected function mockFilesystem(array $methods): void + protected function mockFilesystem(array $methods): Filesystem { $filesystem = Mockery::mock(Filesystem::class, $methods)->makePartial(); app()->instance(Filesystem::class, $filesystem); + + return $filesystem; } } From abfe1bf84bdd76fdd5418e8884813c1bcc6df125 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 21:15:17 +0200 Subject: [PATCH 10/72] Refactor to use a tap call --- packages/testing/src/UnitTestCase.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index 5747094edfd..dcad9c52bc0 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -65,10 +65,9 @@ protected static function mockConfig(array $items = []): void protected function mockFilesystem(array $methods): Filesystem { - $filesystem = Mockery::mock(Filesystem::class, $methods)->makePartial(); - - app()->instance(Filesystem::class, $filesystem); - - return $filesystem; + return tap( + Mockery::mock(Filesystem::class, $methods)->makePartial(), + fn ($filesystem) => app()->instance(Filesystem::class, $filesystem) + ); } } From 1da6f5cd8182ff18456c98758f5a40c0a8609064 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 21:15:29 +0200 Subject: [PATCH 11/72] Use a traditional closure --- packages/testing/src/UnitTestCase.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index dcad9c52bc0..a83a2d57b5a 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -65,9 +65,8 @@ protected static function mockConfig(array $items = []): void protected function mockFilesystem(array $methods): Filesystem { - return tap( - Mockery::mock(Filesystem::class, $methods)->makePartial(), - fn ($filesystem) => app()->instance(Filesystem::class, $filesystem) - ); + return tap(Mockery::mock(Filesystem::class, $methods)->makePartial(), function ($filesystem) { + app()->instance(Filesystem::class, $filesystem); + }); } } From 5fb412e51c01c2458627631ca1b5fad67be31eba Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 21:17:37 +0200 Subject: [PATCH 12/72] Refactor to use a tap call --- packages/testing/src/UnitTestCase.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index a83a2d57b5a..0f5c479008f 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -58,9 +58,9 @@ protected static function mockCurrentRouteKey(?string $routeKey = null): void protected static function mockConfig(array $items = []): void { - app()->bind('config', fn (): Repository => new Repository($items)); - - Config::swap(app('config')); + Config::swap(tap(new Repository($items), function ($config) { + app()->instance('config', $config); + })); } protected function mockFilesystem(array $methods): Filesystem From 4f1a51827f2a9c363f3555449cc6b8bc9b8d8a63 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 21:22:49 +0200 Subject: [PATCH 13/72] Refactor to use a tap call --- packages/testing/src/UnitTestCase.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index 0f5c479008f..ca28f9dd51a 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -46,9 +46,9 @@ protected static function resetKernel(): void protected static function mockRender(): Render { - Render::swap(new RenderData()); - - return new Render(); + return tap(new Render(), function () { + Render::swap(new RenderData()); + }); } protected static function mockCurrentRouteKey(?string $routeKey = null): void From adc511cffe4a744973d8fb86e5a143e21be1bede Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 21:25:50 +0200 Subject: [PATCH 14/72] Extract helper to verify mockery expectations --- packages/framework/tests/Unit/BreadcrumbsComponentTest.php | 2 +- packages/framework/tests/Unit/BuildTaskServiceUnitTest.php | 7 ------- packages/framework/tests/Unit/DataCollectionUnitTest.php | 7 +++---- .../framework/tests/Unit/ServeCommandOptionsUnitTest.php | 4 +--- packages/testing/src/UnitTestCase.php | 7 +++++++ 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/packages/framework/tests/Unit/BreadcrumbsComponentTest.php b/packages/framework/tests/Unit/BreadcrumbsComponentTest.php index c94d6627cc9..dc5231ada67 100644 --- a/packages/framework/tests/Unit/BreadcrumbsComponentTest.php +++ b/packages/framework/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/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php index 0b5b89908e5..f5634463256 100644 --- a/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php @@ -321,13 +321,6 @@ 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()]) diff --git a/packages/framework/tests/Unit/DataCollectionUnitTest.php b/packages/framework/tests/Unit/DataCollectionUnitTest.php index fc4cfdea17b..9cc58b798cd 100644 --- a/packages/framework/tests/Unit/DataCollectionUnitTest.php +++ b/packages/framework/tests/Unit/DataCollectionUnitTest.php @@ -56,8 +56,7 @@ public function testFindMarkdownFilesCallsProperGlobPattern() DataCollections::markdown('foo')->keys()->toArray(); - $this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount()); - Mockery::close(); + $this->verifyMockeryExpectations(); } public function testFindMarkdownFilesWithNoFiles() @@ -71,7 +70,7 @@ public function testFindMarkdownFilesWithNoFiles() $this->assertSame([], DataCollections::markdown('foo')->keys()->toArray()); - Mockery::close(); + $this->verifyMockeryExpectations(); } public function testFindMarkdownFilesWithFiles() @@ -86,7 +85,7 @@ public function testFindMarkdownFilesWithFiles() $this->assertSame(['bar.md'], DataCollections::markdown('foo')->keys()->toArray()); - Mockery::close(); + $this->verifyMockeryExpectations(); } public function testStaticMarkdownHelperReturnsNewDataCollectionInstance() diff --git a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php index ad04c70ea41..f76d70e35ee 100644 --- a/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php +++ b/packages/framework/tests/Unit/ServeCommandOptionsUnitTest.php @@ -34,9 +34,7 @@ protected function setUp(): void protected function tearDown(): void { - $this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount()); - - Mockery::close(); + $this->verifyMockeryExpectations(); } public function testGetHostSelection() diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index ca28f9dd51a..a961369dd16 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -69,4 +69,11 @@ protected function mockFilesystem(array $methods): Filesystem app()->instance(Filesystem::class, $filesystem); }); } + + protected function verifyMockeryExpectations(): void + { + $this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount()); + + Mockery::close(); + } } From cedab993f2ceb5b3d556dbd802147b40acbd73a6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 21:32:22 +0200 Subject: [PATCH 15/72] Extract common code for filesystem mocking --- .../tests/Unit/DataCollectionUnitTest.php | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/packages/framework/tests/Unit/DataCollectionUnitTest.php b/packages/framework/tests/Unit/DataCollectionUnitTest.php index 9cc58b798cd..26791d39fa5 100644 --- a/packages/framework/tests/Unit/DataCollectionUnitTest.php +++ b/packages/framework/tests/Unit/DataCollectionUnitTest.php @@ -47,12 +47,7 @@ public function testCanConvertCollectionToJson() public function testFindMarkdownFilesCallsProperGlobPattern() { - $filesystem = Mockery::mock(Filesystem::class, ['exists' => true]); - $filesystem->shouldReceive('glob') - ->with(Hyde::path('resources/collections/foo/*.{md}'), GLOB_BRACE) - ->once(); - - app()->instance(Filesystem::class, $filesystem); + $this->mockFilesystemFacade(['shouldReceiveGlob' => true]); DataCollections::markdown('foo')->keys()->toArray(); @@ -61,12 +56,7 @@ public function testFindMarkdownFilesCallsProperGlobPattern() public function testFindMarkdownFilesWithNoFiles() { - $filesystem = Mockery::mock(Filesystem::class, [ - 'exists' => true, - 'glob' => [], - ]); - - app()->instance(Filesystem::class, $filesystem); + $this->mockFilesystemFacade(); $this->assertSame([], DataCollections::markdown('foo')->keys()->toArray()); @@ -75,13 +65,7 @@ public function testFindMarkdownFilesWithNoFiles() public function testFindMarkdownFilesWithFiles() { - $filesystem = Mockery::mock(Filesystem::class, [ - 'exists' => true, - 'glob' => ['bar.md'], - 'get' => 'foo', - ]); - - app()->instance(Filesystem::class, $filesystem); + $this->mockFilesystemFacade(['glob' => ['bar.md']]); $this->assertSame(['bar.md'], DataCollections::markdown('foo')->keys()->toArray()); @@ -92,4 +76,26 @@ public function testStaticMarkdownHelperReturnsNewDataCollectionInstance() { $this->assertInstanceOf(DataCollections::class, DataCollections::markdown('foo')); } + + protected function mockFilesystemFacade(array $config = []): void + { + $defaults = [ + 'exists' => true, + 'glob' => [], + 'get' => 'foo', + ]; + + $config = array_merge($defaults, $config); + + $filesystem = Mockery::mock(Filesystem::class, $config); + + if (isset($config['shouldReceiveGlob'])) { + $filesystem->shouldReceive('glob') + ->with(Hyde::path('resources/collections/foo/*.{md}'), GLOB_BRACE) + ->once() + ->andReturn($config['glob']); + } + + app()->instance(Filesystem::class, $filesystem); + } } From d0834e54d02fc52fa4c8b0845e872f6034316fc0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 21:34:05 +0200 Subject: [PATCH 16/72] Refactor unit test to use unit test case --- .../tests/Unit/Pages/PageModelParseHelperTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php index 8e4c55fedd7..eb26156c7f3 100644 --- a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php @@ -9,13 +9,16 @@ 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 { + protected static bool $needsKernel = true; + protected static bool $needsConfig = true; + public function testBladePageGetHelperReturnsBladePageObject() { Filesystem::touch('_pages/foo.blade.php'); From aae35d832fb6f57a625fbc21b5124941b23b1b77 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 21:37:03 +0200 Subject: [PATCH 17/72] Update parser to use mockable filesystem facade --- .../framework/src/Framework/Actions/BladeMatterParser.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Actions/BladeMatterParser.php b/packages/framework/src/Framework/Actions/BladeMatterParser.php index 2f2dcc6eb9c..df3c402d427 100644 --- a/packages/framework/src/Framework/Actions/BladeMatterParser.php +++ b/packages/framework/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 From 85810be7d03a71372efba746b926f28ef5a9d82d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 21:55:13 +0200 Subject: [PATCH 18/72] Make parameter optional --- packages/testing/src/UnitTestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index a961369dd16..416f35e58a5 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -63,7 +63,7 @@ protected static function mockConfig(array $items = []): void })); } - protected function mockFilesystem(array $methods): Filesystem + protected function mockFilesystem(array $methods = []): Filesystem { return tap(Mockery::mock(Filesystem::class, $methods)->makePartial(), function ($filesystem) { app()->instance(Filesystem::class, $filesystem); From f29d54f5659376f9d1b25ebce69c37124041d752 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:11:23 +0200 Subject: [PATCH 19/72] Annotate the return type combination --- packages/testing/src/UnitTestCase.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index 416f35e58a5..ef2d7c83004 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -63,6 +63,9 @@ protected static function mockConfig(array $items = []): void })); } + /** + * @return \Illuminate\Filesystem\Filesystem&\Mockery\MockInterface + */ protected function mockFilesystem(array $methods = []): Filesystem { return tap(Mockery::mock(Filesystem::class, $methods)->makePartial(), function ($filesystem) { From 467ac9fad3c1fef2a4aad1af9f16ef9372e84f58 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:11:39 +0200 Subject: [PATCH 20/72] Compress comment --- packages/testing/src/UnitTestCase.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index ef2d7c83004..f9c8c666783 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -63,9 +63,7 @@ protected static function mockConfig(array $items = []): void })); } - /** - * @return \Illuminate\Filesystem\Filesystem&\Mockery\MockInterface - */ + /** @return \Illuminate\Filesystem\Filesystem&\Mockery\MockInterface */ protected function mockFilesystem(array $methods = []): Filesystem { return tap(Mockery::mock(Filesystem::class, $methods)->makePartial(), function ($filesystem) { From fbc608e512c3dea9307796abf80ca3edc386e3b9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:14:00 +0200 Subject: [PATCH 21/72] Add parameter to control partial mock --- packages/testing/src/UnitTestCase.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index f9c8c666783..9323d6b1dfa 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -64,9 +64,15 @@ protected static function mockConfig(array $items = []): void } /** @return \Illuminate\Filesystem\Filesystem&\Mockery\MockInterface */ - protected function mockFilesystem(array $methods = []): Filesystem + protected function mockFilesystem(array $methods = [], bool $partial = true): Filesystem { - return tap(Mockery::mock(Filesystem::class, $methods)->makePartial(), function ($filesystem) { + $mock = Mockery::mock(Filesystem::class, $methods); + + if ($partial) { + $mock->makePartial(); + } + + return tap($mock, function ($filesystem) { app()->instance(Filesystem::class, $filesystem); }); } From 9c950ae31847c176292ba208556c91e3e4894431 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:14:09 +0200 Subject: [PATCH 22/72] Revert "Add parameter to control partial mock" This reverts commit fbc608e512c3dea9307796abf80ca3edc386e3b9. --- packages/testing/src/UnitTestCase.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index 9323d6b1dfa..f9c8c666783 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -64,15 +64,9 @@ protected static function mockConfig(array $items = []): void } /** @return \Illuminate\Filesystem\Filesystem&\Mockery\MockInterface */ - protected function mockFilesystem(array $methods = [], bool $partial = true): Filesystem + protected function mockFilesystem(array $methods = []): Filesystem { - $mock = Mockery::mock(Filesystem::class, $methods); - - if ($partial) { - $mock->makePartial(); - } - - return tap($mock, function ($filesystem) { + return tap(Mockery::mock(Filesystem::class, $methods)->makePartial(), function ($filesystem) { app()->instance(Filesystem::class, $filesystem); }); } From 26bfbe1d89b86dddb4bbb03e2f4c69a26ec73e6b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:16:31 +0200 Subject: [PATCH 23/72] Add method without partial mock --- packages/testing/src/UnitTestCase.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index f9c8c666783..e9c3eb470e7 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -71,6 +71,14 @@ protected function mockFilesystem(array $methods = []): Filesystem }); } + /** @return \Illuminate\Filesystem\Filesystem&\Mockery\MockInterface */ + protected function mockFilesystemStrict(array $methods = []): Filesystem + { + return tap(Mockery::mock(Filesystem::class, $methods), function ($filesystem) { + app()->instance(Filesystem::class, $filesystem); + }); + } + protected function verifyMockeryExpectations(): void { $this->addToAssertionCount(Mockery::getContainer()->mockery_getExpectationCount()); From 18a265293ec5491a2c1af5a5e7e82d1e577fd05e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:17:43 +0200 Subject: [PATCH 24/72] Forward methods using argument list function to make mocking easier And way less confusing... --- packages/framework/src/Facades/Filesystem.php | 4 ++-- .../tests/Feature/FilesystemFacadeTest.php | 18 ++++++++++++++++-- .../tests/Feature/ReadingTimeTest.php | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Facades/Filesystem.php b/packages/framework/src/Facades/Filesystem.php index 3229bc584c3..fa0434212e1 100644 --- a/packages/framework/src/Facades/Filesystem.php +++ b/packages/framework/src/Facades/Filesystem.php @@ -113,7 +113,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()); } /** @@ -126,7 +126,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/packages/framework/tests/Feature/FilesystemFacadeTest.php b/packages/framework/tests/Feature/FilesystemFacadeTest.php index db00aefc30c..085cc442ab1 100644 --- a/packages/framework/tests/Feature/FilesystemFacadeTest.php +++ b/packages/framework/tests/Feature/FilesystemFacadeTest.php @@ -80,18 +80,32 @@ public function testUnlinkIfExists() public function testGetContents() { - $this->createExpectation('get', 'string', Hyde::path('path'), false); + $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', false); + $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')); diff --git a/packages/framework/tests/Feature/ReadingTimeTest.php b/packages/framework/tests/Feature/ReadingTimeTest.php index 93dbe654526..6c2478d1f84 100644 --- a/packages/framework/tests/Feature/ReadingTimeTest.php +++ b/packages/framework/tests/Feature/ReadingTimeTest.php @@ -111,7 +111,7 @@ 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()); + app()->instance(Filesystem::class, Mockery::mock(Filesystem::class)->shouldReceive('get')->with(Hyde::path('foo.md'))->andReturn('Hello world')->getMock()); $this->assertInstanceOf(ReadingTime::class, ReadingTime::fromFile('foo.md')); $this->assertEquals(new ReadingTime('Hello world'), ReadingTime::fromFile('foo.md')); From 00b51487832a4587eef4d214b07046deb6a788a6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:33:58 +0200 Subject: [PATCH 25/72] Extract unit test and refactor it to be ten times faster Now runs in 0.065s instead of 0.70s --- .../tests/Feature/FilesystemFacadeTest.php | 375 +---------------- .../tests/Unit/FilesystemFacadeUnitTest.php | 398 ++++++++++++++++++ 2 files changed, 400 insertions(+), 373 deletions(-) create mode 100644 packages/framework/tests/Unit/FilesystemFacadeUnitTest.php diff --git a/packages/framework/tests/Feature/FilesystemFacadeTest.php b/packages/framework/tests/Feature/FilesystemFacadeTest.php index 085cc442ab1..1c4216c438a 100644 --- a/packages/framework/tests/Feature/FilesystemFacadeTest.php +++ b/packages/framework/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,356 +62,6 @@ public function testUnlinkIfExists() $this->assertFileDoesNotExist(Hyde::path('foo')); } - 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 testMethodWithoutMocking() { $this->assertSame(3, Filesystem::put('foo', 'bar')); @@ -460,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/packages/framework/tests/Unit/FilesystemFacadeUnitTest.php b/packages/framework/tests/Unit/FilesystemFacadeUnitTest.php new file mode 100644 index 00000000000..8aa29c7bc7f --- /dev/null +++ b/packages/framework/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); + } +} From 6b3bff32506aca23fcabc6916cb189aaafc58743 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:39:25 +0200 Subject: [PATCH 26/72] Refactor unit test to mock the filesystem --- .../Unit/Pages/PageModelParseHelperTest.php | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php index eb26156c7f3..274d84e5f7c 100644 --- a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php @@ -4,7 +4,7 @@ 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; @@ -21,41 +21,45 @@ class PageModelParseHelperTest extends UnitTestCase public function testBladePageGetHelperReturnsBladePageObject() { - Filesystem::touch('_pages/foo.blade.php'); + $this->mockFilesystemStrict() + ->shouldReceive('missing')->with(Hyde::path('_pages/foo.blade.php'))->andReturn(false) + ->shouldReceive('isFile')->with(Hyde::path('_pages/foo.blade.php'))->andReturn(true) + ->shouldReceive('get')->with(Hyde::path('_pages/foo.blade.php'))->andReturn('foo'); $object = BladePage::parse('foo'); $this->assertInstanceOf(BladePage::class, $object); - - Filesystem::unlink('_pages/foo.blade.php'); } public function testMarkdownPageGetHelperReturnsMarkdownPageObject() { - Filesystem::touch('_pages/foo.md'); + $this->mockFilesystemStrict() + ->shouldReceive('missing')->with(Hyde::path('_pages/foo.md'))->andReturn(false) + ->shouldReceive('isFile')->with(Hyde::path('_pages/foo.md'))->andReturn(true) + ->shouldReceive('get')->with(Hyde::path('_pages/foo.md'))->andReturn('foo'); $object = MarkdownPage::parse('foo'); $this->assertInstanceOf(MarkdownPage::class, $object); - - Filesystem::unlink('_pages/foo.md'); } public function testMarkdownPostGetHelperReturnsMarkdownPostObject() { - Filesystem::touch('_posts/foo.md'); + $this->mockFilesystemStrict() + ->shouldReceive('missing')->with(Hyde::path('_posts/foo.md'))->andReturn(false) + ->shouldReceive('isFile')->with(Hyde::path('_posts/foo.md'))->andReturn(true) + ->shouldReceive('get')->with(Hyde::path('_posts/foo.md'))->andReturn('foo'); $object = MarkdownPost::parse('foo'); $this->assertInstanceOf(MarkdownPost::class, $object); - - Filesystem::unlink('_posts/foo.md'); } public function testDocumentationPageGetHelperReturnsDocumentationPageObject() { - Filesystem::touch('_docs/foo.md'); + $this->mockFilesystemStrict() + ->shouldReceive('missing')->with(Hyde::path('_docs/foo.md'))->andReturn(false) + ->shouldReceive('isFile')->with(Hyde::path('_docs/foo.md'))->andReturn(true) + ->shouldReceive('get')->with(Hyde::path('_docs/foo.md'))->andReturn('foo'); $object = DocumentationPage::parse('foo'); $this->assertInstanceOf(DocumentationPage::class, $object); - - Filesystem::unlink('_docs/foo.md'); } } From 07057c4a5fa1df1797d7328e76a0c010dc564968 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:40:44 +0200 Subject: [PATCH 27/72] Extract helper for mocking the filesystem calls --- .../Unit/Pages/PageModelParseHelperTest.php | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php index 274d84e5f7c..cdc1a10da96 100644 --- a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php @@ -21,10 +21,7 @@ class PageModelParseHelperTest extends UnitTestCase public function testBladePageGetHelperReturnsBladePageObject() { - $this->mockFilesystemStrict() - ->shouldReceive('missing')->with(Hyde::path('_pages/foo.blade.php'))->andReturn(false) - ->shouldReceive('isFile')->with(Hyde::path('_pages/foo.blade.php'))->andReturn(true) - ->shouldReceive('get')->with(Hyde::path('_pages/foo.blade.php'))->andReturn('foo'); + $this->mockFilesystemCalls('_pages/foo.blade.php'); $object = BladePage::parse('foo'); $this->assertInstanceOf(BladePage::class, $object); @@ -32,10 +29,7 @@ public function testBladePageGetHelperReturnsBladePageObject() public function testMarkdownPageGetHelperReturnsMarkdownPageObject() { - $this->mockFilesystemStrict() - ->shouldReceive('missing')->with(Hyde::path('_pages/foo.md'))->andReturn(false) - ->shouldReceive('isFile')->with(Hyde::path('_pages/foo.md'))->andReturn(true) - ->shouldReceive('get')->with(Hyde::path('_pages/foo.md'))->andReturn('foo'); + $this->mockFilesystemCalls('_pages/foo.md'); $object = MarkdownPage::parse('foo'); $this->assertInstanceOf(MarkdownPage::class, $object); @@ -43,10 +37,7 @@ public function testMarkdownPageGetHelperReturnsMarkdownPageObject() public function testMarkdownPostGetHelperReturnsMarkdownPostObject() { - $this->mockFilesystemStrict() - ->shouldReceive('missing')->with(Hyde::path('_posts/foo.md'))->andReturn(false) - ->shouldReceive('isFile')->with(Hyde::path('_posts/foo.md'))->andReturn(true) - ->shouldReceive('get')->with(Hyde::path('_posts/foo.md'))->andReturn('foo'); + $this->mockFilesystemCalls('_posts/foo.md'); $object = MarkdownPost::parse('foo'); $this->assertInstanceOf(MarkdownPost::class, $object); @@ -54,12 +45,17 @@ public function testMarkdownPostGetHelperReturnsMarkdownPostObject() public function testDocumentationPageGetHelperReturnsDocumentationPageObject() { - $this->mockFilesystemStrict() - ->shouldReceive('missing')->with(Hyde::path('_docs/foo.md'))->andReturn(false) - ->shouldReceive('isFile')->with(Hyde::path('_docs/foo.md'))->andReturn(true) - ->shouldReceive('get')->with(Hyde::path('_docs/foo.md'))->andReturn('foo'); + $this->mockFilesystemCalls('_docs/foo.md'); $object = DocumentationPage::parse('foo'); $this->assertInstanceOf(DocumentationPage::class, $object); } + + protected function mockFilesystemCalls(string $path): void + { + $this->mockFilesystemStrict() + ->shouldReceive('missing')->with(Hyde::path($path))->andReturn(false) + ->shouldReceive('isFile')->with(Hyde::path($path))->andReturn(true) + ->shouldReceive('get')->with(Hyde::path($path))->andReturn('foo'); + } } From 6da08c152b8f36ea63c90b392c30ebf99884ef8c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:41:22 +0200 Subject: [PATCH 28/72] Inline local variables --- .../tests/Unit/Pages/PageModelParseHelperTest.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php index cdc1a10da96..c57b618abc7 100644 --- a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php @@ -23,32 +23,28 @@ public function testBladePageGetHelperReturnsBladePageObject() { $this->mockFilesystemCalls('_pages/foo.blade.php'); - $object = BladePage::parse('foo'); - $this->assertInstanceOf(BladePage::class, $object); + $this->assertInstanceOf(BladePage::class, BladePage::parse('foo')); } public function testMarkdownPageGetHelperReturnsMarkdownPageObject() { $this->mockFilesystemCalls('_pages/foo.md'); - $object = MarkdownPage::parse('foo'); - $this->assertInstanceOf(MarkdownPage::class, $object); + $this->assertInstanceOf(MarkdownPage::class, MarkdownPage::parse('foo')); } public function testMarkdownPostGetHelperReturnsMarkdownPostObject() { $this->mockFilesystemCalls('_posts/foo.md'); - $object = MarkdownPost::parse('foo'); - $this->assertInstanceOf(MarkdownPost::class, $object); + $this->assertInstanceOf(MarkdownPost::class, MarkdownPost::parse('foo')); } public function testDocumentationPageGetHelperReturnsDocumentationPageObject() { $this->mockFilesystemCalls('_docs/foo.md'); - $object = DocumentationPage::parse('foo'); - $this->assertInstanceOf(DocumentationPage::class, $object); + $this->assertInstanceOf(DocumentationPage::class, DocumentationPage::parse('foo')); } protected function mockFilesystemCalls(string $path): void From c3dddb2b26a3f8bbf61367f8f9bfc8208119e58b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:42:29 +0200 Subject: [PATCH 29/72] Set expectations to once --- .../framework/tests/Unit/Pages/PageModelParseHelperTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php index c57b618abc7..fa83a8a8eb8 100644 --- a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php @@ -50,8 +50,8 @@ public function testDocumentationPageGetHelperReturnsDocumentationPageObject() protected function mockFilesystemCalls(string $path): void { $this->mockFilesystemStrict() - ->shouldReceive('missing')->with(Hyde::path($path))->andReturn(false) - ->shouldReceive('isFile')->with(Hyde::path($path))->andReturn(true) - ->shouldReceive('get')->with(Hyde::path($path))->andReturn('foo'); + ->shouldReceive('missing')->once()->with(Hyde::path($path))->andReturn(false) + ->shouldReceive('isFile')->once()->with(Hyde::path($path))->andReturn(true) + ->shouldReceive('get')->once()->with(Hyde::path($path))->andReturn('foo'); } } From 495e0af533136a0fd844baf10cad94ff430dbc1b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:43:33 +0200 Subject: [PATCH 30/72] Verify mockery expectations at teardown --- .../framework/tests/Unit/Pages/PageModelParseHelperTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php index fa83a8a8eb8..256a2aa6d99 100644 --- a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php @@ -19,6 +19,11 @@ class PageModelParseHelperTest extends UnitTestCase protected static bool $needsKernel = true; protected static bool $needsConfig = true; + protected function tearDown(): void + { + $this->verifyMockeryExpectations(); + } + public function testBladePageGetHelperReturnsBladePageObject() { $this->mockFilesystemCalls('_pages/foo.blade.php'); From 91f034face8fe97b6a835e77c173a6ce695aaf33 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:43:47 +0200 Subject: [PATCH 31/72] Remove method no longer called by fixed mocks --- packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php index 256a2aa6d99..885a35a817f 100644 --- a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php @@ -56,7 +56,6 @@ protected function mockFilesystemCalls(string $path): void { $this->mockFilesystemStrict() ->shouldReceive('missing')->once()->with(Hyde::path($path))->andReturn(false) - ->shouldReceive('isFile')->once()->with(Hyde::path($path))->andReturn(true) ->shouldReceive('get')->once()->with(Hyde::path($path))->andReturn('foo'); } } From 7bc8e8110b94554a81b846b51ae641831cf424b2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:45:14 +0200 Subject: [PATCH 32/72] Generate test for HtmlPage class --- .../tests/Unit/Pages/PageModelParseHelperTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php index 885a35a817f..4d43f3047df 100644 --- a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php @@ -5,6 +5,7 @@ namespace Hyde\Framework\Testing\Unit\Pages; use Hyde\Hyde; +use Hyde\Pages\HtmlPage; use Hyde\Pages\BladePage; use Hyde\Pages\DocumentationPage; use Hyde\Pages\MarkdownPage; @@ -51,6 +52,13 @@ public function testDocumentationPageGetHelperReturnsDocumentationPageObject() $this->assertInstanceOf(DocumentationPage::class, DocumentationPage::parse('foo')); } + + public function testHtmlPageGetHelperReturnsHtmlPageObject() + { + $this->mockFilesystemCalls('_pages/foo.html'); + + $this->assertInstanceOf(HtmlPage::class, HtmlPage::parse('foo')); + } protected function mockFilesystemCalls(string $path): void { From ab6e886c74f7aebba0a06728d50f1c75070c21ea Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:45:17 +0200 Subject: [PATCH 33/72] Revert "Generate test for HtmlPage class" This reverts commit 7bc8e8110b94554a81b846b51ae641831cf424b2. --- .../tests/Unit/Pages/PageModelParseHelperTest.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php index 4d43f3047df..885a35a817f 100644 --- a/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelParseHelperTest.php @@ -5,7 +5,6 @@ namespace Hyde\Framework\Testing\Unit\Pages; use Hyde\Hyde; -use Hyde\Pages\HtmlPage; use Hyde\Pages\BladePage; use Hyde\Pages\DocumentationPage; use Hyde\Pages\MarkdownPage; @@ -52,13 +51,6 @@ public function testDocumentationPageGetHelperReturnsDocumentationPageObject() $this->assertInstanceOf(DocumentationPage::class, DocumentationPage::parse('foo')); } - - public function testHtmlPageGetHelperReturnsHtmlPageObject() - { - $this->mockFilesystemCalls('_pages/foo.html'); - - $this->assertInstanceOf(HtmlPage::class, HtmlPage::parse('foo')); - } protected function mockFilesystemCalls(string $path): void { From 75ad4a4a1df787ab9acb3104e6a36ccaa7b8ffaf Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 8 Sep 2024 22:46:53 +0200 Subject: [PATCH 34/72] Allow dynamic properties in test --- packages/framework/tests/Unit/TestingSupportHelpersMetaTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/tests/Unit/TestingSupportHelpersMetaTest.php b/packages/framework/tests/Unit/TestingSupportHelpersMetaTest.php index 83c694546b3..55abd8b70e2 100644 --- a/packages/framework/tests/Unit/TestingSupportHelpersMetaTest.php +++ b/packages/framework/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; @@ -17,6 +18,7 @@ * * @coversNothing */ +#[AllowDynamicProperties] class TestingSupportHelpersMetaTest extends UnitTestCase { use MocksKernelFeatures; From 17e39412575e5359d97eef5cdd2ee654c8bd27ae Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 10:45:04 +0200 Subject: [PATCH 35/72] Refactor to use the new filesystem mocking helper --- packages/framework/tests/Feature/ReadingTimeTest.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/framework/tests/Feature/ReadingTimeTest.php b/packages/framework/tests/Feature/ReadingTimeTest.php index 6c2478d1f84..c55014d6bb2 100644 --- a/packages/framework/tests/Feature/ReadingTimeTest.php +++ b/packages/framework/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'))->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 From eca861c6933050f0a71b55b2b6dcac2f0d850fcb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 10:47:58 +0200 Subject: [PATCH 36/72] Convert closures to arrow functions --- .../framework/tests/Unit/BuildTaskServiceUnitTest.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php index f5634463256..630c48399b3 100644 --- a/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php @@ -151,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(Mockery::mock(OutputStyle::class))); } public function testGenerateBuildManifestExtendsPostBuildTask() From c5594a95711db1a25229616e25132519f54346d1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 10:48:32 +0200 Subject: [PATCH 37/72] Normalize closure syntax --- packages/framework/tests/Unit/BuildTaskServiceUnitTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php index 630c48399b3..ca381fe3d37 100644 --- a/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php @@ -181,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() From 6f212933734e2f3695c9d6c11825c5c80fbea8ac Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 10:55:11 +0200 Subject: [PATCH 38/72] Extract helper method to set up the mocks --- .../tests/Unit/BuildTaskServiceUnitTest.php | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php index ca381fe3d37..0ea55cd8935 100644 --- a/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php @@ -207,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(); @@ -215,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(); @@ -223,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(); @@ -231,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(); @@ -239,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(); @@ -247,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(); @@ -256,7 +256,7 @@ public function testRunPostBuildTasksCallsRunMethodsWithNullWhenServiceHasNoOutp public function testRunPreBuildTasksCallsRunMethodsWithOutputWhenServiceHasOutput() { $output = Mockery::mock(OutputStyle::class)->makePartial(); - $task = Mockery::mock(TestPreBuildTask::class)->makePartial()->shouldReceive('run')->with($output)->once()->getMock(); + $task = $this->setupMock(TestPreBuildTask::class, 'run')->with($output)->once()->getMock(); $this->service->setOutput($output); $this->service->registerTask($task); @@ -266,7 +266,7 @@ public function testRunPreBuildTasksCallsRunMethodsWithOutputWhenServiceHasOutpu public function testRunPostBuildTasksCallsRunMethodsWithOutputWhenServiceHasOutput() { $output = Mockery::mock(OutputStyle::class)->makePartial(); - $task = Mockery::mock(TestPostBuildTask::class)->makePartial()->shouldReceive('run')->with($output)->once()->getMock(); + $task = $this->setupMock(TestPostBuildTask::class, 'run')->with($output)->once()->getMock(); $this->service->setOutput($output); $this->service->registerTask($task); @@ -319,8 +319,7 @@ protected function createService(): BuildTaskService 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(); @@ -332,6 +331,11 @@ 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(); + } } class InstantiableTestBuildTask extends BuildTask From e423d8a04e9b783cef5ec8e813f14c52ad4fe4d4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 10:58:26 +0200 Subject: [PATCH 39/72] Extract helper method create mocks --- .../framework/tests/Unit/BuildTaskServiceUnitTest.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php index 0ea55cd8935..e4515a92d80 100644 --- a/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php @@ -156,7 +156,7 @@ public function testCanSetOutputWithNull() public function testCanSetOutputWithOutputStyle() { - $this->can(fn () => $this->service->setOutput(Mockery::mock(OutputStyle::class))); + $this->can(fn () => $this->service->setOutput($this->mockOutput())); } public function testGenerateBuildManifestExtendsPostBuildTask() @@ -255,7 +255,7 @@ public function testRunPostBuildTasksCallsRunMethodsWithNullWhenServiceHasNoOutp public function testRunPreBuildTasksCallsRunMethodsWithOutputWhenServiceHasOutput() { - $output = Mockery::mock(OutputStyle::class)->makePartial(); + $output = $this->mockOutput(); $task = $this->setupMock(TestPreBuildTask::class, 'run')->with($output)->once()->getMock(); $this->service->setOutput($output); @@ -265,7 +265,7 @@ public function testRunPreBuildTasksCallsRunMethodsWithOutputWhenServiceHasOutpu public function testRunPostBuildTasksCallsRunMethodsWithOutputWhenServiceHasOutput() { - $output = Mockery::mock(OutputStyle::class)->makePartial(); + $output = $this->mockOutput(); $task = $this->setupMock(TestPostBuildTask::class, 'run')->with($output)->once()->getMock(); $this->service->setOutput($output); @@ -336,6 +336,11 @@ protected function setupMock(string $class, string $method): Mockery\Expectation { 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 From 452811a37b126951c400a864b729efa338d0bb55 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 10:59:40 +0200 Subject: [PATCH 40/72] Add newline to code --- packages/framework/tests/Unit/BuildTaskServiceUnitTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php index e4515a92d80..a18d88e0926 100644 --- a/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskServiceUnitTest.php @@ -291,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(...)); From 28f938de9a889a285c804b49897d0ae61ecc4fb8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 11:03:07 +0200 Subject: [PATCH 41/72] Refactor unit test to extend unit test case Runs in 60ms instead of 285ms --- .../tests/Unit/Pages/PageModelGetHelperTest.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php index f832755d53c..1d264ffaa1f 100644 --- a/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php @@ -9,14 +9,21 @@ use Hyde\Pages\DocumentationPage; use Hyde\Pages\MarkdownPage; use Hyde\Pages\MarkdownPost; -use Hyde\Testing\TestCase; +use Hyde\Testing\UnitTestCase; use Illuminate\Support\Collection; /** * @see \Hyde\Pages\Concerns\HydePage::all() */ -class PageModelGetHelperTest extends TestCase +class PageModelGetHelperTest extends UnitTestCase { + protected static bool $needsConfig = true; + + protected function setUp(): void + { + self::setupKernel(); + } + public function testBladePageGetHelperReturnsBladePageCollection() { $collection = BladePage::all(); From cd883e897da59d7910cf6397f66f81fc6c5f7e44 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 11:08:42 +0200 Subject: [PATCH 42/72] Update class to use mockable filesystem helper --- packages/framework/src/Foundation/Kernel/FileCollection.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Foundation/Kernel/FileCollection.php b/packages/framework/src/Foundation/Kernel/FileCollection.php index 9013be74f77..b52355ddef8 100644 --- a/packages/framework/src/Foundation/Kernel/FileCollection.php +++ b/packages/framework/src/Foundation/Kernel/FileCollection.php @@ -4,13 +4,13 @@ namespace Hyde\Foundation\Kernel; +use Hyde\Facades\Filesystem; use Hyde\Foundation\Concerns\BaseFoundationCollection; use Hyde\Framework\Exceptions\FileNotFoundException; use Hyde\Pages\Concerns\HydePage; use Hyde\Support\Filesystem\SourceFile; use function basename; -use function glob; use function str_starts_with; /** @@ -59,7 +59,7 @@ protected function runExtensionHandlers(): void protected function discoverFilesFor(string $pageClass): void { // Scan the source directory, and directories therein, for files that match the model's file extension. - foreach (glob($this->kernel->path($pageClass::sourcePath('{*,**/*}')), GLOB_BRACE) as $path) { + foreach (Filesystem::smartGlob($pageClass::sourcePath('{*,**/*}'), GLOB_BRACE) as $path) { if (! str_starts_with(basename((string) $path), '_')) { $this->addFile(SourceFile::make($path, $pageClass)); } From 06a2bd8bc6758800247d81dd716d2bf492f86914 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 11:18:30 +0200 Subject: [PATCH 43/72] Refactor test to mock the filesystem --- .../Unit/Pages/PageModelGetHelperTest.php | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php index 1d264ffaa1f..5e0f459ecb0 100644 --- a/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php @@ -4,7 +4,7 @@ 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; @@ -19,52 +19,66 @@ class PageModelGetHelperTest extends UnitTestCase { protected static bool $needsConfig = true; + /** @var \Illuminate\Filesystem\Filesystem&\Mockery\MockInterface */ + protected $filesystem; + protected function setUp(): void { self::setupKernel(); + + $this->filesystem = $this->mockFilesystemStrict() + ->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.html'), GLOB_BRACE)->andReturn([])->byDefault() + ->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.blade.php'), GLOB_BRACE)->andReturn([])->byDefault() + ->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault() + ->shouldReceive('glob')->once()->with(Hyde::path('_posts/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault() + ->shouldReceive('glob')->once()->with(Hyde::path('_docs/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault(); } public function testBladePageGetHelperReturnsBladePageCollection() { + $this->filesystem->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.blade.php'), GLOB_BRACE)->andReturn(['_pages/test-page.blade.php']); + $this->filesystem->shouldReceive('missing')->once()->with(Hyde::path('_pages/test-page.blade.php'))->andReturnFalse(); + $this->filesystem->shouldReceive('get')->once()->with(Hyde::path('_pages/test-page.blade.php'))->andReturn('content'); + $collection = BladePage::all(); - $this->assertCount(2, $collection); + $this->assertCount(1, $collection); $this->assertInstanceOf(Collection::class, $collection); $this->assertContainsOnlyInstancesOf(BladePage::class, $collection); } public function testMarkdownPageGetHelperReturnsMarkdownPageCollection() { - Filesystem::touch('_pages/test-page.md'); + $this->filesystem->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.md'), GLOB_BRACE)->andReturn(['_pages/test-page.md']); + $this->filesystem->shouldReceive('missing')->once()->with(Hyde::path('_pages/test-page.md'))->andReturnFalse(); + $this->filesystem->shouldReceive('get')->once()->with(Hyde::path('_pages/test-page.md'))->andReturn('content'); $collection = MarkdownPage::all(); $this->assertCount(1, $collection); $this->assertInstanceOf(Collection::class, $collection); $this->assertContainsOnlyInstancesOf(MarkdownPage::class, $collection); - - Filesystem::unlink('_pages/test-page.md'); } public function testMarkdownPostGetHelperReturnsMarkdownPostCollection() { - Filesystem::touch('_posts/test-post.md'); + $this->filesystem->shouldReceive('glob')->once()->with(Hyde::path('_posts/{*,**/*}.md'), GLOB_BRACE)->andReturn(['_posts/test-post.md']); + $this->filesystem->shouldReceive('missing')->once()->with(Hyde::path('_posts/test-post.md'))->andReturnFalse(); + $this->filesystem->shouldReceive('get')->once()->with(Hyde::path('_posts/test-post.md'))->andReturn('content'); $collection = MarkdownPost::all(); $this->assertCount(1, $collection); $this->assertInstanceOf(Collection::class, $collection); $this->assertContainsOnlyInstancesOf(MarkdownPost::class, $collection); - - Filesystem::unlink('_posts/test-post.md'); } public function testDocumentationPageGetHelperReturnsDocumentationPageCollection() { - Filesystem::touch('_docs/test-page.md'); + $this->filesystem->shouldReceive('glob')->once()->with(Hyde::path('_docs/{*,**/*}.md'), GLOB_BRACE)->andReturn(['_docs/test-page.md']); + $this->filesystem->shouldReceive('missing')->once()->with(Hyde::path('_docs/test-page.md'))->andReturnFalse(); + $this->filesystem->shouldReceive('get')->once()->with(Hyde::path('_docs/test-page.md'))->andReturn('content'); $collection = DocumentationPage::all(); $this->assertCount(1, $collection); $this->assertInstanceOf(Collection::class, $collection); $this->assertContainsOnlyInstancesOf(DocumentationPage::class, $collection); - - Filesystem::unlink('_docs/test-page.md'); } } From 1a93e731dbd8e683022d85d722da310d0189d99f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 11:24:05 +0200 Subject: [PATCH 44/72] Extract fluent expectation helpers --- .../Unit/Pages/PageModelGetHelperTest.php | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php index 5e0f459ecb0..534071d5b24 100644 --- a/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php @@ -10,6 +10,7 @@ use Hyde\Pages\MarkdownPage; use Hyde\Pages\MarkdownPost; use Hyde\Testing\UnitTestCase; +use Mockery\ExpectationInterface; use Illuminate\Support\Collection; /** @@ -36,9 +37,8 @@ protected function setUp(): void public function testBladePageGetHelperReturnsBladePageCollection() { - $this->filesystem->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.blade.php'), GLOB_BRACE)->andReturn(['_pages/test-page.blade.php']); - $this->filesystem->shouldReceive('missing')->once()->with(Hyde::path('_pages/test-page.blade.php'))->andReturnFalse(); - $this->filesystem->shouldReceive('get')->once()->with(Hyde::path('_pages/test-page.blade.php'))->andReturn('content'); + $this->shouldReceiveGlob('_pages/{*,**/*}.blade.php')->andReturn(['_pages/test-page.blade.php']); + $this->shouldFindFile('_pages/test-page.blade.php'); $collection = BladePage::all(); $this->assertCount(1, $collection); @@ -48,9 +48,8 @@ public function testBladePageGetHelperReturnsBladePageCollection() public function testMarkdownPageGetHelperReturnsMarkdownPageCollection() { - $this->filesystem->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.md'), GLOB_BRACE)->andReturn(['_pages/test-page.md']); - $this->filesystem->shouldReceive('missing')->once()->with(Hyde::path('_pages/test-page.md'))->andReturnFalse(); - $this->filesystem->shouldReceive('get')->once()->with(Hyde::path('_pages/test-page.md'))->andReturn('content'); + $this->shouldReceiveGlob('_pages/{*,**/*}.md')->andReturn(['_pages/test-page.md']); + $this->shouldFindFile('_pages/test-page.md'); $collection = MarkdownPage::all(); $this->assertCount(1, $collection); @@ -60,9 +59,8 @@ public function testMarkdownPageGetHelperReturnsMarkdownPageCollection() public function testMarkdownPostGetHelperReturnsMarkdownPostCollection() { - $this->filesystem->shouldReceive('glob')->once()->with(Hyde::path('_posts/{*,**/*}.md'), GLOB_BRACE)->andReturn(['_posts/test-post.md']); - $this->filesystem->shouldReceive('missing')->once()->with(Hyde::path('_posts/test-post.md'))->andReturnFalse(); - $this->filesystem->shouldReceive('get')->once()->with(Hyde::path('_posts/test-post.md'))->andReturn('content'); + $this->shouldReceiveGlob('_posts/{*,**/*}.md')->andReturn(['_posts/test-post.md']); + $this->shouldFindFile('_posts/test-post.md'); $collection = MarkdownPost::all(); $this->assertCount(1, $collection); @@ -72,13 +70,23 @@ public function testMarkdownPostGetHelperReturnsMarkdownPostCollection() public function testDocumentationPageGetHelperReturnsDocumentationPageCollection() { - $this->filesystem->shouldReceive('glob')->once()->with(Hyde::path('_docs/{*,**/*}.md'), GLOB_BRACE)->andReturn(['_docs/test-page.md']); - $this->filesystem->shouldReceive('missing')->once()->with(Hyde::path('_docs/test-page.md'))->andReturnFalse(); - $this->filesystem->shouldReceive('get')->once()->with(Hyde::path('_docs/test-page.md'))->andReturn('content'); + $this->shouldReceiveGlob('_docs/{*,**/*}.md')->andReturn(['_docs/test-page.md']); + $this->shouldFindFile('_docs/test-page.md'); $collection = DocumentationPage::all(); $this->assertCount(1, $collection); $this->assertInstanceOf(Collection::class, $collection); $this->assertContainsOnlyInstancesOf(DocumentationPage::class, $collection); } + + protected function shouldReceiveGlob(string $withPath): ExpectationInterface + { + return $this->filesystem->shouldReceive('glob')->once()->with(Hyde::path($withPath), GLOB_BRACE); + } + + protected function shouldFindFile(string $file): void + { + $this->filesystem->shouldReceive('missing')->once()->with(Hyde::path($file))->andReturnFalse(); + $this->filesystem->shouldReceive('get')->once()->with(Hyde::path($file))->andReturn('content'); + } } From c03b9ebb9ee9721fa6547913c701f8d7ddad820a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 11:24:35 +0200 Subject: [PATCH 45/72] Verify mockery expectations at tear down --- .../framework/tests/Unit/Pages/PageModelGetHelperTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php index 534071d5b24..cf6232c2d1e 100644 --- a/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelGetHelperTest.php @@ -35,6 +35,11 @@ protected function setUp(): void ->shouldReceive('glob')->once()->with(Hyde::path('_docs/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault(); } + protected function tearDown(): void + { + $this->verifyMockeryExpectations(); + } + public function testBladePageGetHelperReturnsBladePageCollection() { $this->shouldReceiveGlob('_pages/{*,**/*}.blade.php')->andReturn(['_pages/test-page.blade.php']); From 36e2413c5f2638700560571702af4dacf6bd5fd6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 11:54:49 +0200 Subject: [PATCH 46/72] Merge unit test into feature test --- .../tests/Feature/MarkdownPostTest.php | 34 ++++++++++++++ .../BlogPostFrontMatterIsOptionalTest.php | 45 ------------------- 2 files changed, 34 insertions(+), 45 deletions(-) delete mode 100644 packages/framework/tests/Unit/BlogPostFrontMatterIsOptionalTest.php diff --git a/packages/framework/tests/Feature/MarkdownPostTest.php b/packages/framework/tests/Feature/MarkdownPostTest.php index 4040d1c35c7..f3f3b3152d9 100644 --- a/packages/framework/tests/Feature/MarkdownPostTest.php +++ b/packages/framework/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/packages/framework/tests/Unit/BlogPostFrontMatterIsOptionalTest.php b/packages/framework/tests/Unit/BlogPostFrontMatterIsOptionalTest.php deleted file mode 100644 index b90fe0a0c19..00000000000 --- a/packages/framework/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'); - } -} From 3d79c4481e48f42557fedc5617e7aaf51af63956 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 11:57:37 +0200 Subject: [PATCH 47/72] Move test to more suitable test class --- packages/framework/tests/Feature/HydeKernelTest.php | 6 ++++++ .../tests/Unit/BuildOutputDirectoryCanBeChangedTest.php | 7 ------- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/framework/tests/Feature/HydeKernelTest.php b/packages/framework/tests/Feature/HydeKernelTest.php index 1d3bcfb54f1..d2e58f58abc 100644 --- a/packages/framework/tests/Feature/HydeKernelTest.php +++ b/packages/framework/tests/Feature/HydeKernelTest.php @@ -360,6 +360,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/packages/framework/tests/Unit/BuildOutputDirectoryCanBeChangedTest.php b/packages/framework/tests/Unit/BuildOutputDirectoryCanBeChangedTest.php index 8205f17dae7..c2fca62832a 100644 --- a/packages/framework/tests/Unit/BuildOutputDirectoryCanBeChangedTest.php +++ b/packages/framework/tests/Unit/BuildOutputDirectoryCanBeChangedTest.php @@ -73,11 +73,4 @@ public function testSiteOutputDirectoryCanBeChangedInConfiguration() File::deleteDirectory(Hyde::path('_site/build')); } - - public function testSiteOutputDirectoryPathIsNormalizedToTrimTrailingSlashes() - { - Hyde::setOutputDirectory('foo/bar/'); - - $this->assertSame('foo/bar', Hyde::kernel()->getOutputDirectory()); - } } From 456b08728ad6fe83ccfed761f2bdf2d5cf316b56 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 11:58:12 +0200 Subject: [PATCH 48/72] Merge unit test into service feature test --- .../tests/Feature/StaticSiteServiceTest.php | 64 ++++++++++++++++ .../BuildOutputDirectoryCanBeChangedTest.php | 76 ------------------- 2 files changed, 64 insertions(+), 76 deletions(-) delete mode 100644 packages/framework/tests/Unit/BuildOutputDirectoryCanBeChangedTest.php diff --git a/packages/framework/tests/Feature/StaticSiteServiceTest.php b/packages/framework/tests/Feature/StaticSiteServiceTest.php index 4e26d8d3865..77c75506eb2 100644 --- a/packages/framework/tests/Feature/StaticSiteServiceTest.php +++ b/packages/framework/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/packages/framework/tests/Unit/BuildOutputDirectoryCanBeChangedTest.php b/packages/framework/tests/Unit/BuildOutputDirectoryCanBeChangedTest.php deleted file mode 100644 index c2fca62832a..00000000000 --- a/packages/framework/tests/Unit/BuildOutputDirectoryCanBeChangedTest.php +++ /dev/null @@ -1,76 +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')); - } -} From 8789fa381e95dcd2b9a95a28c1b2df80ccc66fcd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 12:06:33 +0200 Subject: [PATCH 49/72] Update unit test to use the unit test case --- .../tests/Unit/CreatesNewMarkdownPostFileTest.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php b/packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php index 47b81290328..338db38c31a 100644 --- a/packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php +++ b/packages/framework/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)); } From 09dd00d74b7ec740684cbbc7a2a9a545f321c6c1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 12:12:07 +0200 Subject: [PATCH 50/72] Add a weak warning when unit tests don't use the unit test case --- monorepo/HydeStan/HydeStan.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/monorepo/HydeStan/HydeStan.php b/monorepo/HydeStan/HydeStan.php index ea49692f419..1f4fed1872b 100644 --- a/monorepo/HydeStan/HydeStan.php +++ b/monorepo/HydeStan/HydeStan.php @@ -21,6 +21,7 @@ final class HydeStan NoFixMeAnalyser::class, NoUsingAssertEqualsForScalarTypesTestAnalyser::class, NoParentSetUpTearDownInUnitTestCaseAnalyser::class, + UnitTestCaseExtensionAnalyzer::class, ]; private const LINE_ANALYSERS = [ @@ -404,3 +405,21 @@ public function run(string $file, string $contents): void } } } + +class UnitTestCaseExtensionAnalyzer extends FileAnalyser +{ + public function run(string $file, string $contents): void + { + // Check if the file is in the unit namespace + if (! str_contains($file, 'Unit')) { + return; + } + + // Check if the class extends TestCase but not UnitTestCase + if (str_contains($contents, 'extends TestCase') && ! str_contains($contents, 'extends UnitTestCase')) { + $lineNumber = substr_count(substr($contents, 0, strpos($contents, 'extends TestCase')), "\n") + 1; + + echo sprintf('Test in unit namespace extends TestCase instead of UnitTestCase at %s', fileLink($file, $lineNumber, false))."\n"; + } + } +} From ad55e0c20017c9327cc814cd948a6caa3ff4000f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 12:25:14 +0200 Subject: [PATCH 51/72] Add HydeStan helper to create a todo list --- monorepo/HydeStan/.gitignore | 1 + monorepo/HydeStan/HydeStan.php | 2 + monorepo/HydeStan/includes/helpers.php | 58 ++++++++++++++++++++++++++ monorepo/HydeStan/run.php | 3 ++ 4 files changed, 64 insertions(+) create mode 100644 monorepo/HydeStan/.gitignore diff --git a/monorepo/HydeStan/.gitignore b/monorepo/HydeStan/.gitignore new file mode 100644 index 00000000000..3f6dfa69862 --- /dev/null +++ b/monorepo/HydeStan/.gitignore @@ -0,0 +1 @@ +todo.md \ No newline at end of file diff --git a/monorepo/HydeStan/HydeStan.php b/monorepo/HydeStan/HydeStan.php index 1f4fed1872b..1a4cc36f0c3 100644 --- a/monorepo/HydeStan/HydeStan.php +++ b/monorepo/HydeStan/HydeStan.php @@ -419,6 +419,8 @@ public function run(string $file, string $contents): void if (str_contains($contents, 'extends TestCase') && ! str_contains($contents, 'extends UnitTestCase')) { $lineNumber = substr_count(substr($contents, 0, strpos($contents, 'extends TestCase')), "\n") + 1; + todo(realpath(__DIR__.'/../../packages/framework/'.$file), $lineNumber, 'Refactor unit test to extend UnitTestCase instead of TestCase'); + echo sprintf('Test in unit namespace extends TestCase instead of UnitTestCase at %s', fileLink($file, $lineNumber, false))."\n"; } } diff --git a/monorepo/HydeStan/includes/helpers.php b/monorepo/HydeStan/includes/helpers.php index 7e542f683d3..6f3e2d94e1d 100644 --- a/monorepo/HydeStan/includes/helpers.php +++ b/monorepo/HydeStan/includes/helpers.php @@ -77,3 +77,61 @@ function recursiveFileFinder(string $directory): array return $files; } + +class TodoBuffer +{ + private static array $todos = []; + + public static function add(string $file, int $line, string $todo): void + { + self::$todos[] = [ + 'file' => $file, + 'line' => $line, + 'todo' => $todo, + ]; + } + + public static function getTodos(): array + { + return self::$todos; + } + + public static function writeTaskFile(): void + { + $todos = self::getTodos(); + + if (empty($todos)) { + return; + } + + $taskFile = __DIR__.'/../todo.md'; + + $content = '# Todos'."\n\n"; + $groupedTodos = []; + + $baseDir = realpath(__DIR__.'/../../../'); + + foreach ($todos as $todo) { + $path = "{$todo['file']}:{$todo['line']}"; + $path = str_replace('\\', '/', $path); + + $path = substr($path, strlen($baseDir) + 1); + + $groupedTodos[$todo['todo']][] = "[$path]($path)"; + } + + foreach ($groupedTodos as $todo => $items) { + $content .= "## $todo\n\n"; + foreach ($items as $item) { + $content .= "- $item\n"; + } + } + + file_put_contents($taskFile, $content); + } +} + +function todo(string $file, int $line, string $todo): void +{ + TodoBuffer::add($file, $line, $todo); +} diff --git a/monorepo/HydeStan/run.php b/monorepo/HydeStan/run.php index 00582d506ba..9bb308320dc 100644 --- a/monorepo/HydeStan/run.php +++ b/monorepo/HydeStan/run.php @@ -12,6 +12,9 @@ $analyser = new HydeStan($debug); $analyser->run(); +// Todo: Could add a flag for this +TodoBuffer::writeTaskFile(); + if ($analyser->hasErrors()) { exit(1); } From e2b90b525778df54905e9434ac622525c4ddc54f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 14:23:00 +0200 Subject: [PATCH 52/72] Refactor test to unit test Saves about 100ms (down from 300ms) --- .../framework/tests/Unit/Facades/AssetFacadeTest.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/Facades/AssetFacadeTest.php b/packages/framework/tests/Unit/Facades/AssetFacadeTest.php index df8344a3771..a3e504c2c19 100644 --- a/packages/framework/tests/Unit/Facades/AssetFacadeTest.php +++ b/packages/framework/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()); From 40bf24d2735bd06bd3179ded2e404d5135e632b1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 14:32:16 +0200 Subject: [PATCH 53/72] Refactor unit test to use the unit test case --- packages/framework/tests/Unit/Facades/AuthorTest.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/Facades/AuthorTest.php b/packages/framework/tests/Unit/Facades/AuthorTest.php index f89502369a2..b8d9016cdf5 100644 --- a/packages/framework/tests/Unit/Facades/AuthorTest.php +++ b/packages/framework/tests/Unit/Facades/AuthorTest.php @@ -5,14 +5,21 @@ namespace Hyde\Framework\Testing\Unit\Facades; use Hyde\Facades\Author; +use Hyde\Testing\UnitTestCase; use Hyde\Framework\Features\Blogging\Models\PostAuthor; -use Hyde\Testing\TestCase; /** * @covers \Hyde\Facades\Author */ -class AuthorTest extends TestCase +class AuthorTest extends UnitTestCase { + protected function setUp(): void + { + self::mockConfig(['hyde.authors' => [ + Author::create('mr_hyde', 'Mr. Hyde', 'https://hydephp.com'), + ]]); + } + public function testCreate() { $author = Author::create('john_doe', 'John Doe', 'https://johndoe.com'); From 6aa823756325d320dc7e8f4f102dbc1c8d1607f4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 14:32:41 +0200 Subject: [PATCH 54/72] Remove redundant arguments --- packages/framework/tests/Unit/Facades/AuthorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/Facades/AuthorTest.php b/packages/framework/tests/Unit/Facades/AuthorTest.php index b8d9016cdf5..56fedcfa097 100644 --- a/packages/framework/tests/Unit/Facades/AuthorTest.php +++ b/packages/framework/tests/Unit/Facades/AuthorTest.php @@ -28,7 +28,7 @@ public function testCreate() $this->assertSame('John Doe', $author->name); $this->assertSame('https://johndoe.com', $author->website); - $this->assertEquals(Author::create('foo', null, null), Author::create('foo')); + $this->assertEquals(Author::create('foo'), Author::create('foo')); } public function testGet() From a64453c48f9c5e5f4ad079a60f3441d4b1912b8f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 14:32:49 +0200 Subject: [PATCH 55/72] Remove useless assertion --- packages/framework/tests/Unit/Facades/AuthorTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/framework/tests/Unit/Facades/AuthorTest.php b/packages/framework/tests/Unit/Facades/AuthorTest.php index 56fedcfa097..9a5995975da 100644 --- a/packages/framework/tests/Unit/Facades/AuthorTest.php +++ b/packages/framework/tests/Unit/Facades/AuthorTest.php @@ -27,8 +27,6 @@ public function testCreate() $this->assertSame('john_doe', $author->username); $this->assertSame('John Doe', $author->name); $this->assertSame('https://johndoe.com', $author->website); - - $this->assertEquals(Author::create('foo'), Author::create('foo')); } public function testGet() From 9165a16797b9eaad69bd0b3319e76c190ad071df Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 14:36:40 +0200 Subject: [PATCH 56/72] Add additional tests --- .../tests/Unit/Facades/AuthorTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/framework/tests/Unit/Facades/AuthorTest.php b/packages/framework/tests/Unit/Facades/AuthorTest.php index 9a5995975da..b4519c8d56c 100644 --- a/packages/framework/tests/Unit/Facades/AuthorTest.php +++ b/packages/framework/tests/Unit/Facades/AuthorTest.php @@ -29,6 +29,15 @@ public function testCreate() $this->assertSame('https://johndoe.com', $author->website); } + public function testCreateWithOnlyRequiredFields() + { + $author = Author::create('john_doe'); + + $this->assertSame('john_doe', $author->username); + $this->assertSame('john_doe', $author->name); + $this->assertNull($author->website); + } + public function testGet() { $author = Author::get('mr_hyde'); @@ -36,7 +45,15 @@ public function testGet() $this->assertSame('mr_hyde', $author->username); $this->assertSame('Mr. Hyde', $author->name); $this->assertSame('https://hydephp.com', $author->website); + } + public function testGetWithNotSetUsername() + { + $this->assertEquals(Author::create('foo'), Author::get('foo')); + } + + public function testGetAliasesPostAuthor() + { $this->assertEquals(PostAuthor::get('foo'), Author::get('foo')); } @@ -45,5 +62,12 @@ public function testAll() $authors = Author::all(); $this->assertCount(1, $authors); $this->assertContainsOnlyInstancesOf(PostAuthor::class, $authors); + $this->assertEquals(Author::get('mr_hyde'), $authors->first()); + } + + public function testAllWithNoAuthors() + { + self::mockConfig(['hyde.authors' => []]); + $this->assertEmpty(Author::all()); } } From e1123ddd78f61e8535d7e26d9bff1b287f8119e1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 14:38:43 +0200 Subject: [PATCH 57/72] Refactor unit test to use the unit test case --- .../Facades/HydeFacadesAreAliasedInAppConfigTest.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/Facades/HydeFacadesAreAliasedInAppConfigTest.php b/packages/framework/tests/Unit/Facades/HydeFacadesAreAliasedInAppConfigTest.php index 132e7bda662..07efeb0a5c3 100644 --- a/packages/framework/tests/Unit/Facades/HydeFacadesAreAliasedInAppConfigTest.php +++ b/packages/framework/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')); From 62b756af9ce888a6f17b155521e76f99e50ebbf1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 14:49:16 +0200 Subject: [PATCH 58/72] Update testing helper to redirect to main helper --- packages/testing/src/FluentTestingHelpers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/testing/src/FluentTestingHelpers.php b/packages/testing/src/FluentTestingHelpers.php index 77bc23b3fdc..49ee56a4684 100644 --- a/packages/testing/src/FluentTestingHelpers.php +++ b/packages/testing/src/FluentTestingHelpers.php @@ -45,7 +45,7 @@ protected function withSiteUrl(?string $url = 'https://example.com'): void */ protected function withoutSiteUrl(): void { - config(['hyde.url' => null]); + $this->withSiteUrl(null); } /** Assert that all the given variables are the same. */ From 4a8a5544cf97e716d21f8638f1c6098bbc334a73 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 14:49:57 +0200 Subject: [PATCH 59/72] Update testing helper to work on unit tests --- packages/testing/src/FluentTestingHelpers.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/testing/src/FluentTestingHelpers.php b/packages/testing/src/FluentTestingHelpers.php index 49ee56a4684..117a339467b 100644 --- a/packages/testing/src/FluentTestingHelpers.php +++ b/packages/testing/src/FluentTestingHelpers.php @@ -37,7 +37,11 @@ protected function throwOnConsoleException(bool $throw = true): void */ protected function withSiteUrl(?string $url = 'https://example.com'): void { - config(['hyde.url' => $url]); + if ($this instanceof UnitTestCase) { + self::mockConfig(['hyde.url' => $url]); + } else { + config(['hyde.url' => $url]); + } } /** From 59a999a4c588c4181425f2f7d3e683748ae81c61 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 14:52:06 +0200 Subject: [PATCH 60/72] Refactor unit test to use unit test case Now runs in 30ms instead of almost 500ms... --- .../HyperlinksUrlPathHelpersTest.php | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php index d228f3aee3d..9d2a394b247 100644 --- a/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php +++ b/packages/framework/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')); From ead940e391cc457f0ecbc6d55f7f9089638425d6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 14:57:51 +0200 Subject: [PATCH 61/72] Refactor to use the unit test case --- packages/framework/tests/Unit/HydeHelperFacadeTest.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/HydeHelperFacadeTest.php b/packages/framework/tests/Unit/HydeHelperFacadeTest.php index d6a3aef3a23..d03a47a49f0 100644 --- a/packages/framework/tests/Unit/HydeHelperFacadeTest.php +++ b/packages/framework/tests/Unit/HydeHelperFacadeTest.php @@ -7,13 +7,16 @@ 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( From f40c204f1f758c87255d4f5afeaf9306fa93c762 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 14:58:45 +0200 Subject: [PATCH 62/72] Slim down formatting --- .../framework/tests/Unit/HydeHelperFacadeTest.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/packages/framework/tests/Unit/HydeHelperFacadeTest.php b/packages/framework/tests/Unit/HydeHelperFacadeTest.php index d03a47a49f0..f8b5d2df5c8 100644 --- a/packages/framework/tests/Unit/HydeHelperFacadeTest.php +++ b/packages/framework/tests/Unit/HydeHelperFacadeTest.php @@ -19,23 +19,16 @@ class HydeHelperFacadeTest extends UnitTestCase 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)); } } From 1da2b9648eee2739b0cdbf5523df2122516a3d51 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 15:00:51 +0200 Subject: [PATCH 63/72] Refactor to use the unit test case --- .../tests/Unit/HydeFileHelpersTest.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/HydeFileHelpersTest.php b/packages/framework/tests/Unit/HydeFileHelpersTest.php index 7bb1974bf1d..d373c9b2e3f 100644 --- a/packages/framework/tests/Unit/HydeFileHelpersTest.php +++ b/packages/framework/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'); From 11a1e9b70624870c005e9851f3d0be105c7473dd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 15:09:14 +0200 Subject: [PATCH 64/72] Unit view tests are allowed to extend TestCase --- monorepo/HydeStan/HydeStan.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/monorepo/HydeStan/HydeStan.php b/monorepo/HydeStan/HydeStan.php index 1a4cc36f0c3..bf23c01ecfa 100644 --- a/monorepo/HydeStan/HydeStan.php +++ b/monorepo/HydeStan/HydeStan.php @@ -415,6 +415,11 @@ public function run(string $file, string $contents): void return; } + // Unit view tests are allowed to extend TestCase + if (str_contains($file, 'ViewTest')) { + return; + } + // Check if the class extends TestCase but not UnitTestCase if (str_contains($contents, 'extends TestCase') && ! str_contains($contents, 'extends UnitTestCase')) { $lineNumber = substr_count(substr($contents, 0, strpos($contents, 'extends TestCase')), "\n") + 1; From 57c3993bfbf6ed73d8e878a8650e3330f3260939 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 15:10:28 +0200 Subject: [PATCH 65/72] Mark analysed expressions --- monorepo/HydeStan/HydeStan.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/monorepo/HydeStan/HydeStan.php b/monorepo/HydeStan/HydeStan.php index bf23c01ecfa..205488ee70f 100644 --- a/monorepo/HydeStan/HydeStan.php +++ b/monorepo/HydeStan/HydeStan.php @@ -412,16 +412,26 @@ public function run(string $file, string $contents): void { // Check if the file is in the unit namespace if (! str_contains($file, 'Unit')) { + AnalysisStatisticsContainer::analysedExpression(); + return; } + AnalysisStatisticsContainer::analysedExpression(); + // Unit view tests are allowed to extend TestCase if (str_contains($file, 'ViewTest')) { + AnalysisStatisticsContainer::analysedExpression(); + return; } + AnalysisStatisticsContainer::analysedExpression(); + // Check if the class extends TestCase but not UnitTestCase if (str_contains($contents, 'extends TestCase') && ! str_contains($contents, 'extends UnitTestCase')) { + AnalysisStatisticsContainer::analysedExpressions(2); + $lineNumber = substr_count(substr($contents, 0, strpos($contents, 'extends TestCase')), "\n") + 1; todo(realpath(__DIR__.'/../../packages/framework/'.$file), $lineNumber, 'Refactor unit test to extend UnitTestCase instead of TestCase'); From e9e650dd453f0744d712fc4c620de020e97726f7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 15:12:57 +0200 Subject: [PATCH 66/72] Move test to the proper namespace --- .../framework/tests/{Unit => Feature}/DocumentationPageTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename packages/framework/tests/{Unit => Feature}/DocumentationPageTest.php (99%) diff --git a/packages/framework/tests/Unit/DocumentationPageTest.php b/packages/framework/tests/Feature/DocumentationPageTest.php similarity index 99% rename from packages/framework/tests/Unit/DocumentationPageTest.php rename to packages/framework/tests/Feature/DocumentationPageTest.php index 48a2af77b84..f110ea54a1f 100644 --- a/packages/framework/tests/Unit/DocumentationPageTest.php +++ b/packages/framework/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; From d1415d04d4a068a7971089e305be183579af0edd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 16:49:53 +0200 Subject: [PATCH 67/72] Forget any mock instances --- packages/testing/src/UnitTestCase.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/testing/src/UnitTestCase.php b/packages/testing/src/UnitTestCase.php index e9c3eb470e7..90f1b141a80 100644 --- a/packages/testing/src/UnitTestCase.php +++ b/packages/testing/src/UnitTestCase.php @@ -34,6 +34,13 @@ public static function setUpBeforeClass(): void } } + public static function tearDownAfterClass(): void + { + if (app()->bound(Filesystem::class) && app()->make(Filesystem::class) instanceof Mockery\MockInterface) { + app()->forgetInstance(Filesystem::class); + } + } + protected static function setupKernel(): void { HydeKernel::setInstance(new HydeKernel()); From 22dcacedcc14a1880ffa852de1cf4dc41b691c8b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 19:32:01 +0200 Subject: [PATCH 68/72] Use the unit test case --- packages/framework/tests/Unit/ValidatesExistenceTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/ValidatesExistenceTest.php b/packages/framework/tests/Unit/ValidatesExistenceTest.php index 51414ea4507..efcc3b88e06 100644 --- a/packages/framework/tests/Unit/ValidatesExistenceTest.php +++ b/packages/framework/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(); From 416671ec09ce123edb3fd0682d700388df8aa299 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 19:33:14 +0200 Subject: [PATCH 69/72] Add section completion output --- monorepo/HydeStan/HydeStan.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/monorepo/HydeStan/HydeStan.php b/monorepo/HydeStan/HydeStan.php index 205488ee70f..279fed01ce4 100644 --- a/monorepo/HydeStan/HydeStan.php +++ b/monorepo/HydeStan/HydeStan.php @@ -87,6 +87,8 @@ public function run(): void $this->analyseFile($file, $this->getFileContents($file)); } + $this->console->info('Finished analyzing files!'); + $this->runTestStan(); $endTime = microtime(true) - $time; From b3f839f00bd79d98176e6082a14f45063ce6559d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 19:33:54 +0200 Subject: [PATCH 70/72] Normalize view unit test name --- .../Views/{LinkComponentTest.php => LinkComponentViewTest.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename packages/framework/tests/Unit/Views/{LinkComponentTest.php => LinkComponentViewTest.php} (96%) diff --git a/packages/framework/tests/Unit/Views/LinkComponentTest.php b/packages/framework/tests/Unit/Views/LinkComponentViewTest.php similarity index 96% rename from packages/framework/tests/Unit/Views/LinkComponentTest.php rename to packages/framework/tests/Unit/Views/LinkComponentViewTest.php index 30b2b016dee..7d8839ac082 100644 --- a/packages/framework/tests/Unit/Views/LinkComponentTest.php +++ b/packages/framework/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() { From 13444a34df56a8cbf86aac74330a39368e61774e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 9 Sep 2024 19:50:30 +0200 Subject: [PATCH 71/72] Refactor to unit test and use filesystem mocks --- .../InteractsWithDirectoriesConcernTest.php | 67 +++++++++++-------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/packages/framework/tests/Unit/InteractsWithDirectoriesConcernTest.php b/packages/framework/tests/Unit/InteractsWithDirectoriesConcernTest.php index d788f101506..9d23ff0bd50 100644 --- a/packages/framework/tests/Unit/InteractsWithDirectoriesConcernTest.php +++ b/packages/framework/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']); } } From 66f6a216e7eeca1baf8cd828b16b8ad04293dc28 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 10 Sep 2024 12:00:42 +0200 Subject: [PATCH 72/72] Refactor test to be proper unit test with filesystem mocks --- .../Pages/PageModelGetAllFilesHelperTest.php | 52 ++++++++++++++----- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/packages/framework/tests/Unit/Pages/PageModelGetAllFilesHelperTest.php b/packages/framework/tests/Unit/Pages/PageModelGetAllFilesHelperTest.php index 8ccd211d324..6ca5e43d91f 100644 --- a/packages/framework/tests/Unit/Pages/PageModelGetAllFilesHelperTest.php +++ b/packages/framework/tests/Unit/Pages/PageModelGetAllFilesHelperTest.php @@ -4,59 +4,85 @@ namespace Hyde\Framework\Testing\Unit\Pages; -use Hyde\Facades\Filesystem; +use Hyde\Hyde; use Hyde\Pages\BladePage; +use Hyde\Testing\UnitTestCase; use Hyde\Pages\DocumentationPage; use Hyde\Pages\MarkdownPage; use Hyde\Pages\MarkdownPost; -use Hyde\Testing\TestCase; +use Mockery\ExpectationInterface; /** * @see \Hyde\Pages\Concerns\HydePage::files() */ -class PageModelGetAllFilesHelperTest extends TestCase +class PageModelGetAllFilesHelperTest extends UnitTestCase { + protected static bool $needsConfig = true; + + /** @var \Illuminate\Filesystem\Filesystem&\Mockery\MockInterface */ + protected $filesystem; + + protected function setUp(): void + { + self::setupKernel(); + + $this->filesystem = $this->mockFilesystemStrict() + ->shouldReceive('missing')->withAnyArgs()->andReturn(false)->byDefault() + ->shouldReceive('get')->withAnyArgs()->andReturn('foo')->byDefault() + ->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.html'), GLOB_BRACE)->andReturn([])->byDefault() + ->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.blade.php'), GLOB_BRACE)->andReturn([])->byDefault() + ->shouldReceive('glob')->once()->with(Hyde::path('_pages/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault() + ->shouldReceive('glob')->once()->with(Hyde::path('_posts/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault() + ->shouldReceive('glob')->once()->with(Hyde::path('_docs/{*,**/*}.md'), GLOB_BRACE)->andReturn([])->byDefault(); + } + + protected function tearDown(): void + { + $this->verifyMockeryExpectations(); + } + public function testBladePageGetHelperReturnsBladePageArray() { + $this->shouldReceiveGlob('_pages/{*,**/*}.blade.php')->andReturn(['_pages/test-page.blade.php']); + $array = BladePage::files(); - $this->assertCount(2, $array); + $this->assertCount(1, $array); $this->assertIsArray($array); - $this->assertEquals(['404', 'index'], $array); + $this->assertEquals(['test-page'], $array); } public function testMarkdownPageGetHelperReturnsMarkdownPageArray() { - Filesystem::touch('_pages/test-page.md'); + $this->shouldReceiveGlob('_pages/{*,**/*}.md')->andReturn(['_pages/test-page.md']); $array = MarkdownPage::files(); $this->assertCount(1, $array); $this->assertIsArray($array); $this->assertEquals(['test-page'], $array); - - Filesystem::unlink('_pages/test-page.md'); } public function testMarkdownPostGetHelperReturnsMarkdownPostArray() { - Filesystem::touch('_posts/test-post.md'); + $this->shouldReceiveGlob('_posts/{*,**/*}.md')->andReturn(['_posts/test-post.md']); $array = MarkdownPost::files(); $this->assertCount(1, $array); $this->assertIsArray($array); $this->assertEquals(['test-post'], $array); - - Filesystem::unlink('_posts/test-post.md'); } public function testDocumentationPageGetHelperReturnsDocumentationPageArray() { - Filesystem::touch('_docs/test-page.md'); + $this->shouldReceiveGlob('_docs/{*,**/*}.md')->andReturn(['_docs/test-page.md']); $array = DocumentationPage::files(); $this->assertCount(1, $array); $this->assertIsArray($array); $this->assertEquals(['test-page'], $array); + } - Filesystem::unlink('_docs/test-page.md'); + protected function shouldReceiveGlob(string $withPath): ExpectationInterface + { + return $this->filesystem->shouldReceive('glob')->once()->with(Hyde::path($withPath), GLOB_BRACE); } }