-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
check()
function to ensure requirements are met
- Loading branch information
Showing
17 changed files
with
157 additions
and
19 deletions.
There are no files selected for viewing
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
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
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,46 @@ | ||
# Assertion | ||
|
||
## The `check()` function | ||
|
||
Castor provides a `check()` function to ensure some requirements are met: | ||
|
||
```php | ||
use Castor\Attribute\AsTask; | ||
use Symfony\Component\Process\ExecutableFinder; | ||
|
||
use function Castor\check; | ||
|
||
#[AsTask()] | ||
function git() | ||
{ | ||
check( | ||
'Check if Git is installed', | ||
'Git is not installed. Please install it before.', | ||
fn () => (new ExecutableFinder())->find('git'), | ||
); | ||
} | ||
``` | ||
|
||
## The `ProblemException` exception | ||
|
||
If you must stop a task execution because of a problem, you can throw a | ||
`ProblemException` exception: | ||
|
||
```php | ||
use Castor\Attribute\AsTask; | ||
use Castor\Exception\ProblemException; | ||
|
||
use function Castor\capture; | ||
|
||
#[AsTask()] | ||
function git() | ||
{ | ||
if (capture('git status --porcelain')) { | ||
throw new ProblemException('There are uncommitted changes.'); | ||
} | ||
} | ||
``` | ||
|
||
It will stop the execution of the task and display the message in the console. | ||
And it will also return a non-zero exit code (default to 1) to indicate that the | ||
task failed. |
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
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
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,24 @@ | ||
<?php | ||
|
||
namespace assertion; | ||
|
||
use Castor\Attribute\AsTask; | ||
use Castor\Exception\ProblemException; | ||
|
||
use function Castor\check; | ||
|
||
#[AsTask(description: 'Ensure we are in the future')] | ||
function ensure_we_are_in_the_future(): void | ||
{ | ||
check( | ||
'Check if we are in the future', | ||
'We are not in the future 😱', | ||
fn () => (!usleep(500_000) && new \DateTime() > new \DateTime('2015-10-21')) | ||
); | ||
} | ||
|
||
#[AsTask(description: 'Throws a Problem exception')] | ||
function throw_an_exception(): never | ||
{ | ||
throw new ProblemException('Houston, we have a problem'); | ||
} |
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
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
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
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,22 @@ | ||
<?php | ||
|
||
namespace Castor\Tests\Generated; | ||
|
||
use Castor\Tests\TaskTestCase; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
|
||
class AssertionEnsureWeAreInTheFutureTest extends TaskTestCase | ||
{ | ||
// assertion:ensure-we-are-in-the-future | ||
public function test(): void | ||
{ | ||
$process = $this->runTask(['assertion:ensure-we-are-in-the-future']); | ||
|
||
if (0 !== $process->getExitCode()) { | ||
throw new ProcessFailedException($process); | ||
} | ||
|
||
$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput()); | ||
$this->assertSame('', $process->getErrorOutput()); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
tests/Generated/AssertionEnsureWeAreInTheFutureTest.php.output.txt
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 @@ | ||
Check if we are in the future ✅ |
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,22 @@ | ||
<?php | ||
|
||
namespace Castor\Tests\Generated; | ||
|
||
use Castor\Tests\TaskTestCase; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
|
||
class AssertionThrowAnExceptionTest extends TaskTestCase | ||
{ | ||
// assertion:throw-an-exception | ||
public function test(): void | ||
{ | ||
$process = $this->runTask(['assertion:throw-an-exception']); | ||
|
||
if (1 !== $process->getExitCode()) { | ||
throw new ProcessFailedException($process); | ||
} | ||
|
||
$this->assertStringEqualsFile(__FILE__ . '.output.txt', $process->getOutput()); | ||
$this->assertSame('', $process->getErrorOutput()); | ||
} | ||
} |
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,2 @@ | ||
[ERROR] Houston, we have a problem | ||
|
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 |
---|---|---|
@@ -1 +1 @@ | ||
Number of PHP files: 34 | ||
Number of PHP files: 35 |
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
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
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