pest
pest copied to clipboard
[Bug]: `DescribeCall` class is missing `with` method
What Happened
When I use describe()->with() in my tests, I get an error:
ArgumentCountError
Too few arguments to function PHPUnit\Runner\TestSuiteLoader::{closure}(),
0 passed in ~/Desktop/my-org/my-app/vendor/pestphp/pest/src/PendingCalls/DescribeCall.php
on line 56 and exactly 1 expected
How to Reproduce
- Install PHP 8.3.12 (
brew install php:8.3.12 && composer require php:8.3.12) - Install Pest 3.2.5 (
composer require --dev pestphp/pest:3.2.5 -W) - Add a test file with the code below (
touch ./tests/MyTest.php) - Run tests (
./vendor/bin/pest)
tests/MyTest.php:
<?php
declare(strict_types=1);
function getRequestMethod(): ?string {
return $_SERVER['REQUEST_METHOD'];
}
describe('when the http request method is set', function(string $method) {
$server = $_SERVER;
beforeEach(fn() => ($_SERVER['REQUEST_METHOD'] = $method));
afterEach(fn() => ($_SERVER = $server));
it('should get the request method', fn() =>
expect(getRequestMethod())->toBe($method));
})->with([
'CONNECT',
'DELETE',
'GET',
'HEAD',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'TRACE',
]);
describe('when the http request method is not set', function() {
$server = $_SERVER;
beforeEach(function() {
unset($_SERVER['REQUEST_METHOD']);
});
afterEach(fn() => ($_SERVER = $server));
it('should get null', fn() => expect(getRequestMethod())->toBeNull());
});
Here is the result:
composer test -- --filter=My
> Composer\Config::disableProcessTimeout
> pest '--filter=My'
ArgumentCountError
Too few arguments to function PHPUnit\Runner\TestSuiteLoader::{closure}(), 0 passed in ~/Desktop/my-org/my-app/vendor/pestphp/pest/src/PendingCalls/DescribeCall.php on line 56 and exactly 1 expected
at tests/Unit/MyTest.php:9
5▕ function getRequestMethod(): ?string {
6▕ return $_SERVER['REQUEST_METHOD'];
7▕ }
8▕
➜ 9▕ describe('when the http request method is set', function(string $method) {
10▕ $server = $_SERVER;
11▕
12▕ beforeEach(fn() => ($_SERVER['REQUEST_METHOD'] = $method));
13▕ afterEach(fn() => ($_SERVER = $server));
+1 vendor frames
2 tests/Unit/MyTest.php:17
Pest\PendingCalls\DescribeCall::__destruct()
Script pest handling the test event returned with error code 1
Sample Repository
https://github.com/paulshryock/pest-describe-missing-with-method
Pest Version
3.2.5
PHP Version
8.3.12
Operation System
macOS 14.4.1
Notes
Updated example and tested with latest versions of PHP and Pest on 2024-10-06.
Honestly neither of these examples is wonderful, having to pass variables down through use scope declarations, but I guess that's a limitation of PHP.