pest icon indicating copy to clipboard operation
pest copied to clipboard

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

Open bakerkretzmar opened this issue 1 year ago • 4 comments

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.

bakerkretzmar avatar Mar 15 '24 18:03 bakerkretzmar

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

  • first iteration: the first argument gonna be 'controller' and the second Controller::class.
  • second iteration: the first argument gonna be 'model' and Model::class for the second. and so on

faissaloux avatar Mar 15 '24 18:03 faissaloux

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

bakerkretzmar avatar Mar 15 '24 18:03 bakerkretzmar

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

bakerkretzmar avatar Mar 15 '24 18:03 bakerkretzmar

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']);

bakerkretzmar avatar Mar 15 '24 19:03 bakerkretzmar