Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/pestphp/pest
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Jul 26, 2021
2 parents 09d2b16 + f56556e commit 2e0d1bb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/Concerns/Testable.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public function __construct(Closure $test, string $description, array $data)
{
$this->__test = $test;
$this->__description = $description;
self::$beforeAll = null;
self::$afterAll = null;

parent::__construct('__test', $data);
}
Expand Down
30 changes: 27 additions & 3 deletions tests/Hooks/AfterAllTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,50 @@

global $globalHook;

// NOTE: this test does not have a $globalHook->calls offset since it is first
// in the directory and thus will always run before the others. See also the
// BeforeAllTest.php for details.

uses()->afterAll(function () use ($globalHook) {
expect($globalHook)
->toHaveProperty('afterAll')
->and($globalHook->afterAll)
->toBe(0);
->toBe(0)
->and($globalHook->calls)
->afterAll
->toBe(1);

$globalHook->afterAll = 1;
$globalHook->calls->afterAll++;
});

afterAll(function () use ($globalHook) {
expect($globalHook)
->toHaveProperty('afterAll')
->and($globalHook->afterAll)
->toBe(1);
->toBe(1)
->and($globalHook->calls)
->afterAll
->toBe(2);

$globalHook->afterAll = 2;
$globalHook->calls->afterAll++;
});

test('global afterAll execution order', function () use ($globalHook) {
expect($globalHook)
->not()
->toHaveProperty('afterAll');
->toHaveProperty('afterAll')
->and($globalHook->calls)
->afterAll
->toBe(0);
});

it('only gets called once per file', function () use ($globalHook) {
expect($globalHook)
->not()
->toHaveProperty('afterAll')
->and($globalHook->calls)
->afterAll
->toBe(0);
});
40 changes: 34 additions & 6 deletions tests/Hooks/BeforeAllTest.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
<?php

use Pest\Support\Str;

global $globalHook;

uses()->beforeAll(function () use ($globalHook) {
// HACK: we have to determine our $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]);
$offset = $single ? 0 : 2;

uses()->beforeAll(function () use ($globalHook, $offset) {
expect($globalHook)
->toHaveProperty('beforeAll')
->and($globalHook->beforeAll)
->toBe(0);
->toBe(0)
->and($globalHook->calls)
->beforeAll
->toBe(1 + $offset);

$globalHook->beforeAll = 1;
$globalHook->calls->beforeAll++;
});

beforeAll(function () use ($globalHook) {
beforeAll(function () use ($globalHook, $offset) {
expect($globalHook)
->toHaveProperty('beforeAll')
->and($globalHook->beforeAll)
->toBe(1);
->toBe(1)
->and($globalHook->calls)
->beforeAll
->toBe(2 + $offset);

$globalHook->beforeAll = 2;
$globalHook->calls->beforeAll++;
});

test('global beforeAll execution order', function () use ($globalHook) {
test('global beforeAll execution order', function () use ($globalHook, $offset) {
expect($globalHook)
->toHaveProperty('beforeAll')
->and($globalHook->beforeAll)
->toBe(2);
->toBe(2)
->and($globalHook->calls)
->beforeAll
->toBe(3 + $offset);
});

it('only gets called once per file', function () use ($globalHook, $offset) {
expect($globalHook)
->beforeAll
->toBe(2)
->and($globalHook->calls)
->beforeAll
->toBe(3 + $offset);
});
5 changes: 4 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

uses()->group('integration')->in('Visual');

$globalHook = (object) []; // NOTE: global test value container to be mutated and checked across files, as needed
// NOTE: global test value container to be mutated and checked across files, as needed
$globalHook = (object) ['calls' => (object) ['beforeAll' => 0, 'afterAll' => 0]];

uses()
->beforeEach(function () {
$this->baz = 0;
})
->beforeAll(function () use ($globalHook) {
$globalHook->beforeAll = 0;
$globalHook->calls->beforeAll++;
})
->afterEach(function () {
$this->ith = 0;
})
->afterAll(function () use ($globalHook) {
$globalHook->afterAll = 0;
$globalHook->calls->afterAll++;
})
->in('Hooks');

0 comments on commit 2e0d1bb

Please sign in to comment.