Skip to content

Commit 4261257

Browse files
Update AdvancedUsage.md: Before/After improvements (#888)
* Update AdvancedUsage.md: Before/After improvments Page: https://codeception.com/docs/AdvancedUsage#BeforeAfter-Attributes This is the most important change: > The referenced method needs to be in the same class as the public function. I took the info from here: https://github.com/Codeception/Codeception/blob/main/src/Codeception/Subscriber/BeforeAfterTest.php#L49 - please doublecheck. * Update AdvancedUsage.md
1 parent c015ee2 commit 4261257

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

docs/AdvancedUsage.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,8 @@ class PageCest
344344

345345
## Before/After Attributes
346346

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

351350
```php
352351
<?php
@@ -356,9 +355,9 @@ use Codeception\Attribute\Before;
356355
use Codeception\Attribute\After;
357356
use Tests\Support\FunctionalTester;
358357

359-
class ModeratorCest
358+
final class ModeratorCest
360359
{
361-
protected function login(AcceptanceTester $I)
360+
protected function login(FunctionalTester $I): void
362361
{
363362
$I->amOnPage('/login');
364363
$I->fillField('Username', 'miles');
@@ -367,21 +366,26 @@ class ModeratorCest
367366
}
368367

369368
#[Before('login')]
370-
public function banUser(AcceptanceTester $I)
369+
public function banUser(FunctionalTester $I): void
371370
{
372371
$I->amOnPage('/users/charlie-parker');
373372
$I->see('Ban', '.button');
374373
$I->click('Ban');
375374
}
376375

377-
// you can specify multiple before and after methods:
376+
// Multiple `#[Before]` or `#[After]` attributes are invoked in order from top to bottom:
377+
#[Before('login')]
378+
#[Before('cleanup')]
379+
public function addUser(FunctionalTester $I): void
380+
{
381+
// ...
382+
}
383+
384+
// But you can also pass multiple methods in each attribute:
378385
#[Before('login', 'cleanup')]
379-
#[After('logout', 'close')]
380-
public function addUser(AcceptanceTester $I)
386+
public function addUser(FunctionalTester $I): void
381387
{
382-
$I->amOnPage('/users/charlie-parker');
383-
$I->see('Ban', '.button');
384-
$I->click('Ban');
388+
// ...
385389
}
386390
}
387391
```

0 commit comments

Comments
 (0)