Skip to content

Update AdvancedUsage.md: Before/After improvements #888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions docs/AdvancedUsage.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,8 @@ class PageCest

## Before/After Attributes

You can control execution flow with `#[Before]` and `#[After]` attributes. You may move common actions
into protected (i.e. non-test) methods and invoke them before or after the test method by putting them into attributes.
When adding multiple `#[Before]` or `#[After]` attributes, methods are invoked in order from top to bottom.
You may move common actions into private/protected (i.e. non-test) methods and invoke them before or after the test method
by passing them as `#[Before]` and `#[After]` attributes. The referenced method needs to be in the same class as the public function.

```php
<?php
Expand All @@ -356,9 +355,9 @@ use \Tests\Support\FunctionalTester;
use Codeception\Attribute\Before;
use Codeception\Attribute\After;

class ModeratorCest {

protected function login(AcceptanceTester $I)
final class ModeratorCest
{
protected function login(FunctionalTester $I): void
{
$I->amOnPage('/login');
$I->fillField('Username', 'miles');
Expand All @@ -367,21 +366,26 @@ class ModeratorCest {
}

#[Before('login')]
public function banUser(AcceptanceTester $I)
public function banUser(FunctionalTester $I): void
{
$I->amOnPage('/users/charlie-parker');
$I->see('Ban', '.button');
$I->click('Ban');
}

// you can specify multiple before and after methods:
// Multiple `#[Before]` or `#[After]` attributes are invoked in order from top to bottom:
#[Before('login')]
#[Before('cleanup')]
public function addUser(FunctionalTester $I): void
{
// ...
}

// But you can also pass multiple methods in each attribute:
#[Before('login', 'cleanup')]
#[After('logout', 'close')]
public function addUser(AcceptanceTester $I)
public function addUser(FunctionalTester $I): void
{
$I->amOnPage('/users/charlie-parker');
$I->see('Ban', '.button');
$I->click('Ban');
// ...
}
}
```
Expand Down