From 5b1acc40e3e63539edb1b6289ac1fad5338a3b78 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 13:31:54 +0200 Subject: [PATCH 01/55] Remove directory after test class --- .../tests/Feature/SourceDirectoriesCanBeChangedTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/framework/tests/Feature/SourceDirectoriesCanBeChangedTest.php b/packages/framework/tests/Feature/SourceDirectoriesCanBeChangedTest.php index 51bd2016184..5be594d3a8c 100644 --- a/packages/framework/tests/Feature/SourceDirectoriesCanBeChangedTest.php +++ b/packages/framework/tests/Feature/SourceDirectoriesCanBeChangedTest.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Testing\Feature; +use Illuminate\Support\Facades\File; use Hyde\Framework\HydeServiceProvider; use Hyde\Pages\BladePage; use Hyde\Pages\DocumentationPage; @@ -17,6 +18,13 @@ */ class SourceDirectoriesCanBeChangedTest extends TestCase { + public static function tearDownAfterClass(): void + { + parent::tearDownAfterClass(); + + File::deleteDirectory('_source'); + } + public function testBaselines() { $this->assertEquals('_pages', HtmlPage::sourceDirectory()); From 3f899bcdff3261969e498a32d1025980bd8866f0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 17:22:20 +0200 Subject: [PATCH 02/55] Create BuildTaskSkippedException.php --- .../Features/BuildTasks/BuildTaskSkippedException.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 packages/framework/src/Framework/Features/BuildTasks/BuildTaskSkippedException.php diff --git a/packages/framework/src/Framework/Features/BuildTasks/BuildTaskSkippedException.php b/packages/framework/src/Framework/Features/BuildTasks/BuildTaskSkippedException.php new file mode 100644 index 00000000000..1071595ed89 --- /dev/null +++ b/packages/framework/src/Framework/Features/BuildTasks/BuildTaskSkippedException.php @@ -0,0 +1,10 @@ + Date: Fri, 12 Apr 2024 17:23:23 +0200 Subject: [PATCH 03/55] Extend RuntimeException --- .../Features/BuildTasks/BuildTaskSkippedException.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/BuildTasks/BuildTaskSkippedException.php b/packages/framework/src/Framework/Features/BuildTasks/BuildTaskSkippedException.php index 1071595ed89..92ad5dd8cda 100644 --- a/packages/framework/src/Framework/Features/BuildTasks/BuildTaskSkippedException.php +++ b/packages/framework/src/Framework/Features/BuildTasks/BuildTaskSkippedException.php @@ -4,7 +4,9 @@ namespace Hyde\Framework\Features\BuildTasks; -class BuildTaskSkippedException +use RuntimeException; + +class BuildTaskSkippedException extends RuntimeException { // } From 573b8f24a1ac67948a1aa4b9f86286558a389422 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 17:24:14 +0200 Subject: [PATCH 04/55] Add helper method to skip task --- .../src/Framework/Features/BuildTasks/BuildTask.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php index 7ad324fdd02..bbdd67f9b7c 100644 --- a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php +++ b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php @@ -84,6 +84,12 @@ public function writeln(string $message): void $this->output?->writeln($message); } + /** Write a fluent message to the output that the task is skipping. */ + public function skip(string $reason): void + { + throw new BuildTaskSkippedException($reason); + } + /** Write a fluent message to the output that the task created the specified file. */ public function createdSiteFile(string $path): static { From 41025363051b49ad7a7ba787e85d415ef3514bb0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 17:27:00 +0200 Subject: [PATCH 05/55] Annotate thrown exception --- .../src/Framework/Features/BuildTasks/BuildTask.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php index bbdd67f9b7c..346976b8394 100644 --- a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php +++ b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php @@ -84,7 +84,11 @@ public function writeln(string $message): void $this->output?->writeln($message); } - /** Write a fluent message to the output that the task is skipping. */ + /** + * Write a fluent message to the output that the task is skipping. + * + * @throws \Hyde\Framework\Features\BuildTasks\BuildTaskSkippedException + */ public function skip(string $reason): void { throw new BuildTaskSkippedException($reason); From 3730b96c1be8451d5880fcb5d9e3aac7c8a24118 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 17:27:31 +0200 Subject: [PATCH 06/55] Add base constructor --- .../Features/BuildTasks/BuildTaskSkippedException.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/BuildTasks/BuildTaskSkippedException.php b/packages/framework/src/Framework/Features/BuildTasks/BuildTaskSkippedException.php index 92ad5dd8cda..cc4cff9b406 100644 --- a/packages/framework/src/Framework/Features/BuildTasks/BuildTaskSkippedException.php +++ b/packages/framework/src/Framework/Features/BuildTasks/BuildTaskSkippedException.php @@ -8,5 +8,8 @@ class BuildTaskSkippedException extends RuntimeException { - // + public function __construct(string $message = 'Task was skipped', int $code = 0) + { + parent::__construct($message, $code); + } } From a2265bbf02cc8efe84f63f622d4f4adee362d3a5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 17:29:58 +0200 Subject: [PATCH 07/55] Catch skipped exceptions to render different message --- .../src/Framework/Features/BuildTasks/BuildTask.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php index 346976b8394..274a80aef14 100644 --- a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php +++ b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php @@ -49,6 +49,11 @@ public function run(?OutputStyle $output = null): int $this->handle(); $this->printFinishMessage(); } catch (Throwable $exception) { + if ($exception instanceof BuildTaskSkippedException) { + $this->writeln('Skipped'); + $this->writeln(" > {$exception->getMessage()}"); + } + $this->writeln('Failed'); $this->writeln("{$exception->getMessage()}"); $this->exitCode = $exception->getCode(); From 86543bc500355dc6dc2cdcd1f74aa95843370ee4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 17:30:10 +0200 Subject: [PATCH 08/55] Add default output in else block --- .../src/Framework/Features/BuildTasks/BuildTask.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php index 274a80aef14..5a7657e43d0 100644 --- a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php +++ b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php @@ -52,11 +52,12 @@ public function run(?OutputStyle $output = null): int if ($exception instanceof BuildTaskSkippedException) { $this->writeln('Skipped'); $this->writeln(" > {$exception->getMessage()}"); + } else { + $this->writeln('Failed'); + $this->writeln("{$exception->getMessage()}"); } - $this->writeln('Failed'); - $this->writeln("{$exception->getMessage()}"); - $this->exitCode = $exception->getCode(); + $this->exitCode = $exception->getCode(); } $this->write("\n"); From 21096a295e403daf6d1f69390ad2d3574b04d000 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 17:30:26 +0200 Subject: [PATCH 09/55] Fix formatting --- .../framework/src/Framework/Features/BuildTasks/BuildTask.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php index 5a7657e43d0..ea55069d15a 100644 --- a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php +++ b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php @@ -57,7 +57,7 @@ public function run(?OutputStyle $output = null): int $this->writeln("{$exception->getMessage()}"); } - $this->exitCode = $exception->getCode(); + $this->exitCode = $exception->getCode(); } $this->write("\n"); From 81e5690d6003af11e2156fc026c2949d27c84c39 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 20:00:44 +0200 Subject: [PATCH 10/55] Update RELEASE_NOTES.md --- RELEASE_NOTES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 2a2fe9a5bab..aa4747b7632 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -14,6 +14,7 @@ This serves two purposes: - Added a `Hyde::route()` helper to the `Hyde` facade in https://github.com/hydephp/develop/pull/1591 - Added new global helper functions (`asset()`, `route()`, `url()`) in https://github.com/hydephp/develop/pull/1592 - Added a new `Feature` enum to improve the `Features` facade in https://github.com/hydephp/develop/pull/1650 +- Added a helper to `->skip()` build tasks in https://github.com/hydephp/develop/pull/1656 ### Changed - The `features` array in the `config/hyde.php` configuration file is now an array of `Feature` enums in https://github.com/hydephp/develop/pull/1650 From 7b7b126e1757270c697569fa6e6c6f79d0487be0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 20:03:47 +0200 Subject: [PATCH 11/55] Document task skipping --- docs/advanced-features/build-tasks.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/advanced-features/build-tasks.md b/docs/advanced-features/build-tasks.md index e928736bec1..d0918e03256 100644 --- a/docs/advanced-features/build-tasks.md +++ b/docs/advanced-features/build-tasks.md @@ -173,3 +173,20 @@ public function handle(): void $this->output->writeln('This is a line of text'); } ``` + +### Skipping tasks + +If you for some reason need to skip the task during its execution, you can call the `skip()` method. + +```php +public function handle(): void +{ + if ($this->someCondition() !== true) { + $this->skip('Some condition was not met'); + + // The task will not be executed past this point + } +} +``` + +This will then halt the execution of the task, and display a notice with the message you provided to the console. From eb7b0faeae6c744c702d7ed01e264314551453c8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 20:04:33 +0200 Subject: [PATCH 12/55] Add release time notice --- docs/advanced-features/build-tasks.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/advanced-features/build-tasks.md b/docs/advanced-features/build-tasks.md index d0918e03256..51180d15d62 100644 --- a/docs/advanced-features/build-tasks.md +++ b/docs/advanced-features/build-tasks.md @@ -176,6 +176,8 @@ public function handle(): void ### Skipping tasks +>info This feature was added in HydePHP v1.6.0 + If you for some reason need to skip the task during its execution, you can call the `skip()` method. ```php From b4b56b38ec40a10a6307f410001b0233c5d2ac23 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 20:32:03 +0200 Subject: [PATCH 13/55] Create BuildTaskSkippedExceptionTest.php --- .../Unit/BuildTaskSkippedExceptionTest.php | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 packages/framework/tests/Unit/BuildTaskSkippedExceptionTest.php diff --git a/packages/framework/tests/Unit/BuildTaskSkippedExceptionTest.php b/packages/framework/tests/Unit/BuildTaskSkippedExceptionTest.php new file mode 100644 index 00000000000..3d3e91aa581 --- /dev/null +++ b/packages/framework/tests/Unit/BuildTaskSkippedExceptionTest.php @@ -0,0 +1,51 @@ +assertInstanceOf(BuildTaskSkippedException::class, $exception); + } + + public function testItThrowsAnExceptionWithDefaultMessage() + { + $this->expectException(BuildTaskSkippedException::class); + $this->expectExceptionMessage('Task was skipped'); + + throw new BuildTaskSkippedException(); + } + + public function testItThrowsAnExceptionWithCustomMessage() + { + $this->expectException(BuildTaskSkippedException::class); + $this->expectExceptionMessage('Custom message'); + + throw new BuildTaskSkippedException('Custom message'); + } + + public function testDefaultExceptionCode() + { + $exception = new BuildTaskSkippedException(); + + $this->assertSame(0, $exception->getCode()); + } + + public function testCustomExceptionCode() + { + $exception = new BuildTaskSkippedException('Custom message', 123); + + $this->assertSame(123, $exception->getCode()); + } +} From 41b410697b631477c6fc8fd6e26380de1087f1de Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:14:54 +0200 Subject: [PATCH 14/55] Create BuildTaskUnitTest.php --- .../framework/tests/Unit/BuildTaskUnitTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 packages/framework/tests/Unit/BuildTaskUnitTest.php diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php new file mode 100644 index 00000000000..449b5064735 --- /dev/null +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -0,0 +1,15 @@ + Date: Fri, 12 Apr 2024 21:18:11 +0200 Subject: [PATCH 15/55] Test can create build task --- .../framework/tests/Unit/BuildTaskUnitTest.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 449b5064735..8d4a891069e 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -5,11 +5,23 @@ namespace Hyde\Framework\Testing\Unit; use Hyde\Testing\UnitTestCase; +use Hyde\Framework\Features\BuildTasks\BuildTask; /** * @covers \Hyde\Framework\Features\BuildTasks\BuildTask */ class BuildTaskUnitTest extends UnitTestCase { - // + public function testCanCreateBuildTask() + { + $task = new class extends BuildTask + { + public function handle(): void + { + // Do nothing + } + }; + + $this->assertInstanceOf(BuildTask::class, $task); + } } From 105db30ac83f9264dd34cb16ac8d02562f53cbcc Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:20:25 +0200 Subject: [PATCH 16/55] Extract testing class --- .../framework/tests/Unit/BuildTaskUnitTest.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 8d4a891069e..a96ddfc1a2b 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -14,14 +14,16 @@ class BuildTaskUnitTest extends UnitTestCase { public function testCanCreateBuildTask() { - $task = new class extends BuildTask - { - public function handle(): void - { - // Do nothing - } - }; + $task = new EmptyTestBuildTask(); $this->assertInstanceOf(BuildTask::class, $task); } } + +class EmptyTestBuildTask extends BuildTask +{ + public function handle(): void + { + // Do nothing + } +} From f1c6ee62176df960920b452ebe4fd0221cd6feed Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:21:50 +0200 Subject: [PATCH 17/55] Add inspectable test build task --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index a96ddfc1a2b..7c43c2d0d06 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -27,3 +27,16 @@ public function handle(): void // Do nothing } } + +class InspectableTestBuildTask extends BuildTask +{ + public function handle(): void + { + // Do nothing + } + + public function property(string $name): mixed + { + return $this->{$name}; + } +} From 9bb60bf5a8cce73e3b00d28bb0fe2230e37d26de Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:22:57 +0200 Subject: [PATCH 18/55] Add state testing helper --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 7c43c2d0d06..c542523096b 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -35,6 +35,11 @@ public function handle(): void // Do nothing } + public function isset(string $name): bool + { + return isset($this->{$name}); + } + public function property(string $name): mixed { return $this->{$name}; From d13994c90a2c8f1b16510783484eb7ff7a01736e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:23:34 +0200 Subject: [PATCH 19/55] Add call helper --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index c542523096b..9be8e6555de 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -44,4 +44,9 @@ public function property(string $name): mixed { return $this->{$name}; } + + public function call(string $name, mixed ...$args): mixed + { + return $this->{$name}(...$args); + } } From c787f160c6e9d277d53afcd77c86b6c8731ea911 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:23:56 +0200 Subject: [PATCH 20/55] Test it tracks execution time --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 9be8e6555de..15f5cc0b0fd 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -18,6 +18,16 @@ public function testCanCreateBuildTask() $this->assertInstanceOf(BuildTask::class, $task); } + + public function testItTracksExecutionTime() + { + $task = new InspectableTestBuildTask(); + + $task->run(); + + $this->assertTrue($task->isset('timeStart')); + $this->assertGreaterThan(0, $task->property('timeStart')); + } } class EmptyTestBuildTask extends BuildTask From daa4ae125bf88d853feda4e65ed118bf342bbd78 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:27:29 +0200 Subject: [PATCH 21/55] Test it can run without output --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 15f5cc0b0fd..eeb98c4ed70 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -28,6 +28,15 @@ public function testItTracksExecutionTime() $this->assertTrue($task->isset('timeStart')); $this->assertGreaterThan(0, $task->property('timeStart')); } + + public function testItCanRunWithoutOutput() + { + $task = new InspectableTestBuildTask(); + + $task->run(); + + $this->assertFalse($task->isset('output')); + } } class EmptyTestBuildTask extends BuildTask From abd624e207dbee9120797aab2d9c363b6809b006 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:29:29 +0200 Subject: [PATCH 22/55] Test it can run with output --- .../framework/tests/Unit/BuildTaskUnitTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index eeb98c4ed70..18fa13e4b6d 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -4,7 +4,9 @@ namespace Hyde\Framework\Testing\Unit; +use Mockery; use Hyde\Testing\UnitTestCase; +use Illuminate\Console\OutputStyle; use Hyde\Framework\Features\BuildTasks\BuildTask; /** @@ -37,6 +39,18 @@ public function testItCanRunWithoutOutput() $this->assertFalse($task->isset('output')); } + + public function testItCanRunWithOutput() + { + $task = new InspectableTestBuildTask(); + + $output = Mockery::mock(OutputStyle::class); + $output->shouldReceive('write')->once(); + $output->shouldReceive('writeln')->once(); + + $task->run($output); + $this->assertTrue($task->isset('output')); + } } class EmptyTestBuildTask extends BuildTask From 9d2c710cde409eb76baa05e3825b8acc41ae4fba Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:30:01 +0200 Subject: [PATCH 23/55] Use shorthand mocking helper --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 18fa13e4b6d..b77b6e7d54d 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -44,9 +44,10 @@ public function testItCanRunWithOutput() { $task = new InspectableTestBuildTask(); - $output = Mockery::mock(OutputStyle::class); - $output->shouldReceive('write')->once(); - $output->shouldReceive('writeln')->once(); + $output = Mockery::mock(OutputStyle::class, [ + 'write' => null, + 'writeln' => null, + ]); $task->run($output); $this->assertTrue($task->isset('output')); From 163c2ea5fcb36677948bf6356d8e0516586342c6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:36:31 +0200 Subject: [PATCH 24/55] Add buffered test build task class --- .../framework/tests/Unit/BuildTaskUnitTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index b77b6e7d54d..751a2677990 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -84,3 +84,18 @@ public function call(string $name, mixed ...$args): mixed return $this->{$name}(...$args); } } + +class BufferedTestBuildTask extends InspectableTestBuildTask +{ + public array $buffer = []; + + public function write(string $message): void + { + $this->buffer[] = $message; + } + + public function writeln(string $message): void + { + $this->buffer[] = $message; + } +} From 83fd8312dd213b76930888d9d0a13cbe144b8423 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:38:32 +0200 Subject: [PATCH 25/55] Test it prints start message --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 751a2677990..0190b16731d 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -52,6 +52,15 @@ public function testItCanRunWithOutput() $task->run($output); $this->assertTrue($task->isset('output')); } + + public function testItPrintsStartMessage() + { + $task = new BufferedTestBuildTask(); + + $task->run(); + + $this->assertStringContainsString('Running generic build task', $task->buffer[0]); + } } class EmptyTestBuildTask extends BuildTask From 37a3c6be691bfe77be819ea4758a172b749a4e79 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:39:00 +0200 Subject: [PATCH 26/55] Test it prints finish message --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 0190b16731d..6abc7c2089b 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -61,6 +61,16 @@ public function testItPrintsStartMessage() $this->assertStringContainsString('Running generic build task', $task->buffer[0]); } + + public function testItPrintsFinishMessage() + { + $task = new BufferedTestBuildTask(); + + $task->run(); + + $this->assertStringContainsString('Done in', $task->buffer[1]); + $this->assertStringContainsString('ms', $task->buffer[1]); + } } class EmptyTestBuildTask extends BuildTask From fdfcb711e87f4603d6cbde0086638f9354d4fc3f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:39:58 +0200 Subject: [PATCH 27/55] Shorten lines using tap call --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 6abc7c2089b..54438b45eff 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -55,18 +55,14 @@ public function testItCanRunWithOutput() public function testItPrintsStartMessage() { - $task = new BufferedTestBuildTask(); - - $task->run(); + $task = tap(new BufferedTestBuildTask(), fn ($task) => $task->run()); $this->assertStringContainsString('Running generic build task', $task->buffer[0]); } public function testItPrintsFinishMessage() { - $task = new BufferedTestBuildTask(); - - $task->run(); + $task = tap(new BufferedTestBuildTask(), fn ($task) => $task->run()); $this->assertStringContainsString('Done in', $task->buffer[1]); $this->assertStringContainsString('ms', $task->buffer[1]); From 543af9698dce1b4deb29d24d4efe490cbebc0b91 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:43:04 +0200 Subject: [PATCH 28/55] Test run method handles task --- .../framework/tests/Unit/BuildTaskUnitTest.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 54438b45eff..423b1cbe96a 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -67,6 +67,17 @@ public function testItPrintsFinishMessage() $this->assertStringContainsString('Done in', $task->buffer[1]); $this->assertStringContainsString('ms', $task->buffer[1]); } + + public function testRunMethodHandlesTask() + { + $task = new InspectableTestBuildTask(); + + $this->assertFalse($task->property('wasHandled')); + + $task->run(); + + $this->assertTrue($task->property('wasHandled')); + } } class EmptyTestBuildTask extends BuildTask @@ -79,9 +90,11 @@ public function handle(): void class InspectableTestBuildTask extends BuildTask { + protected bool $wasHandled = false; + public function handle(): void { - // Do nothing + $this->wasHandled = true; } public function isset(string $name): bool From 9034e2691ee0a65039d9e13c7fbe6b09f9610cda Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:45:49 +0200 Subject: [PATCH 29/55] Add helper to set value --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 423b1cbe96a..29b1b1190b8 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -111,6 +111,11 @@ public function call(string $name, mixed ...$args): mixed { return $this->{$name}(...$args); } + + public function set(string $name, mixed $value): void + { + $this->{$name} = $value; + } } class BufferedTestBuildTask extends InspectableTestBuildTask From 31c8742419f2e6277f8486167c4a20bc76688ef8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:45:59 +0200 Subject: [PATCH 30/55] Test run method returns exit code --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 29b1b1190b8..a2b4b37cb65 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -78,6 +78,17 @@ public function testRunMethodHandlesTask() $this->assertTrue($task->property('wasHandled')); } + + public function testRunMethodReturnsExitCode() + { + $task = new InspectableTestBuildTask(); + + $this->assertEquals(0, $task->run()); + + $task->set('exitCode', 1); + + $this->assertEquals(1, $task->run()); + } } class EmptyTestBuildTask extends BuildTask From a09778638012ce367a7589852f642c0e011cc0eb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:46:10 +0200 Subject: [PATCH 31/55] Use assert same --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index a2b4b37cb65..222e5c77b3a 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -83,11 +83,11 @@ public function testRunMethodReturnsExitCode() { $task = new InspectableTestBuildTask(); - $this->assertEquals(0, $task->run()); + $this->assertSame(0, $task->run()); $task->set('exitCode', 1); - $this->assertEquals(1, $task->run()); + $this->assertSame(1, $task->run()); } } From 02a744bb45ea5981a8695784181132b10b72cf7b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:52:49 +0200 Subject: [PATCH 32/55] Test message accessor --- .../framework/tests/Unit/BuildTaskUnitTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 222e5c77b3a..124c8a12932 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -89,6 +89,21 @@ public function testRunMethodReturnsExitCode() $this->assertSame(1, $task->run()); } + + public function testCanGetMessage() + { + $this->assertSame('Running generic build task', (new BufferedTestBuildTask())->getMessage()); + } + + public function testCanGetCustomMessage() + { + $task = new class extends BufferedTestBuildTask + { + protected static string $message = 'Custom message'; + }; + + $this->assertSame('Custom message', $task->getMessage()); + } } class EmptyTestBuildTask extends BuildTask From b4a433a8bcd09bc142041c2aa36c6c390d243871 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 21:59:27 +0200 Subject: [PATCH 33/55] Test can print start message --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 124c8a12932..93841c99bd0 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -104,6 +104,13 @@ public function testCanGetCustomMessage() $this->assertSame('Custom message', $task->getMessage()); } + + public function testCanPrintStartMessage() + { + $task = tap(new BufferedTestBuildTask(), fn ($task) => $task->printStartMessage()); + + $this->assertSame('Running generic build task...', trim($task->buffer[0])); + } } class EmptyTestBuildTask extends BuildTask From 3afe2a3d07ab65265bdc7ce49cc7cbcfafe68df1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 22:00:14 +0200 Subject: [PATCH 34/55] Extract helper class --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 93841c99bd0..bc467768824 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -97,10 +97,7 @@ public function testCanGetMessage() public function testCanGetCustomMessage() { - $task = new class extends BufferedTestBuildTask - { - protected static string $message = 'Custom message'; - }; + $task = new BufferedTestBuildTaskWithCustomMessage(); $this->assertSame('Custom message', $task->getMessage()); } @@ -165,3 +162,8 @@ public function writeln(string $message): void $this->buffer[] = $message; } } + +class BufferedTestBuildTaskWithCustomMessage extends BufferedTestBuildTask +{ + protected static string $message = 'Custom message'; +} From 6de0a8719b77baad307c045ddf2437a44e791ef5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 22:01:27 +0200 Subject: [PATCH 35/55] Test can print custom start message --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index bc467768824..b5cb3e21069 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -108,6 +108,13 @@ public function testCanPrintStartMessage() $this->assertSame('Running generic build task...', trim($task->buffer[0])); } + + public function testCanPrintCustomStartMessage() + { + $task = tap(new BufferedTestBuildTaskWithCustomMessage(), fn ($task) => $task->printStartMessage()); + + $this->assertSame('Custom message...', trim($task->buffer[0])); + } } class EmptyTestBuildTask extends BuildTask From 1065eb4a2a124af8c5ecef4deb387a4639bce62f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 22:20:41 +0200 Subject: [PATCH 36/55] Test finish message handling --- .../tests/Unit/BuildTaskUnitTest.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index b5cb3e21069..3028dc124ee 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -115,6 +115,28 @@ public function testCanPrintCustomStartMessage() $this->assertSame('Custom message...', trim($task->buffer[0])); } + + public function testCanPrintFinishMessage() + { + $task = tap(new BufferedTestBuildTask(), function ($task) { + $task->set('timeStart', time()); + $task->printFinishMessage(); + }); + + $this->assertStringContainsString('Done in', $task->buffer[0]); + $this->assertStringContainsString('ms', $task->buffer[0]); + } + + public function testFinishMessagePrintingFormatsExecutionTime() + { + $task = tap(new BufferedTestBuildTask(), function ($task) { + $task->set('timeStart', 1000); + $task->mockClock(1001.23456); + $task->printFinishMessage(); + }); + + $this->assertSame('Done in 1,234.56ms', $task->buffer[0]); + } } class EmptyTestBuildTask extends BuildTask @@ -128,6 +150,7 @@ public function handle(): void class InspectableTestBuildTask extends BuildTask { protected bool $wasHandled = false; + protected float $mockedEndTime; public function handle(): void { @@ -153,6 +176,20 @@ public function set(string $name, mixed $value): void { $this->{$name} = $value; } + + public function mockClock(float $time): void + { + $this->mockedEndTime = $time; + } + + protected function stopClock(): float + { + if (isset($this->mockedEndTime)) { + return $this->mockedEndTime - $this->timeStart; + } + + return parent::stopClock(); + } } class BufferedTestBuildTask extends InspectableTestBuildTask From ca0966126f46bb3664975f0e9d1fa44d279d286b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 22:30:53 +0200 Subject: [PATCH 37/55] Test with execution time --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 3028dc124ee..a0ee7818d13 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -137,6 +137,18 @@ public function testFinishMessagePrintingFormatsExecutionTime() $this->assertSame('Done in 1,234.56ms', $task->buffer[0]); } + + public function testWithExecutionTime() + { + $task = tap(new BufferedTestBuildTask(), function ($task) { + $task->set('timeStart', 1000); + $task->mockClock(1001.23456); + }); + + $this->assertSame($task, $task->withExecutionTime()); + + $this->assertSame(' in 1,234.56ms', $task->buffer[0]); + } } class EmptyTestBuildTask extends BuildTask From d38f1ae56ab5eaa426ceee9dca8c040e34b874b3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 22:31:09 +0200 Subject: [PATCH 38/55] Formatting --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index a0ee7818d13..23f739b1d0a 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -146,7 +146,6 @@ public function testWithExecutionTime() }); $this->assertSame($task, $task->withExecutionTime()); - $this->assertSame(' in 1,234.56ms', $task->buffer[0]); } } From f5247bb17122c9d1e58aea72360cd92e34f3dfb2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 22:31:33 +0200 Subject: [PATCH 39/55] Test can write to output --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 23f739b1d0a..e0bdbe86301 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -138,6 +138,16 @@ public function testFinishMessagePrintingFormatsExecutionTime() $this->assertSame('Done in 1,234.56ms', $task->buffer[0]); } + public function testCanWriteToOutput() + { + $task = new BufferedTestBuildTask(); + + $task->write('foo'); + $task->writeln('bar'); + + $this->assertSame(['foo', 'bar'], $task->buffer); + } + public function testWithExecutionTime() { $task = tap(new BufferedTestBuildTask(), function ($task) { From ba0b4be5278cada6a471822e335b9fd39e34d70c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 22:32:27 +0200 Subject: [PATCH 40/55] Test created site file --- .../tests/Unit/BuildTaskUnitTest.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index e0bdbe86301..0ee0ac81be4 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -5,6 +5,7 @@ namespace Hyde\Framework\Testing\Unit; use Mockery; +use Hyde\Hyde; use Hyde\Testing\UnitTestCase; use Illuminate\Console\OutputStyle; use Hyde\Framework\Features\BuildTasks\BuildTask; @@ -158,6 +159,28 @@ public function testWithExecutionTime() $this->assertSame($task, $task->withExecutionTime()); $this->assertSame(' in 1,234.56ms', $task->buffer[0]); } + + public function testCreatedSiteFile() + { + self::needsKernel(); + + $task = new BufferedTestBuildTask(); + + $task->createdSiteFile('foo'); + + $this->assertSame("\n > Created foo", $task->buffer[0]); + } + + public function testCreatedSiteFileWithAbsolutePath() + { + self::needsKernel(); + + $task = new BufferedTestBuildTask(); + + $task->createdSiteFile(Hyde::path('foo')); + + $this->assertSame("\n > Created foo", $task->buffer[0]); + } } class EmptyTestBuildTask extends BuildTask From bde44c159af39b6d48014b09c5a7981e9234f4f5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 12 Apr 2024 22:34:55 +0200 Subject: [PATCH 41/55] Reorder tests to match source order --- .../tests/Unit/BuildTaskUnitTest.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 0ee0ac81be4..038bd4aa566 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -149,17 +149,6 @@ public function testCanWriteToOutput() $this->assertSame(['foo', 'bar'], $task->buffer); } - public function testWithExecutionTime() - { - $task = tap(new BufferedTestBuildTask(), function ($task) { - $task->set('timeStart', 1000); - $task->mockClock(1001.23456); - }); - - $this->assertSame($task, $task->withExecutionTime()); - $this->assertSame(' in 1,234.56ms', $task->buffer[0]); - } - public function testCreatedSiteFile() { self::needsKernel(); @@ -181,6 +170,17 @@ public function testCreatedSiteFileWithAbsolutePath() $this->assertSame("\n > Created foo", $task->buffer[0]); } + + public function testWithExecutionTime() + { + $task = tap(new BufferedTestBuildTask(), function ($task) { + $task->set('timeStart', 1000); + $task->mockClock(1001.23456); + }); + + $this->assertSame($task, $task->withExecutionTime()); + $this->assertSame(' in 1,234.56ms', $task->buffer[0]); + } } class EmptyTestBuildTask extends BuildTask From 2b14ab6079ffc046ae458d8eb295c5e9fcc780e8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 13 Apr 2024 11:15:32 +0200 Subject: [PATCH 42/55] Add closure type --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 038bd4aa566..74a6cf18c6f 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -173,7 +173,7 @@ public function testCreatedSiteFileWithAbsolutePath() public function testWithExecutionTime() { - $task = tap(new BufferedTestBuildTask(), function ($task) { + $task = tap(new BufferedTestBuildTask(), function (BufferedTestBuildTask $task) { $task->set('timeStart', 1000); $task->mockClock(1001.23456); }); From 60cddd389e49d0a3c16bff05b0221fb1ec9105b4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 13 Apr 2024 11:26:35 +0200 Subject: [PATCH 43/55] Test exception handling --- .../tests/Unit/BuildTaskUnitTest.php | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 74a6cf18c6f..f1a453d6890 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -5,7 +5,9 @@ namespace Hyde\Framework\Testing\Unit; use Mockery; +use Closure; use Hyde\Hyde; +use Exception; use Hyde\Testing\UnitTestCase; use Illuminate\Console\OutputStyle; use Hyde\Framework\Features\BuildTasks\BuildTask; @@ -181,6 +183,24 @@ public function testWithExecutionTime() $this->assertSame($task, $task->withExecutionTime()); $this->assertSame(' in 1,234.56ms', $task->buffer[0]); } + + public function testExceptionHandling() + { + $task = new BufferedTestBuildTask(); + + $task->set('exitCode', 0); + $task->set('timeStart', time()); + + $task->mockHandle(function () { + throw new Exception('Test exception', 123); + }); + + $task->run(); + + $this->assertSame(123, $task->property('exitCode')); + $this->assertSame('Failed', $task->buffer[1]); + $this->assertSame('Test exception', $task->buffer[2]); + } } class EmptyTestBuildTask extends BuildTask @@ -195,10 +215,15 @@ class InspectableTestBuildTask extends BuildTask { protected bool $wasHandled = false; protected float $mockedEndTime; + protected Closure $mockHandle; public function handle(): void { - $this->wasHandled = true; + if (isset($this->mockHandle)) { + ($this->mockHandle)(); + } else { + $this->wasHandled = true; + } } public function isset(string $name): bool @@ -226,6 +251,11 @@ public function mockClock(float $time): void $this->mockedEndTime = $time; } + public function mockHandle(Closure $handle): void + { + $this->mockHandle = $handle; + } + protected function stopClock(): float { if (isset($this->mockedEndTime)) { From 87c3e1540f2f6767b91d0e71abe3d4d0103d98be Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 13 Apr 2024 11:45:17 +0200 Subject: [PATCH 44/55] Simplify test --- packages/framework/tests/Unit/BuildTaskUnitTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index f1a453d6890..41aa5ea099b 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -188,9 +188,6 @@ public function testExceptionHandling() { $task = new BufferedTestBuildTask(); - $task->set('exitCode', 0); - $task->set('timeStart', time()); - $task->mockHandle(function () { throw new Exception('Test exception', 123); }); From e8c2435a6796fee5b7323ba2e8858cc630202c87 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 13 Apr 2024 11:48:29 +0200 Subject: [PATCH 45/55] Make skip message optional --- .../framework/src/Framework/Features/BuildTasks/BuildTask.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php index ea55069d15a..14c4793e003 100644 --- a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php +++ b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php @@ -95,9 +95,9 @@ public function writeln(string $message): void * * @throws \Hyde\Framework\Features\BuildTasks\BuildTaskSkippedException */ - public function skip(string $reason): void + public function skip(string $reason = null): void { - throw new BuildTaskSkippedException($reason); + throw new BuildTaskSkippedException($reason ?? 'Task was skipped'); } /** Write a fluent message to the output that the task created the specified file. */ From 33a2862e1bb75a1ab24659d654e5eb69f77a9e54 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 13 Apr 2024 11:51:03 +0200 Subject: [PATCH 46/55] Change null coalesce to default value --- .../framework/src/Framework/Features/BuildTasks/BuildTask.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php index 14c4793e003..281e49361f9 100644 --- a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php +++ b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php @@ -95,9 +95,9 @@ public function writeln(string $message): void * * @throws \Hyde\Framework\Features\BuildTasks\BuildTaskSkippedException */ - public function skip(string $reason = null): void + public function skip(string $reason = 'Task was skipped'): void { - throw new BuildTaskSkippedException($reason ?? 'Task was skipped'); + throw new BuildTaskSkippedException($reason); } /** Write a fluent message to the output that the task created the specified file. */ From fad38fc961c5a8dcb1885c4b4ba228859dfcd58f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 13 Apr 2024 11:55:11 +0200 Subject: [PATCH 47/55] Test task skipping --- .../tests/Unit/BuildTaskUnitTest.php | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 41aa5ea099b..9f066185215 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -184,6 +184,36 @@ public function testWithExecutionTime() $this->assertSame(' in 1,234.56ms', $task->buffer[0]); } + public function testTaskSkipping() + { + $task = new BufferedTestBuildTask(); + + $task->mockHandle(function (BufferedTestBuildTask $task) { + $task->skip(); + }); + + $task->run(); + + $this->assertSame(0, $task->property('exitCode')); + $this->assertSame('Skipped', $task->buffer[1]); + $this->assertSame(' > Task was skipped', $task->buffer[2]); + } + + public function testTaskSkippingWithCustomMessage() + { + $task = new BufferedTestBuildTask(); + + $task->mockHandle(function (BufferedTestBuildTask $task) { + $task->skip('Custom reason'); + }); + + $task->run(); + + $this->assertSame(0, $task->property('exitCode')); + $this->assertSame('Skipped', $task->buffer[1]); + $this->assertSame(' > Custom reason', $task->buffer[2]); + } + public function testExceptionHandling() { $task = new BufferedTestBuildTask(); @@ -217,7 +247,7 @@ class InspectableTestBuildTask extends BuildTask public function handle(): void { if (isset($this->mockHandle)) { - ($this->mockHandle)(); + ($this->mockHandle)($this); } else { $this->wasHandled = true; } From d2860af1275c441ecb9a6c7d0a813a89c0371ccb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 13 Apr 2024 11:57:25 +0200 Subject: [PATCH 48/55] Clean up test setup --- .../tests/Unit/BuildTaskUnitTest.php | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/framework/tests/Unit/BuildTaskUnitTest.php b/packages/framework/tests/Unit/BuildTaskUnitTest.php index 9f066185215..1a0d540cb2b 100644 --- a/packages/framework/tests/Unit/BuildTaskUnitTest.php +++ b/packages/framework/tests/Unit/BuildTaskUnitTest.php @@ -186,14 +186,12 @@ public function testWithExecutionTime() public function testTaskSkipping() { - $task = new BufferedTestBuildTask(); - - $task->mockHandle(function (BufferedTestBuildTask $task) { - $task->skip(); + $task = tap(new BufferedTestBuildTask(), function (BufferedTestBuildTask $task) { + $task->mockHandle(function (BufferedTestBuildTask $task) { + $task->skip(); + })->run(); }); - $task->run(); - $this->assertSame(0, $task->property('exitCode')); $this->assertSame('Skipped', $task->buffer[1]); $this->assertSame(' > Task was skipped', $task->buffer[2]); @@ -201,14 +199,12 @@ public function testTaskSkipping() public function testTaskSkippingWithCustomMessage() { - $task = new BufferedTestBuildTask(); - - $task->mockHandle(function (BufferedTestBuildTask $task) { - $task->skip('Custom reason'); + $task = tap(new BufferedTestBuildTask(), function (BufferedTestBuildTask $task) { + $task->mockHandle(function (BufferedTestBuildTask $task) { + $task->skip('Custom reason'); + })->run(); }); - $task->run(); - $this->assertSame(0, $task->property('exitCode')); $this->assertSame('Skipped', $task->buffer[1]); $this->assertSame(' > Custom reason', $task->buffer[2]); @@ -278,9 +274,11 @@ public function mockClock(float $time): void $this->mockedEndTime = $time; } - public function mockHandle(Closure $handle): void + public function mockHandle(Closure $handle): static { $this->mockHandle = $handle; + + return $this; } protected function stopClock(): float From 967803e651e29bfce21605bbac00095cc8a45946 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 13 Apr 2024 12:01:45 +0200 Subject: [PATCH 49/55] Document that method halts execution --- .../framework/src/Framework/Features/BuildTasks/BuildTask.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php index 281e49361f9..76a50aa382e 100644 --- a/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php +++ b/packages/framework/src/Framework/Features/BuildTasks/BuildTask.php @@ -91,7 +91,7 @@ public function writeln(string $message): void } /** - * Write a fluent message to the output that the task is skipping. + * Write a fluent message to the output that the task is skipping and halt the execution. * * @throws \Hyde\Framework\Features\BuildTasks\BuildTaskSkippedException */ From 3189eb32f1f9086c3f2616527b181ad2337daa1d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 13 Apr 2024 12:35:24 +0200 Subject: [PATCH 50/55] Fix extra parentheses in Blade code Fixes an issue introduced in https://github.com/hydephp/develop/pull/1652 --- .../views/components/navigation/theme-toggle-button.blade.php | 2 +- packages/framework/resources/views/layouts/head.blade.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/resources/views/components/navigation/theme-toggle-button.blade.php b/packages/framework/resources/views/components/navigation/theme-toggle-button.blade.php index d5732f996ad..2b21c1f9528 100644 --- a/packages/framework/resources/views/components/navigation/theme-toggle-button.blade.php +++ b/packages/framework/resources/views/components/navigation/theme-toggle-button.blade.php @@ -1,4 +1,4 @@ -@if(Features::hasDarkmode())) +@if(Features::hasDarkmode())