|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hyde\Framework\Testing\Feature; |
| 6 | + |
| 7 | +use function func_get_args; |
| 8 | +use Hyde\Foundation\Concerns\HydeExtension; |
| 9 | +use Hyde\Foundation\FileCollection; |
| 10 | +use Hyde\Foundation\HydeKernel; |
| 11 | +use Hyde\Foundation\PageCollection; |
| 12 | +use Hyde\Foundation\RouteCollection; |
| 13 | +use Hyde\Hyde; |
| 14 | +use Hyde\Pages\Concerns\HydePage; |
| 15 | +use Hyde\Testing\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * @covers \Hyde\Foundation\Concerns\HydeExtension |
| 19 | + * @covers \Hyde\Foundation\Concerns\ManagesHydeKernel |
| 20 | + * @covers \Hyde\Foundation\HydeKernel |
| 21 | + * @covers \Hyde\Foundation\FileCollection |
| 22 | + * @covers \Hyde\Foundation\PageCollection |
| 23 | + * @covers \Hyde\Foundation\RouteCollection |
| 24 | + */ |
| 25 | +class HydeExtensionFeatureTest extends TestCase |
| 26 | +{ |
| 27 | + protected HydeKernel $kernel; |
| 28 | + |
| 29 | + protected function setUp(): void |
| 30 | + { |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + $this->kernel = HydeKernel::getInstance(); |
| 34 | + } |
| 35 | + |
| 36 | + public function testBaseClassGetPageClasses() |
| 37 | + { |
| 38 | + $this->assertSame([], HydeExtension::getPageClasses()); |
| 39 | + } |
| 40 | + |
| 41 | + public function testBaseClassDiscoveryHandlers() |
| 42 | + { |
| 43 | + HydeExtension::discoverFiles(Hyde::files()); |
| 44 | + HydeExtension::discoverPages(Hyde::pages()); |
| 45 | + HydeExtension::discoverRoutes(Hyde::routes()); |
| 46 | + |
| 47 | + $this->markTestSuccessful(); |
| 48 | + } |
| 49 | + |
| 50 | + public function testCanRegisterNewExtension() |
| 51 | + { |
| 52 | + $this->kernel->registerExtension(HydeTestExtension::class); |
| 53 | + $this->assertSame([HydeTestExtension::class], $this->kernel->getRegisteredExtensions()); |
| 54 | + } |
| 55 | + |
| 56 | + public function testHandlerMethodsAreCalledByDiscovery() |
| 57 | + { |
| 58 | + $this->kernel->registerExtension(HydeTestExtension::class); |
| 59 | + |
| 60 | + $this->assertSame([], HydeTestExtension::$callCache); |
| 61 | + |
| 62 | + $this->kernel->boot(); |
| 63 | + |
| 64 | + $this->assertSame(['files', 'pages', 'routes'], HydeTestExtension::$callCache); |
| 65 | + |
| 66 | + HydeTestExtension::$callCache = []; |
| 67 | + } |
| 68 | + |
| 69 | + public function testFileHandlerDependencyInjection() |
| 70 | + { |
| 71 | + $this->kernel->registerExtension(SpyableTestExtension::class); |
| 72 | + $this->kernel->boot(); |
| 73 | + |
| 74 | + $this->assertInstanceOf(FileCollection::class, ...SpyableTestExtension::getCalled('files')); |
| 75 | + } |
| 76 | + |
| 77 | + public function testPageHandlerDependencyInjection() |
| 78 | + { |
| 79 | + $this->kernel->registerExtension(SpyableTestExtension::class); |
| 80 | + $this->kernel->boot(); |
| 81 | + |
| 82 | + $this->assertInstanceOf(PageCollection::class, ...SpyableTestExtension::getCalled('pages')); |
| 83 | + } |
| 84 | + |
| 85 | + public function testRouteHandlerDependencyInjection() |
| 86 | + { |
| 87 | + $this->kernel->registerExtension(SpyableTestExtension::class); |
| 88 | + $this->kernel->boot(); |
| 89 | + |
| 90 | + $this->assertInstanceOf(RouteCollection::class, ...SpyableTestExtension::getCalled('routes')); |
| 91 | + } |
| 92 | + |
| 93 | + protected function markTestSuccessful(): void |
| 94 | + { |
| 95 | + $this->assertTrue(true); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +class HydeTestExtension extends HydeExtension |
| 100 | +{ |
| 101 | + // An easy way to assert the handlers are called. |
| 102 | + public static array $callCache = []; |
| 103 | + |
| 104 | + public static function getPageClasses(): array |
| 105 | + { |
| 106 | + return [ |
| 107 | + HydeExtensionTestPage::class, |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | + public static function discoverFiles(FileCollection $collection): void |
| 112 | + { |
| 113 | + static::$callCache[] = 'files'; |
| 114 | + } |
| 115 | + |
| 116 | + public static function discoverPages(PageCollection $collection): void |
| 117 | + { |
| 118 | + static::$callCache[] = 'pages'; |
| 119 | + } |
| 120 | + |
| 121 | + public static function discoverRoutes(RouteCollection $collection): void |
| 122 | + { |
| 123 | + static::$callCache[] = 'routes'; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +class SpyableTestExtension extends HydeExtension |
| 128 | +{ |
| 129 | + private static array $callCache = []; |
| 130 | + |
| 131 | + public static function discoverFiles(FileCollection $collection): void |
| 132 | + { |
| 133 | + self::$callCache['files'] = func_get_args(); |
| 134 | + } |
| 135 | + |
| 136 | + public static function discoverPages(PageCollection $collection): void |
| 137 | + { |
| 138 | + self::$callCache['pages'] = func_get_args(); |
| 139 | + } |
| 140 | + |
| 141 | + public static function discoverRoutes(RouteCollection $collection): void |
| 142 | + { |
| 143 | + self::$callCache['routes'] = func_get_args(); |
| 144 | + } |
| 145 | + |
| 146 | + public static function getCalled(string $method): array |
| 147 | + { |
| 148 | + return self::$callCache[$method]; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +class HydeExtensionTestPage extends HydePage |
| 153 | +{ |
| 154 | + public function compile(): string |
| 155 | + { |
| 156 | + return ''; |
| 157 | + } |
| 158 | +} |
0 commit comments