Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal: Test code refactors and cleanup #1958

Merged
merged 23 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions packages/framework/tests/Unit/DropdownNavItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Hyde\Framework\Features\Navigation\DropdownNavItem;
use Hyde\Framework\Features\Navigation\NavItem;
use Hyde\Pages\MarkdownPage;
use Hyde\Support\Facades\Render;
use Hyde\Support\Models\RenderData;
use Hyde\Support\Models\Route;
use Hyde\Testing\UnitTestCase;

Expand All @@ -20,17 +18,7 @@ class DropdownNavItemTest extends UnitTestCase
{
protected static bool $needsKernel = true;
protected static bool $needsConfig = true;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
Render::swap(new RenderData());
}

public static function tearDownAfterClass(): void
{
Render::swap(new RenderData());
}
protected static bool $needsRender = true;

public function testConstruct()
{
Expand Down
15 changes: 2 additions & 13 deletions packages/framework/tests/Unit/Facades/RouteFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use Hyde\Pages\BladePage;
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Hyde\Support\Facades\Render;
use Hyde\Support\Models\RenderData;
use Hyde\Support\Models\Route;
use Hyde\Testing\UnitTestCase;

Expand Down Expand Up @@ -61,20 +59,16 @@ public function testCurrentReturnsCurrentRoute()
{
$route = new Route(new MarkdownPage('foo'));

Render::shouldReceive('getRoute')->andReturn($route);
self::mockRender()->shouldReceive('getRoute')->andReturn($route);

$this->assertSame($route, Routes::current());

$this->resetMockInstance();
}

public function testCurrentReturnsNullIfRouteIsNotFound()
{
Render::shouldReceive('getRoute')->andReturn(null);
self::mockRender()->shouldReceive('getRoute')->andReturn(null);

$this->assertNull(Routes::current());

$this->resetMockInstance();
}

public function testExistsForExistingRoute()
Expand All @@ -86,9 +80,4 @@ public function testExistsForNonExistingRoute()
{
$this->assertFalse(Routes::exists('not-found'));
}

protected function resetMockInstance(): void
{
Render::swap(new RenderData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Hyde\Framework\Testing\Unit\Foundation;

use Hyde\Hyde;
use Hyde\Support\Facades\Render;
use Hyde\Support\Models\RenderData;
use Hyde\Testing\InteractsWithPages;
use Hyde\Testing\UnitTestCase;
use Illuminate\Support\Facades\View;
Expand All @@ -22,10 +20,10 @@ class HyperlinkFileHelperRelativeLinkTest extends UnitTestCase

protected static bool $needsKernel = true;
protected static bool $needsConfig = true;
protected static bool $needsRender = true;

protected function setUp(): void
{
Render::swap(new RenderData());
View::swap(Mockery::mock(Factory::class)->makePartial());
}

Expand Down
5 changes: 0 additions & 5 deletions packages/framework/tests/Unit/NavItemIsCurrentHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ class NavItemIsCurrentHelperTest extends UnitTestCase
protected static bool $needsKernel = true;
protected static bool $needsConfig = true;

protected function tearDown(): void
{
Render::swap(new RenderData());
}

public function testIsCurrent()
{
$this->mockRenderData($this->makeRoute('foo'));
Expand Down
11 changes: 5 additions & 6 deletions packages/framework/tests/Unit/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Hyde\Hyde;
use Hyde\Pages\MarkdownPage;
use Hyde\Support\Facades\Render;
use Hyde\Support\Models\RenderData;
use Hyde\Support\Models\Route;
use Hyde\Support\Models\RouteKey;
use Hyde\Testing\UnitTestCase;
Expand All @@ -17,11 +15,12 @@
*/
class RouteTest extends UnitTestCase
{
protected static bool $needsKernel = true;
protected static bool $needsConfig = true;

protected function setUp(): void
{
self::setupKernel();
self::mockConfig();
Render::swap(new RenderData());
self::mockRender();
}

public function testConstructorCreatesRouteFromPageModel()
Expand Down Expand Up @@ -80,7 +79,7 @@ public function testGetLinkReturnsCorrectPathForNestedPages()
public function testGetLinkReturnsCorrectPathForNestedCurrentPage()
{
$route = new Route(new MarkdownPage('foo'));
Render::shouldReceive('getRouteKey')->andReturn('foo/bar');
self::mockCurrentRouteKey('foo/bar');

$this->assertSame(Hyde::relativeLink($route->getOutputPath()), $route->getLink());
$this->assertSame('../foo.html', $route->getLink());
Expand Down
3 changes: 1 addition & 2 deletions packages/hyde/tests/DefaultContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

declare(strict_types=1);

namespace Hyde\Testing\Hyde;
namespace Hyde\Testing;

use Hyde\Hyde;
use Hyde\Testing\UnitTestCase;

class DefaultContentTest extends UnitTestCase
{
Expand Down
4 changes: 1 addition & 3 deletions packages/hyde/tests/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

declare(strict_types=1);

namespace Hyde\Testing\Hyde;

use Hyde\Testing\UnitTestCase;
namespace Hyde\Testing;

class ExampleTest extends UnitTestCase
{
Expand Down
4 changes: 1 addition & 3 deletions packages/hyde/tests/HydeCLITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

declare(strict_types=1);

namespace Hyde\Testing\Hyde;

use Hyde\Testing\TestCase;
namespace Hyde\Testing;

class HydeCLITest extends TestCase
{
Expand Down
3 changes: 1 addition & 2 deletions packages/hyde/tests/StaticSiteBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

declare(strict_types=1);

namespace Hyde\Testing\Hyde;
namespace Hyde\Testing;

use Hyde\Hyde;
use Hyde\Testing\TestCase;
use Illuminate\Support\Facades\File;

class StaticSiteBuilderTest extends TestCase
Expand Down
4 changes: 1 addition & 3 deletions packages/testing/src/Common/BaseHydePageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\View\Factory;
use Hyde\Testing\UnitTestCase;
use Hyde\Support\Facades\Render;
use Hyde\Support\Models\RenderData;
use Illuminate\Support\Facades\View;
use Hyde\Testing\CreatesTemporaryFiles;

Expand All @@ -27,6 +26,7 @@ protected function setUp(): void
{
self::setupKernel();
self::mockConfig();
self::mockRender();

View::swap($mock = Mockery::mock(Factory::class, [
'make' => Mockery::mock(Factory::class, [
Expand All @@ -39,8 +39,6 @@ protected function setUp(): void
]));
app()->bind(\Illuminate\Contracts\View\Factory::class, fn () => $mock);
app()->bind('view', fn () => $mock);

Render::swap(new RenderData());
}

protected function tearDown(): void
Expand Down
19 changes: 19 additions & 0 deletions packages/testing/src/UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Hyde\Testing;

use Hyde\Foundation\HydeKernel;
use Hyde\Support\Facades\Render;
use Illuminate\Config\Repository;
use Hyde\Support\Models\RenderData;
use Illuminate\Support\Facades\Config;
use PHPUnit\Framework\TestCase as BaseTestCase;

Expand All @@ -15,6 +17,7 @@ abstract class UnitTestCase extends BaseTestCase

protected static bool $needsKernel = false;
protected static bool $needsConfig = false;
protected static bool $needsRender = false;

protected static function needsKernel(): void
{
Expand All @@ -32,6 +35,10 @@ public static function setUpBeforeClass(): void
if (static::$needsConfig) {
self::mockConfig();
}

if (static::$needsRender) {
self::mockRender();
}
}

protected static function setupKernel(): void
Expand All @@ -45,6 +52,18 @@ protected static function resetKernel(): void
HydeKernel::setInstance(new HydeKernel());
}

protected static function mockRender(): Render
{
Render::swap(new RenderData());

return new Render();
}

protected static function mockCurrentRouteKey(?string $routeKey = null): void
{
self::mockRender()->shouldReceive('getRouteKey')->andReturn($routeKey);
}

protected static function mockConfig(array $items = []): void
{
app()->bind('config', fn (): Repository => new Repository($items));
Expand Down