mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
first
This commit is contained in:
15
tests/Features/AfterAll.php
Normal file
15
tests/Features/AfterAll.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
$file = __DIR__ . DIRECTORY_SEPARATOR . 'after-all-test';
|
||||
|
||||
afterAll(function () use ($file) {
|
||||
unlink($file);
|
||||
});
|
||||
|
||||
test('deletes file after all', function () use ($file) {
|
||||
file_put_contents($file, 'foo');
|
||||
assertFileExists($file);
|
||||
register_shutdown_function(function () use ($file) {
|
||||
assertFileNotExists($file);
|
||||
});
|
||||
});
|
||||
20
tests/Features/AfterEach.php
Normal file
20
tests/Features/AfterEach.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
$state = new stdClass();
|
||||
|
||||
beforeEach(function () use ($state) {
|
||||
$this->state = $state;
|
||||
});
|
||||
|
||||
afterEach(function () use ($state) {
|
||||
$this->state->bar = 2;
|
||||
});
|
||||
|
||||
it('does not get executed before the test', function () {
|
||||
assertFalse(property_exists($this->state, 'bar'));
|
||||
});
|
||||
|
||||
it('gets executed after the test', function () {
|
||||
assertTrue(property_exists($this->state, 'bar'));
|
||||
assertEquals(2, $this->state->bar);
|
||||
});
|
||||
18
tests/Features/BeforeAll.php
Normal file
18
tests/Features/BeforeAll.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
$foo = new \stdClass();
|
||||
$foo->bar = 0;
|
||||
|
||||
beforeAll(function () use ($foo) {
|
||||
$foo->bar++;
|
||||
});
|
||||
|
||||
it('gets executed before tests', function () use ($foo) {
|
||||
assertEquals($foo->bar, 1);
|
||||
|
||||
$foo->bar = 'changed';
|
||||
});
|
||||
|
||||
it('do not get executed before each test', function () use ($foo) {
|
||||
assertEquals($foo->bar, 'changed');
|
||||
});
|
||||
15
tests/Features/BeforeEach.php
Normal file
15
tests/Features/BeforeEach.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
beforeEach(function () {
|
||||
$this->bar = 2;
|
||||
});
|
||||
|
||||
it('gets executed before each test', function () {
|
||||
assertEquals($this->bar, 2);
|
||||
|
||||
$this->bar = 'changed';
|
||||
});
|
||||
|
||||
it('gets executed before each test once again', function () {
|
||||
assertEquals($this->bar, 2);
|
||||
});
|
||||
108
tests/Features/Datasets.php
Normal file
108
tests/Features/Datasets.php
Normal file
@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
use Pest\Datasets;
|
||||
use Pest\Exceptions\DatasetAlreadyExist;
|
||||
use Pest\Exceptions\DatasetDoesNotExist;
|
||||
|
||||
it('throws exception if dataset does not exist', function () {
|
||||
$this->expectException(DatasetDoesNotExist::class);
|
||||
$this->expectExceptionMessage("A dataset with the name `first` does not exist. You can create it using `dataset('first', ['a', 'b']);`.");
|
||||
Datasets::get('first');
|
||||
});
|
||||
|
||||
it('throws exception if dataset already exist', function () {
|
||||
Datasets::set('second', [[]]);
|
||||
$this->expectException(DatasetAlreadyExist::class);
|
||||
$this->expectExceptionMessage('A dataset with the name `second` already exist.');
|
||||
Datasets::set('second', [[]]);
|
||||
});
|
||||
|
||||
it('sets closures', function () {
|
||||
Datasets::set('foo', function () {
|
||||
yield [1];
|
||||
});
|
||||
|
||||
assertEquals([[1]], iterator_to_array(Datasets::get('foo')()));
|
||||
});
|
||||
|
||||
it('sets arrays', function () {
|
||||
Datasets::set('bar', [[2]]);
|
||||
|
||||
assertEquals([[2]], Datasets::get('bar'));
|
||||
});
|
||||
|
||||
it('gets bound to test case object', function () {
|
||||
$this->assertTrue(true);
|
||||
})->with([['a'], ['b']]);
|
||||
|
||||
test('it truncates the description', function () {
|
||||
assertTrue(true);
|
||||
// it gets tested by the integration test
|
||||
})->with([str_repeat('Fooo', 10000000)]);
|
||||
|
||||
$state = new stdClass();
|
||||
$state->text = '';
|
||||
|
||||
$datasets = [[1], [2]];
|
||||
|
||||
test('lazy datasets', function ($text) use ($state, $datasets) {
|
||||
$state->text .= $text;
|
||||
assertTrue(in_array([$text], $datasets));
|
||||
})->with($datasets);
|
||||
|
||||
test('lazy datasets did the job right', function () use ($state) {
|
||||
assertEquals('12', $state->text);
|
||||
});
|
||||
|
||||
$state->text = '';
|
||||
|
||||
test('eager datasets', function ($text) use ($state, $datasets) {
|
||||
$state->text .= $text;
|
||||
assertTrue(in_array([$text], $datasets));
|
||||
})->with(function () use ($datasets) {
|
||||
return $datasets;
|
||||
});
|
||||
|
||||
test('eager datasets did the job right', function () use ($state) {
|
||||
assertEquals('1212', $state->text);
|
||||
});
|
||||
|
||||
test('lazy registered datasets', function ($text) use ($state, $datasets) {
|
||||
$state->text .= $text;
|
||||
assertTrue(in_array([$text], $datasets));
|
||||
})->with('numbers.array');
|
||||
|
||||
test('lazy registered datasets did the job right', function () use ($state) {
|
||||
assertEquals('121212', $state->text);
|
||||
});
|
||||
|
||||
test('eager registered datasets', function ($text) use ($state, $datasets) {
|
||||
$state->text .= $text;
|
||||
assertTrue(in_array([$text], $datasets));
|
||||
})->with('numbers.closure');
|
||||
|
||||
test('eager registered datasets did the job right', function () use ($state) {
|
||||
assertEquals('12121212', $state->text);
|
||||
});
|
||||
|
||||
test('eager wrapped registered datasets', function ($text) use ($state, $datasets) {
|
||||
$state->text .= $text;
|
||||
assertTrue(in_array([$text], $datasets));
|
||||
})->with('numbers.closure.wrapped');
|
||||
|
||||
test('eager registered wrapped datasets did the job right', function () use ($state) {
|
||||
assertEquals('1212121212', $state->text);
|
||||
});
|
||||
|
||||
class Bar
|
||||
{
|
||||
public $name = 1;
|
||||
}
|
||||
|
||||
$namedDatasets = [
|
||||
new Bar(),
|
||||
];
|
||||
|
||||
test('lazy named datasets', function ($text) use ($state, $datasets) {
|
||||
assertTrue(true);
|
||||
})->with($namedDatasets);
|
||||
15
tests/Features/Exceptions.php
Normal file
15
tests/Features/Exceptions.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
it('gives access the the underlying expectException', function () {
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
throw new InvalidArgumentException();
|
||||
});
|
||||
|
||||
it('catch exceptions', function () {
|
||||
throw new Exception('Something bad happened');
|
||||
})->throws(Exception::class);
|
||||
|
||||
it('catch exceptions and messages', function () {
|
||||
throw new Exception('Something bad happened');
|
||||
})->throws(Exception::class, 'Something bad happened');
|
||||
7
tests/Features/HigherOrderMessages.php
Normal file
7
tests/Features/HigherOrderMessages.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
beforeEach()->assertTrue(true);
|
||||
|
||||
it('proxies calls to object')->assertTrue(true);
|
||||
|
||||
afterEach()->assertTrue(true);
|
||||
7
tests/Features/It.php
Normal file
7
tests/Features/It.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
it('is a test', function () {
|
||||
assertArrayHasKey('key', ['key' => 'foo']);
|
||||
});
|
||||
|
||||
it('is a higher order message test')->assertTrue(true);
|
||||
15
tests/Features/Mocks.php
Normal file
15
tests/Features/Mocks.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
interface Foo
|
||||
{
|
||||
public function bar(): int;
|
||||
}
|
||||
|
||||
it('has bar', function () {
|
||||
$mock = Mockery::mock(Foo::class);
|
||||
$mock->shouldReceive('bar')
|
||||
->times(1)
|
||||
->andReturn(2);
|
||||
|
||||
assertEquals(2, $mock->bar());
|
||||
});
|
||||
29
tests/Features/Skip.php
Normal file
29
tests/Features/Skip.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
it('do not skips')
|
||||
->skip(false)
|
||||
->assertTrue(true);
|
||||
|
||||
it('skips with truthy')
|
||||
->skip(1)
|
||||
->assertTrue(false);
|
||||
|
||||
it('skips with truthy condition by default')
|
||||
->skip()
|
||||
->assertTrue(false);
|
||||
|
||||
it('skips with message')
|
||||
->skip('skipped because bar')
|
||||
->assertTrue(false);
|
||||
|
||||
it('skips with truthy closure condition')
|
||||
->skip(function () { return '1'; })
|
||||
->assertTrue(false);
|
||||
|
||||
it('do not skips with falsy closure condition')
|
||||
->skip(function () { return false; })
|
||||
->assertTrue(true);
|
||||
|
||||
it('skips with condition and messsage')
|
||||
->skip(true, 'skipped because foo')
|
||||
->assertTrue(false);
|
||||
7
tests/Features/Test.php
Normal file
7
tests/Features/Test.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
test('a test', function () {
|
||||
assertArrayHasKey('key', ['key' => 'foo']);
|
||||
});
|
||||
|
||||
test('higher order message test')->assertTrue(true);
|
||||
27
tests/Features/TestCycle.php
Normal file
27
tests/Features/TestCycle.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
$foo = new stdClass();
|
||||
$foo->beforeAll = false;
|
||||
$foo->beforeEach = false;
|
||||
$foo->afterEach = false;
|
||||
$foo->afterAll = false;
|
||||
|
||||
beforeAll(function () {
|
||||
$foo->beforeAll = true;
|
||||
});
|
||||
beforeEach(function () {
|
||||
$foo->beforeEach = true;
|
||||
});
|
||||
afterEach(function () {
|
||||
$foo->afterEach = true;
|
||||
});
|
||||
afterAll(function () {
|
||||
$foo->afterAll = true;
|
||||
});
|
||||
|
||||
register_shutdown_function(function () use ($foo) {
|
||||
assertFalse($foo->beforeAll);
|
||||
assertFalse($foo->beforeEach);
|
||||
assertFalse($foo->afterEach);
|
||||
assertFalse($foo->afterAll);
|
||||
});
|
||||
Reference in New Issue
Block a user