Skip to content

Commit 714656d

Browse files
authored
Merge pull request #1641 from hydephp/add-testing-helper-to-mock-kernel-features
Internal: Add testing helper to mock kernel features
2 parents 5e1fb1f + c04edb9 commit 714656d

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\Framework\Testing\Unit;
6+
7+
use Hyde\Pages\InMemoryPage;
8+
use Hyde\Testing\UnitTestCase;
9+
use Hyde\Testing\MocksKernelFeatures;
10+
11+
/**
12+
* Meta test for internal testing helpers.
13+
*
14+
* @see \Hyde\Testing\Support
15+
* @see \Hyde\Testing\MocksKernelFeatures
16+
*
17+
* @coversNothing
18+
*/
19+
class TestingSupportHelpersMetaTest extends UnitTestCase
20+
{
21+
use MocksKernelFeatures;
22+
23+
protected static bool $needsKernel = true;
24+
protected static bool $needsConfig = true;
25+
26+
public function testWithPages()
27+
{
28+
$page = new InMemoryPage('foo');
29+
30+
$this->withPages([$page]);
31+
32+
$this->assertSame(['foo' => $page], $this->kernel->pages()->all());
33+
$this->assertEquals(['foo' => $page->getRoute()], $this->kernel->routes()->all());
34+
}
35+
36+
public function testWithPagesReplacesExistingPages()
37+
{
38+
$this->withPages([new InMemoryPage('foo')]);
39+
$this->assertSame(['foo'], $this->getPageIdentifiers());
40+
41+
$this->withPages([new InMemoryPage('bar')]);
42+
$this->assertSame(['bar'], $this->getPageIdentifiers());
43+
}
44+
45+
public function testWithPagesReplacesExistingRoutes()
46+
{
47+
$this->withPages([new InMemoryPage('foo')]);
48+
$this->assertSame(['foo'], $this->getRouteKeys());
49+
50+
$this->withPages([new InMemoryPage('bar')]);
51+
$this->assertSame(['bar'], $this->getRouteKeys());
52+
}
53+
54+
public function testWithPagesWhenSupplyingStrings()
55+
{
56+
$this->withPages(['foo', 'bar', 'baz']);
57+
58+
$this->assertSame(['foo', 'bar', 'baz'], $this->getRouteKeys());
59+
$this->assertSame(['foo', 'bar', 'baz'], $this->getPageIdentifiers());
60+
61+
$this->assertContainsOnlyInstancesOf(InMemoryPage::class, $this->kernel->pages());
62+
}
63+
64+
protected function getPageIdentifiers()
65+
{
66+
return $this->kernel->pages()->keys()->all();
67+
}
68+
69+
protected function getRouteKeys(): array
70+
{
71+
return $this->kernel->routes()->keys()->all();
72+
}
73+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\Testing;
6+
7+
use Hyde\Pages\InMemoryPage;
8+
use Hyde\Support\Models\Route;
9+
use Hyde\Foundation\HydeKernel;
10+
use Hyde\Pages\Concerns\HydePage;
11+
use Hyde\Foundation\Kernel\PageCollection;
12+
use Hyde\Foundation\Kernel\RouteCollection;
13+
use Illuminate\Support\Collection;
14+
15+
use function collect;
16+
use function is_string;
17+
18+
/**
19+
* A trait to mock kernel features for testing.
20+
*
21+
* @property \Hyde\Testing\TestKernel $kernel
22+
*/
23+
trait MocksKernelFeatures
24+
{
25+
/**
26+
* Create a new mock kernel with the given pages added to its routes.
27+
*
28+
* @param array<\Hyde\Pages\Concerns\HydePage|string> $pages
29+
* @return $this
30+
*/
31+
protected function withPages(array $pages): static
32+
{
33+
$this->setupTestKernel();
34+
35+
// If the given pages are strings, convert them to InMemoryPage instances.
36+
$pages = collect($pages)->map(fn (HydePage|string $page): HydePage => is_string($page) ? new InMemoryPage($page) : $page);
37+
$routes = collect($pages)->map(fn (HydePage $page) => $page->getRoute());
38+
39+
$this->kernel->setPages($pages);
40+
$this->kernel->setRoutes($routes);
41+
42+
return $this;
43+
}
44+
45+
protected function setupTestKernel(): void
46+
{
47+
$this->kernel = new TestKernel();
48+
49+
HydeKernel::setInstance($this->kernel);
50+
}
51+
}
52+
53+
class TestKernel extends HydeKernel
54+
{
55+
protected ?PageCollection $mockPages = null;
56+
protected ?RouteCollection $mockRoutes = null;
57+
58+
public function setPages(Collection $pages): void
59+
{
60+
$this->mockPages = PageCollection::make($pages->mapWithKeys(fn (HydePage $page) => [$page->getIdentifier() => $page]));
61+
}
62+
63+
public function setRoutes(Collection $routes): void
64+
{
65+
$this->mockRoutes = RouteCollection::make($routes->mapWithKeys(fn (Route $route) => [$route->getRouteKey() => $route]));
66+
}
67+
68+
/** @return \Hyde\Foundation\Kernel\PageCollection<string, \Hyde\Pages\Concerns\HydePage> */
69+
public function pages(): PageCollection
70+
{
71+
return $this->mockPages ?? parent::pages();
72+
}
73+
74+
/** @return \Hyde\Foundation\Kernel\RouteCollection<string, \Hyde\Support\Models\Route> */
75+
public function routes(): RouteCollection
76+
{
77+
return $this->mockRoutes ?? parent::routes();
78+
}
79+
}

0 commit comments

Comments
 (0)