-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1641 from hydephp/add-testing-helper-to-mock-kern…
…el-features Internal: Add testing helper to mock kernel features
- Loading branch information
Showing
2 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
packages/framework/tests/Unit/TestingSupportHelpersMetaTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |