TL; DR: This RFC it's about the $this
binding of the given closures to the underlying PHPUnit test case class - It proposes the uses
function that you can use to instruct Pest to use a specific class/trait at a file-level or directory-level.
Summary
By default, every given closure to globals functions of Pest is always bound to the current test case. As an example, on the code below, the $this
is bound to PhpUnit\Framework\TestCase
.
it('has welcome page', function () {
echo get_class($this); // Uses PhpUnit\Framework\TestCase
});
The goal of binding the closure to $this
is giving access to underlying functionalities that may exist in current test case classes of the PHP Ecosystem - just like the Laravel's test case class.
To swap the underlying test case class, Pest proposes the uses
function that can instruct Pest to use a specific class/trait a file level or at directory level. Here is an example with the Laravel Framework:
// Tests/Pest.php:
uses(TestCase::class, RefreshDatabase::class)->in('Features');
// Tests/Features/Example.php:
it('has welcome page', function () {
echo get_class($this); // Uses Tests\TestCase + RefreshDatabase trait
});
Of course, you can also improve the granularity of this, using the uses
function at file level:
// Tests/Pest.php:
uses(TestCase::class)->in('Features');
// Tests/Features/Example.php:
uses(RefreshDatabase::class);
it('has welcome page', function () {
echo get_class($this); // Uses Tests\TestCase + RefreshDatabase trait
});
One of the main goals of Pest is to provide seamless integration with Laravel. Everything should fell really natural and sexy. Let me know what do you think?