Adds the ability to fake registered checks. #135
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hey all!
Motivation
If you're a bit of a testing nut, like myself, you want to ensure that your health checks are registered correctly and throw the correct error status when in use. Up to now, this was not possible, because checks cannot be faked and will actually run as they would in production.
Implementation
To support this feature, I went down a similar route to
Event::fake
in Laravel. That is to say, we swap out the underlyingHealth
instance with aFakeHealth
instance that is able to take fake responses and use them for given health checks.Here is a basic example:
Advanced usage
There are occasions where it is necessary to have more control over the check. For example, imagine we want to override the
shouldRun
functionality, which by default defers to the original check. To satisfy this requirement, you may use theFakeCheck::result
method.In the above example, we might imagine that in the testing environment, the check would not run because
::shouldRun
returns false. We can force it to run by passingtrue
as the second parameter toFakeCheck::result
.Let's imagine another example, where we use multiple
DatabaseCheck
instances to check multiple connections. What if each connection should return a different result in the test? No problem, we can pass a Closure toHealth::fake
:I hope you find this as valuable as I will. Thanks for all the hard work you continue to do for the Laravel community!
Kind Regards,
Luke