|
1 | | -# Given When Then (GWT) Plugin for Pest |
| 1 | +# PSpec |
2 | 2 |
|
3 | | -> A simple API allows you to structure your tests focused on the behaviour. Given-When-Then separation makes the test easier to understand at a glance. |
| 3 | +> PSpec is a Pest plugin for composing multi scenarios tests with a simple API, based on RSpec let. |
4 | 4 |
|
5 | 5 | ### Install |
6 | 6 | ```shell |
7 | | -composer require milroyfraser/pest-plugin-gwt --dev |
| 7 | +composer require cfx/pspec --dev |
8 | 8 | ``` |
9 | 9 |
|
10 | | -### Usage |
| 10 | +### Simple usage |
11 | 11 | ```php |
12 | | -use App\Exceptions\BlockedUserException; |
13 | | -use App\Models\User; |
14 | | -use function Pest\Gwt\scenario; |
15 | | -use function Pest\Laravel\assertDatabaseHas; |
16 | | - |
17 | | -scenario('activate user') |
18 | | - ->given(fn() => User::factory()->create()) |
19 | | - ->when(fn(User $user) => $user->activate()) |
20 | | - ->then(fn(User $user) => assertDatabaseHas('users', [ |
21 | | - 'id' => $user->id, |
22 | | - 'activated' => true, |
23 | | - ])); |
24 | | - |
25 | | -scenario('activate blocked user') |
26 | | - ->given(fn() => User::factory()->blocked()->create()) |
27 | | - ->when(fn(User $user) => $user->activate()) |
28 | | - ->throws(BlockedUserException::class); |
29 | | -``` |
| 12 | +use function Cfx\PSpec\context; |
| 13 | +use function Cfx\PSpec\expectSubject; |
| 14 | +use function Cfx\PSpec\get; |
| 15 | +use function Cfx\PSpec\let; |
| 16 | +use function Cfx\PSpec\subject; |
30 | 17 |
|
31 | | -[more examples](https://github.com/milroyfraser/pest-plugin-gwt/blob/master/tests/Example.php) |
| 18 | +subject(fn () => User::factory()->create(['is_admin' => get('is_admin')])); |
32 | 19 |
|
| 20 | +context('when is admin', function () { |
| 21 | + let('is_admin', fn() => true); |
33 | 22 |
|
34 | | -**Given** a state |
| 23 | + it('returns true', function () { |
| 24 | + expectSubject()->is_admin->toBeTrue(); |
| 25 | + }); |
| 26 | +}); |
35 | 27 |
|
36 | | -Given method accepts a `Closure`. This is where we `Arrange` application state. The return values will become argument/s of the `when` closure. |
| 28 | +context('when is not admin', function () { |
| 29 | + let('is_admin', fn() => false); |
37 | 30 |
|
38 | | -**When** I do something |
| 31 | + it('returns false', function () { |
| 32 | + expectSubject()->is_admin->toBeFalse(); |
| 33 | + }); |
| 34 | +}); |
| 35 | +``` |
39 | 36 |
|
40 | | -When method accepts a `Closure`. This is where we `Act` (perform) the operation. The return values will become argument/s of the `then` closure. |
| 37 | +### Higher order testing |
41 | 38 |
|
42 | | -**Then** I expect an outcome |
| 39 | +```php |
| 40 | +use function Cfx\PSpec\context; |
| 41 | +use function Cfx\PSpec\getSubject; |
| 42 | +use function Cfx\PSpec\let; |
43 | 43 |
|
44 | | -Then method accepts a `Closure`. This is where we `Assert` the outcome. |
| 44 | +context('when using high order testing', function () { |
| 45 | + let('param2', fn () => 2); |
| 46 | + |
| 47 | + it('can use high order testing') |
| 48 | + ->expect(getSubject(...)) |
| 49 | + ->toEqual(2); |
| 50 | +}); |
| 51 | +``` |
45 | 52 |
|
46 | | -> If you want to start testing your application with Pest, visit the main **[Pest Repository](https://github.com/pestphp/pest)**. |
| 53 | +[more examples](https://github.com/coderfoxbrasil/cfx-pspec/blob/master/tests/Example.php) |
47 | 54 |
|
48 | | -- Explore the docs: **[pestphp.com/docs/plugins/creating-plugins »](https://pestphp.com/docs/plugins/creating-plugins)** |
49 | | -- Follow us on Twitter: **[@pestphp »](https://twitter.com/pestphp)** |
50 | | -- Join us on the Discord Server: **[discord.gg/bMAJv82 »](https://discord.gg/bMAJv82)** |
51 | 55 |
|
52 | | -Pest was created by **[Nuno Maduro](https://twitter.com/enunomaduro)** under the **[Sponsorware license](https://github.com/sponsorware/docs)**. It got open-sourced and is now licensed under the **[MIT license](https://opensource.org/licenses/MIT)**. |
|
0 commit comments