pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.07k stars 315 forks source link

[Bug]: Pest errors if namespaced class name string appears multiple times inside a single dataset #1115

Open bakerkretzmar opened 3 months ago

bakerkretzmar commented 3 months ago

What Happened

If more than one of the values in a single dataset are namespaced class strings Pest fails or errors.

How to Reproduce

The following test causes Pest to error out with the message Typed static property P\Tests\Feature\ExampleTest::$__latestDescription must not be accessed before initialization:

use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Model;

test('returns a successful response', function (string $first) {
    expect(true)->toBeTrue();
})->with([Model::class, Controller::class]);

Changing either Model::class or Controller::class to a literal string (e.g. 'App\Model') fixes that particular example, but this doesn't always work.

Sample Repository

No response

Pest Version

2.34.4

PHP Version

8.2.15

Operation System

macOS

Notes

This doesn't seem to reproduce consistently. I can make it fail with some class name strings but not others, sometimes using ::class notation and sometimes with literal strings, etc.

faissaloux commented 3 months ago

I can confirm the issue in your first example.

In your second example it's working as it is supposed to, actually you are passing only one argument.

It's going through the dataset one by one so it is providing only one argument in your case (Controller::class in first iteration then Model::class in second). If you need to pass two your need to rewrite your dataset like:

[
    ['controller', Controller::class],
    ['model', Model::class]
    .
    .
    .
]

In this case

bakerkretzmar commented 3 months ago

Ah thanks you're right, missed that. I'll update the examples.

bakerkretzmar commented 3 months ago

I think this might be happening because Pest is trying to parse any data providers that are 2-element arrays as callables...

bakerkretzmar commented 3 months ago

Works fine:

test('returns a successful response', function (string $first, string $second) {
    expect(true)->toBeTrue();
})->with([Controller::class])->with([User::class, 'update']);

Errors:

test('returns a successful response', function (string $first, string $second) {
    expect(true)->toBeTrue();
})->with([Controller::class])->with([User::class, 'notAMethodOnTheUserClass']);