Skip to content

Commit

Permalink
Merge pull request #1641 from hydephp/add-testing-helper-to-mock-kern…
Browse files Browse the repository at this point in the history
…el-features

Internal: Add testing helper to mock kernel features
  • Loading branch information
caendesilva authored Mar 25, 2024
2 parents 5e1fb1f + c04edb9 commit 714656d
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
73 changes: 73 additions & 0 deletions packages/framework/tests/Unit/TestingSupportHelpersMetaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Testing\Unit;

use Hyde\Pages\InMemoryPage;
use Hyde\Testing\UnitTestCase;
use Hyde\Testing\MocksKernelFeatures;

/**
* Meta test for internal testing helpers.
*
* @see \Hyde\Testing\Support
* @see \Hyde\Testing\MocksKernelFeatures
*
* @coversNothing
*/
class TestingSupportHelpersMetaTest extends UnitTestCase
{
use MocksKernelFeatures;

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

public function testWithPages()
{
$page = new InMemoryPage('foo');

$this->withPages([$page]);

$this->assertSame(['foo' => $page], $this->kernel->pages()->all());
$this->assertEquals(['foo' => $page->getRoute()], $this->kernel->routes()->all());
}

public function testWithPagesReplacesExistingPages()
{
$this->withPages([new InMemoryPage('foo')]);
$this->assertSame(['foo'], $this->getPageIdentifiers());

$this->withPages([new InMemoryPage('bar')]);
$this->assertSame(['bar'], $this->getPageIdentifiers());
}

public function testWithPagesReplacesExistingRoutes()
{
$this->withPages([new InMemoryPage('foo')]);
$this->assertSame(['foo'], $this->getRouteKeys());

$this->withPages([new InMemoryPage('bar')]);
$this->assertSame(['bar'], $this->getRouteKeys());
}

public function testWithPagesWhenSupplyingStrings()
{
$this->withPages(['foo', 'bar', 'baz']);

$this->assertSame(['foo', 'bar', 'baz'], $this->getRouteKeys());
$this->assertSame(['foo', 'bar', 'baz'], $this->getPageIdentifiers());

$this->assertContainsOnlyInstancesOf(InMemoryPage::class, $this->kernel->pages());
}

protected function getPageIdentifiers()
{
return $this->kernel->pages()->keys()->all();
}

protected function getRouteKeys(): array
{
return $this->kernel->routes()->keys()->all();
}
}
79 changes: 79 additions & 0 deletions packages/testing/src/MocksKernelFeatures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Hyde\Testing;

use Hyde\Pages\InMemoryPage;
use Hyde\Support\Models\Route;
use Hyde\Foundation\HydeKernel;
use Hyde\Pages\Concerns\HydePage;
use Hyde\Foundation\Kernel\PageCollection;
use Hyde\Foundation\Kernel\RouteCollection;
use Illuminate\Support\Collection;

use function collect;
use function is_string;

/**
* A trait to mock kernel features for testing.
*
* @property \Hyde\Testing\TestKernel $kernel
*/
trait MocksKernelFeatures
{
/**
* Create a new mock kernel with the given pages added to its routes.
*
* @param array<\Hyde\Pages\Concerns\HydePage|string> $pages
* @return $this
*/
protected function withPages(array $pages): static
{
$this->setupTestKernel();

// If the given pages are strings, convert them to InMemoryPage instances.
$pages = collect($pages)->map(fn (HydePage|string $page): HydePage => is_string($page) ? new InMemoryPage($page) : $page);
$routes = collect($pages)->map(fn (HydePage $page) => $page->getRoute());

$this->kernel->setPages($pages);
$this->kernel->setRoutes($routes);

return $this;
}

protected function setupTestKernel(): void
{
$this->kernel = new TestKernel();

HydeKernel::setInstance($this->kernel);
}
}

class TestKernel extends HydeKernel
{
protected ?PageCollection $mockPages = null;
protected ?RouteCollection $mockRoutes = null;

public function setPages(Collection $pages): void
{
$this->mockPages = PageCollection::make($pages->mapWithKeys(fn (HydePage $page) => [$page->getIdentifier() => $page]));
}

public function setRoutes(Collection $routes): void
{
$this->mockRoutes = RouteCollection::make($routes->mapWithKeys(fn (Route $route) => [$route->getRouteKey() => $route]));
}

/** @return \Hyde\Foundation\Kernel\PageCollection<string, \Hyde\Pages\Concerns\HydePage> */
public function pages(): PageCollection
{
return $this->mockPages ?? parent::pages();
}

/** @return \Hyde\Foundation\Kernel\RouteCollection<string, \Hyde\Support\Models\Route> */
public function routes(): RouteCollection
{
return $this->mockRoutes ?? parent::routes();
}
}

0 comments on commit 714656d

Please sign in to comment.