Skip to content

Commit

Permalink
Update testing helper to also mock the pages
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Mar 25, 2024
1 parent 48769f7 commit 45ac8c4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/framework/tests/Unit/TestingSupportHelpersMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ public function testWithPages()

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

$this->assertSame([$page], $this->kernel->pages()->all());
$this->assertEquals([$page->getRoute()], $this->kernel->routes()->all());
$this->assertSame($page, $this->kernel->routes()->first()->getPage());
}

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()
Expand All @@ -48,7 +57,14 @@ 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->getRoutePages());
$this->assertContainsOnlyInstancesOf(InMemoryPage::class, $this->kernel->pages());
}

protected function getPageIdentifiers()
{
return $this->kernel->pages()->map(fn (InMemoryPage $page) => $page->getIdentifier())->all();
}

protected function getRouteKeys(): array
Expand Down
14 changes: 14 additions & 0 deletions packages/testing/src/MocksKernelFeatures.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Hyde\Pages\InMemoryPage;
use Hyde\Foundation\HydeKernel;
use Hyde\Pages\Concerns\HydePage;
use Hyde\Foundation\Kernel\PageCollection;
use Hyde\Foundation\Kernel\RouteCollection;
use Illuminate\Contracts\Support\Arrayable;

Expand All @@ -30,6 +31,7 @@ protected function withPages(array $pages): static
// 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);

$this->kernel->setPages($pages);
$this->kernel->setRoutes(collect($pages)->map(fn (HydePage $page) => $page->getRoute()));

return $this;
Expand All @@ -45,13 +47,25 @@ protected function setupTestKernel(): void

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

public function setPages(Arrayable $pages): void
{
$this->mockPages = PageCollection::make($pages);
}

public function setRoutes(Arrayable $routes): void
{
$this->mockRoutes = RouteCollection::make($routes);
}

/** @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
{
Expand Down

0 comments on commit 45ac8c4

Please sign in to comment.