From d0ee682439b7dba3cb0e01d1fdb87f92ca9c674a Mon Sep 17 00:00:00 2001 From: github-actions Date: Tue, 10 Sep 2024 10:05:03 +0000 Subject: [PATCH] Merge pull request #1960 from hydephp/test-cleanup Internal: Test code refactors and cleanup https://github.com/hydephp/develop/commit/1eb450241434972784a1db3073d7059b6156a6ed --- tests/Unit/AssetServiceUnitTest.php | 2 +- tests/Unit/BaseFoundationCollectionTest.php | 2 +- tests/Unit/BreadcrumbsComponentTest.php | 2 +- tests/Unit/BuildTaskUnitTest.php | 4 +- tests/Unit/Facades/AuthorTest.php | 35 ++++++++++- tests/Unit/HtmlTestingSupportMetaTest.php | 4 +- tests/Unit/NavItemTest.php | 7 ++- tests/Unit/NavigationDataFactoryUnitTest.php | 2 +- .../Pages/PageModelGetAllFilesHelperTest.php | 62 ------------------ tests/Unit/Pages/PageModelGetHelperTest.php | 63 ------------------- tests/Unit/UnixsumTest.php | 2 +- 11 files changed, 45 insertions(+), 140 deletions(-) delete mode 100644 tests/Unit/Pages/PageModelGetAllFilesHelperTest.php delete mode 100644 tests/Unit/Pages/PageModelGetHelperTest.php diff --git a/tests/Unit/AssetServiceUnitTest.php b/tests/Unit/AssetServiceUnitTest.php index e9f4809b..4554ec53 100644 --- a/tests/Unit/AssetServiceUnitTest.php +++ b/tests/Unit/AssetServiceUnitTest.php @@ -17,7 +17,7 @@ class AssetServiceUnitTest extends UnitTestCase { protected function setUp(): void { - self::setupKernel(); + self::resetKernel(); self::mockConfig(); } diff --git a/tests/Unit/BaseFoundationCollectionTest.php b/tests/Unit/BaseFoundationCollectionTest.php index cdcc215d..95725ec5 100644 --- a/tests/Unit/BaseFoundationCollectionTest.php +++ b/tests/Unit/BaseFoundationCollectionTest.php @@ -17,7 +17,7 @@ class BaseFoundationCollectionTest extends UnitTestCase { public function testBaseFoundationCollectionInitialization() { - $this->setupKernel(); + self::resetKernel(); $booted = BaseFoundationCollectionTestClass::init(HydeKernel::getInstance())->boot(); diff --git a/tests/Unit/BreadcrumbsComponentTest.php b/tests/Unit/BreadcrumbsComponentTest.php index ffd7c29f..dc5231ad 100644 --- a/tests/Unit/BreadcrumbsComponentTest.php +++ b/tests/Unit/BreadcrumbsComponentTest.php @@ -23,7 +23,7 @@ class BreadcrumbsComponentTest extends UnitTestCase { protected function setUp(): void { - self::setupKernel(); + self::resetKernel(); self::mockConfig(); } diff --git a/tests/Unit/BuildTaskUnitTest.php b/tests/Unit/BuildTaskUnitTest.php index be9aec94..45127d4d 100644 --- a/tests/Unit/BuildTaskUnitTest.php +++ b/tests/Unit/BuildTaskUnitTest.php @@ -153,7 +153,7 @@ public function testCanWriteToOutput() public function testCreatedSiteFile() { - self::setupKernel(); + self::resetKernel(); $task = new BufferedTestBuildTask(); @@ -164,7 +164,7 @@ public function testCreatedSiteFile() public function testCreatedSiteFileWithAbsolutePath() { - self::setupKernel(); + self::resetKernel(); $task = new BufferedTestBuildTask(); diff --git a/tests/Unit/Facades/AuthorTest.php b/tests/Unit/Facades/AuthorTest.php index f8950236..b4519c8d 100644 --- a/tests/Unit/Facades/AuthorTest.php +++ b/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'); @@ -20,8 +27,15 @@ 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', null, null), Author::create('foo')); + 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() @@ -31,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')); } @@ -40,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()); } } diff --git a/tests/Unit/HtmlTestingSupportMetaTest.php b/tests/Unit/HtmlTestingSupportMetaTest.php index 491400cd..3c109d74 100644 --- a/tests/Unit/HtmlTestingSupportMetaTest.php +++ b/tests/Unit/HtmlTestingSupportMetaTest.php @@ -31,9 +31,7 @@ class HtmlTestingSupportMetaTest extends UnitTestCase protected function setUp(): void { - parent::setUp(); - - self::setupKernel(); + self::resetKernel(); $this->html ??= file_get_contents(Hyde::vendorPath('resources/views/homepages/welcome.blade.php')); } diff --git a/tests/Unit/NavItemTest.php b/tests/Unit/NavItemTest.php index 94811a94..e4dbb2f9 100644 --- a/tests/Unit/NavItemTest.php +++ b/tests/Unit/NavItemTest.php @@ -25,8 +25,11 @@ */ class NavItemTest extends UnitTestCase { - protected static bool $needsKernel = true; - protected static bool $needsConfig = true; + public static function setUpBeforeClass(): void + { + self::resetKernel(); + self::mockConfig(); + } protected function setUp(): void { diff --git a/tests/Unit/NavigationDataFactoryUnitTest.php b/tests/Unit/NavigationDataFactoryUnitTest.php index 6833cacd..c5f3524f 100644 --- a/tests/Unit/NavigationDataFactoryUnitTest.php +++ b/tests/Unit/NavigationDataFactoryUnitTest.php @@ -19,7 +19,7 @@ class NavigationDataFactoryUnitTest extends UnitTestCase { protected function setUp(): void { - self::setupKernel(); + self::resetKernel(); self::mockConfig(); } diff --git a/tests/Unit/Pages/PageModelGetAllFilesHelperTest.php b/tests/Unit/Pages/PageModelGetAllFilesHelperTest.php deleted file mode 100644 index 8ccd211d..00000000 --- a/tests/Unit/Pages/PageModelGetAllFilesHelperTest.php +++ /dev/null @@ -1,62 +0,0 @@ -assertCount(2, $array); - $this->assertIsArray($array); - $this->assertEquals(['404', 'index'], $array); - } - - public function testMarkdownPageGetHelperReturnsMarkdownPageArray() - { - Filesystem::touch('_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'); - - $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'); - - $array = DocumentationPage::files(); - $this->assertCount(1, $array); - $this->assertIsArray($array); - $this->assertEquals(['test-page'], $array); - - Filesystem::unlink('_docs/test-page.md'); - } -} diff --git a/tests/Unit/Pages/PageModelGetHelperTest.php b/tests/Unit/Pages/PageModelGetHelperTest.php deleted file mode 100644 index f832755d..00000000 --- a/tests/Unit/Pages/PageModelGetHelperTest.php +++ /dev/null @@ -1,63 +0,0 @@ -assertCount(2, $collection); - $this->assertInstanceOf(Collection::class, $collection); - $this->assertContainsOnlyInstancesOf(BladePage::class, $collection); - } - - public function testMarkdownPageGetHelperReturnsMarkdownPageCollection() - { - Filesystem::touch('_pages/test-page.md'); - - $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'); - - $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'); - - $collection = DocumentationPage::all(); - $this->assertCount(1, $collection); - $this->assertInstanceOf(Collection::class, $collection); - $this->assertContainsOnlyInstancesOf(DocumentationPage::class, $collection); - - Filesystem::unlink('_docs/test-page.md'); - } -} diff --git a/tests/Unit/UnixsumTest.php b/tests/Unit/UnixsumTest.php index 0a001538..81b800ed 100644 --- a/tests/Unit/UnixsumTest.php +++ b/tests/Unit/UnixsumTest.php @@ -63,7 +63,7 @@ public function testMethodReturnsSameValueForStringWithMixedEndOfLineSequences() public function testMethodReturnsSameValueWhenLoadedFromFileUsingShorthand() { - self::setupKernel(); + self::resetKernel(); $string = "foo\nbar\r\nbaz\r\n";