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.06k stars 313 forks source link

Interpolated dataset names #1146

Closed JHWelch closed 2 months ago

JHWelch commented 2 months ago

What:

Description:

This PR is to add the functionality I described in a recent discussion thread.

This PR is solving the problem that though we can name our datasets, we are not able to take a step further and use those dataset names fluently inside of a test name.

To do this you add :dataset into your test name, and the dataset name will be interpolated into the test name string. Similar to Laravel's :attribute inside validation strings

I am very open to any changes!

I targeted 3.x because this would be a breaking change, even if :dataset is unlikely.

Contrived Example

Before

Test

it('can sort by all fields', function (/* ... */) {
    /* ... */
})->with([
    'name' => [ /* ... */ ],
    'email' => [ /* ... */ ],
    'created_at' => [ /* ... */]
]);

Pest output

   PASS  Tests\Feature\TableSortTest
  ✓ it can sort by all fields with dataset "name"                                                                                                            
  ✓ it can sort by all fields with dataset "email"
  ✓ it can sort by all fields with dataset "created_at"

After

Test

it('can sort by :dataset field', function (/* ... */) {
    /* ... */
})->with([
    'name' => [ /* ... */ ],
    'email' => [ /* ... */ ],
    'created_at' => [ /* ... */]
]);

Pest output

   PASS  Tests\Feature\TableSortTest
  ✓ it can sort by "name" field                                                                                                     
  ✓ it can sort by "email" field
  ✓ it can sort by "created_at" field

Questions

I'm not a huge fan of where this functionality is, but I'm not sure where else it could be refactored to. Especially needing to remove the 'dataset ' that is already on the name. However any other approach would require significantly more refactoring I believe.