Created
January 17, 2022 01:04
-
-
Save adrianspacely/9f0bfb50fd9962bff5f0636c13c7696a to your computer and use it in GitHub Desktop.
How I add "private functions" to a pest test file.
This file contains hidden or 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
<?php | |
# | |
# 1. Add the Macroable trait to your base test. | |
# | |
namespace Tests; | |
use Illuminate\Foundation\Testing\TestCase as BaseTestCase; | |
use Illuminate\Support\Traits\Macroable; | |
... | |
abstract class TestCase extends BaseTestCase | |
{ | |
... | |
use Macroable; | |
... | |
} |
This file contains hidden or 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
<?php | |
# | |
# 2. Add these global helpers (just for a neat developer experience) | |
# | |
use Illuminate\Support\Arr; | |
use Illuminate\Support\Traits\Macroable; | |
function private_function(string $name, callable $callback) | |
{ | |
macro($name, $callback, $frame = 2); | |
} | |
function macro(string $name, callable $callback, int $frame = 1) | |
{ | |
$class = Arr::get(debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, $frame + 1), "{$frame}.class", ''); | |
$macroable = class_exists($class) && collect() | |
->wrap($class) | |
->merge(class_parents($class)) | |
->map(fn (string $class) => in_array(Macroable::class, class_uses($class))) | |
->contains(true); | |
throw_unless($macroable, new Exception('You must call macro from a macroable class.')); | |
$class::macro($name, $callback); | |
} |
This file contains hidden or 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
<?php | |
# | |
# 3. I use my `private_function` helper to show that this test file has a function called input. | |
# You could also do `macro('input', fn)` or `$this->macro('input', fn)`. I just prefer | |
# `private_function('input', fn)` for clear intent that you can call `$this->input(...)`. | |
# | |
namespace Tests\Feature; | |
beforeEach(function () { | |
private_function('input', function (array $input = []) { | |
return array_merge([ | |
'name' => 'Happy Path Inputs', | |
'email' => '[email protected]', | |
], $input); | |
}); | |
}); | |
test('example', function () { | |
$response = $this->post('/example', $this->input([ | |
'email' => '[email protected]', | |
])); | |
$response->assertSuccessful(); | |
expect(Example::count())->toBe(1); | |
$example = Example::first(); | |
expect($example->name)->toBe('Happy Path Inputs'); | |
expect($example->email)->toBe('[email protected]'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment