Add ability to define multiple hooks for the same directory in Pest.php

This commit is contained in:
Bastien Philippe
2023-09-15 15:54:26 +02:00
committed by Nuno Maduro
parent 9c077ed352
commit 9d58e1a77e
6 changed files with 160 additions and 19 deletions

View File

@ -0,0 +1,45 @@
<?php
uses()->afterAll(function () {
expect($_SERVER['globalHook'])
->toHaveProperty('afterAll')
->and($_SERVER['globalHook']->afterAll)
->toBe(1)
->and($_SERVER['globalHook']->calls)
->afterAll
->toBe(1);
$_SERVER['globalHook']->afterAll = 2;
$_SERVER['globalHook']->calls->afterAll++;
});
afterAll(function () {
expect($_SERVER['globalHook'])
->toHaveProperty('afterAll')
->and($_SERVER['globalHook']->afterAll)
->toBe(1)
->and($_SERVER['globalHook']->calls)
->afterAll
->toBe(2);
$_SERVER['globalHook']->afterAll = 2;
$_SERVER['globalHook']->calls->afterAll++;
});
test('global afterAll execution order', function () {
expect($_SERVER['globalHook'])
->not()
->toHaveProperty('afterAll')
->and($_SERVER['globalHook']->calls)
->afterAll
->toBe(0);
});
it('only gets called once per file', function () {
expect($_SERVER['globalHook'])
->not()
->toHaveProperty('afterAll')
->and($_SERVER['globalHook']->calls)
->afterAll
->toBe(0);
});

View File

@ -4,16 +4,16 @@ uses()->afterEach(function () {
expect($this)
->toHaveProperty('ith')
->and($this->ith)
->toBe(0);
->toBe(1);
$this->ith = 1;
$this->ith = 2;
});
afterEach(function () {
expect($this)
->toHaveProperty('ith')
->and($this->ith)
->toBe(1);
->toBe(2);
});
test('global afterEach execution order', function () {

View File

@ -0,0 +1,55 @@
<?php
use Pest\Plugins\Parallel;
use Pest\Support\Str;
// HACK: we have to determine our $_SERVER['globalHook-]>calls baseline. This is because
// two other tests are executed before this one due to filename ordering.
$args = $_SERVER['argv'] ?? [];
$single = (isset($args[1]) && Str::endsWith(__FILE__, $args[1])) || Parallel::isWorker();
$offset = $single ? 0 : 2;
uses()->beforeAll(function () use ($offset) {
expect($_SERVER['globalHook'])
->toHaveProperty('beforeAll')
->and($_SERVER['globalHook']->beforeAll)
->toBe(1)
->and($_SERVER['globalHook']->calls)
->beforeAll
->toBe(1 + $offset);
$_SERVER['globalHook']->beforeAll = 2;
$_SERVER['globalHook']->calls->beforeAll++;
});
beforeAll(function () use ($offset) {
expect($_SERVER['globalHook'])
->toHaveProperty('beforeAll')
->and($_SERVER['globalHook']->beforeAll)
->toBe(2)
->and($_SERVER['globalHook']->calls)
->beforeAll
->toBe(2 + $offset);
$_SERVER['globalHook']->beforeAll = 3;
$_SERVER['globalHook']->calls->beforeAll++;
});
test('global beforeAll execution order', function () use ($offset) {
expect($_SERVER['globalHook'])
->toHaveProperty('beforeAll')
->and($_SERVER['globalHook']->beforeAll)
->toBe(3)
->and($_SERVER['globalHook']->calls)
->beforeAll
->toBe(3 + $offset);
});
it('only gets called once per file', function () use ($offset) {
expect($_SERVER['globalHook'])
->beforeAll
->toBe(3)
->and($_SERVER['globalHook']->calls)
->beforeAll
->toBe(3 + $offset);
});

View File

@ -1,15 +1,6 @@
<?php
uses()->beforeEach(function () {
expect($this)
->toHaveProperty('baz')
->and($this->baz)
->toBe(0);
$this->baz = 1;
});
beforeEach(function () {
expect($this)
->toHaveProperty('baz')
->and($this->baz)
@ -18,9 +9,18 @@ beforeEach(function () {
$this->baz = 2;
});
test('global beforeEach execution order', function () {
beforeEach(function () {
expect($this)
->toHaveProperty('baz')
->and($this->baz)
->toBe(2);
$this->baz = 3;
});
test('global beforeEach execution order', function () {
expect($this)
->toHaveProperty('baz')
->and($this->baz)
->toBe(3);
});