feat: adds after

This commit is contained in:
Nuno Maduro
2024-05-08 01:24:30 +01:00
parent 04b099e87c
commit 8169382362
11 changed files with 312 additions and 16 deletions

View File

@ -7,6 +7,17 @@
PASS Tests\Environments\Windows
✓ global functions are loaded
WARN Tests\Features\After
✓ it can run after test
✓ it can run after test twice
- it does not run when skipped
- something → it does not run when skipped
✓ something → it can run after test
✓ something 2 → it can run after test
✓ high order test
- high order test with skip
✓ post 'foo' → defer Closure → expect Closure → toBe 1
PASS Tests\Features\AfterAll
✓ deletes file after all
@ -1444,4 +1455,4 @@
WARN Tests\Visual\Version
- visual snapshot of help command output
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 21 skipped, 1034 passed (2519 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 24 skipped, 1040 passed (2561 assertions)

176
tests/Features/After.php Normal file
View File

@ -0,0 +1,176 @@
<?php
beforeEach(function () {
$this->count = 0;
});
afterEach(function () {
match ($this->name()) {
'__pest_evaluable_it_can_run_after_test' => expect($this->count)->toBe(1),
'__pest_evaluable_it_can_run_after_test_twice' => expect($this->count)->toBe(1),
'__pest_evaluable_it_does_not_run_when_skipped' => expect($this->count)->toBe(0),
'__pest_evaluable__something__→_it_does_not_run_when_skipped' => expect($this->count)->toBe(0),
'__pest_evaluable__something__→_it_can_run_after_test' => expect($this->count)->toBe(1),
'__pest_evaluable__something_2__→_it_can_run_after_test' => expect($this->count)->toBe(1),
'__pest_evaluable_high_order_test' => expect($this->count)->toBe(1),
'__pest_evaluable_high_order_test_with_skip' => expect($this->count)->toBe(0),
'__pest_evaluable_post__foo__→_defer_Closure_→_expect_Closure_→_toBe_1' => expect($this->count)->toBe(1),
default => $this->fail('Unexpected test name: '.$this->name()),
};
$this->count++;
});
it('can run after test', function () {
expect($this->count)->toBe(0);
$this->count++;
})->after(function () {
expect($this->count)->toBe(2);
$this->count++;
});
it('can run after test twice', function () {
expect($this->count)->toBe(0);
$this->count++;
})->after(function () {
expect($this->count)->toBe(2);
$this->count++;
})->after(function () {
expect($this->count)->toBe(3);
$this->count++;
});
it('does not run when skipped', function () {
dd('This should not run 1');
})->skip()->after(function () {
dd('This should not run 2');
});
afterEach(function () {
match ($this->name()) {
'__pest_evaluable_it_can_run_after_test' => expect($this->count)->toBe(3),
'__pest_evaluable_it_can_run_after_test_twice' => expect($this->count)->toBe(4),
'__pest_evaluable_it_does_not_run_when_skipped' => expect($this->count)->toBe(1),
'__pest_evaluable__something__→_it_does_not_run_when_skipped' => expect($this->count)->toBe(1),
'__pest_evaluable__something__→_it_can_run_after_test' => expect($this->count)->toBe(2),
'__pest_evaluable__something_2__→_it_can_run_after_test' => expect($this->count)->toBe(2),
'__pest_evaluable_high_order_test' => expect($this->count)->toBe(2),
'__pest_evaluable_high_order_test_with_skip' => expect($this->count)->toBe(1),
'__pest_evaluable_post__foo__→_defer_Closure_→_expect_Closure_→_toBe_1' => expect($this->count)->toBe(2),
default => $this->fail('Unexpected test name: '.$this->name()),
};
$this->count++;
});
afterEach(function () {
match ($this->name()) {
'__pest_evaluable_it_can_run_after_test' => expect($this->count)->toBe(4),
'__pest_evaluable_it_can_run_after_test_twice' => expect($this->count)->toBe(5),
'__pest_evaluable_it_does_not_run_when_skipped' => expect($this->count)->toBe(2),
'__pest_evaluable__something__→_it_does_not_run_when_skipped' => expect($this->count)->toBe(2),
'__pest_evaluable__something__→_it_can_run_after_test' => expect($this->count)->toBe(3),
'__pest_evaluable__something_2__→_it_can_run_after_test' => expect($this->count)->toBe(3),
'__pest_evaluable_high_order_test' => expect($this->count)->toBe(3),
'__pest_evaluable_high_order_test_with_skip' => expect($this->count)->toBe(2),
'__pest_evaluable_post__foo__→_defer_Closure_→_expect_Closure_→_toBe_1' => expect($this->count)->toBe(3),
default => $this->fail('Unexpected test name: '.$this->name()),
};
$this->count++;
});
describe('something', function () {
it('does not run when skipped', function () {
dd('This should not run 3');
})->skip()->after(function () {
dd('This should not run 4');
});
it('can run after test', function () {
expect($this->count)->toBe(0);
$this->count++;
})->after(function () {
expect($this->count)->toBe(5);
$this->count++;
})->after(function () {
expect($this->count)->toBe(6);
$this->count++;
});
})->after(function () {
expect($this->count)->toBe(4);
$this->count++;
});
describe('something 2', function () {
it('can run after test', function () {
expect($this->count)->toBe(0);
$this->count++;
})->after(function () {
expect($this->count)->toBe(4);
$this->count++;
});
})->after(function () {
expect($this->count)->toBe(4);
$this->count++;
})->after(function () {
expect($this->count)->toBe(5);
$this->count++;
});
test('high order test')
->defer(fn () => $this->count++)
->expect(fn () => $this->count)->toBe(1)
->after(function () {
expect($this->count)->toBe(4);
$this->count++;
});
test('high order test with skip')
->skip()
->defer(fn () => $this->count++)
->expect(fn () => $this->count)->toBe(1)
->after(function () {
dd('This should not run 5');
});
uses(Postable::class);
/**
* @return TestCase|TestCall|Gettable
*/
function post(string $route)
{
return test()->post($route);
}
trait Postable
{
/**
* @return TestCase|TestCall|Gettable
*/
public function post(string $route)
{
expect($route)->not->toBeEmpty();
return $this;
}
}
post('foo')->defer(fn () => $this->count++)->expect(fn () => $this->count)->toBe(1);

View File

@ -1,6 +1,11 @@
<?php
/**
* @return \PHPUnit\Framework\TestCase
*/
function myAssertTrue($value)
{
test()->assertTrue($value);
return test();
}

View File

@ -7,7 +7,8 @@ use Pest\TestSuite;
it('does not allow to add the same test description twice', function () {
$testSuite = new TestSuite(getcwd(), 'tests');
$method = new TestCaseMethodFactory('foo', 'bar', null);
$method = new TestCaseMethodFactory('foo', null);
$method->description = 'bar';
$testSuite->tests->set($method);
$testSuite->tests->set($method);
@ -19,9 +20,11 @@ it('does not allow to add the same test description twice', function () {
it('alerts users about tests with arguments but no input', function () {
$testSuite = new TestSuite(getcwd(), 'tests');
$method = new TestCaseMethodFactory('foo', 'bar', function (int $arg) {
$method = new TestCaseMethodFactory('foo', function (int $arg) {
});
$method->description = 'bar';
$testSuite->tests->set($method);
})->throws(
DatasetMissing::class,
@ -31,8 +34,13 @@ it('alerts users about tests with arguments but no input', function () {
it('can return an array of all test suite filenames', function () {
$testSuite = new TestSuite(getcwd(), 'tests');
$testSuite->tests->set(new TestCaseMethodFactory('a', 'b', null));
$testSuite->tests->set(new TestCaseMethodFactory('c', 'd', null));
$method = new TestCaseMethodFactory('a', null);
$method->description = 'b';
$testSuite->tests->set($method);
$method = new TestCaseMethodFactory('c', null);
$method->description = 'd';
$testSuite->tests->set($method);
expect($testSuite->tests->getFilenames())->toEqual([
'a',

View File

@ -16,7 +16,7 @@ $run = function () {
test('parallel', function () use ($run) {
expect($run('--exclude-group=integration'))
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 16 skipped, 1020 passed (2487 assertions)')
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 13 todos, 19 skipped, 1026 passed (2529 assertions)')
->toContain('Parallel: 3 processes');
})->skipOnWindows();