generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the ability to fake registered checks.
- Loading branch information
1 parent
a258670
commit d45d9c3
Showing
5 changed files
with
188 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Spatie\Health\Testing; | ||
|
||
use Spatie\Health\Checks\Check; | ||
use Spatie\Health\Checks\Result; | ||
|
||
class FakeCheck extends Check | ||
{ | ||
private Check $fakedCheck; | ||
private FakeValues $fakeValues; | ||
|
||
public static function result(Result $result, bool $shouldRun = null): FakeValues | ||
{ | ||
return new FakeValues($result, $shouldRun); | ||
} | ||
|
||
public function fake(Check $fakedCheck, FakeValues $values): self | ||
{ | ||
$this->fakedCheck = $fakedCheck; | ||
$this->fakeValues = $values; | ||
|
||
$this->name($fakedCheck->getName()); | ||
$this->label($fakedCheck->getLabel()); | ||
|
||
return $this; | ||
} | ||
|
||
public function shouldRun(): bool | ||
{ | ||
return $this->fakeValues->shouldRun() === null | ||
? $this->fakedCheck->shouldRun() | ||
: $this->fakeValues->shouldRun(); | ||
} | ||
|
||
public function onTerminate(mixed $request, mixed $response): void | ||
{ | ||
$this->fakedCheck->onTerminate($request, $response); | ||
} | ||
|
||
public function run(): Result | ||
{ | ||
return $this->fakeValues->getResult(); | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
namespace Spatie\Health\Testing; | ||
|
||
use Closure; | ||
use Illuminate\Support\Collection; | ||
use Spatie\Health\Checks\Check; | ||
use Spatie\Health\Checks\Result; | ||
use Spatie\Health\Health; | ||
|
||
class FakeHealth extends Health | ||
{ | ||
/** | ||
* @param array<class-string<Check>, Result|FakeValues|(Closure(Check): Result|FakeValues)> $fakeChecks | ||
*/ | ||
public function __construct(private Health $decoratedHealth, private array $fakeChecks) | ||
{ | ||
} | ||
|
||
public function registeredChecks(): Collection | ||
{ | ||
return $this->decoratedHealth->registeredChecks()->map( | ||
fn(Check $check) => array_key_exists($check::class, $this->fakeChecks) | ||
? $this->buildFakeCheck($check, $this->fakeChecks[$check::class]) | ||
: $check | ||
); | ||
} | ||
|
||
/** | ||
* @param Result|FakeValues|(Closure(Check): Result|FakeValues) $result | ||
*/ | ||
protected function buildFakeCheck(Check $decoratedCheck, Result|FakeValues|Closure $result): FakeCheck | ||
{ | ||
// @phpstan-ignore-next-line | ||
$result = FakeValues::from(value($result, $decoratedCheck)); | ||
|
||
return FakeCheck::new()->fake($decoratedCheck, $result); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace Spatie\Health\Testing; | ||
|
||
use Spatie\Health\Checks\Result; | ||
|
||
class FakeValues | ||
{ | ||
public function __construct( | ||
private Result $result, | ||
private ?bool $shouldRun = null, | ||
) | ||
{ | ||
} | ||
|
||
public static function from(Result|self $values): self | ||
{ | ||
if ($values instanceof Result) { | ||
return new self($values); | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
public function getResult(): Result | ||
{ | ||
return $this->result; | ||
} | ||
|
||
public function shouldRun(): ?bool | ||
{ | ||
return $this->shouldRun; | ||
} | ||
} |
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