Skip to content

Commit

Permalink
Adds Pest 3 docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Sep 9, 2024
1 parent 30a7b64 commit 41a78ca
Show file tree
Hide file tree
Showing 17 changed files with 607 additions and 97 deletions.
355 changes: 330 additions & 25 deletions arch-testing.md

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions configuring-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ it('has home', function () {
});
```

However, you may associate a specific folder or even your entire test suite with another base test case class, thus changing the value of `$this` within tests. To accomplish this, you can utilize the `uses()` and `in()` functions within your `Pest.php` configuration file.
However, you may associate a specific folder or even your entire test suite with another base test case class, thus changing the value of `$this` within tests. To accomplish this, you can utilize the `pest()` function and the `in()` method within your `Pest.php` configuration file.

```php
// tests/Pest.php
uses(Tests\TestCase::class)->in('Feature');
pest()->extend(Tests\TestCase::class)->in('Feature');

// tests/Feature/ExampleTest.php
it('has home', function () {
Expand All @@ -33,7 +33,7 @@ Additionally, Pest supports [glob patterns](https://www.php.net/manual/en/functi

```php
// tests/Pest.php
uses(Tests\TestCase::class)->in('Feature/*Job*.php');
pest()->extend(Tests\TestCase::class)->in('Feature/*Job*.php');

// This will apply the Tests\TestCase to all test files in the "Feature" directory that contains "Job" in their filename.
```
Expand All @@ -42,10 +42,10 @@ Another more complex example would be using a pattern to match multiple director

```php
// tests/Pest.php
uses(
DuskTestCase::class,
DatabaseMigrations::class
)->in('../Modules/*/Tests/Browser');
pest()
->extend(DuskTestCase::class)
->use(DatabaseMigrations::class)
->in('../Modules/*/Tests/Browser');

// This will apply the DuskTestCase class and the DatabaseMigrations trait to all test files within any module's "Browser" directory.
```
Expand All @@ -65,29 +65,29 @@ class TestCase extends BaseTestCase
}

// tests/Pest.php
uses(TestCase::class)->in('Feature');
pest()->extend(TestCase::class)->in('Feature');

// tests/Feature/ExampleTest.php
it('has home', function () {
$this->performThis();
});
```

A trait can be linked to a test or folder, much like classes. For instance, in Laravel, you can employ the `RefreshDatabase` trait to reset the database prior to each test. To include the trait in your test, pass the trait's name to the `uses()` function.
A trait can be linked to a test or folder, much like classes. For instance, in Laravel, you can employ the `RefreshDatabase` trait to reset the database prior to each test. To include the trait in your test, pass the trait's name to the `pest()->use()` method.

```php
<?php

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

uses(TestCase::class, RefreshDatabase::class)->in('Feature');
pest()->extend(TestCase::class)->use(RefreshDatabase::class)->in('Feature');
```

To associate a particular test with a specific test case class or trait, you can utilize the `uses()` function **within that specific test file**, omitting the use of the `in()` method.
To associate a particular test with a specific test case class or trait, you can utilize the `pest()->extend()` and `pest()->use()` methods **within that specific test file**, omitting the use of the `in()` method.

```php
uses(Tests\MySpecificTestCase::class);
pest()->extend(Tests\MySpecificTestCase::class);

it('has home', function () {
echo get_class($this); // \Tests\MySpecificTestCase
Expand Down
21 changes: 21 additions & 0 deletions creating-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@ test('plugin example', function () {

Custom expectations can be incorporated into your plugin's `Autoload.php` file. For information on how to build custom expectations, please refer to the comprehensive documentation on [Custom Expectations](/docs/custom-expectations).

## Adding Arch Presets

If your plugin provides a custom Arch preset, you can define it within the `Autoload.php` file.

```php
pest()->preset('ddd', function () {
return [
expect('Infrastructure')->toOnlyBeUsedIn('Application'),
expect('Domain')->toOnlyBeUsedIn('Application'),
];
});
```

Optionally, may have access to the application PSR-4 namespaces on the first argument of your closure's callback.

```php
pest()->preset('silex', function (array $userNamespaces) {
dump($userNamespaces); // ['App\\']
});
```

---

As you can see, crafting plugins on Pest can serve as a fantastic starting point for your open-source endeavors! In the next chapter, we will explore the concept of "Higher Order Testing": [Higher Order Testing](/docs/higher-order-testing)
2 changes: 1 addition & 1 deletion custom-helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class TestCase extends BaseTestCase
}

// tests/Pest.php
uses(TestCase::class)->in('Feature');
pest()->extend(TestCase::class)->in('Feature');

// tests/Feature/PaymentsTest.php
it('may buy a book', function () {
Expand Down
1 change: 1 addition & 0 deletions documentation.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- ## Press
- [Pest v3 Now Available](/docs/pest3-now-available)
- [Announcing Stressless](/docs/announcing-stressless)
- [Pest's Spicy Summer Release](/docs/pest-spicy-summer-release)
- [Announcing Pest 2.0](/docs/announcing-pest2)
Expand Down
20 changes: 0 additions & 20 deletions expectations.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ With the Pest expectation API, you have access to an extensive collection of ind
- [`toContainEqual()`](#expect-toContainEqual)
- [`toContainOnlyInstancesOf()`](#expect-toContainOnlyInstancesOf)
- [`toHaveCount()`](#expect-toHaveCount)
- [`toHaveMethod()`](#expect-toHaveMethod)
- [`toHaveMethods()`](#expect-toHaveMethods)
- [`toHaveProperty()`](#expect-toHaveProperty)
- [`toHaveProperties()`](#expect-toHaveProperties)
- [`toMatchArray()`](#expect-toMatchArray)
Expand Down Expand Up @@ -290,24 +288,6 @@ This expectation ensures that the `$count` provided matches the number of elemen
expect(['Nuno', 'Luke', 'Alex', 'Dan'])->toHaveCount(4);
```

<a name="expect-toHaveMethod"></a>
### `toHaveMethod(string $name)`

This expectation ensures that `$value` has a method named `$name`.

```php
expect($user)->toHaveMethod('getFullname');
```

<a name="expect-toHaveMethods"></a>
### `toHaveMethods(iterable $names)`

This expectation ensures that `$value` has all the methods contained in `$names`.

```php
expect($user)->toHaveMethods(['getFullname', 'isAuthenticated']);
```

<a name="expect-toHaveProperty"></a>
### `toHaveProperty(string $name, $value = null)`

Expand Down
8 changes: 4 additions & 4 deletions global-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ As previously discussed, hooks allow you to simplify your testing process and au
For instance, if you need to perform some database operations before each test within the `Feature` folder, you may use the `beforeEach()` hook within your `Pest.php` configuration file.

```php
uses(TestCase::class)->beforeEach(function () {
pest()->extend(TestCase::class)->beforeEach(function () {
// Interact with your database...
})->group('integration')->in('Feature');
```

In addition, you can define global hooks that will run before or after your entire test suite, regardless of the folder or group.

```php
uses()->beforeEach(function () {
pest()->beforeEach(function () {
// Interact with your database...
})->in(__DIR__); // All folders, and all groups...
});
```

In fact, any of the hooks mentioned in the [hooks](/docs/hooks) documentation can also be used in your `Pest.php` configuration file.

```php
uses(TestCase::class)->beforeAll(function () {
pest()->extend(TestCase::class)->beforeAll(function () {
// Runs before each file...
})->beforeEach(function () {
// Runs before each test...
Expand Down
6 changes: 3 additions & 3 deletions grouping-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ You can assign tests folders to various groups using Pest's `group()` method. As
For instance, consider the scenario where we assign the tests located in the `tests/Feature` folder to a group named "feature".

```php
uses(TestCase::class)
pest()->extend(TestCase::class)
->group('feature')
->in('Feature');
```
Expand All @@ -37,10 +37,10 @@ it('has home', function () {
})->group('feature', 'browser');
```

In some cases, you may want to assign a whole file to a group. To do so, you may combine the `uses()` and `group()` methods.
In some cases, you may want to assign a whole file to a group. To do so, you may use the `pest()->group()` method within the file.

```php
uses()->group('feature');
pest()->group('feature');

it('has home', function () {
//
Expand Down
12 changes: 12 additions & 0 deletions hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ afterEach(function () {
});
```

Optionally, you can use the `after()` method to perform clean-up tasks after a specific test. This is useful when you need to clean up resources that are specific to a single test, rather than shared across all tests in the file.

```php
it('may be created', function () {
$this->userRepository->create();

expect($user)->toBeInstanceOf(User::class);
})->after(function () {
$this->userRepository->reset();
});
```

<a name="beforeall"></a>
## `beforeAll()`

Expand Down
2 changes: 1 addition & 1 deletion optimizing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ You may even configure Pest to always use the compact printer so you don't have

```php
// tests/Pest.php
uses()->compact();
pest()->theme()->compact();

//
```
Expand Down
2 changes: 1 addition & 1 deletion plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,4 @@ pest --watch=app,routes,tests

---

In this section, we have seen how plugins can enhance your Pest experience. Now, let's dive into architectural testing and how it can benefit your development process. By performing architectural testing, you can evaluate the overall design of your application and identify potential flaws before they become significant issues: [Arch Testing](/docs/arch-testing)
In this section, we have seen how plugins can enhance your Pest experience. Now, let's see how you can manage your team's tasks and responsibilities using Pest: [Team Management](/docs/team-management)
22 changes: 0 additions & 22 deletions skipping-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,6 @@ beforeEach(function () {
})->skip();
```

## Creating Todos

While skipping tests can be a helpful way to exclude specific tests temporarily from your test suite, it can also lead to situations where skipped tests are forgotten or overlooked. To prevent this, Pest provide a way to create "todos", which are essentially placeholders for tests that need attention.

To begin working with todos, simply invoke the `todo()` method.

```php
it('has home')->todo();
```

If you invoke the `todo()` method on a test, Pest's output will inform you that the test is a todo so you don't forget about it.

<div class="code-snippet">
<img src="/assets/img/todo.webp?1" style="--lines: 5" />
</div>

You can easily view a list of pending todos contained in your test suite by including the `--todos` option when running Pest.

```bash
./vendor/bin/pest --todos
```

---

As your codebase expands, it's advisable to consider enhancing the speed of your test suite. To assist you with that, we offer comprehensive documentation on optimizing your test suite: [Optimizing Tests](/docs/optimizing-tests)
10 changes: 5 additions & 5 deletions support-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ We strive to resolve all reported bugs or issues to the best of our abilities as

Bug fixes will be available for outdated versions for a duration of two years following the latest version's release. The previous version will be regarded as outdated once a new version of Pest is released.

| Major Version | PHP Compatibility | Initial Release | Bug Fixes Until
| ---------------- | --- | --- | --- |
| Pest 3 | >= PHP 8.2 | February X, 2024 | To be determined
| Pest 2 | >= PHP 8.1 | March 20, 2023 | February X, 2026
| Pest 1 | >= PHP 7.3 | January 7, 2021 | March 20, 2025
| Major Version | PHP Compatibility | Initial Release | Bug Fixes Until
| ---------------- | --- |-------------------| --- |
| Pest 3 | >= PHP 8.2 | September 9, 2024 | To be determined
| Pest 2 | >= PHP 8.1 | March 20, 2023 | September 9, 2026
| Pest 1 | >= PHP 7.3 | January 7, 2021 | March 20, 2025

Pest adheres to semantic versioning principles, where the version number `x.y.z` conveys the following information:
- When issuing bug fixes, the `z` number is incremented (e.g., 3.10.2 to 3.10.3).
Expand Down
Loading

0 comments on commit 41a78ca

Please sign in to comment.