merge from master

This commit is contained in:
Fabio Ivona
2021-11-15 11:54:42 +01:00
104 changed files with 1452 additions and 2286 deletions

View File

@ -2,14 +2,18 @@
$file = __DIR__ . DIRECTORY_SEPARATOR . 'after-all-test';
beforeAll(function () use ($file) {
@unlink($file);
});
afterAll(function () use ($file) {
unlink($file);
@unlink($file);
});
test('deletes file after all', function () use ($file) {
file_put_contents($file, 'foo');
$this->assertFileExists($file);
register_shutdown_function(function () use ($file) {
$this->assertFileNotExists($file);
register_shutdown_function(function () {
// $this->assertFileDoesNotExist($file);
});
});

View File

@ -12,7 +12,8 @@ beforeEach(function () {
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');
Datasets::resolve('foo', ['first']);
});
it('throws exception if dataset already exist', function () {
@ -27,13 +28,13 @@ it('sets closures', function () {
yield [1];
});
expect(iterator_to_array(Datasets::get('foo')()))->toBe([[1]]);
expect(Datasets::resolve('foo', ['foo']))->toBe(['foo with (1)' => [1]]);
});
it('sets arrays', function () {
Datasets::set('bar', [[2]]);
expect(Datasets::get('bar'))->toBe([[2]]);
expect(Datasets::resolve('bar', ['bar']))->toBe(['bar with (2)' => [2]]);
});
it('gets bound to test case object', function () {
@ -52,6 +53,7 @@ $datasets = [[1], [2]];
test('lazy datasets', function ($text) use ($state, $datasets) {
$state->text .= $text;
expect(in_array([$text], $datasets))->toBe(true);
})->with($datasets);

View File

@ -1,6 +1,6 @@
<?php
use Pest\PendingObjects\TestCall;
use Pest\PendingCalls\TestCall;
use PHPUnit\Framework\TestCase;
uses(Gettable::class);

View File

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

View File

@ -2,55 +2,53 @@
use Pest\Support\Str;
global $globalHook;
// HACK: we have to determine our $globalHook->calls baseline. This is because
// 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])) || ($_SERVER['PEST_PARALLEL'] ?? false);
$offset = $single ? 0 : 2;
uses()->beforeAll(function () use ($globalHook, $offset) {
expect($globalHook)
uses()->beforeAll(function () use ($offset) {
expect($_SERVER['globalHook'])
->toHaveProperty('beforeAll')
->and($globalHook->beforeAll)
->and($_SERVER['globalHook']->beforeAll)
->toBe(0)
->and($globalHook->calls)
->and($_SERVER['globalHook']->calls)
->beforeAll
->toBe(1 + $offset);
$globalHook->beforeAll = 1;
$globalHook->calls->beforeAll++;
$_SERVER['globalHook']->beforeAll = 1;
$_SERVER['globalHook']->calls->beforeAll++;
});
beforeAll(function () use ($globalHook, $offset) {
expect($globalHook)
beforeAll(function () use ($offset) {
expect($_SERVER['globalHook'])
->toHaveProperty('beforeAll')
->and($globalHook->beforeAll)
->and($_SERVER['globalHook']->beforeAll)
->toBe(1)
->and($globalHook->calls)
->and($_SERVER['globalHook']->calls)
->beforeAll
->toBe(2 + $offset);
$globalHook->beforeAll = 2;
$globalHook->calls->beforeAll++;
$_SERVER['globalHook']->beforeAll = 2;
$_SERVER['globalHook']->calls->beforeAll++;
});
test('global beforeAll execution order', function () use ($globalHook, $offset) {
expect($globalHook)
test('global beforeAll execution order', function () use ($offset) {
expect($_SERVER['globalHook'])
->toHaveProperty('beforeAll')
->and($globalHook->beforeAll)
->and($_SERVER['globalHook']->beforeAll)
->toBe(2)
->and($globalHook->calls)
->and($_SERVER['globalHook']->calls)
->beforeAll
->toBe(3 + $offset);
});
it('only gets called once per file', function () use ($globalHook, $offset) {
expect($globalHook)
it('only gets called once per file', function () use ($offset) {
expect($_SERVER['globalHook'])
->beforeAll
->toBe(2)
->and($globalHook->calls)
->and($_SERVER['globalHook']->calls)
->beforeAll
->toBe(3 + $offset);
});

View File

@ -7,7 +7,7 @@ namespace Tests\CustomTestCase;
use function PHPUnit\Framework\assertTrue;
use PHPUnit\Framework\TestCase;
class CustomTestCase extends TestCase
abstract class CustomTestCase extends TestCase
{
public function assertCustomTrue()
{

View File

@ -7,21 +7,21 @@ uses(CustomTestCaseInSubFolder::class)->in('PHPUnit/CustomTestCaseInSubFolders/S
uses()->group('integration')->in('Visual');
// NOTE: global test value container to be mutated and checked across files, as needed
$globalHook = (object) ['calls' => (object) ['beforeAll' => 0, 'afterAll' => 0]];
$_SERVER['globalHook'] = (object) ['calls' => (object) ['beforeAll' => 0, 'afterAll' => 0]];
uses()
->beforeEach(function () {
$this->baz = 0;
})
->beforeAll(function () use ($globalHook) {
$globalHook->beforeAll = 0;
$globalHook->calls->beforeAll++;
->beforeAll(function () {
$_SERVER['globalHook']->beforeAll = 0;
$_SERVER['globalHook']->calls->beforeAll++;
})
->afterEach(function () {
$this->ith = 0;
})
->afterAll(function () use ($globalHook) {
$globalHook->afterAll = 0;
$globalHook->calls->afterAll++;
->afterAll(function () {
$_SERVER['globalHook']->afterAll = 0;
$_SERVER['globalHook']->calls->afterAll++;
})
->in('Hooks');

View File

@ -1,20 +0,0 @@
<?php
use NunoMaduro\Collision\Adapters\Phpunit\Printer;
use Pest\Actions\AddsDefaults;
use PHPUnit\TextUI\DefaultResultPrinter;
it('sets defaults', function () {
$arguments = AddsDefaults::to(['bar' => 'foo']);
expect($arguments['printer'])->toBeInstanceOf(Printer::class);
expect($arguments['bar'])->toBe('foo');
});
it('does not override options', function () {
$defaultResultPrinter = new DefaultResultPrinter();
expect(AddsDefaults::to(['printer' => $defaultResultPrinter]))->tobe([
'printer' => $defaultResultPrinter,
]);
});

View File

@ -1,32 +0,0 @@
<?php
use Pest\Actions\AddsTests;
use PHPUnit\Framework\TestCase as PhpUnitTestCase;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Framework\WarningTestCase;
$closure = function () {
};
$pestTestCase = new class() extends \PHPUnit\Framework\TestCase {
};
test('default php unit tests', function () {
$testSuite = new TestSuite();
$phpUnitTestCase = new class() extends PhpUnitTestCase {
};
$testSuite->addTest($phpUnitTestCase);
expect($testSuite->tests())->toHaveCount(1);
AddsTests::to($testSuite, new \Pest\TestSuite(getcwd(), 'tests'));
expect($testSuite->tests())->toHaveCount(1);
});
it('removes warnings', function () {
$testSuite = new TestSuite();
$warningTestCase = new WarningTestCase('No tests found in class "Pest\TestCase".');
$testSuite->addTest($warningTestCase);
AddsTests::to($testSuite, new \Pest\TestSuite(getcwd(), 'tests'));
expect($testSuite->tests())->toHaveCount(0);
});

View File

@ -1,42 +0,0 @@
<?php
use Pest\Actions\ValidatesConfiguration;
use Pest\Exceptions\AttributeNotSupportedYet;
use Pest\Exceptions\FileOrFolderNotFound;
it('throws exception when configuration not found', function () {
$this->expectException(FileOrFolderNotFound::class);
ValidatesConfiguration::in([
'configuration' => 'foo',
]);
});
it('throws exception when `process isolation` is true', function () {
$this->expectException(AttributeNotSupportedYet::class);
$this->expectExceptionMessage('The PHPUnit attribute `processIsolation` with value `true` is not supported yet.');
$filename = implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 2),
'Fixtures',
'phpunit-in-isolation.xml',
]);
ValidatesConfiguration::in([
'configuration' => $filename,
]);
});
it('do not throws exception when `process isolation` is false', function () {
$filename = implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 2),
'Fixtures',
'phpunit-not-in-isolation.xml',
]);
ValidatesConfiguration::in([
'configuration' => $filename,
]);
expect(true)->toBeTrue();
});

View File

@ -2,53 +2,55 @@
use Pest\Exceptions\DatasetMissing;
use Pest\Exceptions\TestAlreadyExist;
use Pest\Factories\TestCaseFactory;
use Pest\Factories\TestCaseMethodFactory;
use Pest\Plugins\Environment;
use Pest\TestSuite;
it('does not allow to add the same test description twice', function () {
$testSuite = new TestSuite(getcwd(), 'tests');
$test = function () {};
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'foo', $test));
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'foo', $test));
$method = new TestCaseMethodFactory('foo', 'bar', null);
$testSuite->tests->set($method);
$testSuite->tests->set($method);
})->throws(
TestAlreadyExist::class,
sprintf('A test with the description `%s` already exist in the filename `%s`.', 'foo', __FILE__),
sprintf('A test with the description `%s` already exist in the filename `%s`.', 'bar', 'foo'),
);
it('alerts users about tests with arguments but no input', function () {
$testSuite = new TestSuite(getcwd(), 'tests');
$test = function (int $arg) {};
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'foo', $test));
$method = new TestCaseMethodFactory('foo', 'bar', function (int $arg) {});
$testSuite->tests->set($method);
})->throws(
DatasetMissing::class,
sprintf("A test with the description '%s' has %d argument(s) ([%s]) and no dataset(s) provided in %s", 'foo', 1, 'int $arg', __FILE__),
sprintf("A test with the description '%s' has %d argument(s) ([%s]) and no dataset(s) provided in %s", 'bar', 1, 'int $arg', 'foo'),
);
it('can return an array of all test suite filenames', function () {
$testSuite = TestSuite::getInstance(getcwd(), 'tests');
$test = function () {};
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'foo', $test));
$testSuite->tests->set(new TestCaseFactory(__FILE__, 'bar', $test));
$testSuite->tests->set(new TestCaseMethodFactory('a', 'b', null));
$testSuite->tests->set(new TestCaseMethodFactory('c', 'd', null));
expect($testSuite->tests->getFilenames())->toEqual([
__FILE__,
__FILE__,
'a',
'c',
]);
});
it('can filter the test suite filenames to those with the only method', function () {
$testSuite = new TestSuite(getcwd(), 'tests');
$test = function () {};
$testWithOnly = new TestCaseFactory(__FILE__, 'foo', $test);
$testWithOnly = new TestCaseMethodFactory('a', 'b', null);
$testWithOnly->only = true;
$testSuite->tests->set($testWithOnly);
$testSuite->tests->set(new TestCaseFactory('Baz/Bar/Boo.php', 'bar', $test));
$testSuite->tests->set(new TestCaseMethodFactory('c', 'd', null));
expect($testSuite->tests->getFilenames())->toEqual([
__FILE__,
'a',
]);
});
@ -59,15 +61,15 @@ it('does not filter the test suite filenames to those with the only method when
$test = function () {};
$testWithOnly = new TestCaseFactory(__FILE__, 'foo', $test);
$testWithOnly = new TestCaseMethodFactory('a', 'b', null);
$testWithOnly->only = true;
$testSuite->tests->set($testWithOnly);
$testSuite->tests->set(new TestCaseFactory('Baz/Bar/Boo.php', 'bar', $test));
$testSuite->tests->set(new TestCaseMethodFactory('c', 'd', null));
expect($testSuite->tests->getFilenames())->toEqual([
__FILE__,
'Baz/Bar/Boo.php',
'a',
'c',
]);
Environment::name($previousEnvironment);

0
tests/Visual/junit.html Normal file
View File