pest icon indicating copy to clipboard operation
pest copied to clipboard

[Bug]: `DescribeCall` class is missing `with` method

Open paulshryock opened this issue 2 years ago • 1 comments

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

  1. Install PHP 8.3.12 (brew install php:8.3.12 && composer require php:8.3.12)
  2. Install Pest 3.2.5 (composer require --dev pestphp/pest:3.2.5 -W)
  3. Add a test file with the code below (touch ./tests/MyTest.php)
  4. 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.

paulshryock avatar Nov 14 '23 17:11 paulshryock

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.

paulshryock avatar Nov 14 '23 17:11 paulshryock