|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hyde\Framework\Testing\Feature; |
| 6 | + |
| 7 | +use function app; |
| 8 | +use BadMethodCallException; |
| 9 | +use Hyde\Foundation\Facades; |
| 10 | +use Hyde\Foundation\HydeKernel; |
| 11 | +use Hyde\Pages\Concerns\HydePage; |
| 12 | +use Hyde\Support\Filesystem\SourceFile; |
| 13 | +use Hyde\Support\Models\Route; |
| 14 | +use Hyde\Testing\TestCase; |
| 15 | +use InvalidArgumentException; |
| 16 | +use stdClass; |
| 17 | + |
| 18 | +/** |
| 19 | + * @covers \Hyde\Foundation\HydeKernel |
| 20 | + * @covers \Hyde\Foundation\Concerns\ManagesHydeKernel |
| 21 | + * @covers \Hyde\Foundation\FileCollection |
| 22 | + * @covers \Hyde\Foundation\PageCollection |
| 23 | + */ |
| 24 | +class HydeKernelDynamicPageClassesTest extends TestCase |
| 25 | +{ |
| 26 | + public function test_get_registered_page_classes_method() |
| 27 | + { |
| 28 | + $this->assertSame([], app(HydeKernel::class)->getRegisteredPageClasses()); |
| 29 | + } |
| 30 | + |
| 31 | + public function test_register_page_class_method_adds_specified_class_name_to_index() |
| 32 | + { |
| 33 | + app(HydeKernel::class)->registerPageClass(TestPageClass::class); |
| 34 | + $this->assertSame([TestPageClass::class], app(HydeKernel::class)->getRegisteredPageClasses()); |
| 35 | + } |
| 36 | + |
| 37 | + public function test_register_page_class_method_does_not_add_already_added_class_names() |
| 38 | + { |
| 39 | + app(HydeKernel::class)->registerPageClass(TestPageClass::class); |
| 40 | + app(HydeKernel::class)->registerPageClass(TestPageClass::class); |
| 41 | + $this->assertSame([TestPageClass::class], app(HydeKernel::class)->getRegisteredPageClasses()); |
| 42 | + } |
| 43 | + |
| 44 | + public function test_register_page_class_method_only_accepts_instances_of_hyde_page_class() |
| 45 | + { |
| 46 | + $this->expectException(InvalidArgumentException::class); |
| 47 | + $this->expectExceptionMessage('The specified class must be a subclass of HydePage.'); |
| 48 | + app(HydeKernel::class)->registerPageClass(stdClass::class); |
| 49 | + } |
| 50 | + |
| 51 | + public function test_register_page_class_method_throws_exception_when_collection_is_already_booted() |
| 52 | + { |
| 53 | + $this->expectException(BadMethodCallException::class); |
| 54 | + $this->expectExceptionMessage('Cannot register a page class after the Kernel has been booted.'); |
| 55 | + |
| 56 | + app(HydeKernel::class)->boot(); |
| 57 | + app(HydeKernel::class)->registerPageClass(TestPageClass::class); |
| 58 | + } |
| 59 | + |
| 60 | + // Test custom registered pages can be further processed and parsed |
| 61 | + |
| 62 | + public function test_custom_registered_pages_are_discovered_by_the_file_collection_class() |
| 63 | + { |
| 64 | + $this->directory('foo'); |
| 65 | + $this->file('foo/bar.txt'); |
| 66 | + app(HydeKernel::class)->registerPageClass(TestPageClassWithSourceInformation::class); |
| 67 | + |
| 68 | + $this->assertArrayHasKey('foo/bar.txt', Facades\FileCollection::all()); |
| 69 | + $this->assertEquals(new SourceFile('foo/bar.txt', TestPageClassWithSourceInformation::class), Facades\FileCollection::get('foo/bar.txt')); |
| 70 | + } |
| 71 | + |
| 72 | + public function test_custom_registered_pages_are_discovered_by_the_page_collection_class() |
| 73 | + { |
| 74 | + $this->directory('foo'); |
| 75 | + $this->file('foo/bar.txt'); |
| 76 | + app(HydeKernel::class)->registerPageClass(TestPageClassWithSourceInformation::class); |
| 77 | + $this->assertArrayHasKey('foo/bar.txt', Facades\PageCollection::all()); |
| 78 | + $this->assertEquals(new TestPageClassWithSourceInformation('bar'), Facades\PageCollection::get('foo/bar.txt')); |
| 79 | + } |
| 80 | + |
| 81 | + public function test_custom_registered_pages_are_discovered_by_the_route_collection_class() |
| 82 | + { |
| 83 | + $this->directory('foo'); |
| 84 | + $this->file('foo/bar.txt'); |
| 85 | + app(HydeKernel::class)->registerPageClass(TestPageClassWithSourceInformation::class); |
| 86 | + $this->assertArrayHasKey('foo/bar', Facades\Router::all()); |
| 87 | + $this->assertEquals(new Route(new TestPageClassWithSourceInformation('bar')), Facades\Router::get('foo/bar')); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +abstract class TestPageClass extends HydePage |
| 92 | +{ |
| 93 | + // |
| 94 | +} |
| 95 | + |
| 96 | +class TestPageClassWithSourceInformation extends HydePage |
| 97 | +{ |
| 98 | + public static string $sourceDirectory = 'foo'; |
| 99 | + public static string $outputDirectory = 'foo'; |
| 100 | + public static string $fileExtension = '.txt'; |
| 101 | + |
| 102 | + public function compile(): string |
| 103 | + { |
| 104 | + return ''; |
| 105 | + } |
| 106 | +} |
0 commit comments